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