comparison Core/RestApi/RestApiOutput.cpp @ 1514:d73a2178b319

support of deflate and gzip content-types
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Aug 2015 16:43:59 +0200
parents 7962563129c9
children c94353fbd4e9
comparison
equal deleted inserted replaced
1513:fe07f82d83d3 1514:d73a2178b319
43 { 43 {
44 RestApiOutput::RestApiOutput(HttpOutput& output, 44 RestApiOutput::RestApiOutput(HttpOutput& output,
45 HttpMethod method) : 45 HttpMethod method) :
46 output_(output), 46 output_(output),
47 method_(method), 47 method_(method),
48 compression_(HttpCompression_None), 48 allowDeflateCompression_(false),
49 allowGzipCompression_(false),
49 convertJsonToXml_(false) 50 convertJsonToXml_(false)
50 { 51 {
51 alreadySent_ = false; 52 alreadySent_ = false;
52 } 53 }
53 54
76 { 77 {
77 throw OrthancException(ErrorCode_BadSequenceOfCalls); 78 throw OrthancException(ErrorCode_BadSequenceOfCalls);
78 } 79 }
79 } 80 }
80 81
82
83 HttpCompression RestApiOutput::GetPreferredCompression() const
84 {
85 if (allowGzipCompression_)
86 {
87 return HttpCompression_Gzip;
88 }
89 else if (allowDeflateCompression_)
90 {
91 return HttpCompression_Deflate;
92 }
93 else
94 {
95 return HttpCompression_None;
96 }
97 }
98
99
81 void RestApiOutput::AnswerFile(HttpFileSender& sender) 100 void RestApiOutput::AnswerFile(HttpFileSender& sender)
82 { 101 {
83 CheckStatus(); 102 CheckStatus();
84 sender.Send(output_); 103 sender.Send(output_);
85 alreadySent_ = true; 104 alreadySent_ = true;
93 { 112 {
94 #if ORTHANC_PUGIXML_ENABLED == 1 113 #if ORTHANC_PUGIXML_ENABLED == 1
95 std::string s; 114 std::string s;
96 Toolbox::JsonToXml(s, value); 115 Toolbox::JsonToXml(s, value);
97 output_.SetContentType("application/xml"); 116 output_.SetContentType("application/xml");
98 output_.SendBody(s, compression_); 117 output_.SendBody(s, GetPreferredCompression());
99 #else 118 #else
100 LOG(ERROR) << "Orthanc was compiled without XML support"; 119 LOG(ERROR) << "Orthanc was compiled without XML support";
101 throw OrthancException(ErrorCode_InternalError); 120 throw OrthancException(ErrorCode_InternalError);
102 #endif 121 #endif
103 } 122 }
104 else 123 else
105 { 124 {
106 Json::StyledWriter writer; 125 Json::StyledWriter writer;
107 output_.SetContentType("application/json"); 126 output_.SetContentType("application/json");
108 output_.SendBody(writer.write(value), compression_); 127 output_.SendBody(writer.write(value), GetPreferredCompression());
109 } 128 }
110 129
111 alreadySent_ = true; 130 alreadySent_ = true;
112 } 131 }
113 132
114 void RestApiOutput::AnswerBuffer(const std::string& buffer, 133 void RestApiOutput::AnswerBuffer(const std::string& buffer,
115 const std::string& contentType) 134 const std::string& contentType)
116 { 135 {
117 CheckStatus(); 136 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(),
118 output_.SetContentType(contentType.c_str()); 137 buffer.size(), contentType);
119 output_.SendBody(buffer, compression_);
120 alreadySent_ = true;
121 } 138 }
122 139
123 void RestApiOutput::AnswerBuffer(const void* buffer, 140 void RestApiOutput::AnswerBuffer(const void* buffer,
124 size_t length, 141 size_t length,
125 const std::string& contentType) 142 const std::string& contentType)
126 { 143 {
127 CheckStatus(); 144 CheckStatus();
128 output_.SetContentType(contentType.c_str()); 145 output_.SetContentType(contentType.c_str());
129 output_.SendBody(buffer, length, compression_); 146 output_.SendBody(buffer, length, GetPreferredCompression());
130 alreadySent_ = true; 147 alreadySent_ = true;
131 } 148 }
132 149
133 void RestApiOutput::Redirect(const std::string& path) 150 void RestApiOutput::Redirect(const std::string& path)
134 { 151 {