http: add proxy support
[project/uclient.git] / uclient.c
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <arpa/inet.h>
19 #include <libubox/ustream-ssl.h>
20 #include "uclient.h"
21 #include "uclient-utils.h"
22 #include "uclient-backend.h"
23
24 char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
25 {
26         int portval;
27         void *ptr;
28
29         switch(a->sa.sa_family) {
30         case AF_INET:
31                 ptr = &a->sin.sin_addr;
32                 portval = a->sin.sin_port;
33                 break;
34         case AF_INET6:
35                 ptr = &a->sin6.sin6_addr;
36                 portval = a->sin6.sin6_port;
37                 break;
38         default:
39                 return strcpy(dest, "Unknown");
40         }
41
42         inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
43         if (port)
44                 *port = ntohs(portval);
45
46         return dest;
47 }
48
49 struct uclient_url __hidden *
50 __uclient_get_url(const struct uclient_backend *backend,
51                   const char *host, int host_len,
52                   const char *location, const char *auth_str)
53 {
54         struct uclient_url *url;
55         char *host_buf, *uri_buf, *auth_buf, *next;
56
57         url = calloc_a(sizeof(*url),
58                 &host_buf, host_len + 1,
59                 &uri_buf, strlen(location) + 1,
60                 &auth_buf, auth_str ? strlen(auth_str) + 1 : 0);
61
62         url->backend = backend;
63         url->location = strcpy(uri_buf, location);
64         if (host)
65                 url->host = strncpy(host_buf, host, host_len);
66
67         next = strchr(host_buf, '@');
68         if (next) {
69                 *next = 0;
70                 url->host = next + 1;
71
72                 if (uclient_urldecode(host_buf, host_buf, false) < 0)
73                         goto free;
74
75                 url->auth = host_buf;
76         }
77
78         if (!url->auth && auth_str)
79                 url->auth = strcpy(auth_buf, auth_str);
80
81         /* Literal IPv6 address */
82         if (*url->host == '[') {
83                 url->host++;
84                 next = strrchr(url->host, ']');
85                 if (!next)
86                         goto free;
87
88                 *(next++) = 0;
89                 if (*next == ':')
90                         url->port = next + 1;
91         } else {
92                 next = strrchr(url->host, ':');
93                 if (next) {
94                         *next = 0;
95                         url->port = next + 1;
96                 }
97         }
98
99         return url;
100
101 free:
102         free(url);
103         return NULL;
104 }
105
106 static const char *
107 uclient_split_host(const char *base, int *host_len)
108 {
109         char *next, *location;
110
111         next = strchr(base, '/');
112         if (next) {
113                 location = next;
114                 *host_len = next - base;
115         } else {
116                 location = "/";
117                 *host_len = strlen(base);
118         }
119
120         return location;
121 }
122
123 struct uclient_url __hidden *
124 uclient_get_url(const char *url_str, const char *auth_str)
125 {
126         static const struct uclient_backend *backends[] = {
127                 &uclient_backend_http,
128         };
129
130         const struct uclient_backend *backend;
131         const char * const *prefix = NULL;
132         struct uclient_url *url;
133         const char *location;
134         int host_len;
135         int i;
136
137         for (i = 0; i < ARRAY_SIZE(backends); i++) {
138                 int prefix_len = 0;
139
140                 for (prefix = backends[i]->prefix; *prefix; prefix++) {
141                         prefix_len = strlen(*prefix);
142
143                         if (!strncmp(url_str, *prefix, prefix_len))
144                                 break;
145                 }
146
147                 if (!*prefix)
148                         continue;
149
150                 url_str += prefix_len;
151                 backend = backends[i];
152                 break;
153         }
154
155         if (!*prefix)
156                 return NULL;
157
158         location = uclient_split_host(url_str, &host_len);
159         url = __uclient_get_url(backend, url_str, host_len, location, auth_str);
160         if (!url)
161                 return NULL;
162
163         url->prefix = prefix - backend->prefix;
164         return url;
165 }
166
167 static void uclient_connection_timeout(struct uloop_timeout *timeout)
168 {
169         struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
170
171         if (cl->backend->disconnect)
172                 cl->backend->disconnect(cl);
173
174         uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
175 }
176
177 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
178 {
179         struct uclient *cl;
180         struct uclient_url *url;
181
182         url = uclient_get_url(url_str, auth_str);
183         if (!url)
184                 return NULL;
185
186         cl = url->backend->alloc();
187         if (!cl)
188                 return NULL;
189
190         cl->backend = url->backend;
191         cl->cb = cb;
192         cl->url = url;
193         cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
194         cl->connection_timeout.cb = uclient_connection_timeout;
195
196         return cl;
197 }
198
199 int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str)
200 {
201         const struct uclient_backend *backend = cl->backend;
202         struct uclient_url *url;
203         const char *location;
204         int host_len;
205         char *next, *host;
206
207         if (!backend->update_proxy_url)
208                 return -1;
209
210         next = strstr(url_str, "://");
211         if (!next)
212                 return -1;
213
214         host = next + 3;
215         location = uclient_split_host(host, &host_len);
216
217         url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
218         if (!url)
219                 return -1;
220
221         free(cl->proxy_url);
222         cl->proxy_url = url;
223
224         if (backend->update_proxy_url)
225                 backend->update_proxy_url(cl);
226
227         return 0;
228 }
229
230 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
231 {
232         const struct uclient_backend *backend = cl->backend;
233         struct uclient_url *url = cl->url;
234
235         url = uclient_get_url(url_str, auth_str);
236         if (!url)
237                 return -1;
238
239         if (url->backend != cl->backend) {
240                 free(url);
241                 return -1;
242         }
243
244         free(cl->proxy_url);
245         cl->proxy_url = NULL;
246
247         free(cl->url);
248         cl->url = url;
249
250         if (backend->update_url)
251                 backend->update_url(cl);
252
253         return 0;
254 }
255
256 int uclient_set_timeout(struct uclient *cl, int msecs)
257 {
258         if (msecs <= 0)
259                 return -EINVAL;
260
261         cl->timeout_msecs = msecs;
262
263         return 0;
264 }
265
266 int uclient_connect(struct uclient *cl)
267 {
268         return cl->backend->connect(cl);
269 }
270
271 void uclient_free(struct uclient *cl)
272 {
273         struct uclient_url *url = cl->url;
274
275         if (cl->backend->free)
276                 cl->backend->free(cl);
277         else
278                 free(cl);
279
280         free(url);
281 }
282
283 int uclient_write(struct uclient *cl, const char *buf, int len)
284 {
285         if (!cl->backend->write)
286                 return -1;
287
288         return cl->backend->write(cl, buf, len);
289 }
290
291 int uclient_request(struct uclient *cl)
292 {
293         int err;
294
295         if (!cl->backend->request)
296                 return -1;
297
298         err = cl->backend->request(cl);
299         if (err)
300                 return err;
301
302         uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
303
304         return 0;
305 }
306
307 int uclient_read(struct uclient *cl, char *buf, int len)
308 {
309         if (!cl->backend->read)
310                 return -1;
311
312         return cl->backend->read(cl, buf, len);
313 }
314
315 void uclient_disconnect(struct uclient *cl)
316 {
317         uloop_timeout_cancel(&cl->connection_timeout);
318
319         if (!cl->backend->disconnect)
320                 return;
321
322         cl->backend->disconnect(cl);
323 }
324
325 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
326 {
327         struct uclient *cl = container_of(timeout, struct uclient, timeout);
328
329         if (cl->error_code && cl->cb->error)
330                 cl->cb->error(cl, cl->error_code);
331         else if (cl->eof && cl->cb->data_eof)
332                 cl->cb->data_eof(cl);
333 }
334
335 static void uclient_backend_change_state(struct uclient *cl)
336 {
337         cl->timeout.cb = __uclient_backend_change_state;
338         uloop_timeout_set(&cl->timeout, 1);
339 }
340
341 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
342 {
343         if (cl->error_code)
344                 return;
345
346         uloop_timeout_cancel(&cl->connection_timeout);
347         cl->error_code = code;
348         uclient_backend_change_state(cl);
349 }
350
351 void __hidden uclient_backend_set_eof(struct uclient *cl)
352 {
353         if (cl->eof || cl->error_code)
354                 return;
355
356         uloop_timeout_cancel(&cl->connection_timeout);
357         cl->eof = true;
358         uclient_backend_change_state(cl);
359 }
360
361 void __hidden uclient_backend_reset_state(struct uclient *cl)
362 {
363         cl->data_eof = false;
364         cl->eof = false;
365         cl->error_code = 0;
366         uloop_timeout_cancel(&cl->timeout);
367 }