uclient-fetch: truncate output files (unless resuming)
[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 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)
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 int host_len;
204 char *next, *host;
205
206 if (!backend->update_proxy_url)
207 return -1;
208
209 next = strstr(url_str, "://");
210 if (!next)
211 return -1;
212
213 host = next + 3;
214 uclient_split_host(host, &host_len);
215
216 url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
217 if (!url)
218 return -1;
219
220 free(cl->proxy_url);
221 cl->proxy_url = url;
222
223 if (backend->update_proxy_url)
224 backend->update_proxy_url(cl);
225
226 return 0;
227 }
228
229 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
230 {
231 const struct uclient_backend *backend = cl->backend;
232 struct uclient_url *url = cl->url;
233
234 url = uclient_get_url(url_str, auth_str);
235 if (!url)
236 return -1;
237
238 if (url->backend != cl->backend) {
239 free(url);
240 return -1;
241 }
242
243 free(cl->proxy_url);
244 cl->proxy_url = NULL;
245
246 free(cl->url);
247 cl->url = url;
248
249 if (backend->update_url)
250 backend->update_url(cl);
251
252 return 0;
253 }
254
255 int uclient_set_timeout(struct uclient *cl, int msecs)
256 {
257 if (msecs <= 0)
258 return -EINVAL;
259
260 cl->timeout_msecs = msecs;
261
262 return 0;
263 }
264
265 int uclient_connect(struct uclient *cl)
266 {
267 return cl->backend->connect(cl);
268 }
269
270 void uclient_free(struct uclient *cl)
271 {
272 struct uclient_url *url = cl->url;
273
274 if (cl->backend->free)
275 cl->backend->free(cl);
276 else
277 free(cl);
278
279 free(url);
280 }
281
282 int uclient_write(struct uclient *cl, const char *buf, int len)
283 {
284 if (!cl->backend->write)
285 return -1;
286
287 return cl->backend->write(cl, buf, len);
288 }
289
290 int uclient_request(struct uclient *cl)
291 {
292 int err;
293
294 if (!cl->backend->request)
295 return -1;
296
297 err = cl->backend->request(cl);
298 if (err)
299 return err;
300
301 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
302
303 return 0;
304 }
305
306 int uclient_read(struct uclient *cl, char *buf, int len)
307 {
308 if (!cl->backend->read)
309 return -1;
310
311 return cl->backend->read(cl, buf, len);
312 }
313
314 void uclient_disconnect(struct uclient *cl)
315 {
316 uloop_timeout_cancel(&cl->connection_timeout);
317
318 if (!cl->backend->disconnect)
319 return;
320
321 cl->backend->disconnect(cl);
322 }
323
324 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
325 {
326 struct uclient *cl = container_of(timeout, struct uclient, timeout);
327
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);
332 }
333
334 static void uclient_backend_change_state(struct uclient *cl)
335 {
336 cl->timeout.cb = __uclient_backend_change_state;
337 uloop_timeout_set(&cl->timeout, 1);
338 }
339
340 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
341 {
342 if (cl->error_code)
343 return;
344
345 uloop_timeout_cancel(&cl->connection_timeout);
346 cl->error_code = code;
347 uclient_backend_change_state(cl);
348 }
349
350 void __hidden uclient_backend_set_eof(struct uclient *cl)
351 {
352 if (cl->eof || cl->error_code)
353 return;
354
355 uloop_timeout_cancel(&cl->connection_timeout);
356 cl->eof = true;
357 uclient_backend_change_state(cl);
358 }
359
360 void __hidden uclient_backend_reset_state(struct uclient *cl)
361 {
362 cl->data_eof = false;
363 cl->eof = false;
364 cl->error_code = 0;
365 uloop_timeout_cancel(&cl->timeout);
366 }