http_client.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. wpa_printf(MSG_DEBUG, "HTTP: Send client request to %s:%d (%lu of %lu "
  74. "bytes remaining)",
  75. inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port),
  76. (unsigned long) wpabuf_len(c->req),
  77. (unsigned long) wpabuf_len(c->req) - c->req_pos);
  78. res = send(c->sd, wpabuf_head_u8(c->req) + c->req_pos,
  79. wpabuf_len(c->req) - c->req_pos, 0);
  80. if (res < 0) {
  81. wpa_printf(MSG_DEBUG, "HTTP: Failed to send buffer: %s",
  82. strerror(errno));
  83. eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
  84. c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
  85. return;
  86. }
  87. if ((size_t) res < wpabuf_len(c->req) - c->req_pos) {
  88. wpa_printf(MSG_DEBUG, "HTTP: Sent %d of %lu bytes; %lu bytes "
  89. "remaining",
  90. res, (unsigned long) wpabuf_len(c->req),
  91. (unsigned long) wpabuf_len(c->req) - c->req_pos -
  92. 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. http_client_free(c);
  127. return NULL;
  128. }
  129. if (fcntl(c->sd, F_SETFL, O_NONBLOCK) != 0) {
  130. wpa_printf(MSG_DEBUG, "HTTP: fnctl(O_NONBLOCK) failed: %s",
  131. strerror(errno));
  132. http_client_free(c);
  133. return NULL;
  134. }
  135. if (connect(c->sd, (struct sockaddr *) dst, sizeof(*dst))) {
  136. if (errno != EINPROGRESS) {
  137. wpa_printf(MSG_DEBUG, "HTTP: Failed to connect: %s",
  138. strerror(errno));
  139. http_client_free(c);
  140. return NULL;
  141. }
  142. /*
  143. * Continue connecting in the background; eloop will call us
  144. * once the connection is ready (or failed).
  145. */
  146. }
  147. if (eloop_register_sock(c->sd, EVENT_TYPE_WRITE, http_client_tx_ready,
  148. c, NULL)) {
  149. http_client_free(c);
  150. return NULL;
  151. }
  152. if (eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
  153. http_client_timeout, c, NULL)) {
  154. http_client_free(c);
  155. return NULL;
  156. }
  157. c->req = req;
  158. return c;
  159. }
  160. char * http_client_url_parse(const char *url, struct sockaddr_in *dst,
  161. char **ret_path)
  162. {
  163. char *u, *addr, *port, *path;
  164. u = os_strdup(url);
  165. if (u == NULL)
  166. return NULL;
  167. os_memset(dst, 0, sizeof(*dst));
  168. dst->sin_family = AF_INET;
  169. addr = u + 7;
  170. path = os_strchr(addr, '/');
  171. port = os_strchr(addr, ':');
  172. if (path == NULL) {
  173. path = "/";
  174. } else {
  175. *path = '\0'; /* temporary nul termination for address */
  176. if (port > path)
  177. port = NULL;
  178. }
  179. if (port)
  180. *port++ = '\0';
  181. if (inet_aton(addr, &dst->sin_addr) == 0) {
  182. /* TODO: name lookup */
  183. wpa_printf(MSG_DEBUG, "HTTP: Unsupported address in URL '%s' "
  184. "(addr='%s' port='%s')",
  185. url, addr, port);
  186. os_free(u);
  187. return NULL;
  188. }
  189. if (port)
  190. dst->sin_port = htons(atoi(port));
  191. else
  192. dst->sin_port = htons(80);
  193. if (*path == '\0') {
  194. /* remove temporary nul termination for address */
  195. *path = '/';
  196. }
  197. *ret_path = path;
  198. return u;
  199. }
  200. struct http_client * http_client_url(const char *url,
  201. struct wpabuf *req, size_t max_response,
  202. void (*cb)(void *ctx,
  203. struct http_client *c,
  204. enum http_client_event event),
  205. void *cb_ctx)
  206. {
  207. struct sockaddr_in dst;
  208. struct http_client *c;
  209. char *u, *path;
  210. struct wpabuf *req_buf = NULL;
  211. if (os_strncmp(url, "http://", 7) != 0)
  212. return NULL;
  213. u = http_client_url_parse(url, &dst, &path);
  214. if (u == NULL)
  215. return NULL;
  216. if (req == NULL) {
  217. req_buf = wpabuf_alloc(os_strlen(url) + 1000);
  218. if (req_buf == NULL) {
  219. os_free(u);
  220. return NULL;
  221. }
  222. req = req_buf;
  223. wpabuf_printf(req,
  224. "GET %s HTTP/1.1\r\n"
  225. "Cache-Control: no-cache\r\n"
  226. "Pragma: no-cache\r\n"
  227. "Accept: text/xml, application/xml\r\n"
  228. "User-Agent: wpa_supplicant\r\n"
  229. "Host: %s:%d\r\n"
  230. "\r\n",
  231. path, inet_ntoa(dst.sin_addr),
  232. ntohs(dst.sin_port));
  233. }
  234. os_free(u);
  235. c = http_client_addr(&dst, req, max_response, cb, cb_ctx);
  236. if (c == NULL) {
  237. wpabuf_free(req_buf);
  238. return NULL;
  239. }
  240. return c;
  241. }
  242. void http_client_free(struct http_client *c)
  243. {
  244. if (c == NULL)
  245. return;
  246. httpread_destroy(c->hread);
  247. wpabuf_free(c->req);
  248. if (c->sd >= 0) {
  249. eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
  250. close(c->sd);
  251. }
  252. eloop_cancel_timeout(http_client_timeout, c, NULL);
  253. os_free(c);
  254. }
  255. struct wpabuf * http_client_get_body(struct http_client *c)
  256. {
  257. if (c->hread == NULL)
  258. return NULL;
  259. wpabuf_set(&c->body, httpread_data_get(c->hread),
  260. httpread_length_get(c->hread));
  261. return &c->body;
  262. }
  263. char * http_client_get_hdr_line(struct http_client *c, const char *tag)
  264. {
  265. if (c->hread == NULL)
  266. return NULL;
  267. return httpread_hdr_line_get(c->hread, tag);
  268. }
  269. char * http_link_update(char *url, const char *base)
  270. {
  271. char *n;
  272. size_t len;
  273. const char *pos;
  274. /* RFC 2396, Chapter 5.2 */
  275. /* TODO: consider adding all cases described in RFC 2396 */
  276. if (url == NULL)
  277. return NULL;
  278. if (os_strncmp(url, "http://", 7) == 0)
  279. return url; /* absolute link */
  280. if (os_strncmp(base, "http://", 7) != 0)
  281. return url; /* unable to handle base URL */
  282. len = os_strlen(url) + 1 + os_strlen(base) + 1;
  283. n = os_malloc(len);
  284. if (n == NULL)
  285. return url; /* failed */
  286. if (url[0] == '/') {
  287. pos = os_strchr(base + 7, '/');
  288. if (pos == NULL) {
  289. os_snprintf(n, len, "%s%s", base, url);
  290. } else {
  291. os_memcpy(n, base, pos - base);
  292. os_memcpy(n + (pos - base), url, os_strlen(url) + 1);
  293. }
  294. } else {
  295. pos = os_strrchr(base + 7, '/');
  296. if (pos == NULL) {
  297. os_snprintf(n, len, "%s/%s", base, url);
  298. } else {
  299. os_memcpy(n, base, pos - base + 1);
  300. os_memcpy(n + (pos - base) + 1, url, os_strlen(url) +
  301. 1);
  302. }
  303. }
  304. os_free(url);
  305. return n;
  306. }