void HttpOutput::Answer(IHttpStreamAnswer& stream) { HttpCompression compression = stream.SetupHttpCompression(isGzipAllowed_, isDeflateAllowed_); switch (compression) { case HttpCompression_None: break; case HttpCompression_Gzip: stateMachine_.AddHeader("Content-Encoding", "gzip"); break; case HttpCompression_Deflate: stateMachine_.AddHeader("Content-Encoding", "deflate"); break; default: throw OrthancException(ErrorCode_ParameterOutOfRange); } stateMachine_.SetContentLength(stream.GetContentLength()); std::string contentType = stream.GetContentType(); if (contentType.empty()) { contentType = "application/octet-stream"; } stateMachine_.SetContentType(contentType.c_str()); std::string filename; if (stream.HasContentFilename(filename)) { SetContentFilename(filename.c_str()); } while (stream.ReadNextChunk()) { stateMachine_.SendBody(stream.GetChunkContent(), stream.GetChunkSize()); } stateMachine_.CloseBody(); }
static bool ReadAllStream(std::string& result, IHttpStreamAnswer& stream, bool allowGzip = false, bool allowDeflate = false) { stream.SetupHttpCompression(allowGzip, allowDeflate); result.resize(static_cast<size_t>(stream.GetContentLength())); size_t pos = 0; while (stream.ReadNextChunk()) { size_t s = stream.GetChunkSize(); if (pos + s > result.size()) { return false; } memcpy(&result[pos], stream.GetChunkContent(), s); pos += s; } return pos == result.size(); }