comparison Core/HttpServer/MongooseServer.cpp @ 1443:895ab369d63c

refactoring: OrthancHttpHandler
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 11:30:19 +0200
parents f3672356c121
children 8dc80ba768aa
comparison
equal deleted inserted replaced
1442:4ff8dd753d79 1443:895ab369d63c
550 550
551 static void InternalCallback(struct mg_connection *connection, 551 static void InternalCallback(struct mg_connection *connection,
552 const struct mg_request_info *request) 552 const struct mg_request_info *request)
553 { 553 {
554 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data); 554 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data);
555
555 MongooseOutputStream stream(connection); 556 MongooseOutputStream stream(connection);
556 HttpOutput output(stream, that->IsKeepAliveEnabled()); 557 HttpOutput output(stream, that->IsKeepAliveEnabled());
557 558
558 // Check remote calls 559 // Check remote calls
559 if (!that->IsRemoteAccessAllowed() && 560 if (!that->IsRemoteAccessAllowed() &&
678 output.SendStatus(HttpStatus_400_BadRequest); 679 output.SendStatus(HttpStatus_400_BadRequest);
679 return; 680 return;
680 } 681 }
681 682
682 683
683 // Loop over the candidate handlers for this URI
684 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); 684 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri);
685
685 bool found = false; 686 bool found = false;
686 687
687 for (MongooseServer::Handlers::const_iterator it = 688 try
688 that->GetHandlers().begin(); it != that->GetHandlers().end() && !found; ++it) 689 {
689 { 690 if (that->HasHandler())
691 {
692 found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body);
693 }
694 }
695 catch (OrthancException& e)
696 {
697 // Using this candidate handler results in an exception
698 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
699
690 try 700 try
691 { 701 {
692 found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); 702 switch (e.GetErrorCode())
693 }
694 catch (OrthancException& e)
695 {
696 // Using this candidate handler results in an exception
697 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
698
699 try
700 { 703 {
701 switch (e.GetErrorCode()) 704 case ErrorCode_InexistentFile:
702 { 705 case ErrorCode_InexistentItem:
703 case ErrorCode_InexistentFile: 706 case ErrorCode_UnknownResource:
704 case ErrorCode_InexistentItem: 707 output.SendStatus(HttpStatus_404_NotFound);
705 case ErrorCode_UnknownResource: 708 break;
706 output.SendStatus(HttpStatus_404_NotFound); 709
707 break; 710 case ErrorCode_BadRequest:
708 711 case ErrorCode_UriSyntax:
709 case ErrorCode_BadRequest: 712 output.SendStatus(HttpStatus_400_BadRequest);
710 case ErrorCode_UriSyntax: 713 break;
711 output.SendStatus(HttpStatus_400_BadRequest); 714
712 break; 715 default:
713 716 output.SendStatus(HttpStatus_500_InternalServerError);
714 default:
715 output.SendStatus(HttpStatus_500_InternalServerError);
716 }
717 } 717 }
718 catch (OrthancException&) 718 }
719 { 719 catch (OrthancException&)
720 // An exception here reflects the fact that an exception was 720 {
721 // triggered after the status code was sent by the HTTP handler. 721 // An exception here reflects the fact that an exception was
722 } 722 // triggered after the status code was sent by the HTTP handler.
723 723 }
724 return; 724
725 } 725 return;
726 catch (boost::bad_lexical_cast&) 726 }
727 { 727 catch (boost::bad_lexical_cast&)
728 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast"; 728 {
729 output.SendStatus(HttpStatus_400_BadRequest); 729 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast";
730 return; 730 output.SendStatus(HttpStatus_400_BadRequest);
731 } 731 return;
732 catch (std::runtime_error&) 732 }
733 { 733 catch (std::runtime_error&)
734 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request"; 734 {
735 output.SendStatus(HttpStatus_400_BadRequest); 735 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request";
736 return; 736 output.SendStatus(HttpStatus_400_BadRequest);
737 } 737 return;
738 } 738 }
739 739
740 if (!found) 740 if (!found)
741 { 741 {
742 output.SendStatus(HttpStatus_404_NotFound); 742 output.SendStatus(HttpStatus_404_NotFound);
787 787
788 788
789 MongooseServer::MongooseServer() : pimpl_(new PImpl) 789 MongooseServer::MongooseServer() : pimpl_(new PImpl)
790 { 790 {
791 pimpl_->context_ = NULL; 791 pimpl_->context_ = NULL;
792 handler_ = NULL;
792 remoteAllowed_ = false; 793 remoteAllowed_ = false;
793 authentication_ = false; 794 authentication_ = false;
794 ssl_ = false; 795 ssl_ = false;
795 port_ = 8000; 796 port_ = 8000;
796 filter_ = NULL; 797 filter_ = NULL;
809 810
810 811
811 MongooseServer::~MongooseServer() 812 MongooseServer::~MongooseServer()
812 { 813 {
813 Stop(); 814 Stop();
814 ClearHandlers();
815 } 815 }
816 816
817 817
818 void MongooseServer::SetPortNumber(uint16_t port) 818 void MongooseServer::SetPortNumber(uint16_t port)
819 { 819 {
874 pimpl_->context_ = NULL; 874 pimpl_->context_ = NULL;
875 } 875 }
876 } 876 }
877 877
878 878
879 void MongooseServer::RegisterHandler(IHttpHandler& handler)
880 {
881 Stop();
882
883 handlers_.push_back(&handler);
884 }
885
886
887 void MongooseServer::ClearHandlers()
888 {
889 Stop();
890 }
891
892
893 void MongooseServer::ClearUsers() 879 void MongooseServer::ClearUsers()
894 { 880 {
895 Stop(); 881 Stop();
896 registeredUsers_.clear(); 882 registeredUsers_.clear();
897 } 883 }
961 947
962 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const 948 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const
963 { 949 {
964 return registeredUsers_.find(basic) != registeredUsers_.end(); 950 return registeredUsers_.find(basic) != registeredUsers_.end();
965 } 951 }
952
953
954 void MongooseServer::Register(IHttpHandler& handler)
955 {
956 Stop();
957 handler_ = &handler;
958 }
959
960
961 IHttpHandler& MongooseServer::GetHandler() const
962 {
963 if (handler_ == NULL)
964 {
965 throw OrthancException(ErrorCode_InternalError);
966 }
967
968 return *handler_;
969 }
966 } 970 }