Mercurial > hg > orthanc
comparison Core/HttpServer/HttpOutput.cpp @ 1521:3606278d305e
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Aug 2015 13:37:24 +0200 |
parents | 8bd0d897763f |
children | f938f7779bcb |
comparison
equal
deleted
inserted
replaced
1520:4a503a8c7749 | 1521:3606278d305e |
---|---|
318 stateMachine_.SetHttpStatus(HttpStatus_401_Unauthorized); | 318 stateMachine_.SetHttpStatus(HttpStatus_401_Unauthorized); |
319 stateMachine_.AddHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\""); | 319 stateMachine_.AddHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\""); |
320 stateMachine_.SendBody(NULL, 0); | 320 stateMachine_.SendBody(NULL, 0); |
321 } | 321 } |
322 | 322 |
323 void HttpOutput::SendBody(const void* buffer, | 323 void HttpOutput::Answer(const void* buffer, |
324 size_t length) | 324 size_t length) |
325 { | 325 { |
326 if (length == 0) | 326 if (length == 0) |
327 { | 327 { |
328 stateMachine_.SendBody(NULL, 0); | 328 AnswerEmpty(); |
329 return; | |
330 } | |
331 | |
332 HttpCompression compression = GetPreferredCompression(length); | |
333 | |
334 if (compression == HttpCompression_None) | |
335 { | |
336 stateMachine_.SetContentLength(length); | |
337 stateMachine_.SendBody(buffer, length); | |
338 return; | |
339 } | |
340 | |
341 std::string compressed, encoding; | |
342 | |
343 switch (compression) | |
344 { | |
345 case HttpCompression_Deflate: | |
346 { | |
347 encoding = "deflate"; | |
348 ZlibCompressor compressor; | |
349 // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate" | |
350 compressor.SetPrefixWithUncompressedSize(false); | |
351 compressor.Compress(compressed, buffer, length); | |
352 break; | |
353 } | |
354 | |
355 case HttpCompression_Gzip: | |
356 { | |
357 encoding = "gzip"; | |
358 GzipCompressor compressor; | |
359 compressor.Compress(compressed, buffer, length); | |
360 break; | |
361 } | |
362 | |
363 default: | |
364 throw OrthancException(ErrorCode_InternalError); | |
365 } | |
366 | |
367 LOG(TRACE) << "Compressing a HTTP answer using " << encoding; | |
368 | |
369 // The body is empty, do not use HTTP compression | |
370 if (compressed.size() == 0) | |
371 { | |
372 AnswerEmpty(); | |
329 } | 373 } |
330 else | 374 else |
331 { | 375 { |
332 HttpCompression compression = GetPreferredCompression(length); | 376 stateMachine_.AddHeader("Content-Encoding", encoding); |
333 | 377 stateMachine_.SetContentLength(compressed.size()); |
334 switch (compression) | 378 stateMachine_.SendBody(compressed.c_str(), compressed.size()); |
335 { | 379 } |
336 case HttpCompression_None: | 380 |
337 { | 381 stateMachine_.CloseBody(); |
338 stateMachine_.SendBody(buffer, length); | 382 } |
339 break; | 383 |
340 } | 384 |
341 | 385 void HttpOutput::Answer(const std::string& str) |
342 case HttpCompression_Gzip: | 386 { |
343 case HttpCompression_Deflate: | 387 Answer(str.size() == 0 ? NULL : str.c_str(), str.size()); |
344 { | 388 } |
345 std::string compressed, encoding; | 389 |
346 | 390 |
347 if (compression == HttpCompression_Deflate) | 391 void HttpOutput::AnswerEmpty() |
348 { | |
349 encoding = "deflate"; | |
350 ZlibCompressor compressor; | |
351 // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate" | |
352 compressor.SetPrefixWithUncompressedSize(false); | |
353 compressor.Compress(compressed, buffer, length); | |
354 } | |
355 else | |
356 { | |
357 encoding = "gzip"; | |
358 GzipCompressor compressor; | |
359 compressor.Compress(compressed, buffer, length); | |
360 } | |
361 | |
362 LOG(TRACE) << "Compressing a HTTP answer using " << encoding; | |
363 | |
364 // The body is empty, do not use Deflate compression | |
365 if (compressed.size() == 0) | |
366 { | |
367 stateMachine_.SendBody(NULL, 0); | |
368 } | |
369 else | |
370 { | |
371 stateMachine_.AddHeader("Content-Encoding", encoding); | |
372 stateMachine_.SendBody(compressed.c_str(), compressed.size()); | |
373 } | |
374 | |
375 break; | |
376 } | |
377 | |
378 default: | |
379 throw OrthancException(ErrorCode_NotImplemented); | |
380 } | |
381 } | |
382 } | |
383 | |
384 void HttpOutput::SendBody(const std::string& str) | |
385 { | |
386 SendBody(str.size() == 0 ? NULL : str.c_str(), str.size()); | |
387 } | |
388 | |
389 void HttpOutput::SendEmptyBody() | |
390 { | 392 { |
391 stateMachine_.SetContentLength(0); | 393 stateMachine_.SetContentLength(0); |
392 stateMachine_.SendBody(NULL, 0); | 394 stateMachine_.SendBody(NULL, 0); |
393 stateMachine_.CloseBody(); | 395 stateMachine_.CloseBody(); |
394 } | 396 } |