1
0

HttpHandler.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include "HttpHandler.h"
  2. #include "hversion.h"
  3. #include "herr.h"
  4. #include "hlog.h"
  5. #include "htime.h"
  6. #include "hurl.h"
  7. #include "hasync.h" // import hv::async for http_async_handler
  8. #include "httpdef.h"
  9. #include "http2def.h"
  10. #include "wsdef.h"
  11. #include "http_page.h"
  12. #include "EventLoop.h" // import hv::setInterval
  13. using namespace hv;
  14. #define MIN_HTTP_REQUEST "GET / HTTP/1.1\r\n\r\n"
  15. #define MIN_HTTP_REQUEST_LEN 14 // exclude CRLF
  16. HttpHandler::HttpHandler(hio_t* io) :
  17. protocol(HttpHandler::UNKNOWN),
  18. state(WANT_RECV),
  19. ssl(false),
  20. keepalive(true),
  21. proxy(false),
  22. upgrade(false),
  23. port(0),
  24. pid(0),
  25. tid(0),
  26. io(io),
  27. service(NULL),
  28. ws_service(NULL),
  29. api_handler(NULL),
  30. last_send_ping_time(0),
  31. last_recv_pong_time(0),
  32. files(NULL),
  33. file(NULL)
  34. {
  35. // Init();
  36. }
  37. HttpHandler::~HttpHandler() {
  38. Close();
  39. }
  40. bool HttpHandler::Init(int http_version) {
  41. parser.reset(HttpParser::New(HTTP_SERVER, (enum http_version)http_version));
  42. if (parser == NULL) {
  43. return false;
  44. }
  45. req.reset(new HttpRequest);
  46. resp.reset(new HttpResponse);
  47. if(http_version == 1) {
  48. protocol = HTTP_V1;
  49. } else if (http_version == 2) {
  50. protocol = HTTP_V2;
  51. resp->http_major = req->http_major = 2;
  52. resp->http_minor = req->http_minor = 0;
  53. }
  54. if (io) {
  55. hloop_t* loop = hevent_loop(io);
  56. pid = hloop_pid(loop);
  57. tid = hloop_tid(loop);
  58. writer.reset(new hv::HttpResponseWriter(io, resp));
  59. writer->status = hv::SocketChannel::CONNECTED;
  60. } else {
  61. pid = hv_getpid();
  62. tid = hv_gettid();
  63. }
  64. parser->InitRequest(req.get());
  65. // NOTE: hook http_cb
  66. req->http_cb = [this](HttpMessage* msg, http_parser_state state, const char* data, size_t size) {
  67. switch (state) {
  68. case HP_HEADERS_COMPLETE:
  69. onHeadersComplete();
  70. break;
  71. case HP_BODY:
  72. if (api_handler && api_handler->state_handler) {
  73. break;
  74. }
  75. msg->body.append(data, size);
  76. return;
  77. case HP_MESSAGE_COMPLETE:
  78. onMessageComplete();
  79. return;
  80. default:
  81. break;
  82. }
  83. if (api_handler && api_handler->state_handler) {
  84. api_handler->state_handler(getHttpContext(), state, data, size);
  85. }
  86. };
  87. return true;
  88. }
  89. void HttpHandler::Reset() {
  90. state = WANT_RECV;
  91. req->Reset();
  92. resp->Reset();
  93. ctx = NULL;
  94. api_handler = NULL;
  95. closeFile();
  96. if (writer) {
  97. writer->Begin();
  98. writer->onwrite = NULL;
  99. writer->onclose = NULL;
  100. }
  101. parser->InitRequest(req.get());
  102. }
  103. void HttpHandler::Close() {
  104. if (writer) {
  105. writer->status = hv::SocketChannel::DISCONNECTED;
  106. }
  107. // close proxy
  108. if (proxy) {
  109. if (io) hio_close_upstream(io);
  110. }
  111. // close file
  112. closeFile();
  113. // onclose
  114. if (protocol == HttpHandler::WEBSOCKET) {
  115. WebSocketOnClose();
  116. } else {
  117. if (writer && writer->onclose) {
  118. writer->onclose();
  119. }
  120. }
  121. }
  122. bool HttpHandler::SwitchHTTP2() {
  123. parser.reset(HttpParser::New(HTTP_SERVER, ::HTTP_V2));
  124. if (parser == NULL) {
  125. return false;
  126. }
  127. protocol = HTTP_V2;
  128. resp->http_major = req->http_major = 2;
  129. resp->http_minor = req->http_minor = 0;
  130. parser->InitRequest(req.get());
  131. return true;
  132. }
  133. bool HttpHandler::SwitchWebSocket() {
  134. if(!io) return false;
  135. protocol = WEBSOCKET;
  136. ws_parser.reset(new WebSocketParser);
  137. ws_channel.reset(new hv::WebSocketChannel(io, WS_SERVER));
  138. ws_parser->onMessage = [this](int opcode, const std::string& msg){
  139. ws_channel->opcode = (enum ws_opcode)opcode;
  140. switch(opcode) {
  141. case WS_OPCODE_CLOSE:
  142. ws_channel->close();
  143. break;
  144. case WS_OPCODE_PING:
  145. // printf("recv ping\n");
  146. // printf("send pong\n");
  147. ws_channel->sendPong();
  148. break;
  149. case WS_OPCODE_PONG:
  150. // printf("recv pong\n");
  151. this->last_recv_pong_time = gethrtime_us();
  152. break;
  153. case WS_OPCODE_TEXT:
  154. case WS_OPCODE_BINARY:
  155. // onmessage
  156. if (ws_service && ws_service->onmessage) {
  157. ws_service->onmessage(ws_channel, msg);
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. };
  164. // NOTE: cancel keepalive timer, judge alive by heartbeat.
  165. ws_channel->setKeepaliveTimeout(0);
  166. if (ws_service && ws_service->ping_interval > 0) {
  167. int ping_interval = MAX(ws_service->ping_interval, 1000);
  168. ws_channel->setHeartbeat(ping_interval, [this](){
  169. if (last_recv_pong_time < last_send_ping_time) {
  170. hlogw("[%s:%d] websocket no pong!", ip, port);
  171. ws_channel->close();
  172. } else {
  173. // printf("send ping\n");
  174. ws_channel->sendPing();
  175. last_send_ping_time = gethrtime_us();
  176. }
  177. });
  178. }
  179. return true;
  180. }
  181. const HttpContextPtr& HttpHandler::getHttpContext() {
  182. if (!ctx) {
  183. ctx = std::make_shared<hv::HttpContext>();
  184. ctx->service = service;
  185. ctx->request = req;
  186. ctx->response = resp;
  187. ctx->writer = writer;
  188. }
  189. return ctx;
  190. }
  191. int HttpHandler::customHttpHandler(const http_handler& handler) {
  192. return invokeHttpHandler(&handler);
  193. }
  194. int HttpHandler::invokeHttpHandler(const http_handler* handler) {
  195. int status_code = HTTP_STATUS_NOT_IMPLEMENTED;
  196. if (handler->sync_handler) {
  197. // NOTE: sync_handler run on IO thread
  198. status_code = handler->sync_handler(req.get(), resp.get());
  199. } else if (handler->async_handler) {
  200. // NOTE: async_handler run on hv::async threadpool
  201. hv::async(std::bind(handler->async_handler, req, writer));
  202. status_code = HTTP_STATUS_NEXT;
  203. } else if (handler->ctx_handler) {
  204. // NOTE: ctx_handler run on IO thread, you can easily post HttpContextPtr to your consumer thread for processing.
  205. status_code = handler->ctx_handler(getHttpContext());
  206. } else if (handler->state_handler) {
  207. status_code = handler->state_handler(getHttpContext(), HP_MESSAGE_COMPLETE, NULL, 0);
  208. }
  209. return status_code;
  210. }
  211. void HttpHandler::onHeadersComplete() {
  212. HttpRequest* pReq = req.get();
  213. pReq->scheme = ssl ? "https" : "http";
  214. pReq->client_addr.ip = ip;
  215. pReq->client_addr.port = port;
  216. // keepalive
  217. keepalive = pReq->IsKeepAlive();
  218. // NOTE: Detect proxy before ParseUrl
  219. proxy = 0;
  220. if (hv::startswith(pReq->url, "http")) {
  221. // forward proxy
  222. proxy = 1;
  223. auto iter = pReq->headers.find("Proxy-Connection");
  224. if (iter != pReq->headers.end()) {
  225. const char* keepalive_value = iter->second.c_str();
  226. if (stricmp(keepalive_value, "keep-alive") == 0) {
  227. keepalive = true;
  228. }
  229. else if (stricmp(keepalive_value, "close") == 0) {
  230. keepalive = false;
  231. }
  232. else if (stricmp(keepalive_value, "upgrade") == 0) {
  233. keepalive = true;
  234. }
  235. }
  236. }
  237. // printf("url=%s\n", pReq->url.c_str());
  238. pReq->ParseUrl();
  239. if (service && service->pathHandlers.size() != 0) {
  240. service->GetRoute(pReq, &api_handler);
  241. }
  242. if (api_handler && api_handler->state_handler && writer) {
  243. writer->onclose = [this](){
  244. // HP_ERROR
  245. if (!parser->IsComplete()) {
  246. if (api_handler && api_handler->state_handler) {
  247. api_handler->state_handler(getHttpContext(), HP_ERROR, NULL, 0);
  248. }
  249. }
  250. };
  251. return;
  252. }
  253. if (proxy) {
  254. // forward proxy
  255. if (service && service->enable_forward_proxy) {
  256. proxyConnect(pReq->url);
  257. } else {
  258. proxy = 0;
  259. resp->status_code = HTTP_STATUS_FORBIDDEN;
  260. hlogw("Forbidden to forward proxy %s", pReq->url.c_str());
  261. }
  262. }
  263. else if (service && service->proxies.size() != 0) {
  264. // reverse proxy
  265. std::string proxy_url = service->GetProxyUrl(pReq->path.c_str());
  266. if (!proxy_url.empty()) {
  267. proxy = 1;
  268. pReq->url = proxy_url;
  269. proxyConnect(pReq->url);
  270. }
  271. }
  272. else {
  273. // TODO: rewrite
  274. }
  275. }
  276. void HttpHandler::onMessageComplete() {
  277. int status_code = 200;
  278. // Server:
  279. static char s_Server[64] = {'\0'};
  280. if (s_Server[0] == '\0') {
  281. snprintf(s_Server, sizeof(s_Server), "httpd/%s", hv_version());
  282. }
  283. resp->headers["Server"] = s_Server;
  284. // Connection:
  285. resp->headers["Connection"] = keepalive ? "keep-alive" : "close";
  286. // Upgrade ? SwitchHTTP2 / SwitchWebSocket : HandleHttpRequest ->
  287. // while (GetSendData) -> send
  288. upgrade = false;
  289. HttpHandler::ProtocolType upgrade_protocol = HttpHandler::UNKNOWN;
  290. auto iter_upgrade = req->headers.find("upgrade");
  291. if (iter_upgrade != req->headers.end()) {
  292. upgrade = true;
  293. const char* upgrade_proto = iter_upgrade->second.c_str();
  294. hlogi("[%s:%d] Upgrade: %s", ip, port, upgrade_proto);
  295. // websocket
  296. if (stricmp(upgrade_proto, "websocket") == 0) {
  297. /*
  298. HTTP/1.1 101 Switching Protocols
  299. Connection: Upgrade
  300. Upgrade: websocket
  301. Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
  302. */
  303. resp->status_code = HTTP_STATUS_SWITCHING_PROTOCOLS;
  304. resp->headers["Connection"] = "Upgrade";
  305. resp->headers["Upgrade"] = "websocket";
  306. auto iter_key = req->headers.find(SEC_WEBSOCKET_KEY);
  307. if (iter_key != req->headers.end()) {
  308. char ws_accept[32] = {0};
  309. ws_encode_key(iter_key->second.c_str(), ws_accept);
  310. resp->headers[SEC_WEBSOCKET_ACCEPT] = ws_accept;
  311. }
  312. auto iter_protocol = req->headers.find(SEC_WEBSOCKET_PROTOCOL);
  313. if (iter_protocol != req->headers.end()) {
  314. hv::StringList subprotocols = hv::split(iter_protocol->second, ',');
  315. if (subprotocols.size() > 0) {
  316. hlogw("%s: %s => just select first protocol %s", SEC_WEBSOCKET_PROTOCOL, iter_protocol->second.c_str(), subprotocols[0].c_str());
  317. resp->headers[SEC_WEBSOCKET_PROTOCOL] = subprotocols[0];
  318. }
  319. }
  320. upgrade_protocol = HttpHandler::WEBSOCKET;
  321. // NOTE: SwitchWebSocket after send handshake response
  322. }
  323. // h2/h2c
  324. else if (strnicmp(upgrade_proto, "h2", 2) == 0) {
  325. /*
  326. HTTP/1.1 101 Switching Protocols
  327. Connection: Upgrade
  328. Upgrade: h2c
  329. */
  330. if (io) hio_write(io, HTTP2_UPGRADE_RESPONSE, strlen(HTTP2_UPGRADE_RESPONSE));
  331. if (!SwitchHTTP2()) {
  332. hloge("[%s:%d] unsupported HTTP2", ip, port);
  333. if (io) hio_close(io);
  334. return;
  335. }
  336. }
  337. else {
  338. hloge("[%s:%d] unsupported Upgrade: %s", upgrade_proto);
  339. if (io) hio_close(io);
  340. return;
  341. }
  342. } else {
  343. status_code = HandleHttpRequest();
  344. }
  345. if (io) {
  346. char* data = NULL;
  347. size_t len = 0;
  348. while (GetSendData(&data, &len)) {
  349. // printf("%.*s\n", (int)len, data);
  350. if (data && len) {
  351. hio_write(io, data, len);
  352. }
  353. }
  354. }
  355. // access log
  356. if (service && service->enable_access_log) {
  357. hlogi("[%ld-%ld][%s:%d][%s %s]=>[%d %s]",
  358. pid, tid, ip, port,
  359. http_method_str(req->method), req->path.c_str(),
  360. resp->status_code, resp->status_message());
  361. }
  362. // switch protocol to websocket
  363. if (upgrade && upgrade_protocol == HttpHandler::WEBSOCKET) {
  364. if (!SwitchWebSocket()) {
  365. hloge("[%s:%d] unsupported websocket", ip, port);
  366. if (io) hio_close(io);
  367. return;
  368. }
  369. // onopen
  370. WebSocketOnOpen();
  371. return;
  372. }
  373. if (status_code != HTTP_STATUS_NEXT) {
  374. if (keepalive) {
  375. Reset();
  376. } else {
  377. if (io) hio_close(io);
  378. }
  379. }
  380. }
  381. int HttpHandler::HandleHttpRequest() {
  382. // preprocessor -> processor -> postprocessor
  383. HttpRequest* pReq = req.get();
  384. HttpResponse* pResp = resp.get();
  385. // NOTE: Not all users want to parse body, we comment it out.
  386. // pReq->ParseBody();
  387. int status_code = pResp->status_code;
  388. if (status_code != HTTP_STATUS_OK) {
  389. goto postprocessor;
  390. }
  391. preprocessor:
  392. state = HANDLE_BEGIN;
  393. if (service->preprocessor) {
  394. status_code = customHttpHandler(service->preprocessor);
  395. if (status_code != HTTP_STATUS_NEXT) {
  396. goto postprocessor;
  397. }
  398. }
  399. middleware:
  400. for (const auto& middleware : service->middleware) {
  401. status_code = customHttpHandler(middleware);
  402. if (status_code != HTTP_STATUS_NEXT) {
  403. goto postprocessor;
  404. }
  405. }
  406. processor:
  407. if (service->processor) {
  408. status_code = customHttpHandler(service->processor);
  409. } else {
  410. status_code = defaultRequestHandler();
  411. }
  412. postprocessor:
  413. if (status_code >= 100 && status_code < 600) {
  414. pResp->status_code = (http_status)status_code;
  415. if (pResp->status_code >= 400 && pResp->body.size() == 0 && pReq->method != HTTP_HEAD) {
  416. if (service->errorHandler) {
  417. customHttpHandler(service->errorHandler);
  418. } else {
  419. defaultErrorHandler();
  420. }
  421. }
  422. }
  423. if (fc) {
  424. pResp->content = fc->filebuf.base;
  425. pResp->content_length = fc->filebuf.len;
  426. pResp->headers["Content-Type"] = fc->content_type;
  427. pResp->headers["Last-Modified"] = fc->last_modified;
  428. pResp->headers["Etag"] = fc->etag;
  429. }
  430. if (service->postprocessor) {
  431. customHttpHandler(service->postprocessor);
  432. }
  433. if (writer && writer->state != hv::HttpResponseWriter::SEND_BEGIN) {
  434. status_code = HTTP_STATUS_NEXT;
  435. }
  436. if (status_code == HTTP_STATUS_NEXT) {
  437. state = HANDLE_CONTINUE;
  438. } else {
  439. state = HANDLE_END;
  440. parser->SubmitResponse(resp.get());
  441. }
  442. return status_code;
  443. }
  444. int HttpHandler::defaultRequestHandler() {
  445. int status_code = HTTP_STATUS_OK;
  446. if (api_handler) {
  447. status_code = invokeHttpHandler(api_handler);
  448. }
  449. else if (req->method == HTTP_GET || req->method == HTTP_HEAD) {
  450. // static handler
  451. if (service->staticHandler) {
  452. status_code = customHttpHandler(service->staticHandler);
  453. }
  454. else if (service->staticDirs.size() > 0) {
  455. status_code = defaultStaticHandler();
  456. }
  457. else {
  458. status_code = HTTP_STATUS_NOT_FOUND;
  459. }
  460. }
  461. else {
  462. // Not Implemented
  463. status_code = HTTP_STATUS_NOT_IMPLEMENTED;
  464. }
  465. return status_code;
  466. }
  467. int HttpHandler::defaultStaticHandler() {
  468. // file service
  469. std::string path = req->Path();
  470. const char* req_path = path.c_str();
  471. // path safe check
  472. if (req_path[0] != '/' || strstr(req_path, "/../")) {
  473. return HTTP_STATUS_BAD_REQUEST;
  474. }
  475. std::string filepath;
  476. bool is_dir = path.back() == '/' &&
  477. service->index_of.size() > 0 &&
  478. hv_strstartswith(req_path, service->index_of.c_str());
  479. if (is_dir) {
  480. filepath = service->document_root + path;
  481. } else {
  482. filepath = service->GetStaticFilepath(req_path);
  483. }
  484. if (filepath.empty()) {
  485. return HTTP_STATUS_NOT_FOUND;
  486. }
  487. int status_code = HTTP_STATUS_OK;
  488. // Range:
  489. bool has_range = false;
  490. long from, to = 0;
  491. if (req->GetRange(from, to)) {
  492. has_range = true;
  493. if (openFile(filepath.c_str()) != 0) {
  494. return HTTP_STATUS_NOT_FOUND;
  495. }
  496. long total = file->size();
  497. if (to == 0 || to >= total) to = total - 1;
  498. file->seek(from);
  499. status_code = HTTP_STATUS_PARTIAL_CONTENT;
  500. resp->status_code = HTTP_STATUS_PARTIAL_CONTENT;
  501. resp->content_length = to - from + 1;
  502. resp->SetContentTypeByFilename(filepath.c_str());
  503. resp->SetRange(from, to, total);
  504. if(resp->content_length < service->max_file_cache_size) {
  505. // read into body directly
  506. int nread = file->readrange(resp->body, from, to);
  507. closeFile();
  508. if (nread != resp->content_length) {
  509. resp->content_length = 0;
  510. resp->body.clear();
  511. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  512. }
  513. }
  514. else {
  515. if (service->largeFileHandler) {
  516. status_code = customHttpHandler(service->largeFileHandler);
  517. } else {
  518. status_code = defaultLargeFileHandler();
  519. }
  520. }
  521. return status_code;
  522. }
  523. // FileCache
  524. FileCache::OpenParam param;
  525. param.max_read = service->max_file_cache_size;
  526. param.need_read = !(req->method == HTTP_HEAD || has_range);
  527. param.path = req_path;
  528. if (files) {
  529. fc = files->Open(filepath.c_str(), &param);
  530. }
  531. if (fc == NULL) {
  532. if (param.error == ERR_OVER_LIMIT) {
  533. if (service->largeFileHandler) {
  534. status_code = customHttpHandler(service->largeFileHandler);
  535. } else {
  536. status_code = defaultLargeFileHandler();
  537. }
  538. } else {
  539. status_code = HTTP_STATUS_NOT_FOUND;
  540. }
  541. }
  542. else {
  543. // Not Modified
  544. auto iter = req->headers.find("if-not-match");
  545. if (iter != req->headers.end() &&
  546. strcmp(iter->second.c_str(), fc->etag) == 0) {
  547. fc = NULL;
  548. return HTTP_STATUS_NOT_MODIFIED;
  549. }
  550. iter = req->headers.find("if-modified-since");
  551. if (iter != req->headers.end() &&
  552. strcmp(iter->second.c_str(), fc->last_modified) == 0) {
  553. fc = NULL;
  554. return HTTP_STATUS_NOT_MODIFIED;
  555. }
  556. }
  557. return status_code;
  558. }
  559. int HttpHandler::defaultLargeFileHandler() {
  560. if (!writer) return HTTP_STATUS_NOT_IMPLEMENTED;
  561. if (!isFileOpened()) {
  562. std::string filepath = service->GetStaticFilepath(req->Path().c_str());
  563. if (filepath.empty() || openFile(filepath.c_str()) != 0) {
  564. return HTTP_STATUS_NOT_FOUND;
  565. }
  566. resp->content_length = file->size();
  567. resp->SetContentTypeByFilename(filepath.c_str());
  568. }
  569. if (service->limit_rate == 0) {
  570. // forbidden to send large file
  571. resp->content_length = 0;
  572. resp->status_code = HTTP_STATUS_FORBIDDEN;
  573. } else {
  574. size_t bufsize = 40960; // 40K
  575. file->buf.resize(bufsize);
  576. if (service->limit_rate < 0) {
  577. // unlimited: sendFile when writable
  578. writer->onwrite = [this](HBuf* buf) {
  579. if (writer->isWriteComplete()) {
  580. sendFile();
  581. }
  582. };
  583. } else {
  584. // limit_rate=40KB/s interval_ms=1000
  585. // limit_rate=500KB/s interval_ms=80
  586. int interval_ms = file->buf.len * 1000 / 1024 / service->limit_rate;
  587. // limit_rate=40MB/s interval_m=1: 40KB/ms = 40MB/s = 320Mbps
  588. if (interval_ms == 0) interval_ms = 1;
  589. // printf("limit_rate=%dKB/s interval_ms=%d\n", service->limit_rate, interval_ms);
  590. file->timer = setInterval(interval_ms, std::bind(&HttpHandler::sendFile, this));
  591. }
  592. }
  593. writer->EndHeaders();
  594. return HTTP_STATUS_UNFINISHED;
  595. }
  596. int HttpHandler::defaultErrorHandler() {
  597. // error page
  598. if (service->error_page.size() != 0) {
  599. std::string filepath = service->document_root + '/' + service->error_page;
  600. if (files) {
  601. // cache and load error page
  602. FileCache::OpenParam param;
  603. fc = files->Open(filepath.c_str(), &param);
  604. }
  605. }
  606. // status page
  607. if (fc == NULL && resp->body.size() == 0) {
  608. resp->content_type = TEXT_HTML;
  609. make_http_status_page(resp->status_code, resp->body);
  610. }
  611. return 0;
  612. }
  613. int HttpHandler::FeedRecvData(const char* data, size_t len) {
  614. int nfeed = 0;
  615. if (protocol == HttpHandler::UNKNOWN) {
  616. int http_version = 1;
  617. #if WITH_NGHTTP2
  618. if (strncmp(data, HTTP2_MAGIC, MIN(len, HTTP2_MAGIC_LEN)) == 0) {
  619. http_version = 2;
  620. }
  621. #else
  622. // check request-line
  623. if (len < MIN_HTTP_REQUEST_LEN) {
  624. hloge("[%s:%d] http request-line too small", ip, port);
  625. return 0;
  626. }
  627. for (int i = 0; i < MIN_HTTP_REQUEST_LEN; ++i) {
  628. if (!IS_GRAPH(data[i])) {
  629. hloge("[%s:%d] http request-line not plain", ip, port);
  630. return 0;
  631. }
  632. }
  633. #endif
  634. if (!Init(http_version)) {
  635. hloge("[%s:%d] unsupported HTTP%d", ip, port, http_version);
  636. return 0;
  637. }
  638. }
  639. switch (protocol) {
  640. case HttpHandler::HTTP_V1:
  641. case HttpHandler::HTTP_V2:
  642. if (state != WANT_RECV) {
  643. Reset();
  644. }
  645. nfeed = parser->FeedRecvData(data, len);
  646. if (nfeed != len) {
  647. hloge("[%s:%d] http parse error: %s", ip, port, parser->StrError(parser->GetError()));
  648. }
  649. break;
  650. case HttpHandler::WEBSOCKET:
  651. nfeed = ws_parser->FeedRecvData(data, len);
  652. if (nfeed != len) {
  653. hloge("[%s:%d] websocket parse error!", ip, port);
  654. }
  655. break;
  656. default:
  657. hloge("[%s:%d] unknown protocol", ip, port);
  658. return 0;
  659. }
  660. return nfeed;
  661. }
  662. int HttpHandler::GetSendData(char** data, size_t* len) {
  663. if (state == HANDLE_CONTINUE) {
  664. return 0;
  665. }
  666. HttpRequest* pReq = req.get();
  667. HttpResponse* pResp = resp.get();
  668. if (protocol == HTTP_V1) {
  669. switch(state) {
  670. case WANT_RECV:
  671. if (parser->IsComplete()) state = WANT_SEND;
  672. else return 0;
  673. case HANDLE_END:
  674. state = WANT_SEND;
  675. case WANT_SEND:
  676. state = SEND_HEADER;
  677. case SEND_HEADER:
  678. {
  679. size_t content_length = 0;
  680. const char* content = NULL;
  681. // HEAD
  682. if (pReq->method == HTTP_HEAD) {
  683. if (fc) {
  684. pResp->headers["Accept-Ranges"] = "bytes";
  685. pResp->headers["Content-Length"] = hv::to_string(fc->st.st_size);
  686. } else {
  687. pResp->headers["Content-Type"] = "text/html";
  688. pResp->headers["Content-Length"] = "0";
  689. }
  690. state = SEND_DONE;
  691. goto return_nobody;
  692. }
  693. // File service
  694. if (fc) {
  695. // FileCache
  696. // NOTE: no copy filebuf, more efficient
  697. header = pResp->Dump(true, false);
  698. fc->prepend_header(header.c_str(), header.size());
  699. *data = fc->httpbuf.base;
  700. *len = fc->httpbuf.len;
  701. state = SEND_DONE;
  702. return *len;
  703. }
  704. // API service
  705. content_length = pResp->ContentLength();
  706. content = (const char*)pResp->Content();
  707. if (content) {
  708. if (content_length > (1 << 20)) {
  709. state = SEND_BODY;
  710. goto return_header;
  711. } else {
  712. // NOTE: header+body in one package if <= 1M
  713. header = pResp->Dump(true, false);
  714. header.append(content, content_length);
  715. state = SEND_DONE;
  716. goto return_header;
  717. }
  718. } else {
  719. state = SEND_DONE;
  720. goto return_header;
  721. }
  722. return_nobody:
  723. pResp->content_length = 0;
  724. return_header:
  725. if (header.empty()) header = pResp->Dump(true, false);
  726. *data = (char*)header.c_str();
  727. *len = header.size();
  728. return *len;
  729. }
  730. case SEND_BODY:
  731. {
  732. *data = (char*)pResp->Content();
  733. *len = pResp->ContentLength();
  734. state = SEND_DONE;
  735. return *len;
  736. }
  737. case SEND_DONE:
  738. {
  739. // NOTE: remove file cache if > FILE_CACHE_MAX_SIZE
  740. if (fc && fc->filebuf.len > FILE_CACHE_MAX_SIZE) {
  741. files->Close(fc);
  742. }
  743. fc = NULL;
  744. header.clear();
  745. return 0;
  746. }
  747. default:
  748. return 0;
  749. }
  750. } else if (protocol == HTTP_V2) {
  751. return parser->GetSendData(data, len);
  752. }
  753. return 0;
  754. }
  755. int HttpHandler::openFile(const char* filepath) {
  756. closeFile();
  757. file = new LargeFile;
  758. file->timer = INVALID_TIMER_ID;
  759. return file->open(filepath, "rb");
  760. }
  761. bool HttpHandler::isFileOpened() {
  762. return file && file->isopen();
  763. }
  764. int HttpHandler::sendFile() {
  765. if (!writer || !writer->isWriteComplete() ||
  766. !isFileOpened() ||
  767. file->buf.len == 0 ||
  768. resp->content_length == 0) {
  769. return -1;
  770. }
  771. int readbytes = MIN(file->buf.len, resp->content_length);
  772. size_t nread = file->read(file->buf.base, readbytes);
  773. if (nread <= 0) {
  774. hloge("read file error!");
  775. writer->close(true);
  776. return nread;
  777. }
  778. int nwrite = writer->WriteBody(file->buf.base, nread);
  779. if (nwrite < 0) {
  780. // disconnectd
  781. writer->close(true);
  782. return nwrite;
  783. }
  784. resp->content_length -= nread;
  785. if (resp->content_length == 0) {
  786. writer->End();
  787. closeFile();
  788. }
  789. return nread;
  790. }
  791. void HttpHandler::closeFile() {
  792. if (file) {
  793. if (file->timer != INVALID_TIMER_ID) {
  794. killTimer(file->timer);
  795. file->timer = INVALID_TIMER_ID;
  796. }
  797. delete file;
  798. file = NULL;
  799. }
  800. }
  801. void HttpHandler::onProxyConnect(hio_t* upstream_io) {
  802. // printf("onProxyConnect\n");
  803. HttpHandler* handler = (HttpHandler*)hevent_userdata(upstream_io);
  804. hio_t* io = hio_get_upstream(upstream_io);
  805. assert(handler != NULL && io != NULL);
  806. HttpRequest* req = handler->req.get();
  807. // NOTE: send head + received body
  808. req->headers.erase("Proxy-Connection");
  809. req->headers["Connection"] = handler->keepalive ? "keep-alive" : "close";
  810. req->headers["X-Real-IP"] = handler->ip;
  811. std::string msg = req->Dump(true, true);
  812. // printf("%s\n", msg.c_str());
  813. hio_write(upstream_io, msg.c_str(), msg.size());
  814. // NOTE: start recv body continue then upstream
  815. hio_setcb_read(io, hio_write_upstream);
  816. hio_read_start(io);
  817. hio_setcb_read(upstream_io, hio_write_upstream);
  818. hio_read_start(upstream_io);
  819. }
  820. int HttpHandler::proxyConnect(const std::string& strUrl) {
  821. if (!io) return ERR_NULL_POINTER;
  822. hloop_t* loop = hevent_loop(io);
  823. HUrl url;
  824. if (!url.parse(strUrl)) {
  825. return ERR_PARSE;
  826. }
  827. hlogi("proxy_pass %s", strUrl.c_str());
  828. hio_t* upstream_io = hio_create_socket(loop, url.host.c_str(), url.port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  829. if (upstream_io == NULL) {
  830. hio_close_async(io);
  831. return ERR_SOCKET;
  832. }
  833. if (url.scheme == "https") {
  834. hio_enable_ssl(upstream_io);
  835. }
  836. hevent_set_userdata(upstream_io, this);
  837. hio_setup_upstream(io, upstream_io);
  838. hio_setcb_connect(upstream_io, HttpHandler::onProxyConnect);
  839. hio_setcb_close(upstream_io, hio_close_upstream);
  840. if (service->proxy_connect_timeout > 0) {
  841. hio_set_connect_timeout(upstream_io, service->proxy_connect_timeout);
  842. }
  843. if (service->proxy_read_timeout > 0) {
  844. hio_set_read_timeout(io, service->proxy_read_timeout);
  845. }
  846. if (service->proxy_write_timeout > 0) {
  847. hio_set_write_timeout(io, service->proxy_write_timeout);
  848. }
  849. hio_connect(upstream_io);
  850. // NOTE: wait upstream_io connected then start read
  851. hio_read_stop(io);
  852. return 0;
  853. }