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 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)
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;
203 const char *location;
207 if (!backend->update_proxy_url)
210 next = strstr(url_str, "://");
215 location = uclient_split_host(host, &host_len);
217 url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
224 if (backend->update_proxy_url)
225 backend->update_proxy_url(cl);
230 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
232 const struct uclient_backend *backend = cl->backend;
233 struct uclient_url *url = cl->url;
235 url = uclient_get_url(url_str, auth_str);
239 if (url->backend != cl->backend) {
245 cl->proxy_url = NULL;
250 if (backend->update_url)
251 backend->update_url(cl);
256 int uclient_set_timeout(struct uclient *cl, int msecs)
261 cl->timeout_msecs = msecs;
266 int uclient_connect(struct uclient *cl)
268 return cl->backend->connect(cl);
271 void uclient_free(struct uclient *cl)
273 struct uclient_url *url = cl->url;
275 if (cl->backend->free)
276 cl->backend->free(cl);
283 int uclient_write(struct uclient *cl, const char *buf, int len)
285 if (!cl->backend->write)
288 return cl->backend->write(cl, buf, len);
291 int uclient_request(struct uclient *cl)
295 if (!cl->backend->request)
298 err = cl->backend->request(cl);
302 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
307 int uclient_read(struct uclient *cl, char *buf, int len)
309 if (!cl->backend->read)
312 return cl->backend->read(cl, buf, len);
315 void uclient_disconnect(struct uclient *cl)
317 uloop_timeout_cancel(&cl->connection_timeout);
319 if (!cl->backend->disconnect)
322 cl->backend->disconnect(cl);
325 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
327 struct uclient *cl = container_of(timeout, struct uclient, timeout);
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);
335 static void uclient_backend_change_state(struct uclient *cl)
337 cl->timeout.cb = __uclient_backend_change_state;
338 uloop_timeout_set(&cl->timeout, 1);
341 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
346 uloop_timeout_cancel(&cl->connection_timeout);
347 cl->error_code = code;
348 uclient_backend_change_state(cl);
351 void __hidden uclient_backend_set_eof(struct uclient *cl)
353 if (cl->eof || cl->error_code)
356 uloop_timeout_cancel(&cl->connection_timeout);
358 uclient_backend_change_state(cl);
361 void __hidden uclient_backend_reset_state(struct uclient *cl)
363 cl->data_eof = false;
366 uloop_timeout_cancel(&cl->timeout);