http_server.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * http_server - HTTP server
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <fcntl.h>
  16. #include "common.h"
  17. #include "eloop.h"
  18. #include "httpread.h"
  19. #include "http_server.h"
  20. #define HTTP_SERVER_TIMEOUT 30
  21. #define HTTP_SERVER_MAX_REQ_LEN 8000
  22. #define HTTP_SERVER_MAX_CONNECTIONS 10
  23. struct http_request {
  24. struct http_request *next;
  25. struct http_server *srv;
  26. int fd;
  27. struct sockaddr_in cli;
  28. struct httpread *hread;
  29. };
  30. struct http_server {
  31. void (*cb)(void *ctx, struct http_request *req);
  32. void *cb_ctx;
  33. int fd;
  34. int port;
  35. struct http_request *requests;
  36. unsigned int request_count;
  37. };
  38. static void http_request_cb(struct httpread *handle, void *cookie,
  39. enum httpread_event en)
  40. {
  41. struct http_request *req = cookie;
  42. struct http_server *srv = req->srv;
  43. if (en == HTTPREAD_EVENT_FILE_READY) {
  44. wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d received",
  45. inet_ntoa(req->cli.sin_addr),
  46. ntohs(req->cli.sin_port));
  47. srv->cb(srv->cb_ctx, req);
  48. return;
  49. }
  50. wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d could not be received "
  51. "completely", inet_ntoa(req->cli.sin_addr),
  52. ntohs(req->cli.sin_port));
  53. http_request_deinit(req);
  54. }
  55. static struct http_request * http_request_init(struct http_server *srv, int fd,
  56. struct sockaddr_in *cli)
  57. {
  58. struct http_request *req;
  59. if (srv->request_count >= HTTP_SERVER_MAX_CONNECTIONS) {
  60. wpa_printf(MSG_DEBUG, "HTTP: Too many concurrent requests");
  61. return NULL;
  62. }
  63. req = os_zalloc(sizeof(*req));
  64. if (req == NULL)
  65. return NULL;
  66. req->srv = srv;
  67. req->fd = fd;
  68. req->cli = *cli;
  69. req->hread = httpread_create(req->fd, http_request_cb, req,
  70. HTTP_SERVER_MAX_REQ_LEN,
  71. HTTP_SERVER_TIMEOUT);
  72. if (req->hread == NULL) {
  73. http_request_deinit(req);
  74. return NULL;
  75. }
  76. return req;
  77. }
  78. void http_request_deinit(struct http_request *req)
  79. {
  80. struct http_request *r, *p;
  81. struct http_server *srv;
  82. if (req == NULL)
  83. return;
  84. srv = req->srv;
  85. p = NULL;
  86. r = srv->requests;
  87. while (r) {
  88. if (r == req) {
  89. if (p)
  90. p->next = r->next;
  91. else
  92. srv->requests = r->next;
  93. srv->request_count--;
  94. break;
  95. }
  96. p = r;
  97. r = r->next;
  98. }
  99. httpread_destroy(req->hread);
  100. close(req->fd);
  101. os_free(req);
  102. }
  103. static void http_request_free_all(struct http_request *req)
  104. {
  105. struct http_request *prev;
  106. while (req) {
  107. prev = req;
  108. req = req->next;
  109. http_request_deinit(prev);
  110. }
  111. }
  112. void http_request_send(struct http_request *req, struct wpabuf *resp)
  113. {
  114. int res;
  115. wpa_printf(MSG_DEBUG, "HTTP: Send %lu byte response to %s:%d",
  116. (unsigned long) wpabuf_len(resp),
  117. inet_ntoa(req->cli.sin_addr),
  118. ntohs(req->cli.sin_port));
  119. res = send(req->fd, wpabuf_head(resp), wpabuf_len(resp), 0);
  120. if (res < 0) {
  121. wpa_printf(MSG_DEBUG, "HTTP: Send failed: %s",
  122. strerror(errno));
  123. } else if ((size_t) res < wpabuf_len(resp)) {
  124. wpa_printf(MSG_DEBUG, "HTTP: Sent only %d of %lu bytes",
  125. res, (unsigned long) wpabuf_len(resp));
  126. /* TODO: add eloop handler for sending rest of the data */
  127. }
  128. wpabuf_free(resp);
  129. }
  130. void http_request_send_and_deinit(struct http_request *req,
  131. struct wpabuf *resp)
  132. {
  133. http_request_send(req, resp);
  134. http_request_deinit(req);
  135. }
  136. enum httpread_hdr_type http_request_get_type(struct http_request *req)
  137. {
  138. return httpread_hdr_type_get(req->hread);
  139. }
  140. char * http_request_get_uri(struct http_request *req)
  141. {
  142. return httpread_uri_get(req->hread);
  143. }
  144. char * http_request_get_hdr(struct http_request *req)
  145. {
  146. return httpread_hdr_get(req->hread);
  147. }
  148. char * http_request_get_data(struct http_request *req)
  149. {
  150. return httpread_data_get(req->hread);
  151. }
  152. char * http_request_get_hdr_line(struct http_request *req, const char *tag)
  153. {
  154. return httpread_hdr_line_get(req->hread, tag);
  155. }
  156. struct sockaddr_in * http_request_get_cli_addr(struct http_request *req)
  157. {
  158. return &req->cli;
  159. }
  160. static void http_server_cb(int sd, void *eloop_ctx, void *sock_ctx)
  161. {
  162. struct sockaddr_in addr;
  163. socklen_t addr_len = sizeof(addr);
  164. struct http_server *srv = eloop_ctx;
  165. int conn;
  166. struct http_request *req;
  167. conn = accept(srv->fd, (struct sockaddr *) &addr, &addr_len);
  168. if (conn < 0) {
  169. wpa_printf(MSG_DEBUG, "HTTP: Failed to accept new connection: "
  170. "%s", strerror(errno));
  171. return;
  172. }
  173. wpa_printf(MSG_DEBUG, "HTTP: Connection from %s:%d",
  174. inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  175. req = http_request_init(srv, conn, &addr);
  176. if (req == NULL) {
  177. close(conn);
  178. return;
  179. }
  180. req->next = srv->requests;
  181. srv->requests = req;
  182. srv->request_count++;
  183. }
  184. struct http_server * http_server_init(struct in_addr *addr, int port,
  185. void (*cb)(void *ctx,
  186. struct http_request *req),
  187. void *cb_ctx)
  188. {
  189. struct sockaddr_in sin;
  190. struct http_server *srv;
  191. srv = os_zalloc(sizeof(*srv));
  192. if (srv == NULL)
  193. return NULL;
  194. srv->cb = cb;
  195. srv->cb_ctx = cb_ctx;
  196. srv->fd = socket(AF_INET, SOCK_STREAM, 0);
  197. if (srv->fd < 0)
  198. goto fail;
  199. if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
  200. goto fail;
  201. if (port < 0)
  202. srv->port = 49152;
  203. else
  204. srv->port = port;
  205. os_memset(&sin, 0, sizeof(sin));
  206. sin.sin_family = AF_INET;
  207. sin.sin_addr.s_addr = addr->s_addr;
  208. for (;;) {
  209. sin.sin_port = htons(srv->port);
  210. if (bind(srv->fd, (struct sockaddr *) &sin, sizeof(sin)) == 0)
  211. break;
  212. if (errno == EADDRINUSE) {
  213. /* search for unused port */
  214. if (++srv->port == 65535 || port >= 0)
  215. goto fail;
  216. continue;
  217. }
  218. wpa_printf(MSG_DEBUG, "HTTP: Failed to bind server port %d: "
  219. "%s", srv->port, strerror(errno));
  220. goto fail;
  221. }
  222. if (listen(srv->fd, 10 /* max backlog */) < 0)
  223. goto fail;
  224. if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
  225. goto fail;
  226. if (eloop_register_sock(srv->fd, EVENT_TYPE_READ, http_server_cb,
  227. srv, NULL))
  228. goto fail;
  229. wpa_printf(MSG_DEBUG, "HTTP: Started server on %s:%d",
  230. inet_ntoa(*addr), srv->port);
  231. return srv;
  232. fail:
  233. http_server_deinit(srv);
  234. return NULL;
  235. }
  236. void http_server_deinit(struct http_server *srv)
  237. {
  238. if (srv == NULL)
  239. return;
  240. if (srv->fd >= 0) {
  241. eloop_unregister_sock(srv->fd, EVENT_TYPE_READ);
  242. close(srv->fd);
  243. }
  244. http_request_free_all(srv->requests);
  245. os_free(srv);
  246. }
  247. int http_server_get_port(struct http_server *srv)
  248. {
  249. return srv->port;
  250. }