HttpHandler.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "HttpHandler.h"
  2. #include "hbase.h"
  3. #include "http_page.h"
  4. int HttpHandler::HandleHttpRequest() {
  5. // preprocessor -> api -> web -> postprocessor
  6. int status_code = HTTP_STATUS_OK;
  7. http_sync_handler sync_handler = NULL;
  8. http_async_handler async_handler = NULL;
  9. HttpRequest* pReq = req.get();
  10. HttpResponse* pResp = resp.get();
  11. pReq->scheme = ssl ? "https" : "http";
  12. pReq->client_addr.ip = ip;
  13. pReq->client_addr.port = port;
  14. pReq->Host();
  15. pReq->ParseUrl();
  16. preprocessor:
  17. state = HANDLE_BEGIN;
  18. if (service->preprocessor) {
  19. status_code = service->preprocessor(pReq, pResp);
  20. if (status_code != 0) {
  21. goto make_http_status_page;
  22. }
  23. }
  24. if (service->api_handlers.size() != 0) {
  25. service->GetApi(pReq, &sync_handler, &async_handler);
  26. }
  27. if (sync_handler) {
  28. // sync api service
  29. status_code = sync_handler(pReq, pResp);
  30. if (status_code != 0) {
  31. goto make_http_status_page;
  32. }
  33. }
  34. else if (async_handler) {
  35. // async api service
  36. async_handler(req, writer);
  37. status_code = 0;
  38. }
  39. else if (service->document_root.size() != 0 &&
  40. (pReq->method == HTTP_GET || pReq->method == HTTP_HEAD)) {
  41. // file service
  42. status_code = 200;
  43. std::string path = pReq->Path();
  44. const char* req_path = path.c_str();
  45. // path safe check
  46. if (*req_path != '/' || strstr(req_path, "/../")) {
  47. pResp->status_code = HTTP_STATUS_BAD_REQUEST;
  48. goto make_http_status_page;
  49. }
  50. std::string filepath = service->document_root;
  51. filepath += req_path;
  52. if (req_path[1] == '\0') {
  53. filepath += service->home_page;
  54. }
  55. bool is_dir = filepath.c_str()[filepath.size()-1] == '/';
  56. bool is_index_of = false;
  57. if (service->index_of.size() != 0 && strstartswith(req_path, service->index_of.c_str())) {
  58. is_index_of = true;
  59. }
  60. if (!is_dir || is_index_of) {
  61. bool need_read = pReq->method == HTTP_HEAD ? false : true;
  62. fc = files->Open(filepath.c_str(), need_read, (void*)req_path);
  63. }
  64. if (fc == NULL) {
  65. // Not Found
  66. status_code = HTTP_STATUS_NOT_FOUND;
  67. }
  68. else {
  69. // Not Modified
  70. auto iter = pReq->headers.find("if-not-match");
  71. if (iter != pReq->headers.end() &&
  72. strcmp(iter->second.c_str(), fc->etag) == 0) {
  73. status_code = HTTP_STATUS_NOT_MODIFIED;
  74. fc = NULL;
  75. }
  76. else {
  77. iter = pReq->headers.find("if-modified-since");
  78. if (iter != pReq->headers.end() &&
  79. strcmp(iter->second.c_str(), fc->last_modified) == 0) {
  80. status_code = HTTP_STATUS_NOT_MODIFIED;
  81. fc = NULL;
  82. }
  83. }
  84. }
  85. }
  86. else {
  87. // Not Implemented
  88. status_code = HTTP_STATUS_NOT_IMPLEMENTED;
  89. }
  90. make_http_status_page:
  91. if (status_code >= 100 && status_code < 600) {
  92. pResp->status_code = (http_status)status_code;
  93. }
  94. if (pResp->status_code >= 400 && pResp->body.size() == 0 && pReq->method != HTTP_HEAD) {
  95. // error page
  96. if (service->error_page.size() != 0) {
  97. std::string filepath = service->document_root;
  98. filepath += '/';
  99. filepath += service->error_page;
  100. fc = files->Open(filepath.c_str(), true, NULL);
  101. }
  102. // status page
  103. if (fc == NULL && pResp->body.size() == 0) {
  104. pResp->content_type = TEXT_HTML;
  105. make_http_status_page(pResp->status_code, pResp->body);
  106. }
  107. }
  108. if (fc) {
  109. pResp->content = fc->filebuf.base;
  110. pResp->content_length = fc->filebuf.len;
  111. pResp->headers["Content-Type"] = fc->content_type;
  112. pResp->headers["Last-Modified"] = fc->last_modified;
  113. pResp->headers["Etag"] = fc->etag;
  114. }
  115. postprocessor:
  116. if (service->postprocessor) {
  117. service->postprocessor(pReq, pResp);
  118. }
  119. if (status_code == 0) {
  120. state = HANDLE_CONTINUE;
  121. } else {
  122. state = HANDLE_END;
  123. parser->SubmitResponse(pResp);
  124. }
  125. return status_code;
  126. }
  127. int HttpHandler::FeedRecvData(const char* data, size_t len) {
  128. int nfeed = 0;
  129. if (protocol == HttpHandler::WEBSOCKET) {
  130. nfeed = ws->parser->FeedRecvData(data, len);
  131. if (nfeed != len) {
  132. hloge("[%s:%d] websocket parse error!", ip, port);
  133. }
  134. } else {
  135. if (state != WANT_RECV) {
  136. Reset();
  137. }
  138. nfeed = parser->FeedRecvData(data, len);
  139. if (nfeed != len) {
  140. hloge("[%s:%d] http parse error: %s", ip, port, parser->StrError(parser->GetError()));
  141. }
  142. }
  143. return nfeed;
  144. }
  145. int HttpHandler::GetSendData(char** data, size_t* len) {
  146. if (state == HANDLE_CONTINUE) {
  147. return 0;
  148. }
  149. HttpRequest* pReq = req.get();
  150. HttpResponse* pResp = resp.get();
  151. if (protocol == HTTP_V1) {
  152. switch(state) {
  153. case WANT_RECV:
  154. if (parser->IsComplete()) state = WANT_SEND;
  155. else return 0;
  156. case HANDLE_END:
  157. state = WANT_SEND;
  158. case WANT_SEND:
  159. state = SEND_HEADER;
  160. case SEND_HEADER:
  161. {
  162. int content_length = 0;
  163. const char* content = NULL;
  164. // HEAD
  165. if (pReq->method == HTTP_HEAD) {
  166. if (fc) {
  167. pResp->headers["Accept-Ranges"] = "bytes";
  168. pResp->headers["Content-Length"] = hv::to_string(fc->st.st_size);
  169. } else {
  170. pResp->headers["Content-Type"] = "text/html";
  171. pResp->headers["Content-Length"] = "0";
  172. }
  173. state = SEND_DONE;
  174. goto return_nobody;
  175. }
  176. // File service
  177. if (fc) {
  178. long from, to, total;
  179. int nread;
  180. // Range:
  181. if (pReq->GetRange(from, to)) {
  182. HFile file;
  183. if (file.open(fc->filepath.c_str(), "rb") != 0) {
  184. pResp->status_code = HTTP_STATUS_NOT_FOUND;
  185. state = SEND_DONE;
  186. goto return_nobody;
  187. }
  188. total = file.size();
  189. if (to == 0 || to >= total) to = total - 1;
  190. pResp->content_length = to - from + 1;
  191. nread = file.readrange(body, from, to);
  192. if (nread != pResp->content_length) {
  193. pResp->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  194. state = SEND_DONE;
  195. goto return_nobody;
  196. }
  197. pResp->SetRange(from, to, total);
  198. state = SEND_BODY;
  199. goto return_header;
  200. }
  201. // FileCache
  202. // NOTE: no copy filebuf, more efficient
  203. header = pResp->Dump(true, false);
  204. fc->prepend_header(header.c_str(), header.size());
  205. *data = fc->httpbuf.base;
  206. *len = fc->httpbuf.len;
  207. state = SEND_DONE;
  208. return *len;
  209. }
  210. // API service
  211. content_length = pResp->ContentLength();
  212. content = (const char*)pResp->Content();
  213. if (content) {
  214. if (content_length > (1 << 20)) {
  215. state = SEND_BODY;
  216. goto return_header;
  217. } else {
  218. // NOTE: header+body in one package if <= 1M
  219. header = pResp->Dump(true, false);
  220. header.append(content, content_length);
  221. state = SEND_DONE;
  222. goto return_header;
  223. }
  224. } else {
  225. state = SEND_DONE;
  226. goto return_header;
  227. }
  228. return_nobody:
  229. pResp->content_length = 0;
  230. return_header:
  231. if (header.empty()) header = pResp->Dump(true, false);
  232. *data = (char*)header.c_str();
  233. *len = header.size();
  234. return *len;
  235. }
  236. case SEND_BODY:
  237. {
  238. if (body.empty()) {
  239. *data = (char*)pResp->Content();
  240. *len = pResp->ContentLength();
  241. } else {
  242. *data = (char*)body.c_str();
  243. *len = body.size();
  244. }
  245. state = SEND_DONE;
  246. return *len;
  247. }
  248. case SEND_DONE:
  249. {
  250. // NOTE: remove file cache if > 16M
  251. if (fc && fc->filebuf.len > (1 << 24)) {
  252. files->Close(fc);
  253. }
  254. fc = NULL;
  255. header.clear();
  256. body.clear();
  257. return 0;
  258. }
  259. default:
  260. return 0;
  261. }
  262. } else if (protocol == HTTP_V2) {
  263. return parser->GetSendData(data, len);
  264. }
  265. return 0;
  266. }