HttpHandler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "HttpHandler.h"
  2. #include "http_page.h"
  3. int HttpHandler::HandleRequest() {
  4. // preprocessor -> api -> web -> postprocessor
  5. // preprocessor
  6. req.ParseUrl();
  7. if (service->preprocessor) {
  8. if (service->preprocessor(&req, &res) == HANDLE_DONE) {
  9. return HANDLE_DONE;
  10. }
  11. }
  12. http_api_handler api = NULL;
  13. int ret = service->GetApi(req.path.c_str(), req.method, &api);
  14. if (api) {
  15. // api service
  16. if (api(&req, &res) == HANDLE_DONE) {
  17. return HANDLE_DONE;
  18. }
  19. }
  20. else if (ret == HTTP_STATUS_METHOD_NOT_ALLOWED) {
  21. // Method Not Allowed
  22. res.status_code = HTTP_STATUS_METHOD_NOT_ALLOWED;
  23. }
  24. else if (req.method == HTTP_GET) {
  25. // web service
  26. // check path
  27. if (*req.path.c_str() != '/' || strstr(req.path.c_str(), "/../")) {
  28. res.status_code = HTTP_STATUS_BAD_REQUEST;
  29. goto make_http_status_page;
  30. }
  31. std::string filepath = service->document_root;
  32. filepath += req.path.c_str();
  33. if (strcmp(req.path.c_str(), "/") == 0) {
  34. filepath += service->home_page;
  35. }
  36. if (filepath.c_str()[filepath.size()-1] != '/' ||
  37. (service->index_of.size() != 0 &&
  38. req.path.size() >= service->index_of.size() &&
  39. strnicmp(req.path.c_str(), service->index_of.c_str(), service->index_of.size()) == 0)) {
  40. fc = files->Open(filepath.c_str(), (void*)req.path.c_str());
  41. }
  42. if (fc == NULL) {
  43. // Not Found
  44. res.status_code = HTTP_STATUS_NOT_FOUND;
  45. }
  46. else {
  47. // Not Modified
  48. auto iter = req.headers.find("if-not-match");
  49. if (iter != req.headers.end() &&
  50. strcmp(iter->second.c_str(), fc->etag) == 0) {
  51. res.status_code = HTTP_STATUS_NOT_MODIFIED;
  52. fc = NULL;
  53. }
  54. else {
  55. iter = req.headers.find("if-modified-since");
  56. if (iter != req.headers.end() &&
  57. strcmp(iter->second.c_str(), fc->last_modified) == 0) {
  58. res.status_code = HTTP_STATUS_NOT_MODIFIED;
  59. fc = NULL;
  60. }
  61. }
  62. }
  63. }
  64. else {
  65. // Not Implemented
  66. res.status_code = HTTP_STATUS_NOT_IMPLEMENTED;
  67. }
  68. make_http_status_page:
  69. // html page
  70. if (res.status_code >= 400 && res.body.size() == 0) {
  71. // error page
  72. if (service->error_page.size() != 0) {
  73. std::string filepath = service->document_root;
  74. filepath += '/';
  75. filepath += service->error_page;
  76. fc = files->Open(filepath.c_str(), NULL);
  77. }
  78. // status page
  79. if (fc == NULL && res.body.size() == 0) {
  80. res.content_type = TEXT_HTML;
  81. make_http_status_page(res.status_code, res.body);
  82. }
  83. }
  84. // file
  85. if (fc) {
  86. res.content = (unsigned char*)fc->filebuf.base;
  87. res.content_length = fc->filebuf.len;
  88. if (fc->content_type && *fc->content_type != '\0') {
  89. res.headers["Content-Type"] = fc->content_type;
  90. res.FillContentType();
  91. }
  92. res.headers["Content-Length"] = std::to_string(res.content_length);
  93. res.headers["Last-Modified"] = fc->last_modified;
  94. res.headers["Etag"] = fc->etag;
  95. }
  96. // postprocessor
  97. if (service->postprocessor) {
  98. if (service->postprocessor(&req, &res) == HANDLE_DONE) {
  99. return HANDLE_DONE;
  100. }
  101. }
  102. return HANDLE_DONE;
  103. }