comparison OrthancFramework/Sources/RestApi/RestApiOutput.cpp @ 5406:aaf7c49a9ddc am-http-compression

tentative to implement smart HTTP compression with detection of transfer syntax
author Alain Mazy <am@osimis.io>
date Sat, 04 Nov 2023 13:42:30 +0100
parents 0ea402b4d901
children
comparison
equal deleted inserted replaced
5405:62bb63346185 5406:aaf7c49a9ddc
25 #include "RestApiOutput.h" 25 #include "RestApiOutput.h"
26 26
27 #include "../Logging.h" 27 #include "../Logging.h"
28 #include "../OrthancException.h" 28 #include "../OrthancException.h"
29 #include "../Toolbox.h" 29 #include "../Toolbox.h"
30 #include "../SystemToolbox.h"
30 31
31 #include <boost/lexical_cast.hpp> 32 #include <boost/lexical_cast.hpp>
32 33
33 34
34 namespace Orthanc 35 namespace Orthanc
70 } 71 }
71 72
72 73
73 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream) 74 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream)
74 { 75 {
75 CheckStatus(); 76 AnswerStream(stream, ContentCompression_Unknown);
77 }
78
79 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream, ContentCompression contentCompression)
80 {
81 CheckStatus();
82 output_.SetContentCompression(contentCompression);
76 output_.Answer(stream); 83 output_.Answer(stream);
77 alreadySent_ = true; 84 alreadySent_ = true;
78 } 85 }
79 86
80 87
81 void RestApiOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream) 88 void RestApiOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream)
82 { 89 {
83 CheckStatus(); 90 AnswerWithoutBuffering(stream, ContentCompression_Unknown);
91 }
92
93 void RestApiOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream, ContentCompression contentCompression)
94 {
95 CheckStatus();
96 output_.SetContentCompression(contentCompression);
84 output_.AnswerWithoutBuffering(stream); 97 output_.AnswerWithoutBuffering(stream);
85 alreadySent_ = true; 98 alreadySent_ = true;
86 } 99 }
87 100
88 101
95 #if ORTHANC_ENABLE_PUGIXML == 1 108 #if ORTHANC_ENABLE_PUGIXML == 1
96 std::string s; 109 std::string s;
97 Toolbox::JsonToXml(s, value); 110 Toolbox::JsonToXml(s, value);
98 111
99 output_.SetContentType(MIME_XML_UTF8); 112 output_.SetContentType(MIME_XML_UTF8);
113 output_.SetContentCompression(ContentCompression_NotCompressed);
100 output_.Answer(s); 114 output_.Answer(s);
101 #else 115 #else
102 throw OrthancException(ErrorCode_InternalError, 116 throw OrthancException(ErrorCode_InternalError,
103 "Orthanc was compiled without XML support"); 117 "Orthanc was compiled without XML support");
104 #endif 118 #endif
105 } 119 }
106 else 120 else
107 { 121 {
108 std::string s; 122 std::string s;
109 Toolbox::WriteStyledJson(s, value); 123 Toolbox::WriteStyledJson(s, value);
110 output_.SetContentType(MIME_JSON_UTF8); 124 output_.SetContentType(MIME_JSON_UTF8);
125 output_.SetContentCompression(ContentCompression_NotCompressed);
111 output_.Answer(s); 126 output_.Answer(s);
112 } 127 }
113 128
114 alreadySent_ = true; 129 alreadySent_ = true;
115 } 130 }
116 131
117 void RestApiOutput::AnswerBuffer(const std::string& buffer, 132 void RestApiOutput::AnswerBuffer(const std::string& buffer,
118 MimeType contentType) 133 MimeType contentType)
119 { 134 {
135 AnswerBuffer(buffer, contentType, SystemToolbox::GuessContentCompression(contentType));
136 }
137
138
139 void RestApiOutput::AnswerBuffer(const std::string& buffer,
140 MimeType contentType,
141 ContentCompression contentCompression)
142 {
120 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(), 143 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(),
121 buffer.size(), contentType); 144 buffer.size(), contentType, contentCompression);
122 } 145 }
123 146
124 void RestApiOutput::AnswerBuffer(const void* buffer, 147 void RestApiOutput::AnswerBuffer(const void* buffer,
125 size_t length, 148 size_t length,
126 MimeType contentType) 149 MimeType contentType)
127 { 150 {
151 AnswerBuffer(buffer, length, contentType, SystemToolbox::GuessContentCompression(contentType));
152 }
153
154 void RestApiOutput::AnswerBuffer(const void* buffer,
155 size_t length,
156 MimeType contentType,
157 ContentCompression contentCompression)
158 {
128 CheckStatus(); 159 CheckStatus();
129 160
130 if (convertJsonToXml_ && 161 if (convertJsonToXml_ &&
131 contentType == MimeType_Json) 162 contentType == MimeType_Json)
132 { 163 {
142 } 173 }
143 } 174 }
144 else 175 else
145 { 176 {
146 output_.SetContentType(contentType); 177 output_.SetContentType(contentType);
178 output_.SetContentCompression(contentCompression);
147 output_.Answer(buffer, length); 179 output_.Answer(buffer, length);
148 alreadySent_ = true; 180 alreadySent_ = true;
149 } 181 }
150 } 182 }
151 183