comparison Core/HttpServer/MongooseServer.cpp @ 1113:ba5c0908600c

Refactoring of HttpOutput ("Content-Length" header is now always sent)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Sep 2014 15:51:20 +0200
parents a119f9ae3640
children da56a7916e8a
comparison
equal deleted inserted replaced
1112:a119f9ae3640 1113:ba5c0908600c
583 583
584 // Compute the HTTP method, taking method faking into consideration 584 // Compute the HTTP method, taking method faking into consideration
585 HttpMethod method; 585 HttpMethod method;
586 if (!ExtractMethod(method, request, headers, argumentsGET)) 586 if (!ExtractMethod(method, request, headers, argumentsGET))
587 { 587 {
588 output.SendHeader(HttpStatus_400_BadRequest); 588 output.SendStatus(HttpStatus_400_BadRequest);
589 return; 589 return;
590 } 590 }
591 591
592 592
593 // Authenticate this connection 593 // Authenticate this connection
594 if (that->IsAuthenticationEnabled() && 594 if (that->IsAuthenticationEnabled() &&
595 !Authorize(*that, headers, output)) 595 !Authorize(*that, headers, output))
596 { 596 {
597 output.SendUnauthorized(ORTHANC_REALM);
597 return; 598 return;
598 } 599 }
599 600
600 601
601 // Apply the filter, if it is installed 602 // Apply the filter, if it is installed
647 } 648 }
648 649
649 switch (status) 650 switch (status)
650 { 651 {
651 case PostDataStatus_NoLength: 652 case PostDataStatus_NoLength:
652 output.SendHeader(HttpStatus_411_LengthRequired); 653 output.SendStatus(HttpStatus_411_LengthRequired);
653 return; 654 return;
654 655
655 case PostDataStatus_Failure: 656 case PostDataStatus_Failure:
656 output.SendHeader(HttpStatus_400_BadRequest); 657 output.SendStatus(HttpStatus_400_BadRequest);
657 return; 658 return;
658 659
659 case PostDataStatus_Pending: 660 case PostDataStatus_Pending:
660 output.AnswerBufferWithContentType(NULL, 0, ""); 661 output.SendBody();
661 return; 662 return;
662 663
663 default: 664 default:
664 break; 665 break;
665 } 666 }
672 { 673 {
673 Toolbox::SplitUriComponents(uri, request->uri); 674 Toolbox::SplitUriComponents(uri, request->uri);
674 } 675 }
675 catch (OrthancException) 676 catch (OrthancException)
676 { 677 {
677 output.SendHeader(HttpStatus_400_BadRequest); 678 output.SendStatus(HttpStatus_400_BadRequest);
678 return; 679 return;
679 } 680 }
680 681
681 682
682 // Loop over the candidate handlers for this URI 683 // Loop over the candidate handlers for this URI
692 } 693 }
693 catch (OrthancException& e) 694 catch (OrthancException& e)
694 { 695 {
695 // Using this candidate handler results in an exception 696 // Using this candidate handler results in an exception
696 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); 697 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
698
699 switch (e.GetErrorCode())
700 {
701 case ErrorCode_InexistentFile:
702 case ErrorCode_InexistentItem:
703 case ErrorCode_UnknownResource:
704 output.SendStatus(HttpStatus_404_NotFound);
705 break;
706
707 case ErrorCode_BadRequest:
708 case ErrorCode_UriSyntax:
709 output.SendStatus(HttpStatus_400_BadRequest);
710 break;
711
712 default:
713 output.SendStatus(HttpStatus_500_InternalServerError);
714 }
715
697 return; 716 return;
698 } 717 }
699 catch (boost::bad_lexical_cast&) 718 catch (boost::bad_lexical_cast&)
700 { 719 {
701 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast"; 720 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast";
721 output.SendStatus(HttpStatus_400_BadRequest);
702 return; 722 return;
703 } 723 }
704 catch (std::runtime_error&) 724 catch (std::runtime_error&)
705 { 725 {
706 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request"; 726 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request";
727 output.SendStatus(HttpStatus_400_BadRequest);
707 return; 728 return;
708 } 729 }
709 } 730 }
710 731
711 if (!found) 732 if (!found)
712 { 733 {
713 try 734 output.SendStatus(HttpStatus_404_NotFound);
714 {
715 output.SendHeader(HttpStatus_404_NotFound);
716 }
717 catch (OrthancException&)
718 {
719 }
720 } 735 }
721 } 736 }
722 737
723 738
724 #if MONGOOSE_USE_CALLBACKS == 0 739 #if MONGOOSE_USE_CALLBACKS == 0