# HG changeset patch # User Sebastien Jodogne # Date 1439293044 -7200 # Node ID 3606278d305e3af92546d24035446c8ff920ca57 # Parent 4a503a8c7749004548e02460f2d46cf0b292fed2 refactoring diff -r 4a503a8c7749 -r 3606278d305e Core/HttpServer/EmbeddedResourceHttpHandler.cpp --- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -81,7 +81,7 @@ size_t size = EmbeddedResources::GetDirectoryResourceSize(resourceId_, resourcePath.c_str()); output.SetContentType(contentType.c_str()); - output.SendBody(buffer, size); + output.Answer(buffer, size); } catch (OrthancException&) { diff -r 4a503a8c7749 -r 3606278d305e Core/HttpServer/FilesystemHttpHandler.cpp --- a/Core/HttpServer/FilesystemHttpHandler.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/HttpServer/FilesystemHttpHandler.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -104,7 +104,7 @@ s += ""; output.SetContentType("text/html"); - output.SendBody(s); + output.Answer(s); } diff -r 4a503a8c7749 -r 3606278d305e Core/HttpServer/HttpOutput.cpp --- a/Core/HttpServer/HttpOutput.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/HttpServer/HttpOutput.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -320,73 +320,75 @@ stateMachine_.SendBody(NULL, 0); } - void HttpOutput::SendBody(const void* buffer, - size_t length) + void HttpOutput::Answer(const void* buffer, + size_t length) { if (length == 0) { - stateMachine_.SendBody(NULL, 0); + AnswerEmpty(); + return; + } + + HttpCompression compression = GetPreferredCompression(length); + + if (compression == HttpCompression_None) + { + stateMachine_.SetContentLength(length); + stateMachine_.SendBody(buffer, length); + return; + } + + std::string compressed, encoding; + + switch (compression) + { + case HttpCompression_Deflate: + { + encoding = "deflate"; + ZlibCompressor compressor; + // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate" + compressor.SetPrefixWithUncompressedSize(false); + compressor.Compress(compressed, buffer, length); + break; + } + + case HttpCompression_Gzip: + { + encoding = "gzip"; + GzipCompressor compressor; + compressor.Compress(compressed, buffer, length); + break; + } + + default: + throw OrthancException(ErrorCode_InternalError); + } + + LOG(TRACE) << "Compressing a HTTP answer using " << encoding; + + // The body is empty, do not use HTTP compression + if (compressed.size() == 0) + { + AnswerEmpty(); } else { - HttpCompression compression = GetPreferredCompression(length); - - switch (compression) - { - case HttpCompression_None: - { - stateMachine_.SendBody(buffer, length); - break; - } - - case HttpCompression_Gzip: - case HttpCompression_Deflate: - { - std::string compressed, encoding; + stateMachine_.AddHeader("Content-Encoding", encoding); + stateMachine_.SetContentLength(compressed.size()); + stateMachine_.SendBody(compressed.c_str(), compressed.size()); + } - if (compression == HttpCompression_Deflate) - { - encoding = "deflate"; - ZlibCompressor compressor; - // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate" - compressor.SetPrefixWithUncompressedSize(false); - compressor.Compress(compressed, buffer, length); - } - else - { - encoding = "gzip"; - GzipCompressor compressor; - compressor.Compress(compressed, buffer, length); - } - - LOG(TRACE) << "Compressing a HTTP answer using " << encoding; - - // The body is empty, do not use Deflate compression - if (compressed.size() == 0) - { - stateMachine_.SendBody(NULL, 0); - } - else - { - stateMachine_.AddHeader("Content-Encoding", encoding); - stateMachine_.SendBody(compressed.c_str(), compressed.size()); - } - - break; - } - - default: - throw OrthancException(ErrorCode_NotImplemented); - } - } + stateMachine_.CloseBody(); } - void HttpOutput::SendBody(const std::string& str) + + void HttpOutput::Answer(const std::string& str) { - SendBody(str.size() == 0 ? NULL : str.c_str(), str.size()); + Answer(str.size() == 0 ? NULL : str.c_str(), str.size()); } - void HttpOutput::SendEmptyBody() + + void HttpOutput::AnswerEmpty() { stateMachine_.SetContentLength(0); stateMachine_.SendBody(NULL, 0); diff -r 4a503a8c7749 -r 3606278d305e Core/HttpServer/HttpOutput.h --- a/Core/HttpServer/HttpOutput.h Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/HttpServer/HttpOutput.h Tue Aug 11 13:37:24 2015 +0200 @@ -170,12 +170,12 @@ stateMachine_.AddHeader(key, value); } - void SendBody(const void* buffer, - size_t length); + void Answer(const void* buffer, + size_t length); - void SendBody(const std::string& str); + void Answer(const std::string& str); - void SendEmptyBody(); + void AnswerEmpty(); void SendMethodNotAllowed(const std::string& allowed); diff -r 4a503a8c7749 -r 3606278d305e Core/HttpServer/MongooseServer.cpp --- a/Core/HttpServer/MongooseServer.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/HttpServer/MongooseServer.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -695,7 +695,7 @@ return; case PostDataStatus_Pending: - output.SendEmptyBody(); + output.AnswerEmpty(); return; default: diff -r 4a503a8c7749 -r 3606278d305e Core/RestApi/RestApiOutput.cpp --- a/Core/RestApi/RestApiOutput.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Core/RestApi/RestApiOutput.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -95,7 +95,7 @@ std::string s; Toolbox::JsonToXml(s, value); output_.SetContentType("application/xml"); - output_.SendBody(s); + output_.Answer(s); #else LOG(ERROR) << "Orthanc was compiled without XML support"; throw OrthancException(ErrorCode_InternalError); @@ -105,7 +105,7 @@ { Json::StyledWriter writer; output_.SetContentType("application/json"); - output_.SendBody(writer.write(value)); + output_.Answer(writer.write(value)); } alreadySent_ = true; @@ -124,7 +124,7 @@ { CheckStatus(); output_.SetContentType(contentType.c_str()); - output_.SendBody(buffer, length); + output_.Answer(buffer, length); alreadySent_ = true; } diff -r 4a503a8c7749 -r 3606278d305e OrthancServer/ParsedDicomFile.cpp --- a/OrthancServer/ParsedDicomFile.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/OrthancServer/ParsedDicomFile.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -273,7 +273,7 @@ length_(element.getLength(transferSyntax)), offset_(0) { - static const size_t CHUNK_SIZE = 64 * 1024; // Use a 64KB chunk + static const size_t CHUNK_SIZE = 64 * 1024; // Use chunks of max 64KB chunk_.resize(CHUNK_SIZE); } diff -r 4a503a8c7749 -r 3606278d305e Plugins/Engine/OrthancPlugins.cpp --- a/Plugins/Engine/OrthancPlugins.cpp Tue Aug 11 13:26:42 2015 +0200 +++ b/Plugins/Engine/OrthancPlugins.cpp Tue Aug 11 13:37:24 2015 +0200 @@ -477,7 +477,7 @@ HttpOutput* translatedOutput = reinterpret_cast(p.output); translatedOutput->SetContentType(p.mimeType); - translatedOutput->SendBody(p.answer, p.answerSize); + translatedOutput->Answer(p.answer, p.answerSize); } @@ -583,7 +583,7 @@ writer.WriteToMemory(png, accessor); translatedOutput->SetContentType("image/png"); - translatedOutput->SendBody(png); + translatedOutput->Answer(png); }