HttpHandler.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (fc->content_type && *fc->content_type != '\0') {
  112. pResp->headers["Content-Type"] = fc->content_type;
  113. }
  114. pResp->headers["Last-Modified"] = fc->last_modified;
  115. pResp->headers["Etag"] = fc->etag;
  116. }
  117. postprocessor:
  118. if (service->postprocessor) {
  119. service->postprocessor(pReq, pResp);
  120. }
  121. if (status_code == 0) {
  122. state = HANDLE_CONTINUE;
  123. } else {
  124. state = HANDLE_END;
  125. parser->SubmitResponse(pResp);
  126. }
  127. return status_code;
  128. }
  129. int HttpHandler::FeedRecvData(const char* data, size_t len) {
  130. int nfeed = 0;
  131. if (protocol == HttpHandler::WEBSOCKET) {
  132. nfeed = ws->parser->FeedRecvData(data, len);
  133. if (nfeed != len) {
  134. hloge("[%s:%d] websocket parse error!", ip, port);
  135. }
  136. } else {
  137. if (state != WANT_RECV) {
  138. Reset();
  139. }
  140. nfeed = parser->FeedRecvData(data, len);
  141. if (nfeed != len) {
  142. hloge("[%s:%d] http parse error: %s", ip, port, parser->StrError(parser->GetError()));
  143. }
  144. }
  145. return nfeed;
  146. }
  147. int HttpHandler::GetSendData(char** data, size_t* len) {
  148. if (state == HANDLE_CONTINUE) {
  149. return 0;
  150. }
  151. HttpRequest* pReq = req.get();
  152. HttpResponse* pResp = resp.get();
  153. if (protocol == HTTP_V1) {
  154. switch(state) {
  155. case WANT_RECV:
  156. if (parser->IsComplete()) state = WANT_SEND;
  157. else return 0;
  158. case HANDLE_END:
  159. state = WANT_SEND;
  160. case WANT_SEND:
  161. state = SEND_HEADER;
  162. case SEND_HEADER:
  163. {
  164. int content_length = 0;
  165. const char* content = NULL;
  166. // HEAD
  167. if (pReq->method == HTTP_HEAD) {
  168. if (fc) {
  169. pResp->headers["Accept-Ranges"] = "bytes";
  170. pResp->headers["Content-Length"] = hv::to_string(fc->st.st_size);
  171. } else {
  172. pResp->headers["Content-Type"] = "text/html";
  173. pResp->headers["Content-Length"] = "0";
  174. }
  175. state = SEND_DONE;
  176. goto return_nobody;
  177. }
  178. // File service
  179. if (fc) {
  180. long from, to, total;
  181. int nread;
  182. // Range:
  183. if (pReq->GetRange(from, to)) {
  184. HFile file;
  185. if (file.open(fc->filepath.c_str(), "rb") != 0) {
  186. pResp->status_code = HTTP_STATUS_NOT_FOUND;
  187. state = SEND_DONE;
  188. goto return_nobody;
  189. }
  190. total = file.size();
  191. if (to == 0 || to >= total) to = total - 1;
  192. pResp->content_length = to - from + 1;
  193. nread = file.readrange(body, from, to);
  194. if (nread != pResp->content_length) {
  195. pResp->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  196. state = SEND_DONE;
  197. goto return_nobody;
  198. }
  199. pResp->SetRange(from, to, total);
  200. state = SEND_BODY;
  201. goto return_header;
  202. }
  203. // FileCache
  204. // NOTE: no copy filebuf, more efficient
  205. header = pResp->Dump(true, false);
  206. fc->prepend_header(header.c_str(), header.size());
  207. *data = fc->httpbuf.base;
  208. *len = fc->httpbuf.len;
  209. state = SEND_DONE;
  210. return *len;
  211. }
  212. // API service
  213. content_length = pResp->ContentLength();
  214. content = (const char*)pResp->Content();
  215. if (content) {
  216. if (content_length > (1 << 20)) {
  217. state = SEND_BODY;
  218. goto return_header;
  219. } else {
  220. // NOTE: header+body in one package if <= 1M
  221. header = pResp->Dump(true, false);
  222. header.append(content, content_length);
  223. state = SEND_DONE;
  224. goto return_header;
  225. }
  226. } else {
  227. state = SEND_DONE;
  228. goto return_header;
  229. }
  230. return_nobody:
  231. pResp->content_length = 0;
  232. return_header:
  233. if (header.empty()) header = pResp->Dump(true, false);
  234. *data = (char*)header.c_str();
  235. *len = header.size();
  236. return *len;
  237. }
  238. case SEND_BODY:
  239. {
  240. if (body.empty()) {
  241. *data = (char*)pResp->Content();
  242. *len = pResp->ContentLength();
  243. } else {
  244. *data = (char*)body.c_str();
  245. *len = body.size();
  246. }
  247. state = SEND_DONE;
  248. return *len;
  249. }
  250. case SEND_DONE:
  251. {
  252. // NOTE: remove file cache if > 16M
  253. if (fc && fc->filebuf.len > (1 << 24)) {
  254. files->Close(fc);
  255. }
  256. fc = NULL;
  257. header.clear();
  258. body.clear();
  259. return 0;
  260. }
  261. default:
  262. return 0;
  263. }
  264. } else if (protocol == HTTP_V2) {
  265. return parser->GetSendData(data, len);
  266. }
  267. return 0;
  268. }