comparison Core/HttpServer/HttpOutput.cpp @ 1519:8bd0d897763f

refactoring: IHttpStreamAnswer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 13:15:16 +0200
parents 4f8c8ef114db
children 3606278d305e
comparison
equal deleted inserted replaced
1518:eb46cc06389a 1519:8bd0d897763f
213 state_ = State_Done; 213 state_ = State_Done;
214 } 214 }
215 } 215 }
216 216
217 217
218 void HttpOutput::StateMachine::CloseBody()
219 {
220 switch (state_)
221 {
222 case State_WritingHeader:
223 LOG(ERROR) << "Closing the HTTP body, but the header has not been sent yet";
224 throw OrthancException(ErrorCode_BadSequenceOfCalls);
225
226 case State_WritingBody:
227 if (!hasContentLength_ ||
228 contentPosition_ == contentLength_)
229 {
230 state_ = State_Done;
231 }
232 else
233 {
234 LOG(ERROR) << "The body size has not reached what was declared with SetContentSize()";
235 throw OrthancException(ErrorCode_BadSequenceOfCalls);
236 }
237
238 break;
239
240 case State_WritingMultipart:
241 LOG(ERROR) << "Cannot invoke CloseBody() with multipart outputs";
242 throw OrthancException(ErrorCode_BadSequenceOfCalls);
243
244 case State_Done:
245 return; // Ignore
246
247 default:
248 throw OrthancException(ErrorCode_InternalError);
249 }
250 }
251
252
218 HttpCompression HttpOutput::GetPreferredCompression(size_t bodySize) const 253 HttpCompression HttpOutput::GetPreferredCompression(size_t bodySize) const
219 { 254 {
220 #if 0 255 #if 0
221 // TODO Do not compress small files? 256 // TODO Do not compress small files?
222 if (bodySize < 512) 257 if (bodySize < 512)
349 void HttpOutput::SendBody(const std::string& str) 384 void HttpOutput::SendBody(const std::string& str)
350 { 385 {
351 SendBody(str.size() == 0 ? NULL : str.c_str(), str.size()); 386 SendBody(str.size() == 0 ? NULL : str.c_str(), str.size());
352 } 387 }
353 388
354 void HttpOutput::SendBody() 389 void HttpOutput::SendEmptyBody()
355 { 390 {
391 stateMachine_.SetContentLength(0);
356 stateMachine_.SendBody(NULL, 0); 392 stateMachine_.SendBody(NULL, 0);
393 stateMachine_.CloseBody();
357 } 394 }
358 395
359 396
360 void HttpOutput::StateMachine::StartMultipart(const std::string& subType, 397 void HttpOutput::StateMachine::StartMultipart(const std::string& subType,
361 const std::string& contentType) 398 const std::string& contentType)
458 else 495 else
459 { 496 {
460 stateMachine_.SendMultipartItem(NULL, 0); 497 stateMachine_.SendMultipartItem(NULL, 0);
461 } 498 }
462 } 499 }
500
501
502 void HttpOutput::Answer(IHttpStreamAnswer& stream)
503 {
504 stateMachine_.SetContentLength(stream.GetContentLength());
505
506 std::string contentType = stream.GetContentType();
507 if (contentType.empty())
508 {
509 contentType = "application/octet-stream";
510 }
511
512 stateMachine_.SetContentType(contentType.c_str());
513
514 std::string filename;
515 if (stream.HasContentFilename(filename))
516 {
517 SetContentFilename(filename.c_str());
518 }
519
520 HttpCompression compression = stream.GetHttpCompression(isGzipAllowed_, isDeflateAllowed_);
521
522 switch (compression)
523 {
524 case HttpCompression_None:
525 break;
526
527 case HttpCompression_Gzip:
528 stateMachine_.AddHeader("Content-Encoding", "gzip");
529 break;
530
531 case HttpCompression_Deflate:
532 stateMachine_.AddHeader("Content-Encoding", "deflate");
533 break;
534
535 default:
536 throw OrthancException(ErrorCode_ParameterOutOfRange);
537 }
538
539 while (stream.ReadNextChunk())
540 {
541 stateMachine_.SendBody(stream.GetChunkContent(),
542 stream.GetChunkSize());
543 }
544
545 stateMachine_.CloseBody();
546 }
547
463 } 548 }