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