comparison Core/RestApi/RestApiOutput.cpp @ 1515:c94353fbd4e9

cont http compression
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Aug 2015 17:18:36 +0200
parents d73a2178b319
children 4f8c8ef114db
comparison
equal deleted inserted replaced
1514:d73a2178b319 1515:c94353fbd4e9
78 throw OrthancException(ErrorCode_BadSequenceOfCalls); 78 throw OrthancException(ErrorCode_BadSequenceOfCalls);
79 } 79 }
80 } 80 }
81 81
82 82
83 HttpCompression RestApiOutput::GetPreferredCompression() const 83 HttpCompression RestApiOutput::GetPreferredCompression(size_t bodySize) const
84 { 84 {
85 #if 0
86 // TODO
87 if (bodySize < 1024)
88 {
89 // Do not compress small answers
90 return HttpCompression_None;
91 }
92 #endif
93
85 if (allowGzipCompression_) 94 if (allowGzipCompression_)
86 { 95 {
87 return HttpCompression_Gzip; 96 return HttpCompression_Gzip;
88 } 97 }
89 else if (allowDeflateCompression_) 98 else if (allowDeflateCompression_)
106 115
107 void RestApiOutput::AnswerJson(const Json::Value& value) 116 void RestApiOutput::AnswerJson(const Json::Value& value)
108 { 117 {
109 CheckStatus(); 118 CheckStatus();
110 119
120 std::string s;
121
111 if (convertJsonToXml_) 122 if (convertJsonToXml_)
112 { 123 {
113 #if ORTHANC_PUGIXML_ENABLED == 1 124 #if ORTHANC_PUGIXML_ENABLED == 1
114 std::string s;
115 Toolbox::JsonToXml(s, value); 125 Toolbox::JsonToXml(s, value);
116 output_.SetContentType("application/xml"); 126 output_.SetContentType("application/xml");
117 output_.SendBody(s, GetPreferredCompression());
118 #else 127 #else
119 LOG(ERROR) << "Orthanc was compiled without XML support"; 128 LOG(ERROR) << "Orthanc was compiled without XML support";
120 throw OrthancException(ErrorCode_InternalError); 129 throw OrthancException(ErrorCode_InternalError);
121 #endif 130 #endif
122 } 131 }
123 else 132 else
124 { 133 {
125 Json::StyledWriter writer; 134 Json::StyledWriter writer;
126 output_.SetContentType("application/json"); 135 output_.SetContentType("application/json");
127 output_.SendBody(writer.write(value), GetPreferredCompression()); 136 s = writer.write(value);
128 } 137 }
138
139 output_.SendBody(s, GetPreferredCompression(s.size()));
129 140
130 alreadySent_ = true; 141 alreadySent_ = true;
131 } 142 }
132 143
133 void RestApiOutput::AnswerBuffer(const std::string& buffer, 144 void RestApiOutput::AnswerBuffer(const std::string& buffer,
141 size_t length, 152 size_t length,
142 const std::string& contentType) 153 const std::string& contentType)
143 { 154 {
144 CheckStatus(); 155 CheckStatus();
145 output_.SetContentType(contentType.c_str()); 156 output_.SetContentType(contentType.c_str());
146 output_.SendBody(buffer, length, GetPreferredCompression()); 157 output_.SendBody(buffer, length, GetPreferredCompression(length));
147 alreadySent_ = true; 158 alreadySent_ = true;
148 } 159 }
149 160
150 void RestApiOutput::Redirect(const std::string& path) 161 void RestApiOutput::Redirect(const std::string& path)
151 { 162 {