Mercurial > hg > orthanc
comparison Core/HttpServer/MongooseServer.cpp @ 1441:f3672356c121
refactoring: IHttpHandler and HttpToolbox
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 01 Jul 2015 10:38:39 +0200 |
parents | af112b7d9cba |
children | 895ab369d63c |
comparison
equal
deleted
inserted
replaced
1440:3567503c00a7 | 1441:f3672356c121 |
---|---|
45 #include <boost/thread.hpp> | 45 #include <boost/thread.hpp> |
46 #include <glog/logging.h> | 46 #include <glog/logging.h> |
47 | 47 |
48 #include "../OrthancException.h" | 48 #include "../OrthancException.h" |
49 #include "../ChunkedBuffer.h" | 49 #include "../ChunkedBuffer.h" |
50 #include "HttpOutput.h" | 50 #include "HttpToolbox.h" |
51 #include "mongoose.h" | 51 #include "mongoose.h" |
52 | 52 |
53 #if ORTHANC_SSL_ENABLED == 1 | 53 #if ORTHANC_SSL_ENABLED == 1 |
54 #include <openssl/opensslv.h> | 54 #include <openssl/opensslv.h> |
55 #endif | 55 #endif |
265 | 265 |
266 | 266 |
267 | 267 |
268 static PostDataStatus ReadBody(std::string& postData, | 268 static PostDataStatus ReadBody(std::string& postData, |
269 struct mg_connection *connection, | 269 struct mg_connection *connection, |
270 const HttpHandler::Arguments& headers) | 270 const IHttpHandler::Arguments& headers) |
271 { | 271 { |
272 HttpHandler::Arguments::const_iterator cs = headers.find("content-length"); | 272 IHttpHandler::Arguments::const_iterator cs = headers.find("content-length"); |
273 if (cs == headers.end()) | 273 if (cs == headers.end()) |
274 { | 274 { |
275 return PostDataStatus_NoLength; | 275 return PostDataStatus_NoLength; |
276 } | 276 } |
277 | 277 |
311 | 311 |
312 | 312 |
313 | 313 |
314 static PostDataStatus ParseMultipartPost(std::string &completedFile, | 314 static PostDataStatus ParseMultipartPost(std::string &completedFile, |
315 struct mg_connection *connection, | 315 struct mg_connection *connection, |
316 const HttpHandler::Arguments& headers, | 316 const IHttpHandler::Arguments& headers, |
317 const std::string& contentType, | 317 const std::string& contentType, |
318 ChunkStore& chunkStore) | 318 ChunkStore& chunkStore) |
319 { | 319 { |
320 std::string boundary = "--" + contentType.substr(multipartLength); | 320 std::string boundary = "--" + contentType.substr(multipartLength); |
321 | 321 |
325 if (status != PostDataStatus_Success) | 325 if (status != PostDataStatus_Success) |
326 { | 326 { |
327 return status; | 327 return status; |
328 } | 328 } |
329 | 329 |
330 /*for (HttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) | 330 /*for (IHttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) |
331 { | 331 { |
332 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; | 332 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; |
333 } | 333 } |
334 printf("CHUNK\n");*/ | 334 printf("CHUNK\n");*/ |
335 | 335 |
336 typedef HttpHandler::Arguments::const_iterator ArgumentIterator; | 336 typedef IHttpHandler::Arguments::const_iterator ArgumentIterator; |
337 | 337 |
338 ArgumentIterator requestedWith = headers.find("x-requested-with"); | 338 ArgumentIterator requestedWith = headers.find("x-requested-with"); |
339 ArgumentIterator fileName = headers.find("x-file-name"); | 339 ArgumentIterator fileName = headers.find("x-file-name"); |
340 ArgumentIterator fileSizeStr = headers.find("x-file-size"); | 340 ArgumentIterator fileSizeStr = headers.find("x-file-size"); |
341 | 341 |
413 return PostDataStatus_Pending; | 413 return PostDataStatus_Pending; |
414 } | 414 } |
415 | 415 |
416 | 416 |
417 static bool IsAccessGranted(const MongooseServer& that, | 417 static bool IsAccessGranted(const MongooseServer& that, |
418 const HttpHandler::Arguments& headers) | 418 const IHttpHandler::Arguments& headers) |
419 { | 419 { |
420 bool granted = false; | 420 bool granted = false; |
421 | 421 |
422 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); | 422 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
423 if (auth != headers.end()) | 423 if (auth != headers.end()) |
424 { | 424 { |
425 std::string s = auth->second; | 425 std::string s = auth->second; |
426 if (s.size() > 6 && | 426 if (s.size() > 6 && |
427 s.substr(0, 6) == "Basic ") | 427 s.substr(0, 6) == "Basic ") |
433 | 433 |
434 return granted; | 434 return granted; |
435 } | 435 } |
436 | 436 |
437 | 437 |
438 static std::string GetAuthenticatedUsername(const HttpHandler::Arguments& headers) | 438 static std::string GetAuthenticatedUsername(const IHttpHandler::Arguments& headers) |
439 { | 439 { |
440 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); | 440 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
441 | 441 |
442 if (auth == headers.end()) | 442 if (auth == headers.end()) |
443 { | 443 { |
444 return ""; | 444 return ""; |
445 } | 445 } |
468 } | 468 } |
469 | 469 |
470 | 470 |
471 static bool ExtractMethod(HttpMethod& method, | 471 static bool ExtractMethod(HttpMethod& method, |
472 const struct mg_request_info *request, | 472 const struct mg_request_info *request, |
473 const HttpHandler::Arguments& headers, | 473 const IHttpHandler::Arguments& headers, |
474 const HttpHandler::GetArguments& argumentsGET) | 474 const IHttpHandler::GetArguments& argumentsGET) |
475 { | 475 { |
476 std::string overriden; | 476 std::string overriden; |
477 | 477 |
478 // Check whether some PUT/DELETE faking is done | 478 // Check whether some PUT/DELETE faking is done |
479 | 479 |
480 // 1. Faking with Google's approach | 480 // 1. Faking with Google's approach |
481 HttpHandler::Arguments::const_iterator methodOverride = | 481 IHttpHandler::Arguments::const_iterator methodOverride = |
482 headers.find("x-http-method-override"); | 482 headers.find("x-http-method-override"); |
483 | 483 |
484 if (methodOverride != headers.end()) | 484 if (methodOverride != headers.end()) |
485 { | 485 { |
486 overriden = methodOverride->second; | 486 overriden = methodOverride->second; |
563 return; | 563 return; |
564 } | 564 } |
565 | 565 |
566 | 566 |
567 // Extract the HTTP headers | 567 // Extract the HTTP headers |
568 HttpHandler::Arguments headers; | 568 IHttpHandler::Arguments headers; |
569 for (int i = 0; i < request->num_headers; i++) | 569 for (int i = 0; i < request->num_headers; i++) |
570 { | 570 { |
571 std::string name = request->http_headers[i].name; | 571 std::string name = request->http_headers[i].name; |
572 std::transform(name.begin(), name.end(), name.begin(), ::tolower); | 572 std::transform(name.begin(), name.end(), name.begin(), ::tolower); |
573 headers.insert(std::make_pair(name, request->http_headers[i].value)); | 573 headers.insert(std::make_pair(name, request->http_headers[i].value)); |
574 } | 574 } |
575 | 575 |
576 | 576 |
577 // Extract the GET arguments | 577 // Extract the GET arguments |
578 HttpHandler::GetArguments argumentsGET; | 578 IHttpHandler::GetArguments argumentsGET; |
579 if (!strcmp(request->request_method, "GET")) | 579 if (!strcmp(request->request_method, "GET")) |
580 { | 580 { |
581 HttpHandler::ParseGetArguments(argumentsGET, request->query_string); | 581 HttpToolbox::ParseGetArguments(argumentsGET, request->query_string); |
582 } | 582 } |
583 | 583 |
584 | 584 |
585 // Compute the HTTP method, taking method faking into consideration | 585 // Compute the HTTP method, taking method faking into consideration |
586 HttpMethod method = HttpMethod_Get; | 586 HttpMethod method = HttpMethod_Get; |
625 if (method == HttpMethod_Post || | 625 if (method == HttpMethod_Post || |
626 method == HttpMethod_Put) | 626 method == HttpMethod_Put) |
627 { | 627 { |
628 PostDataStatus status; | 628 PostDataStatus status; |
629 | 629 |
630 HttpHandler::Arguments::const_iterator ct = headers.find("content-type"); | 630 IHttpHandler::Arguments::const_iterator ct = headers.find("content-type"); |
631 if (ct == headers.end()) | 631 if (ct == headers.end()) |
632 { | 632 { |
633 // No content-type specified. Assume no multi-part content occurs at this point. | 633 // No content-type specified. Assume no multi-part content occurs at this point. |
634 status = ReadBody(body, connection, headers); | 634 status = ReadBody(body, connection, headers); |
635 } | 635 } |
874 pimpl_->context_ = NULL; | 874 pimpl_->context_ = NULL; |
875 } | 875 } |
876 } | 876 } |
877 | 877 |
878 | 878 |
879 void MongooseServer::RegisterHandler(HttpHandler& handler) | 879 void MongooseServer::RegisterHandler(IHttpHandler& handler) |
880 { | 880 { |
881 Stop(); | 881 Stop(); |
882 | 882 |
883 handlers_.push_back(&handler); | 883 handlers_.push_back(&handler); |
884 } | 884 } |