@@ -742,6 +742,7 @@ static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
742
742
743
743
bool bodySet = false ;
744
744
TRI_GET_GLOBAL_STRING (BodyKey);
745
+
745
746
if (TRI_HasProperty (context, isolate, res, BodyKey)) {
746
747
// check if we should apply result transformations
747
748
// transformations turn the result from one type into another
@@ -750,17 +751,17 @@ static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
750
751
// array, e.g. res.transformations = [ "base64encode" ]
751
752
TRI_GET_GLOBAL_STRING (TransformationsKey);
752
753
v8::Handle <v8::Value> transformArray = res->Get (context, TransformationsKey).FromMaybe (v8::Local<v8::Value>());
753
-
754
+ v8:: Handle <v8::Value> bodyVal = res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>());
754
755
switch (response->transportType ()) {
755
756
case Endpoint::TransportType::HTTP: {
756
757
// OBI FIXME - vpack
757
758
// HTTP SHOULD USE vpack interface
758
759
759
760
HttpResponse* httpResponse = dynamic_cast <HttpResponse*>(response);
760
- if ( transformArray-> IsArray ()) {
761
- TRI_GET_GLOBAL_STRING (BodyKey );
762
- std::string out ( TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>())));
763
- v8:: Handle <v8::Array> transformations = transformArray. As <v8::Array>( );
761
+ v8:: Handle <v8::Array> transformations = transformArray. As <v8::Array>();
762
+ bool setRegularBody = !transformArray-> IsArray ( );
763
+ if (!setRegularBody) {
764
+ std::string out ( TRI_ObjectToString (isolate, bodyVal) );
764
765
765
766
for (uint32_t i = 0 ; i < transformations->Length (); i++) {
766
767
v8::Handle <v8::Value> transformator =
@@ -778,38 +779,44 @@ static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
778
779
out = StringUtils::decodeBase64 (out);
779
780
// set the correct content-encoding header
780
781
response->setHeaderNC (StaticStrings::ContentEncoding, StaticStrings::Binary);
782
+ } else if (name == " gzip" ) {
783
+ response->setAllowCompression (true );
784
+ setRegularBody = true ;
785
+ } else if (name == " deflate" ) {
786
+ response->setAllowCompression (true );
787
+ setRegularBody = true ;
781
788
}
782
789
}
783
-
784
- // what type is out? always json?
785
- httpResponse->body ().appendText (out);
786
- httpResponse->sealBody ();
787
- } else {
788
- TRI_GET_GLOBAL_STRING (BodyKey);
789
- v8:: Handle <v8::Value> b = res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>());
790
- if (V8Buffer::hasInstance (isolate, b )) {
790
+ if (!setRegularBody) {
791
+ // what type is out? always json?
792
+ httpResponse->body ().appendText (out);
793
+ httpResponse->sealBody ();
794
+ }
795
+ }
796
+ if (setRegularBody) {
797
+ if (V8Buffer::hasInstance (isolate, bodyVal )) {
791
798
// body is a Buffer
792
- auto obj = b .As <v8::Object>();
799
+ auto obj = bodyVal .As <v8::Object>();
793
800
httpResponse->body ().appendText (V8Buffer::data (isolate, obj),
794
801
V8Buffer::length (isolate, obj));
795
802
httpResponse->sealBody ();
796
803
} else if (autoContent && request->contentTypeResponse () == rest::ContentType::VPACK) {
797
804
// use velocypack
798
805
try {
799
- std::string json = TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>()) );
806
+ std::string json = TRI_ObjectToString (isolate, bodyVal );
800
807
VPackBuffer<uint8_t > buffer;
801
808
VPackBuilder builder (buffer);
802
809
VPackParser parser (builder);
803
810
parser.parse (json);
804
811
httpResponse->setContentType (rest::ContentType::VPACK);
805
812
httpResponse->setPayload (std::move (buffer));
806
813
} catch (...) {
807
- httpResponse->body ().appendText (TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>()) ));
814
+ httpResponse->body ().appendText (TRI_ObjectToString (isolate, bodyVal ));
808
815
httpResponse->sealBody ();
809
816
}
810
817
} else {
811
818
// treat body as a string
812
- httpResponse->body ().appendText (TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>()) ));
819
+ httpResponse->body ().appendText (TRI_ObjectToString (isolate, bodyVal ));
813
820
httpResponse->sealBody ();
814
821
}
815
822
}
@@ -818,14 +825,11 @@ static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
818
825
case Endpoint::TransportType::VST: {
819
826
VPackBuffer<uint8_t > buffer;
820
827
VPackBuilder builder (buffer);
821
-
822
- v8::Handle <v8::Value> v8Body = res->Get (context, BodyKey).FromMaybe (v8::Local<v8::Value>());
823
828
std::string out;
824
829
825
830
// decode and set out
826
831
if (transformArray->IsArray ()) {
827
- TRI_GET_GLOBAL_STRING (BodyKey);
828
- out = TRI_ObjectToString (isolate, res->Get (context, BodyKey).FromMaybe (v8::Local<v8::Value>())); // there is one case where
832
+ out = TRI_ObjectToString (isolate, bodyVal); // there is one case where
829
833
// we do not need a string
830
834
v8::Handle <v8::Array> transformations = transformArray.As <v8::Array>();
831
835
@@ -838,27 +842,31 @@ static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
838
842
// check available transformations
839
843
if (name == " base64decode" ) {
840
844
out = StringUtils::decodeBase64 (out);
845
+ } else if (name == " gzip" ) {
846
+ response->setAllowCompression (true );
847
+ } else if (name == " deflate" ) {
848
+ response->setAllowCompression (true );
841
849
}
842
850
}
843
851
}
844
852
845
853
// out is not set
846
854
if (out.empty ()) {
847
- if (autoContent && !V8Buffer::hasInstance (isolate, v8Body )) {
848
- if (v8Body ->IsString ()) {
849
- out = TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>()) ); // should get moved
855
+ if (autoContent && !V8Buffer::hasInstance (isolate, bodyVal )) {
856
+ if (bodyVal ->IsString ()) {
857
+ out = TRI_ObjectToString (isolate, bodyVal ); // should get moved
850
858
} else {
851
- TRI_V8ToVPack (isolate, builder, v8Body , false );
859
+ TRI_V8ToVPack (isolate, builder, bodyVal , false );
852
860
response->setContentType (rest::ContentType::VPACK);
853
861
}
854
862
} else if (V8Buffer::hasInstance (isolate,
855
- v8Body )) { // body form buffer - could
856
- // contain json or not
863
+ bodyVal )) { // body form buffer - could
864
+ // contain json or not
857
865
// REVIEW (fc) - is this correct?
858
- auto obj = v8Body .As <v8::Object>();
866
+ auto obj = bodyVal .As <v8::Object>();
859
867
out = std::string (V8Buffer::data (isolate, obj), V8Buffer::length (isolate, obj));
860
868
} else { // body is text - does not contain json
861
- out = TRI_ObjectToString (isolate, res-> Get (context, BodyKey). FromMaybe (v8::Local<v8::Value>()) ); // should get moved
869
+ out = TRI_ObjectToString (isolate, bodyVal ); // should get moved
862
870
}
863
871
}
864
872
0 commit comments