Mercurial > hg > orthanc
comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3413:f09bfdea3fc3
rename
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Jun 2019 14:02:57 +0200 |
parents | 3575466a3e57 |
children | b9cba6a91780 |
comparison
equal
deleted
inserted
replaced
3412:327bace7e98a | 3413:f09bfdea3fc3 |
---|---|
2155 } | 2155 } |
2156 } | 2156 } |
2157 }; | 2157 }; |
2158 | 2158 |
2159 | 2159 |
2160 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2160 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2161 static OrthancPluginErrorCode AnswerAddHeaderCallback(void* answer, | 2161 static OrthancPluginErrorCode AnswerAddHeaderCallback(void* answer, |
2162 const char* key, | 2162 const char* key, |
2163 const char* value) | 2163 const char* value) |
2164 { | 2164 { |
2165 assert(answer != NULL && key != NULL && value != NULL); | 2165 assert(answer != NULL && key != NULL && value != NULL); |
2179 } | 2179 } |
2180 } | 2180 } |
2181 #endif | 2181 #endif |
2182 | 2182 |
2183 | 2183 |
2184 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2184 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2185 static OrthancPluginErrorCode AnswerAddChunkCallback(void* answer, | 2185 static OrthancPluginErrorCode AnswerAddChunkCallback(void* answer, |
2186 const void* data, | 2186 const void* data, |
2187 uint32_t size) | 2187 uint32_t size) |
2188 { | 2188 { |
2189 assert(answer != NULL); | 2189 assert(answer != NULL); |
2208 HttpClient::HttpClient() : | 2208 HttpClient::HttpClient() : |
2209 httpStatus_(0), | 2209 httpStatus_(0), |
2210 method_(OrthancPluginHttpMethod_Get), | 2210 method_(OrthancPluginHttpMethod_Get), |
2211 timeout_(0), | 2211 timeout_(0), |
2212 pkcs11_(false), | 2212 pkcs11_(false), |
2213 streamingBody_(NULL) | 2213 chunkedBody_(NULL) |
2214 { | 2214 { |
2215 } | 2215 } |
2216 | 2216 |
2217 | 2217 |
2218 void HttpClient::SetCredentials(const std::string& username, | 2218 void HttpClient::SetCredentials(const std::string& username, |
2248 } | 2248 } |
2249 | 2249 |
2250 | 2250 |
2251 void HttpClient::ClearBody() | 2251 void HttpClient::ClearBody() |
2252 { | 2252 { |
2253 body_.clear(); | 2253 fullBody_.clear(); |
2254 streamingBody_ = NULL; | 2254 chunkedBody_ = NULL; |
2255 } | 2255 } |
2256 | 2256 |
2257 | 2257 |
2258 void HttpClient::SwapBody(std::string& body) | 2258 void HttpClient::SwapBody(std::string& body) |
2259 { | 2259 { |
2260 body_.swap(body); | 2260 fullBody_.swap(body); |
2261 streamingBody_ = NULL; | 2261 chunkedBody_ = NULL; |
2262 } | 2262 } |
2263 | 2263 |
2264 | 2264 |
2265 void HttpClient::SetBody(const std::string& body) | 2265 void HttpClient::SetBody(const std::string& body) |
2266 { | 2266 { |
2267 body_ = body; | 2267 fullBody_ = body; |
2268 streamingBody_ = NULL; | 2268 chunkedBody_ = NULL; |
2269 } | 2269 } |
2270 | 2270 |
2271 | 2271 |
2272 void HttpClient::SetBody(IRequestBody& body) | 2272 void HttpClient::SetBody(IRequestBody& body) |
2273 { | 2273 { |
2274 body_.clear(); | 2274 fullBody_.clear(); |
2275 streamingBody_ = &body; | 2275 chunkedBody_ = &body; |
2276 } | 2276 } |
2277 | 2277 |
2278 | 2278 |
2279 namespace | 2279 namespace |
2280 { | 2280 { |
2406 size_ += chunk.size(); | 2406 size_ += chunk.size(); |
2407 } | 2407 } |
2408 }; | 2408 }; |
2409 | 2409 |
2410 | 2410 |
2411 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2411 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2412 class MemoryAnswer : public HttpClient::IAnswer | 2412 class MemoryAnswer : public HttpClient::IAnswer |
2413 { | 2413 { |
2414 private: | 2414 private: |
2415 HttpClient::HttpHeaders headers_; | 2415 HttpClient::HttpHeaders headers_; |
2416 ChunkedBuffer body_; | 2416 ChunkedBuffer body_; |
2440 }; | 2440 }; |
2441 #endif | 2441 #endif |
2442 } | 2442 } |
2443 | 2443 |
2444 | 2444 |
2445 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2445 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2446 void HttpClient::ExecuteWithStream(uint16_t& httpStatus, | 2446 void HttpClient::ExecuteWithStream(uint16_t& httpStatus, |
2447 IAnswer& answer, | 2447 IAnswer& answer, |
2448 IRequestBody& body) const | 2448 IRequestBody& body) const |
2449 { | 2449 { |
2450 HeadersWrapper h(headers_); | 2450 HeadersWrapper h(headers_); |
2466 h.AddStaticString("Transfer-Encoding", "chunked"); | 2466 h.AddStaticString("Transfer-Encoding", "chunked"); |
2467 } | 2467 } |
2468 | 2468 |
2469 RequestBodyWrapper request(body); | 2469 RequestBodyWrapper request(body); |
2470 | 2470 |
2471 OrthancPluginErrorCode error = OrthancPluginStreamingHttpClient( | 2471 OrthancPluginErrorCode error = OrthancPluginChunkedHttpClient( |
2472 GetGlobalContext(), | 2472 GetGlobalContext(), |
2473 &answer, | 2473 &answer, |
2474 AnswerAddChunkCallback, | 2474 AnswerAddChunkCallback, |
2475 AnswerAddHeaderCallback, | 2475 AnswerAddHeaderCallback, |
2476 &httpStatus, | 2476 &httpStatus, |
2560 | 2560 |
2561 answerBodyBuffer.ToString(answerBody); | 2561 answerBodyBuffer.ToString(answerBody); |
2562 } | 2562 } |
2563 | 2563 |
2564 | 2564 |
2565 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2565 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2566 void HttpClient::Execute(IAnswer& answer) | 2566 void HttpClient::Execute(IAnswer& answer) |
2567 { | 2567 { |
2568 if (streamingBody_ != NULL) | 2568 if (chunkedBody_ != NULL) |
2569 { | 2569 { |
2570 ExecuteWithStream(httpStatus_, answer, *streamingBody_); | 2570 ExecuteWithStream(httpStatus_, answer, *chunkedBody_); |
2571 } | 2571 } |
2572 else | 2572 else |
2573 { | 2573 { |
2574 MemoryRequestBody wrapper(body_); | 2574 MemoryRequestBody wrapper(fullBody_); |
2575 ExecuteWithStream(httpStatus_, answer, wrapper); | 2575 ExecuteWithStream(httpStatus_, answer, wrapper); |
2576 } | 2576 } |
2577 } | 2577 } |
2578 #endif | 2578 #endif |
2579 | 2579 |
2580 | 2580 |
2581 void HttpClient::Execute(HttpHeaders& answerHeaders /* out */, | 2581 void HttpClient::Execute(HttpHeaders& answerHeaders /* out */, |
2582 std::string& answerBody /* out */) | 2582 std::string& answerBody /* out */) |
2583 { | 2583 { |
2584 #if HAS_ORTHANC_PLUGIN_STREAMING_HTTP_CLIENT == 1 | 2584 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 |
2585 MemoryAnswer answer; | 2585 MemoryAnswer answer; |
2586 Execute(answer); | 2586 Execute(answer); |
2587 answerHeaders = answer.GetHeaders(); | 2587 answerHeaders = answer.GetHeaders(); |
2588 answer.GetBody().Flatten(answerBody); | 2588 answer.GetBody().Flatten(answerBody); |
2589 | 2589 |
2590 #else | 2590 #else |
2591 // Compatibility mode for Orthanc SDK <= 1.5.6. This results in | 2591 // Compatibility mode for Orthanc SDK <= 1.5.6. This results in |
2592 // higher memory usage (all chunks from the body request are sent | 2592 // higher memory usage (all chunks from the body request are sent |
2593 // at once) | 2593 // at once) |
2594 | 2594 |
2595 if (streamingBody_ != NULL) | 2595 if (chunkedBody_ != NULL) |
2596 { | 2596 { |
2597 ChunkedBuffer buffer; | 2597 ChunkedBuffer buffer; |
2598 | 2598 |
2599 std::string chunk; | 2599 std::string chunk; |
2600 while (streamingBody_->ReadNextChunk(chunk)) | 2600 while (chunkedBody_->ReadNextChunk(chunk)) |
2601 { | 2601 { |
2602 buffer.AddChunk(chunk); | 2602 buffer.AddChunk(chunk); |
2603 } | 2603 } |
2604 | 2604 |
2605 std::string body; | 2605 std::string body; |
2607 | 2607 |
2608 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, body); | 2608 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, body); |
2609 } | 2609 } |
2610 else | 2610 else |
2611 { | 2611 { |
2612 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, body_); | 2612 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, fullBody_); |
2613 } | 2613 } |
2614 #endif | 2614 #endif |
2615 } | 2615 } |
2616 | 2616 |
2617 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ | 2617 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ |