comparison Core/HttpServer/HttpOutput.cpp @ 1882:5cf2bd0abfa2

OrthancPluginSendMultipartItem2 for DICOMweb
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Dec 2015 09:29:05 +0100
parents a5cd02894534
children b1291df2f780
comparison
equal deleted inserted replaced
1881:d2efbe076653 1882:5cf2bd0abfa2
447 stream_.Send(true, header.c_str(), header.size()); 447 stream_.Send(true, header.c_str(), header.size());
448 state_ = State_WritingMultipart; 448 state_ = State_WritingMultipart;
449 } 449 }
450 450
451 451
452 void HttpOutput::StateMachine::SendMultipartItem(const void* item, size_t length) 452 void HttpOutput::StateMachine::SendMultipartItem(const void* item,
453 { 453 size_t length,
454 const std::map<std::string, std::string>& headers)
455 {
456 if (state_ != State_WritingMultipart)
457 {
458 throw OrthancException(ErrorCode_BadSequenceOfCalls);
459 }
460
454 std::string header = "--" + multipartBoundary_ + "\r\n"; 461 std::string header = "--" + multipartBoundary_ + "\r\n";
455 header += "Content-Type: " + multipartContentType_ + "\r\n"; 462
456 header += "Content-Length: " + boost::lexical_cast<std::string>(length) + "\r\n"; 463 bool hasContentType = false;
457 header += "MIME-Version: 1.0\r\n\r\n"; 464 bool hasContentLength = false;
465 bool hasMimeVersion = false;
466
467 for (std::map<std::string, std::string>::const_iterator
468 it = headers.begin(); it != headers.end(); ++it)
469 {
470 header += it->first + ": " + it->second + "\r\n";
471
472 std::string tmp;
473 Toolbox::ToLowerCase(tmp, it->first);
474
475 if (tmp == "content-type")
476 {
477 hasContentType = true;
478 }
479
480 if (tmp == "content-length")
481 {
482 hasContentLength = true;
483 }
484
485 if (tmp == "mime-version")
486 {
487 hasMimeVersion = true;
488 }
489 }
490
491 if (!hasContentType)
492 {
493 header += "Content-Type: " + multipartContentType_ + "\r\n";
494 }
495
496 if (!hasContentLength)
497 {
498 header += "Content-Length: " + boost::lexical_cast<std::string>(length) + "\r\n";
499 }
500
501 if (!hasMimeVersion)
502 {
503 header += "MIME-Version: 1.0\r\n\r\n";
504 }
458 505
459 stream_.Send(false, header.c_str(), header.size()); 506 stream_.Send(false, header.c_str(), header.size());
460 507
461 if (length > 0) 508 if (length > 0)
462 { 509 {
463 stream_.Send(false, item, length); 510 stream_.Send(false, item, length);
464 } 511 }
465 512
466 stream_.Send(false, "\r\n", 2); 513 stream_.Send(false, "\r\n", 2);
467 } 514 }
468 515
469 516
470 void HttpOutput::StateMachine::CloseMultipart() 517 void HttpOutput::StateMachine::CloseMultipart()
471 { 518 {
487 534
488 state_ = State_Done; 535 state_ = State_Done;
489 } 536 }
490 537
491 538
492 void HttpOutput::SendMultipartItem(const std::string& item)
493 {
494 if (item.size() > 0)
495 {
496 stateMachine_.SendMultipartItem(item.c_str(), item.size());
497 }
498 else
499 {
500 stateMachine_.SendMultipartItem(NULL, 0);
501 }
502 }
503
504
505 void HttpOutput::Answer(IHttpStreamAnswer& stream) 539 void HttpOutput::Answer(IHttpStreamAnswer& stream)
506 { 540 {
507 HttpCompression compression = stream.SetupHttpCompression(isGzipAllowed_, isDeflateAllowed_); 541 HttpCompression compression = stream.SetupHttpCompression(isGzipAllowed_, isDeflateAllowed_);
508 542
509 switch (compression) 543 switch (compression)