comparison Core/HttpServer/MongooseServer.cpp @ 473:c9a5d72f8481

changing the namespace of HTTP enumerations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 15 Jul 2013 17:22:13 +0200
parents b79bf2f4ab2e
children 6f8ae46ed90e
comparison
equal deleted inserted replaced
472:722b56b99093 473:c9a5d72f8481
484 return decoded.substr(0, semicolons); 484 return decoded.substr(0, semicolons);
485 } 485 }
486 } 486 }
487 487
488 488
489 static bool ExtractMethod(Orthanc_HttpMethod& method, 489 static bool ExtractMethod(HttpMethod& method,
490 const struct mg_request_info *request, 490 const struct mg_request_info *request,
491 const HttpHandler::Arguments& headers, 491 const HttpHandler::Arguments& headers,
492 const HttpHandler::Arguments& argumentsGET) 492 const HttpHandler::Arguments& argumentsGET)
493 { 493 {
494 std::string overriden; 494 std::string overriden;
521 521
522 LOG(INFO) << "HTTP method faking has been detected for " << overriden; 522 LOG(INFO) << "HTTP method faking has been detected for " << overriden;
523 523
524 if (overriden == "PUT") 524 if (overriden == "PUT")
525 { 525 {
526 method = Orthanc_HttpMethod_Put; 526 method = HttpMethod_Put;
527 return true; 527 return true;
528 } 528 }
529 else if (overriden == "DELETE") 529 else if (overriden == "DELETE")
530 { 530 {
531 method = Orthanc_HttpMethod_Delete; 531 method = HttpMethod_Delete;
532 return true; 532 return true;
533 } 533 }
534 else 534 else
535 { 535 {
536 return false; 536 return false;
538 } 538 }
539 539
540 // No PUT/DELETE faking was present 540 // No PUT/DELETE faking was present
541 if (!strcmp(request->request_method, "GET")) 541 if (!strcmp(request->request_method, "GET"))
542 { 542 {
543 method = Orthanc_HttpMethod_Get; 543 method = HttpMethod_Get;
544 } 544 }
545 else if (!strcmp(request->request_method, "POST")) 545 else if (!strcmp(request->request_method, "POST"))
546 { 546 {
547 method = Orthanc_HttpMethod_Post; 547 method = HttpMethod_Post;
548 } 548 }
549 else if (!strcmp(request->request_method, "DELETE")) 549 else if (!strcmp(request->request_method, "DELETE"))
550 { 550 {
551 method = Orthanc_HttpMethod_Delete; 551 method = HttpMethod_Delete;
552 } 552 }
553 else if (!strcmp(request->request_method, "PUT")) 553 else if (!strcmp(request->request_method, "PUT"))
554 { 554 {
555 method = Orthanc_HttpMethod_Put; 555 method = HttpMethod_Put;
556 } 556 }
557 else 557 else
558 { 558 {
559 return false; 559 return false;
560 } 560 }
599 HttpHandler::ParseGetQuery(argumentsGET, request->query_string); 599 HttpHandler::ParseGetQuery(argumentsGET, request->query_string);
600 } 600 }
601 601
602 602
603 // Compute the HTTP method, taking method faking into consideration 603 // Compute the HTTP method, taking method faking into consideration
604 Orthanc_HttpMethod method; 604 HttpMethod method;
605 if (!ExtractMethod(method, request, headers, argumentsGET)) 605 if (!ExtractMethod(method, request, headers, argumentsGET))
606 { 606 {
607 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); 607 output.SendHeader(HttpStatus_400_BadRequest);
608 return (void*) ""; 608 return (void*) "";
609 } 609 }
610 610
611 611
612 // Authenticate this connection 612 // Authenticate this connection
638 } 638 }
639 639
640 640
641 // Extract the body of the request for PUT and POST 641 // Extract the body of the request for PUT and POST
642 std::string body; 642 std::string body;
643 if (method == Orthanc_HttpMethod_Post || 643 if (method == HttpMethod_Post ||
644 method == Orthanc_HttpMethod_Put) 644 method == HttpMethod_Put)
645 { 645 {
646 PostDataStatus status; 646 PostDataStatus status;
647 647
648 HttpHandler::Arguments::const_iterator ct = headers.find("content-type"); 648 HttpHandler::Arguments::const_iterator ct = headers.find("content-type");
649 if (ct == headers.end()) 649 if (ct == headers.end())
666 } 666 }
667 667
668 switch (status) 668 switch (status)
669 { 669 {
670 case PostDataStatus_NoLength: 670 case PostDataStatus_NoLength:
671 output.SendHeader(Orthanc_HttpStatus_411_LengthRequired); 671 output.SendHeader(HttpStatus_411_LengthRequired);
672 return (void*) ""; 672 return (void*) "";
673 673
674 case PostDataStatus_Failure: 674 case PostDataStatus_Failure:
675 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); 675 output.SendHeader(HttpStatus_400_BadRequest);
676 return (void*) ""; 676 return (void*) "";
677 677
678 case PostDataStatus_Pending: 678 case PostDataStatus_Pending:
679 output.AnswerBufferWithContentType(NULL, 0, ""); 679 output.AnswerBufferWithContentType(NULL, 0, "");
680 return (void*) ""; 680 return (void*) "";
691 { 691 {
692 Toolbox::SplitUriComponents(uri, request->uri); 692 Toolbox::SplitUriComponents(uri, request->uri);
693 } 693 }
694 catch (OrthancException) 694 catch (OrthancException)
695 { 695 {
696 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); 696 output.SendHeader(HttpStatus_400_BadRequest);
697 return (void*) ""; 697 return (void*) "";
698 } 698 }
699 699
700 700
701 HttpHandler* handler = that->FindHandler(uri); 701 HttpHandler* handler = that->FindHandler(uri);
702 if (handler) 702 if (handler)
703 { 703 {
704 try 704 try
705 { 705 {
706 LOG(INFO) << Orthanc_HttpMethod_ToString(method) << " " << Toolbox::FlattenUri(uri); 706 LOG(INFO) << Toolbox::ToString(method) << " " << Toolbox::FlattenUri(uri);
707 handler->Handle(output, method, uri, headers, argumentsGET, body); 707 handler->Handle(output, method, uri, headers, argumentsGET, body);
708 } 708 }
709 catch (OrthancException& e) 709 catch (OrthancException& e)
710 { 710 {
711 LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; 711 LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]";
712 output.SendHeader(Orthanc_HttpStatus_500_InternalServerError); 712 output.SendHeader(HttpStatus_500_InternalServerError);
713 } 713 }
714 catch (boost::bad_lexical_cast&) 714 catch (boost::bad_lexical_cast&)
715 { 715 {
716 LOG(ERROR) << "MongooseServer Exception: Bad lexical cast"; 716 LOG(ERROR) << "MongooseServer Exception: Bad lexical cast";
717 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); 717 output.SendHeader(HttpStatus_400_BadRequest);
718 } 718 }
719 catch (std::runtime_error&) 719 catch (std::runtime_error&)
720 { 720 {
721 LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request"; 721 LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request";
722 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); 722 output.SendHeader(HttpStatus_400_BadRequest);
723 } 723 }
724 } 724 }
725 else 725 else
726 { 726 {
727 output.SendHeader(Orthanc_HttpStatus_404_NotFound); 727 output.SendHeader(HttpStatus_404_NotFound);
728 } 728 }
729 729
730 // Mark as processed 730 // Mark as processed
731 return (void*) ""; 731 return (void*) "";
732 } 732 }