2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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.
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.
18 #include <arpa/inet.h>
19 #include <libubox/ustream-ssl.h>
21 #include "uclient-utils.h"
22 #include "uclient-backend.h"
24 char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
29 switch(a->sa.sa_family) {
31 ptr = &a->sin.sin_addr;
32 portval = a->sin.sin_port;
35 ptr = &a->sin6.sin6_addr;
36 portval = a->sin6.sin6_port;
39 return strcpy(dest, "Unknown");
42 inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
44 *port = ntohs(portval);
49 static struct uclient_url *
50 __uclient_get_url(const struct uclient_backend *backend,
51 const char *host, int host_len,
52 const char *location, const char *auth_str)
54 struct uclient_url *url;
55 char *host_buf, *uri_buf, *auth_buf, *next;
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);
62 url->backend = backend;
63 url->location = strcpy(uri_buf, location);
65 url->host = strncpy(host_buf, host, host_len);
67 next = strchr(host_buf, '@');
72 if (uclient_urldecode(host_buf, host_buf, false) < 0)
78 if (!url->auth && auth_str)
79 url->auth = strcpy(auth_buf, auth_str);
81 /* Literal IPv6 address */
82 if (*url->host == '[') {
84 next = strrchr(url->host, ']');
92 next = strrchr(url->host, ':');
107 uclient_split_host(const char *base, int *host_len)
109 char *next, *location;
111 next = strchr(base, '/');
114 *host_len = next - base;
117 *host_len = strlen(base);
123 struct uclient_url __hidden *
124 uclient_get_url(const char *url_str, const char *auth_str)
126 static const struct uclient_backend *backends[] = {
127 &uclient_backend_http,
130 const struct uclient_backend *backend;
131 const char * const *prefix = NULL;
132 struct uclient_url *url;
133 const char *location;
137 for (i = 0; i < ARRAY_SIZE(backends); i++) {
140 for (prefix = backends[i]->prefix; *prefix; prefix++) {
141 prefix_len = strlen(*prefix);
143 if (!strncmp(url_str, *prefix, prefix_len))
150 url_str += prefix_len;
151 backend = backends[i];
158 location = uclient_split_host(url_str, &host_len);
159 url = __uclient_get_url(backend, url_str, host_len, location, auth_str);
163 url->prefix = prefix - backend->prefix;
167 static void uclient_connection_timeout(struct uloop_timeout *timeout)
169 struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
171 if (cl->backend->disconnect)
172 cl->backend->disconnect(cl);
174 uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
177 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
180 struct uclient_url *url;
182 url = uclient_get_url(url_str, auth_str);
186 cl = url->backend->alloc();
190 cl->backend = url->backend;
193 cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
194 cl->connection_timeout.cb = uclient_connection_timeout;
199 int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str)
201 const struct uclient_backend *backend = cl->backend;
202 struct uclient_url *url;
206 if (!backend->update_proxy_url)
209 next = strstr(url_str, "://");
214 uclient_split_host(host, &host_len);
216 url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
223 if (backend->update_proxy_url)
224 backend->update_proxy_url(cl);
229 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
231 const struct uclient_backend *backend = cl->backend;
232 struct uclient_url *url = cl->url;
234 url = uclient_get_url(url_str, auth_str);
238 if (url->backend != cl->backend) {
244 cl->proxy_url = NULL;
249 if (backend->update_url)
250 backend->update_url(cl);
255 int uclient_set_timeout(struct uclient *cl, int msecs)
260 cl->timeout_msecs = msecs;
265 int uclient_connect(struct uclient *cl)
267 return cl->backend->connect(cl);
270 void uclient_free(struct uclient *cl)
272 struct uclient_url *url = cl->url;
274 if (cl->backend->free)
275 cl->backend->free(cl);
282 int uclient_write(struct uclient *cl, const char *buf, int len)
284 if (!cl->backend->write)
287 return cl->backend->write(cl, buf, len);
290 int uclient_request(struct uclient *cl)
294 if (!cl->backend->request)
297 err = cl->backend->request(cl);
301 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
306 int uclient_read(struct uclient *cl, char *buf, int len)
308 if (!cl->backend->read)
311 return cl->backend->read(cl, buf, len);
314 void uclient_disconnect(struct uclient *cl)
316 uloop_timeout_cancel(&cl->connection_timeout);
318 if (!cl->backend->disconnect)
321 cl->backend->disconnect(cl);
324 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
326 struct uclient *cl = container_of(timeout, struct uclient, timeout);
328 if (cl->error_code && cl->cb->error)
329 cl->cb->error(cl, cl->error_code);
330 else if (cl->eof && cl->cb->data_eof)
331 cl->cb->data_eof(cl);
334 static void uclient_backend_change_state(struct uclient *cl)
336 cl->timeout.cb = __uclient_backend_change_state;
337 uloop_timeout_set(&cl->timeout, 1);
340 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
345 uloop_timeout_cancel(&cl->connection_timeout);
346 cl->error_code = code;
347 uclient_backend_change_state(cl);
350 void __hidden uclient_backend_set_eof(struct uclient *cl)
352 if (cl->eof || cl->error_code)
355 uloop_timeout_cancel(&cl->connection_timeout);
357 uclient_backend_change_state(cl);
360 void __hidden uclient_backend_reset_state(struct uclient *cl)
362 cl->data_eof = false;
365 uloop_timeout_cancel(&cl->timeout);