comparison OrthancFramework/Sources/HttpServer/HttpServer.cpp @ 4330:a01b1c9cbef4

moving generic type definitions from IHttpHandler to HttpToolbox
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Nov 2020 14:39:10 +0100
parents 6919242d2265
children e1e918e790e8
comparison
equal deleted inserted replaced
4329:9dc0e42f868b 4330:a01b1c9cbef4
29 #include "../FileBuffer.h" 29 #include "../FileBuffer.h"
30 #include "../Logging.h" 30 #include "../Logging.h"
31 #include "../OrthancException.h" 31 #include "../OrthancException.h"
32 #include "../TemporaryFile.h" 32 #include "../TemporaryFile.h"
33 #include "HttpToolbox.h" 33 #include "HttpToolbox.h"
34 #include "IHttpHandler.h"
34 35
35 #if ORTHANC_ENABLE_PUGIXML == 1 36 #if ORTHANC_ENABLE_PUGIXML == 1
36 # include "IWebDavBucket.h" 37 # include "IWebDavBucket.h"
37 #endif 38 #endif
38 39
377 } 378 }
378 379
379 380
380 static PostDataStatus ReadBodyToString(std::string& body, 381 static PostDataStatus ReadBodyToString(std::string& body,
381 struct mg_connection *connection, 382 struct mg_connection *connection,
382 const IHttpHandler::Arguments& headers) 383 const HttpToolbox::Arguments& headers)
383 { 384 {
384 IHttpHandler::Arguments::const_iterator contentLength = headers.find("content-length"); 385 HttpToolbox::Arguments::const_iterator contentLength = headers.find("content-length");
385 386
386 if (contentLength != headers.end()) 387 if (contentLength != headers.end())
387 { 388 {
388 // "Content-Length" is available 389 // "Content-Length" is available
389 return ReadBodyWithContentLength(body, connection, contentLength->second); 390 return ReadBodyWithContentLength(body, connection, contentLength->second);
396 } 397 }
397 398
398 399
399 static PostDataStatus ReadBodyToStream(IHttpHandler::IChunkedRequestReader& stream, 400 static PostDataStatus ReadBodyToStream(IHttpHandler::IChunkedRequestReader& stream,
400 struct mg_connection *connection, 401 struct mg_connection *connection,
401 const IHttpHandler::Arguments& headers) 402 const HttpToolbox::Arguments& headers)
402 { 403 {
403 IHttpHandler::Arguments::const_iterator contentLength = headers.find("content-length"); 404 HttpToolbox::Arguments::const_iterator contentLength = headers.find("content-length");
404 405
405 if (contentLength != headers.end()) 406 if (contentLength != headers.end())
406 { 407 {
407 // "Content-Length" is available 408 // "Content-Length" is available
408 std::string body; 409 std::string body;
443 } 444 }
444 445
445 446
446 static PostDataStatus ParseMultipartForm(std::string &completedFile, 447 static PostDataStatus ParseMultipartForm(std::string &completedFile,
447 struct mg_connection *connection, 448 struct mg_connection *connection,
448 const IHttpHandler::Arguments& headers, 449 const HttpToolbox::Arguments& headers,
449 const std::string& contentType, 450 const std::string& contentType,
450 ChunkStore& chunkStore) 451 ChunkStore& chunkStore)
451 { 452 {
452 std::string boundary = "--" + contentType.substr(MULTIPART_FORM_LENGTH); 453 std::string boundary = "--" + contentType.substr(MULTIPART_FORM_LENGTH);
453 454
457 if (status != PostDataStatus_Success) 458 if (status != PostDataStatus_Success)
458 { 459 {
459 return status; 460 return status;
460 } 461 }
461 462
462 /*for (IHttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) 463 /*for (HttpToolbox::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++)
463 { 464 {
464 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; 465 std::cout << "Header [" << i->first << "] = " << i->second << "\n";
465 } 466 }
466 printf("CHUNK\n");*/ 467 printf("CHUNK\n");*/
467 468
468 typedef IHttpHandler::Arguments::const_iterator ArgumentIterator; 469 typedef HttpToolbox::Arguments::const_iterator ArgumentIterator;
469 470
470 ArgumentIterator requestedWith = headers.find("x-requested-with"); 471 ArgumentIterator requestedWith = headers.find("x-requested-with");
471 ArgumentIterator fileName = headers.find("x-file-name"); 472 ArgumentIterator fileName = headers.find("x-file-name");
472 ArgumentIterator fileSizeStr = headers.find("x-file-size"); 473 ArgumentIterator fileSizeStr = headers.find("x-file-size");
473 474
543 return PostDataStatus_Pending; 544 return PostDataStatus_Pending;
544 } 545 }
545 546
546 547
547 static bool IsAccessGranted(const HttpServer& that, 548 static bool IsAccessGranted(const HttpServer& that,
548 const IHttpHandler::Arguments& headers) 549 const HttpToolbox::Arguments& headers)
549 { 550 {
550 bool granted = false; 551 bool granted = false;
551 552
552 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); 553 HttpToolbox::Arguments::const_iterator auth = headers.find("authorization");
553 if (auth != headers.end()) 554 if (auth != headers.end())
554 { 555 {
555 std::string s = auth->second; 556 std::string s = auth->second;
556 if (s.size() > 6 && 557 if (s.size() > 6 &&
557 s.substr(0, 6) == "Basic ") 558 s.substr(0, 6) == "Basic ")
563 564
564 return granted; 565 return granted;
565 } 566 }
566 567
567 568
568 static std::string GetAuthenticatedUsername(const IHttpHandler::Arguments& headers) 569 static std::string GetAuthenticatedUsername(const HttpToolbox::Arguments& headers)
569 { 570 {
570 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); 571 HttpToolbox::Arguments::const_iterator auth = headers.find("authorization");
571 572
572 if (auth == headers.end()) 573 if (auth == headers.end())
573 { 574 {
574 return ""; 575 return "";
575 } 576 }
598 } 599 }
599 600
600 601
601 static bool ExtractMethod(HttpMethod& method, 602 static bool ExtractMethod(HttpMethod& method,
602 const struct mg_request_info *request, 603 const struct mg_request_info *request,
603 const IHttpHandler::Arguments& headers, 604 const HttpToolbox::Arguments& headers,
604 const IHttpHandler::GetArguments& argumentsGET) 605 const HttpToolbox::GetArguments& argumentsGET)
605 { 606 {
606 std::string overriden; 607 std::string overriden;
607 608
608 // Check whether some PUT/DELETE faking is done 609 // Check whether some PUT/DELETE faking is done
609 610
610 // 1. Faking with Google's approach 611 // 1. Faking with Google's approach
611 IHttpHandler::Arguments::const_iterator methodOverride = 612 HttpToolbox::Arguments::const_iterator methodOverride =
612 headers.find("x-http-method-override"); 613 headers.find("x-http-method-override");
613 614
614 if (methodOverride != headers.end()) 615 if (methodOverride != headers.end())
615 { 616 {
616 overriden = methodOverride->second; 617 overriden = methodOverride->second;
677 return true; 678 return true;
678 } 679 }
679 680
680 681
681 static void ConfigureHttpCompression(HttpOutput& output, 682 static void ConfigureHttpCompression(HttpOutput& output,
682 const IHttpHandler::Arguments& headers) 683 const HttpToolbox::Arguments& headers)
683 { 684 {
684 // Look if the client wishes HTTP compression 685 // Look if the client wishes HTTP compression
685 // https://en.wikipedia.org/wiki/HTTP_compression 686 // https://en.wikipedia.org/wiki/HTTP_compression
686 IHttpHandler::Arguments::const_iterator it = headers.find("accept-encoding"); 687 HttpToolbox::Arguments::const_iterator it = headers.find("accept-encoding");
687 if (it != headers.end()) 688 if (it != headers.end())
688 { 689 {
689 std::vector<std::string> encodings; 690 std::vector<std::string> encodings;
690 Toolbox::TokenizeString(encodings, it->second, ','); 691 Toolbox::TokenizeString(encodings, it->second, ',');
691 692
718 # endif 719 # endif
719 720
720 static bool HandleWebDav(HttpOutput& output, 721 static bool HandleWebDav(HttpOutput& output,
721 const HttpServer::WebDavBuckets& buckets, 722 const HttpServer::WebDavBuckets& buckets,
722 const std::string& method, 723 const std::string& method,
723 const IHttpHandler::Arguments& headers, 724 const HttpToolbox::Arguments& headers,
724 const std::string& uri, 725 const std::string& uri,
725 struct mg_connection *connection /* to read the PUT body if need be */) 726 struct mg_connection *connection /* to read the PUT body if need be */)
726 { 727 {
727 if (buckets.empty()) 728 if (buckets.empty())
728 { 729 {
799 * WebDAV - PROPFIND 800 * WebDAV - PROPFIND
800 **/ 801 **/
801 802
802 if (method == "PROPFIND") 803 if (method == "PROPFIND")
803 { 804 {
804 IHttpHandler::Arguments::const_iterator i = headers.find("depth"); 805 HttpToolbox::Arguments::const_iterator i = headers.find("depth");
805 if (i == headers.end()) 806 if (i == headers.end())
806 { 807 {
807 throw OrthancException(ErrorCode_NetworkProtocol, "WebDAV PROPFIND without depth"); 808 throw OrthancException(ErrorCode_NetworkProtocol, "WebDAV PROPFIND without depth");
808 } 809 }
809 810
1078 return; 1079 return;
1079 } 1080 }
1080 1081
1081 1082
1082 // Extract the HTTP headers 1083 // Extract the HTTP headers
1083 IHttpHandler::Arguments headers; 1084 HttpToolbox::Arguments headers;
1084 for (int i = 0; i < request->num_headers; i++) 1085 for (int i = 0; i < request->num_headers; i++)
1085 { 1086 {
1086 std::string name = request->http_headers[i].name; 1087 std::string name = request->http_headers[i].name;
1087 std::string value = request->http_headers[i].value; 1088 std::string value = request->http_headers[i].value;
1088 1089
1096 ConfigureHttpCompression(output, headers); 1097 ConfigureHttpCompression(output, headers);
1097 } 1098 }
1098 1099
1099 1100
1100 // Extract the GET arguments 1101 // Extract the GET arguments
1101 IHttpHandler::GetArguments argumentsGET; 1102 HttpToolbox::GetArguments argumentsGET;
1102 if (!strcmp(request->request_method, "GET")) 1103 if (!strcmp(request->request_method, "GET"))
1103 { 1104 {
1104 HttpToolbox::ParseGetArguments(argumentsGET, request->query_string); 1105 HttpToolbox::ParseGetArguments(argumentsGET, request->query_string);
1105 } 1106 }
1106 1107
1239 { 1240 {
1240 PostDataStatus status; 1241 PostDataStatus status;
1241 1242
1242 bool isMultipartForm = false; 1243 bool isMultipartForm = false;
1243 1244
1244 IHttpHandler::Arguments::const_iterator ct = headers.find("content-type"); 1245 HttpToolbox::Arguments::const_iterator ct = headers.find("content-type");
1245 if (ct != headers.end() && 1246 if (ct != headers.end() &&
1246 ct->second.size() >= MULTIPART_FORM_LENGTH && 1247 ct->second.size() >= MULTIPART_FORM_LENGTH &&
1247 !memcmp(ct->second.c_str(), MULTIPART_FORM, MULTIPART_FORM_LENGTH)) 1248 !memcmp(ct->second.c_str(), MULTIPART_FORM, MULTIPART_FORM_LENGTH))
1248 { 1249 {
1249 /** 1250 /**