HttpServer.cpp 11 KB

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