2 * http_client - HTTP client
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
15 #include "http_client.h"
18 #define HTTP_CLIENT_TIMEOUT_SEC 30
22 struct sockaddr_in dst;
28 void (*cb)(void *ctx, struct http_client *c,
29 enum http_client_event event);
31 struct httpread *hread;
36 static void http_client_timeout(void *eloop_data, void *user_ctx)
38 struct http_client *c = eloop_data;
39 wpa_printf(MSG_DEBUG, "HTTP: Timeout (c=%p)", c);
40 c->cb(c->cb_ctx, c, HTTP_CLIENT_TIMEOUT);
44 static void http_client_got_response(struct httpread *handle, void *cookie,
45 enum httpread_event e)
47 struct http_client *c = cookie;
49 wpa_printf(MSG_DEBUG, "HTTP: httpread callback: handle=%p cookie=%p "
50 "e=%d", handle, cookie, e);
52 eloop_cancel_timeout(http_client_timeout, c, NULL);
54 case HTTPREAD_EVENT_FILE_READY:
55 if (httpread_hdr_type_get(c->hread) == HTTPREAD_HDR_TYPE_REPLY)
57 int reply_code = httpread_reply_code_get(c->hread);
58 if (reply_code == 200 /* OK */) {
59 wpa_printf(MSG_DEBUG, "HTTP: Response OK from "
61 inet_ntoa(c->dst.sin_addr),
62 ntohs(c->dst.sin_port));
63 c->cb(c->cb_ctx, c, HTTP_CLIENT_OK);
65 wpa_printf(MSG_DEBUG, "HTTP: Error %d from "
67 inet_ntoa(c->dst.sin_addr),
68 ntohs(c->dst.sin_port));
69 c->cb(c->cb_ctx, c, HTTP_CLIENT_INVALID_REPLY);
72 c->cb(c->cb_ctx, c, HTTP_CLIENT_INVALID_REPLY);
74 case HTTPREAD_EVENT_TIMEOUT:
75 c->cb(c->cb_ctx, c, HTTP_CLIENT_TIMEOUT);
77 case HTTPREAD_EVENT_ERROR:
78 c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
84 static void http_client_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
86 struct http_client *c = eloop_ctx;
89 wpa_printf(MSG_DEBUG, "HTTP: Send client request to %s:%d (%lu of %lu "
91 inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port),
92 (unsigned long) wpabuf_len(c->req),
93 (unsigned long) wpabuf_len(c->req) - c->req_pos);
95 res = send(c->sd, wpabuf_head(c->req) + c->req_pos,
96 wpabuf_len(c->req) - c->req_pos, 0);
98 wpa_printf(MSG_DEBUG, "HTTP: Failed to send buffer: %s",
100 eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
101 c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
105 if ((size_t) res < wpabuf_len(c->req) - c->req_pos) {
106 wpa_printf(MSG_DEBUG, "HTTP: Sent %d of %lu bytes; %lu bytes "
108 res, (unsigned long) wpabuf_len(c->req),
109 (unsigned long) wpabuf_len(c->req) - c->req_pos -
115 wpa_printf(MSG_DEBUG, "HTTP: Full client request sent to %s:%d",
116 inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port));
117 eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
121 c->hread = httpread_create(c->sd, http_client_got_response, c,
122 c->max_response, HTTP_CLIENT_TIMEOUT_SEC);
123 if (c->hread == NULL) {
124 c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
130 struct http_client * http_client_addr(struct sockaddr_in *dst,
131 struct wpabuf *req, size_t max_response,
132 void (*cb)(void *ctx,
133 struct http_client *c,
134 enum http_client_event event),
137 struct http_client *c;
139 c = os_zalloc(sizeof(*c));
144 c->max_response = max_response;
148 c->sd = socket(AF_INET, SOCK_STREAM, 0);
154 if (fcntl(c->sd, F_SETFL, O_NONBLOCK) != 0) {
155 wpa_printf(MSG_DEBUG, "HTTP: fnctl(O_NONBLOCK) failed: %s",
161 if (connect(c->sd, (struct sockaddr *) dst, sizeof(*dst))) {
162 if (errno != EINPROGRESS) {
163 wpa_printf(MSG_DEBUG, "HTTP: Failed to connect: %s",
170 * Continue connecting in the background; eloop will call us
171 * once the connection is ready (or failed).
175 if (eloop_register_sock(c->sd, EVENT_TYPE_WRITE, http_client_tx_ready,
181 if (eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
182 http_client_timeout, c, NULL)) {
193 char * http_client_url_parse(const char *url, struct sockaddr_in *dst,
196 char *u, *addr, *port, *path;
202 os_memset(dst, 0, sizeof(*dst));
203 dst->sin_family = AF_INET;
205 path = os_strchr(addr, '/');
206 port = os_strchr(addr, ':');
210 *path = '\0'; /* temporary nul termination for address */
217 if (inet_aton(addr, &dst->sin_addr) == 0) {
218 /* TODO: name lookup */
219 wpa_printf(MSG_DEBUG, "HTTP: Unsupported address in URL '%s' "
220 "(addr='%s' port='%s')",
227 dst->sin_port = htons(atoi(port));
229 dst->sin_port = htons(80);
232 /* remove temporary nul termination for address */
242 struct http_client * http_client_url(const char *url,
243 struct wpabuf *req, size_t max_response,
244 void (*cb)(void *ctx,
245 struct http_client *c,
246 enum http_client_event event),
249 struct sockaddr_in dst;
250 struct http_client *c;
252 struct wpabuf *req_buf = NULL;
254 if (os_strncmp(url, "http://", 7) != 0)
256 u = http_client_url_parse(url, &dst, &path);
261 req_buf = wpabuf_alloc(os_strlen(url) + 1000);
262 if (req_buf == NULL) {
268 "GET %s HTTP/1.1\r\n"
269 "Cache-Control: no-cache\r\n"
270 "Pragma: no-cache\r\n"
271 "Accept: text/xml, application/xml\r\n"
272 "User-Agent: wpa_supplicant\r\n"
275 path, inet_ntoa(dst.sin_addr),
276 ntohs(dst.sin_port));
280 c = http_client_addr(&dst, req, max_response, cb, cb_ctx);
282 wpabuf_free(req_buf);
290 void http_client_free(struct http_client *c)
294 httpread_destroy(c->hread);
297 eloop_unregister_sock(c->sd, EVENT_TYPE_WRITE);
300 eloop_cancel_timeout(http_client_timeout, c, NULL);
305 struct wpabuf * http_client_get_body(struct http_client *c)
307 if (c->hread == NULL)
309 wpabuf_set(&c->body, httpread_data_get(c->hread),
310 httpread_length_get(c->hread));
315 char * http_client_get_hdr_line(struct http_client *c, const char *tag)
317 if (c->hread == NULL)
319 return httpread_hdr_line_get(c->hread, tag);
323 char * http_link_update(char *url, const char *base)
329 /* RFC 2396, Chapter 5.2 */
330 /* TODO: consider adding all cases described in RFC 2396 */
335 if (os_strncmp(url, "http://", 7) == 0)
336 return url; /* absolute link */
338 if (os_strncmp(base, "http://", 7) != 0)
339 return url; /* unable to handle base URL */
341 len = os_strlen(url) + 1 + os_strlen(base) + 1;
344 return url; /* failed */
347 pos = os_strchr(base + 7, '/');
349 os_snprintf(n, len, "%s%s", base, url);
351 os_memcpy(n, base, pos - base);
352 os_memcpy(n + (pos - base), url, os_strlen(url) + 1);
355 pos = os_strrchr(base + 7, '/');
357 os_snprintf(n, len, "%s/%s", base, url);
359 os_memcpy(n, base, pos - base + 1);
360 os_memcpy(n + (pos - base) + 1, url, os_strlen(url) +