http_server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "http_server.h"
  2. #include "h.h"
  3. #include "hmain.h"
  4. #include "hloop.h"
  5. #include "HttpParser.h"
  6. #include "FileCache.h"
  7. #include "httpd_conf.h"
  8. #define RECV_BUFSIZE 4096
  9. #define SEND_BUFSIZE 4096
  10. static HttpService s_default_service;
  11. static FileCache s_filecache;
  12. /*
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <title>404 Not Found</title>
  17. </head>
  18. <body>
  19. <center><h1>404 Not Found</h1></center>
  20. <hr>
  21. </body>
  22. </html>
  23. */
  24. static void make_http_status_page(http_status status_code, std::string& page) {
  25. char szCode[8];
  26. snprintf(szCode, sizeof(szCode), "%d ", status_code);
  27. const char* status_message = http_status_str(status_code);
  28. page += R"(<!DOCTYPE html>
  29. <html>
  30. <head>
  31. <title>)";
  32. page += szCode; page += status_message;
  33. page += R"(</title>
  34. </head>
  35. <body>
  36. <center><h1>)";
  37. page += szCode; page += status_message;
  38. page += R"(</h1></center>
  39. <hr>
  40. </body>
  41. </html>)";
  42. }
  43. static void master_init(void* userdata) {
  44. #ifdef OS_UNIX
  45. char proctitle[256] = {0};
  46. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  47. setproctitle(proctitle);
  48. #endif
  49. }
  50. static void master_proc(void* userdata) {
  51. while(1) sleep(1);
  52. }
  53. static void worker_init(void* userdata) {
  54. #ifdef OS_UNIX
  55. char proctitle[256] = {0};
  56. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  57. setproctitle(proctitle);
  58. signal(SIGNAL_RELOAD, signal_handler);
  59. #endif
  60. }
  61. struct http_connect_userdata {
  62. http_server_t* server;
  63. std::string log;
  64. HttpParser parser;
  65. HttpRequest req;
  66. HttpResponse res;
  67. http_connect_userdata() {
  68. parser.parser_request_init(&req);
  69. }
  70. };
  71. static void on_read(hevent_t* event, void* userdata) {
  72. //printf("on_read fd=%d\n", event->fd);
  73. http_connect_userdata* hcu = (http_connect_userdata*)userdata;
  74. HttpService* service = hcu->server->service;
  75. HttpRequest* req = &hcu->req;
  76. HttpResponse* res = &hcu->res;
  77. char recvbuf[RECV_BUFSIZE] = {0};
  78. int ret, nrecv, nparse, nsend;
  79. recv:
  80. // recv -> http_parser -> http_request -> http_request_handler -> http_response -> send
  81. nrecv = recv(event->fd, recvbuf, sizeof(recvbuf), 0);
  82. //printf("recv retval=%d\n", nrecv);
  83. if (nrecv < 0) {
  84. if (sockerrno != NIO_EAGAIN) {
  85. perror("recv");
  86. hcu->log += asprintf("recv: %s", strerror(errno));
  87. goto recv_error;
  88. }
  89. //goto recv_done;
  90. return;
  91. }
  92. if (nrecv == 0) {
  93. hcu->log += "disconnect";
  94. goto disconnect;
  95. }
  96. //printf("%s\n", recvbuf);
  97. nparse = hcu->parser.execute(recvbuf, nrecv);
  98. if (nparse != nrecv || hcu->parser.get_errno() != HPE_OK) {
  99. hcu->log += asprintf("http parser error: %s", http_errno_description(hcu->parser.get_errno()));
  100. goto parser_error;
  101. }
  102. if (hcu->parser.get_state() == HP_MESSAGE_COMPLETE) {
  103. http_api_handler api = NULL;
  104. file_cache_t* fc = NULL;
  105. const char* content = NULL;
  106. int content_length = 0;
  107. bool send_in_one_packet = false;
  108. hcu->log += asprintf("[%s %s]", http_method_str(req->method), req->url.c_str());
  109. static std::string s_Server = std::string("httpd/") + std::string(get_compile_version());
  110. res->headers["Server"] = s_Server;
  111. // preprocessor
  112. if (service->preprocessor) {
  113. service->preprocessor(req, res);
  114. }
  115. ret = service->GetApi(req->url.c_str(), req->method, &api);
  116. if (api) {
  117. // api service
  118. api(req, res);
  119. }
  120. else {
  121. if (ret == HTTP_STATUS_METHOD_NOT_ALLOWED) {
  122. // Method Not Allowed
  123. res->status_code = HTTP_STATUS_METHOD_NOT_ALLOWED;
  124. }
  125. else if (req->method == HTTP_GET) {
  126. // web service
  127. std::string filepath = service->document_root;
  128. filepath += req->url.c_str();
  129. if (strcmp(req->url.c_str(), "/") == 0) {
  130. filepath += service->home_page;
  131. }
  132. fc = s_filecache.Open(filepath.c_str());
  133. // Not Found
  134. if (fc == NULL) {
  135. res->status_code = HTTP_STATUS_NOT_FOUND;
  136. }
  137. else {
  138. // Not Modified
  139. auto iter = req->headers.find("if-not-match");
  140. if (iter != req->headers.end() &&
  141. strcmp(iter->second.c_str(), fc->etag) == 0) {
  142. res->status_code = HTTP_STATUS_NOT_MODIFIED;
  143. fc = NULL;
  144. }
  145. else {
  146. iter = req->headers.find("if-modified-since");
  147. if (iter != req->headers.end() &&
  148. strcmp(iter->second.c_str(), fc->last_modified) == 0) {
  149. res->status_code = HTTP_STATUS_NOT_MODIFIED;
  150. fc = NULL;
  151. }
  152. }
  153. }
  154. }
  155. else {
  156. // Not Implemented
  157. res->status_code = HTTP_STATUS_NOT_IMPLEMENTED;
  158. }
  159. // html page
  160. if (res->status_code >= 400 && res->body.size() == 0) {
  161. // error page
  162. if (service->error_page.size() != 0) {
  163. std::string filepath = service->document_root;
  164. filepath += '/';
  165. filepath += service->error_page;
  166. fc = s_filecache.Open(filepath.c_str());
  167. }
  168. // status page
  169. if (fc == NULL && res->body.size() == 0) {
  170. res->content_type = TEXT_HTML;
  171. make_http_status_page(res->status_code, res->body);
  172. }
  173. }
  174. }
  175. // postprocessor
  176. if (service->postprocessor) {
  177. service->postprocessor(req, res);
  178. }
  179. // send
  180. std::string header;
  181. time_t tt;
  182. time(&tt);
  183. char c_str[256] = {0};
  184. strftime(c_str, sizeof(c_str), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  185. res->headers["Date"] = c_str;
  186. if (fc && fc->filebuf.len) {
  187. content = (const char*)fc->filebuf.base;
  188. content_length = fc->filebuf.len;
  189. if (fc->content_type && *fc->content_type != '\0') {
  190. res->headers["Content-Type"] = fc->content_type;
  191. }
  192. res->headers["Content-Length"] = std::to_string(content_length);
  193. res->headers["Last-Modified"] = fc->last_modified;
  194. res->headers["Etag"] = fc->etag;
  195. }
  196. else if (res->body.size()) {
  197. content = res->body.c_str();
  198. content_length = res->body.size();
  199. }
  200. header = res->dump(true, false);
  201. if (header.size() + content_length <= SEND_BUFSIZE) {
  202. header.insert(header.size(), content, content_length);
  203. send_in_one_packet = true;
  204. }
  205. // send header
  206. nsend = send(event->fd, header.c_str(), header.size(), 0);
  207. if (nsend != header.size()) {
  208. hcu->log += asprintf("send header: %s", strerror(errno));
  209. goto send_error;
  210. }
  211. // send body
  212. if (!send_in_one_packet && content_length != 0) {
  213. //queue send ?
  214. //if (content_length > SEND_BUFSIZE) {
  215. //}
  216. nsend = send(event->fd, content, content_length, 0);
  217. if (nsend != res->body.size()) {
  218. hcu->log += asprintf("send body: %s", strerror(errno));
  219. goto send_error;
  220. }
  221. }
  222. hcu->log += asprintf("=>[%d %s]", res->status_code, http_status_str(res->status_code));
  223. hlogi("%s", hcu->log.c_str());
  224. goto end;
  225. }
  226. if (nrecv == sizeof(recvbuf)) {
  227. goto recv;
  228. }
  229. goto end;
  230. recv_error:
  231. disconnect:
  232. parser_error:
  233. send_error:
  234. hloge("%s", hcu->log.c_str());
  235. end:
  236. closesocket(event->fd);
  237. hevent_del(event);
  238. delete hcu;
  239. }
  240. static void on_accept(hevent_t* event, void* userdata) {
  241. //printf("on_accept listenfd=%d\n", event->fd);
  242. struct sockaddr_in localaddr, peeraddr;
  243. socklen_t addrlen = sizeof(struct sockaddr_in);
  244. getsockname(event->fd, (struct sockaddr*)&localaddr, &addrlen);
  245. accept:
  246. addrlen = sizeof(struct sockaddr_in);
  247. int connfd = accept(event->fd, (struct sockaddr*)&peeraddr, &addrlen);
  248. if (connfd < 0) {
  249. if (sockerrno != NIO_EAGAIN) {
  250. perror("accept");
  251. hloge("accept failed: %s: %d", strerror(sockerrno), sockerrno);
  252. goto accept_error;
  253. }
  254. //goto accept_done;
  255. return;
  256. }
  257. {
  258. // new http_connect
  259. // delete on on_read
  260. http_connect_userdata* hcu = new http_connect_userdata;
  261. hcu->server = (http_server_t*)userdata;
  262. hcu->log += asprintf("[%s:%d]", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
  263. nonblocking(connfd);
  264. hevent_read(event->loop, connfd, on_read, hcu);
  265. }
  266. goto accept;
  267. accept_error:
  268. closesocket(event->fd);
  269. hevent_del(event);
  270. }
  271. void handle_cached_files(htimer_t* timer, void* userdata) {
  272. FileCache* pfc = (FileCache*)userdata;
  273. if (pfc == NULL) {
  274. htimer_del(timer);
  275. return;
  276. }
  277. file_cache_t* fc = NULL;
  278. time_t tt;
  279. time(&tt);
  280. auto iter = pfc->cached_files.begin();
  281. while (iter != pfc->cached_files.end()) {
  282. fc = iter->second;
  283. if (tt - fc->stat_time > g_conf_ctx.file_cached_time) {
  284. delete fc;
  285. iter = pfc->cached_files.erase(iter);
  286. continue;
  287. }
  288. ++iter;
  289. }
  290. }
  291. static void worker_proc(void* userdata) {
  292. http_server_t* server = (http_server_t*)userdata;
  293. int listenfd = server->listenfd;
  294. hloop_t loop;
  295. hloop_init(&loop);
  296. htimer_add(&loop, handle_cached_files, &s_filecache, MAX(60000, g_conf_ctx.file_cached_time*1000));
  297. hevent_accept(&loop, listenfd, on_accept, server);
  298. hloop_run(&loop);
  299. }
  300. int http_server_run(http_server_t* server, int wait) {
  301. // worker_processes
  302. if (server->worker_processes != 0 && g_worker_processes_num != 0 && g_worker_processes != NULL) {
  303. return ERR_OVER_LIMIT;
  304. }
  305. // service
  306. if (server->service == NULL) {
  307. server->service = &s_default_service;
  308. }
  309. // port
  310. server->listenfd = Listen(server->port);
  311. if (server->listenfd < 0) return server->listenfd;
  312. if (server->worker_processes == 0) {
  313. worker_proc(server);
  314. }
  315. else {
  316. // master-workers processes
  317. g_worker_processes_num = server->worker_processes;
  318. int bytes = g_worker_processes_num * sizeof(proc_ctx_t);
  319. g_worker_processes = (proc_ctx_t*)malloc(bytes);
  320. if (g_worker_processes == NULL) {
  321. perror("malloc");
  322. abort();
  323. }
  324. memset(g_worker_processes, 0, bytes);
  325. for (int i = 0; i < g_worker_processes_num; ++i) {
  326. proc_ctx_t* ctx = g_worker_processes + i;
  327. ctx->init = worker_init;
  328. ctx->init_userdata = NULL;
  329. ctx->proc = worker_proc;
  330. ctx->proc_userdata = server;
  331. spawn_proc(ctx);
  332. }
  333. }
  334. if (wait) {
  335. master_init(NULL);
  336. master_proc(NULL);
  337. }
  338. return 0;
  339. }