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_location(struct uclient_url *url, const char *location)
126 struct uclient_url *new_url;
127 char *host_buf, *uri_buf, *auth_buf, *port_buf;
128 int host_len = strlen(url->host) + 1;
129 int auth_len = url->auth ? strlen(url->auth) + 1 : 0;
130 int port_len = url->port ? strlen(url->port) + 1 : 0;
133 if (strstr(location, "://"))
134 return uclient_get_url(location, url->auth);
136 if (location[0] == '/')
137 uri_len = strlen(location) + 1;
139 uri_len = strlen(url->location) + strlen(location) + 2;
141 new_url = calloc_a(sizeof(*url),
145 &auth_buf, auth_len);
150 new_url->backend = url->backend;
151 new_url->prefix = url->prefix;
152 new_url->host = strcpy(host_buf, url->host);
154 new_url->port = strcpy(port_buf, url->port);
156 new_url->auth = strcpy(auth_buf, url->auth);
158 new_url->location = uri_buf;
159 if (location[0] == '/')
160 strcpy(uri_buf, location);
162 int len = strcspn(url->location, "?#");
165 memcpy(buf, url->location, len);
166 if (buf[len - 1] != '/') {
172 strcpy(buf, location);
178 struct uclient_url __hidden *
179 uclient_get_url(const char *url_str, const char *auth_str)
181 static const struct uclient_backend *backends[] = {
182 &uclient_backend_http,
185 const struct uclient_backend *backend;
186 const char * const *prefix = NULL;
187 struct uclient_url *url;
188 const char *location;
192 for (i = 0; i < ARRAY_SIZE(backends); i++) {
195 for (prefix = backends[i]->prefix; *prefix; prefix++) {
196 prefix_len = strlen(*prefix);
198 if (!strncmp(url_str, *prefix, prefix_len))
205 url_str += prefix_len;
206 backend = backends[i];
213 location = uclient_split_host(url_str, &host_len);
214 url = __uclient_get_url(backend, url_str, host_len, location, auth_str);
218 url->prefix = prefix - backend->prefix;
222 static void uclient_connection_timeout(struct uloop_timeout *timeout)
224 struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
226 if (cl->backend->disconnect)
227 cl->backend->disconnect(cl);
229 uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
232 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
235 struct uclient_url *url;
237 url = uclient_get_url(url_str, auth_str);
241 cl = url->backend->alloc();
245 cl->backend = url->backend;
248 cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
249 cl->connection_timeout.cb = uclient_connection_timeout;
254 int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str)
256 const struct uclient_backend *backend = cl->backend;
257 struct uclient_url *url;
261 if (!backend->update_proxy_url)
264 next = strstr(url_str, "://");
269 uclient_split_host(host, &host_len);
271 url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
278 if (backend->update_proxy_url)
279 backend->update_proxy_url(cl);
284 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
286 const struct uclient_backend *backend = cl->backend;
287 struct uclient_url *url = cl->url;
289 url = uclient_get_url(url_str, auth_str);
293 if (url->backend != cl->backend) {
299 cl->proxy_url = NULL;
304 if (backend->update_url)
305 backend->update_url(cl);
310 int uclient_set_timeout(struct uclient *cl, int msecs)
315 cl->timeout_msecs = msecs;
320 int uclient_connect(struct uclient *cl)
322 return cl->backend->connect(cl);
325 void uclient_free(struct uclient *cl)
327 struct uclient_url *url = cl->url;
329 if (cl->backend->free)
330 cl->backend->free(cl);
337 int uclient_write(struct uclient *cl, const char *buf, int len)
339 if (!cl->backend->write)
342 return cl->backend->write(cl, buf, len);
345 int uclient_request(struct uclient *cl)
349 if (!cl->backend->request)
352 err = cl->backend->request(cl);
356 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
361 int uclient_read(struct uclient *cl, char *buf, int len)
363 if (!cl->backend->read)
366 return cl->backend->read(cl, buf, len);
369 void uclient_disconnect(struct uclient *cl)
371 uloop_timeout_cancel(&cl->connection_timeout);
373 if (!cl->backend->disconnect)
376 cl->backend->disconnect(cl);
379 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
381 struct uclient *cl = container_of(timeout, struct uclient, timeout);
383 if (cl->error_code && cl->cb->error)
384 cl->cb->error(cl, cl->error_code);
385 else if (cl->eof && cl->cb->data_eof)
386 cl->cb->data_eof(cl);
389 static void uclient_backend_change_state(struct uclient *cl)
391 cl->timeout.cb = __uclient_backend_change_state;
392 uloop_timeout_set(&cl->timeout, 1);
395 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
400 uloop_timeout_cancel(&cl->connection_timeout);
401 cl->error_code = code;
402 uclient_backend_change_state(cl);
405 void __hidden uclient_backend_set_eof(struct uclient *cl)
407 if (cl->eof || cl->error_code)
410 uloop_timeout_cancel(&cl->connection_timeout);
412 uclient_backend_change_state(cl);
415 void __hidden uclient_backend_reset_state(struct uclient *cl)
417 cl->data_eof = false;
420 uloop_timeout_cancel(&cl->timeout);