http_client.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * http_client - HTTP client
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <fcntl.h>
  10. #include "common.h"
  11. #include "eloop.h"
  12. #include "httpread.h"
  13. #include "http_client.h"
  14. #define HTTP_CLIENT_TIMEOUT_SEC 30
  15. struct http_client {
  16. struct sockaddr_in dst;
  17. int sd;
  18. struct wpabuf *req;
  19. size_t req_pos;
  20. size_t max_response;
  21. void (*cb)(void *ctx, struct http_client *c,
  22. enum http_client_event event);
  23. void *cb_ctx;
  24. struct httpread *hread;
  25. struct wpabuf body;
  26. };
  27. static void http_client_timeout(void *eloop_data, void *user_ctx)
  28. {
  29. struct http_client *c = eloop_data;
  30. wpa_printf(MSG_DEBUG, "HTTP: Timeout (c=%p)", c);
  31. c->cb(c->cb_ctx, c, HTTP_CLIENT_TIMEOUT);
  32. }
  33. static void http_client_got_response(struct httpread *handle, void *cookie,
  34. enum httpread_event e)
  35. {
  36. struct http_client *c = cookie;
  37. wpa_printf(MSG_DEBUG, "HTTP: httpread callback: handle=%p cookie=%p "
  38. "e=%d", handle, cookie, e);
  39. eloop_cancel_timeout(http_client_timeout, c, NULL);
  40. switch (e) {
  41. case HTTPREAD_EVENT_FILE_READY:
  42. if (httpread_hdr_type_get(c->hread) == HTTPREAD_HDR_TYPE_REPLY)
  43. {
  44. int reply_code = httpread_reply_code_get(c->hread);
  45. if (reply_code == 200 /* OK */) {
  46. wpa_printf(MSG_DEBUG, "HTTP: Response OK from "
  47. "%s:%d",
  48. inet_ntoa(c->dst.sin_addr),
  49. ntohs(c->dst.sin_port));
  50. c->cb(c->cb_ctx, c, HTTP_CLIENT_OK);
  51. } else {
  52. wpa_printf(MSG_DEBUG, "HTTP: Error %d from "
  53. "%s:%d", reply_code,
  54. inet_ntoa(c->dst.sin_addr),
  55. ntohs(c->dst.sin_port));
  56. c->cb(c->cb_ctx, c, HTTP_CLIENT_INVALID_REPLY);
  57. }
  58. } else
  59. c->cb(c->cb_ctx, c, HTTP_CLIENT_INVALID_REPLY);
  60. break;
  61. case HTTPREAD_EVENT_TIMEOUT:
  62. c->cb(c->cb_ctx, c, HTTP_CLIENT_TIMEOUT);
  63. break;
  64. case HTTPREAD_EVENT_ERROR:
  65. c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
  66. break;
  67. }
  68. }
  69. static void http_client_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
  70. {
  71. struct http_client *c = eloop_ctx;
  72. int res;
  73. size_t send_len;
  74. send_len = wpabuf_len(c->req) - c->req_pos;
  75. wpa_printf(MSG_DEBUG, "HTTP: Send client request to %s:%d (%lu of %lu "
  76. "bytes remaining)",
  77. inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port),
  78. (unsigned long) wpabuf_len(c->req),
  79. (unsigned long) send_len);
  80. res = send(c->sd, wpabuf_head_u8(c->req) + c->req_pos, send_len, 0);
  81. if (res < 0) {
  82. wpa_printf(MSG_DEBUG, "HTTP: Failed to send buffer: %s",
  83. strerror(errno));
  84. eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
  85. c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
  86. return;
  87. }
  88. if ((size_t) res < send_len) {
  89. wpa_printf(MSG_DEBUG, "HTTP: Sent %d of %lu bytes; %lu bytes "
  90. "remaining",
  91. res, (unsigned long) wpabuf_len(c->req),
  92. (unsigned long) send_len - res);
  93. c->req_pos += res;
  94. return;
  95. }
  96. wpa_printf(MSG_DEBUG, "HTTP: Full client request sent to %s:%d",
  97. inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port));
  98. eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
  99. wpabuf_free(c->req);
  100. c->req = NULL;
  101. c->hread = httpread_create(c->sd, http_client_got_response, c,
  102. c->max_response, HTTP_CLIENT_TIMEOUT_SEC);
  103. if (c->hread == NULL) {
  104. c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
  105. return;
  106. }
  107. }
  108. struct http_client * http_client_addr(struct sockaddr_in *dst,
  109. struct wpabuf *req, size_t max_response,
  110. void (*cb)(void *ctx,
  111. struct http_client *c,
  112. enum http_client_event event),
  113. void *cb_ctx)
  114. {
  115. struct http_client *c;
  116. c = os_zalloc(sizeof(*c));
  117. if (c == NULL)
  118. return NULL;
  119. c->sd = -1;
  120. c->dst = *dst;
  121. c->max_response = max_response;
  122. c->cb = cb;
  123. c->cb_ctx = cb_ctx;
  124. c->sd = socket(AF_INET, SOCK_STREAM, 0);
  125. if (c->sd < 0)
  126. goto fail;
  127. if (fcntl(c->sd, F_SETFL, O_NONBLOCK) != 0) {
  128. wpa_printf(MSG_DEBUG, "HTTP: fnctl(O_NONBLOCK) failed: %s",
  129. strerror(errno));
  130. goto fail;
  131. }
  132. if (connect(c->sd, (struct sockaddr *) dst, sizeof(*dst))) {
  133. if (errno != EINPROGRESS) {
  134. wpa_printf(MSG_DEBUG, "HTTP: Failed to connect: %s",
  135. strerror(errno));
  136. goto fail;
  137. }
  138. /*
  139. * Continue connecting in the background; eloop will call us
  140. * once the connection is ready (or failed).
  141. */
  142. }
  143. if (eloop_register_sock(c->sd, EVENT_TYPE_WRITE, http_client_tx_ready,
  144. c, NULL) ||
  145. eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
  146. http_client_timeout, c, NULL))
  147. goto fail;
  148. c->req = req;
  149. return c;
  150. fail:
  151. http_client_free(c);
  152. return NULL;
  153. }
  154. char * http_client_url_parse(const char *url, struct sockaddr_in *dst,
  155. char **ret_path)
  156. {
  157. char *u, *addr, *port, *path;
  158. u = os_strdup(url);
  159. if (u == NULL)
  160. return NULL;
  161. os_memset(dst, 0, sizeof(*dst));
  162. dst->sin_family = AF_INET;
  163. addr = u + 7;
  164. path = os_strchr(addr, '/');
  165. port = os_strchr(addr, ':');
  166. if (path == NULL) {
  167. path = "/";
  168. } else {
  169. *path = '\0'; /* temporary nul termination for address */
  170. if (port > path)
  171. port = NULL;
  172. }
  173. if (port)
  174. *port++ = '\0';
  175. if (inet_aton(addr, &dst->sin_addr) == 0) {
  176. /* TODO: name lookup */
  177. wpa_printf(MSG_DEBUG, "HTTP: Unsupported address in URL '%s' "
  178. "(addr='%s' port='%s')",
  179. url, addr, port);
  180. os_free(u);
  181. return NULL;
  182. }
  183. if (port)
  184. dst->sin_port = htons(atoi(port));
  185. else
  186. dst->sin_port = htons(80);
  187. if (*path == '\0') {
  188. /* remove temporary nul termination for address */
  189. *path = '/';
  190. }
  191. *ret_path = path;
  192. return u;
  193. }
  194. struct http_client * http_client_url(const char *url,
  195. struct wpabuf *req, size_t max_response,
  196. void (*cb)(void *ctx,
  197. struct http_client *c,
  198. enum http_client_event event),
  199. void *cb_ctx)
  200. {
  201. struct sockaddr_in dst;
  202. struct http_client *c;
  203. char *u, *path;
  204. struct wpabuf *req_buf = NULL;
  205. if (os_strncmp(url, "http://", 7) != 0)
  206. return NULL;
  207. u = http_client_url_parse(url, &dst, &path);
  208. if (u == NULL)
  209. return NULL;
  210. if (req == NULL) {
  211. req_buf = wpabuf_alloc(os_strlen(url) + 1000);
  212. if (req_buf == NULL) {
  213. os_free(u);
  214. return NULL;
  215. }
  216. req = req_buf;
  217. wpabuf_printf(req,
  218. "GET %s HTTP/1.1\r\n"
  219. "Cache-Control: no-cache\r\n"
  220. "Pragma: no-cache\r\n"
  221. "Accept: text/xml, application/xml\r\n"
  222. "User-Agent: wpa_supplicant\r\n"
  223. "Host: %s:%d\r\n"
  224. "\r\n",
  225. path, inet_ntoa(dst.sin_addr),
  226. ntohs(dst.sin_port));
  227. }
  228. os_free(u);
  229. c = http_client_addr(&dst, req, max_response, cb, cb_ctx);
  230. if (c == NULL) {
  231. wpabuf_free(req_buf);
  232. return NULL;
  233. }
  234. return c;
  235. }
  236. void http_client_free(struct http_client *c)
  237. {
  238. if (c == NULL)
  239. return;
  240. httpread_destroy(c->hread);
  241. wpabuf_free(c->req);
  242. if (c->sd >= 0) {
  243. eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
  244. close(c->sd);
  245. }
  246. eloop_cancel_timeout(http_client_timeout, c, NULL);
  247. os_free(c);
  248. }
  249. struct wpabuf * http_client_get_body(struct http_client *c)
  250. {
  251. if (c->hread == NULL)
  252. return NULL;
  253. wpabuf_set(&c->body, httpread_data_get(c->hread),
  254. httpread_length_get(c->hread));
  255. return &c->body;
  256. }
  257. char * http_client_get_hdr_line(struct http_client *c, const char *tag)
  258. {
  259. if (c->hread == NULL)
  260. return NULL;
  261. return httpread_hdr_line_get(c->hread, tag);
  262. }
  263. char * http_link_update(char *url, const char *base)
  264. {
  265. char *n;
  266. size_t len;
  267. const char *pos;
  268. /* RFC 2396, Chapter 5.2 */
  269. /* TODO: consider adding all cases described in RFC 2396 */
  270. if (url == NULL)
  271. return NULL;
  272. if (os_strncmp(url, "http://", 7) == 0)
  273. return url; /* absolute link */
  274. if (os_strncmp(base, "http://", 7) != 0)
  275. return url; /* unable to handle base URL */
  276. len = os_strlen(url) + 1 + os_strlen(base) + 1;
  277. n = os_malloc(len);
  278. if (n == NULL)
  279. return url; /* failed */
  280. if (url[0] == '/') {
  281. pos = os_strchr(base + 7, '/');
  282. if (pos == NULL) {
  283. os_snprintf(n, len, "%s%s", base, url);
  284. } else {
  285. os_memcpy(n, base, pos - base);
  286. os_memcpy(n + (pos - base), url, os_strlen(url) + 1);
  287. }
  288. } else {
  289. pos = os_strrchr(base + 7, '/');
  290. if (pos == NULL) {
  291. os_snprintf(n, len, "%s/%s", base, url);
  292. } else {
  293. os_memcpy(n, base, pos - base + 1);
  294. os_memcpy(n + (pos - base) + 1, url, os_strlen(url) +
  295. 1);
  296. }
  297. }
  298. os_free(url);
  299. return n;
  300. }