Mercurial > hg > orthanc
comparison Core/HttpServer/MongooseServer.cpp @ 896:c4053ac5db04 plugins
better plugni api
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jun 2014 09:57:02 +0200 |
parents | 7e8cde5905fd |
children | bb0a51561016 |
comparison
equal
deleted
inserted
replaced
895:7e8cde5905fd | 896:c4053ac5db04 |
---|---|
687 break; | 687 break; |
688 } | 688 } |
689 } | 689 } |
690 | 690 |
691 | 691 |
692 // Call the proper handler for this URI | 692 // Decompose the URI into its components |
693 UriComponents uri; | 693 UriComponents uri; |
694 try | 694 try |
695 { | 695 { |
696 Toolbox::SplitUriComponents(uri, request->uri); | 696 Toolbox::SplitUriComponents(uri, request->uri); |
697 } | 697 } |
700 output.SendHeader(HttpStatus_400_BadRequest); | 700 output.SendHeader(HttpStatus_400_BadRequest); |
701 return (void*) ""; | 701 return (void*) ""; |
702 } | 702 } |
703 | 703 |
704 | 704 |
705 // Locate the candidate handlers for this URI | |
706 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); | |
705 std::list<HttpHandler*> handlers; | 707 std::list<HttpHandler*> handlers; |
706 that->FindHandlers(handlers, uri); | 708 that->FindHandlers(handlers, uri); |
707 | 709 |
708 bool found = false; | 710 bool found = false; |
709 | 711 bool isError = false; |
712 HttpStatus errorStatus; | |
713 std::string errorDescription; | |
714 | |
715 | |
716 // Loop over the candidate handlers for this URI | |
710 for (std::list<HttpHandler*>::const_iterator | 717 for (std::list<HttpHandler*>::const_iterator |
711 it = handlers.begin(); it != handlers.end() && !found; it++) | 718 it = handlers.begin(); it != handlers.end() && !found; it++) |
712 { | 719 { |
713 try | 720 try |
714 { | 721 { |
715 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); | |
716 found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); | 722 found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); |
717 } | 723 } |
718 catch (OrthancException& e) | 724 catch (OrthancException& e) |
719 { | 725 { |
720 LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; | 726 // Using this candidate handler results in an exception, try |
721 output.SendHeader(HttpStatus_500_InternalServerError); | 727 // another handler before failing |
722 found = true; | 728 isError = true; |
729 errorStatus = HttpStatus_500_InternalServerError; | |
730 errorDescription = e.What(); | |
723 } | 731 } |
724 catch (boost::bad_lexical_cast&) | 732 catch (boost::bad_lexical_cast&) |
725 { | 733 { |
726 LOG(ERROR) << "MongooseServer Exception: Bad lexical cast"; | 734 isError = true; |
727 output.SendHeader(HttpStatus_400_BadRequest); | 735 errorStatus = HttpStatus_400_BadRequest; |
728 found = true; | 736 errorDescription = "Bad lexical cast"; |
729 } | 737 } |
730 catch (std::runtime_error&) | 738 catch (std::runtime_error&) |
731 { | 739 { |
732 LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request"; | 740 isError = true; |
733 output.SendHeader(HttpStatus_400_BadRequest); | 741 errorStatus = HttpStatus_400_BadRequest; |
734 found = true; | 742 errorDescription = "Presumably a bad JSON request"; |
735 } | 743 } |
736 } | 744 } |
737 | 745 |
738 if (!found) | 746 if (!found) |
739 { | 747 { |
740 output.SendHeader(HttpStatus_404_NotFound); | 748 if (isError) |
741 } | 749 { |
750 LOG(ERROR) << "Exception in the HTTP handler: " << errorDescription; | |
751 output.SendHeader(errorStatus); | |
752 } | |
753 else | |
754 { | |
755 output.SendHeader(HttpStatus_404_NotFound); | |
756 } | |
757 } | |
758 | |
742 | 759 |
743 // Mark as processed | 760 // Mark as processed |
744 return (void*) ""; | 761 return (void*) ""; |
745 } | 762 } |
746 else | 763 else |