HttpHandler.cpp 3.5 KB

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