1
0

HttpHandler.cpp 33 KB

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