pass auth_str to uclient_new()
[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 <libubox/ustream-ssl.h>
19 #include "uclient.h"
20 #include "uclient-utils.h"
21 #include "uclient-backend.h"
22
23 struct uclient_url __hidden *
24 uclient_get_url(const char *url_str, const char *auth_str)
25 {
26         static const struct uclient_backend *backends[] = {
27                 &uclient_backend_http,
28         };
29
30         const struct uclient_backend *backend;
31         const char * const *prefix = NULL;
32         struct uclient_url *url;
33         const char *location;
34         char *host_buf, *uri_buf, *auth_buf, *next;
35         int i, host_len;
36
37         for (i = 0; i < ARRAY_SIZE(backends); i++) {
38                 int prefix_len = 0;
39
40                 for (prefix = backends[i]->prefix; *prefix; prefix++) {
41                         prefix_len = strlen(*prefix);
42
43                         if (!strncmp(url_str, *prefix, prefix_len))
44                                 break;
45                 }
46
47                 if (!*prefix)
48                         continue;
49
50                 url_str += prefix_len;
51                 backend = backends[i];
52                 break;
53         }
54
55         if (!*prefix)
56                 return NULL;
57
58         next = strchr(url_str, '/');
59         if (next) {
60                 location = next;
61                 host_len = next - url_str;
62         } else {
63                 location = "/";
64                 host_len = strlen(url_str);
65         }
66
67         url = calloc_a(sizeof(*url),
68                 &host_buf, host_len + 1,
69                 &uri_buf, strlen(location) + 1,
70                 &auth_buf, auth_str ? strlen(auth_str) + 1 : 0);
71
72         url->backend = backend;
73         url->location = strcpy(uri_buf, location);
74         url->prefix = prefix - backend->prefix;
75
76         url->host = strncpy(host_buf, url_str, host_len);
77
78         next = strchr(host_buf, '@');
79         if (next) {
80                 *next = 0;
81                 url->host = next + 1;
82
83                 if (uclient_urldecode(host_buf, host_buf, false) < 0)
84                         goto free;
85
86                 url->auth = host_buf;
87         }
88
89         if (!url->auth && auth_str)
90                 url->auth = strcpy(auth_buf, auth_str);
91
92         /* Literal IPv6 address */
93         if (*url->host == '[') {
94                 url->host++;
95                 next = strrchr(url->host, ']');
96                 if (!next)
97                         goto free;
98
99                 *(next++) = 0;
100                 if (*next == ':')
101                         url->port = next + 1;
102         } else {
103                 next = strrchr(url->host, ':');
104                 if (next)
105                         url->port = next + 1;
106         }
107
108         return url;
109
110 free:
111         free(url);
112         return NULL;
113 }
114
115 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
116 {
117         struct uclient *cl;
118         struct uclient_url *url;
119
120         url = uclient_get_url(url_str, auth_str);
121         if (!url)
122                 return NULL;
123
124         cl = url->backend->alloc();
125         if (!cl)
126                 return NULL;
127
128         cl->backend = url->backend;
129         cl->cb = cb;
130         cl->url = url;
131
132         return cl;
133 }
134
135 int uclient_connect_url(struct uclient *cl, const char *url_str)
136 {
137         struct uclient_url *url = cl->url;
138         const struct uclient_backend *backend = cl->backend;
139
140         if (url_str) {
141                 url = uclient_get_url(url_str, NULL);
142                 if (!url)
143                         return -1;
144
145                 if (url->backend != cl->backend)
146                         return -1;
147
148                 free(cl->url);
149                 cl->url = url;
150
151                 if (backend->update_url)
152                         backend->update_url(cl);
153         }
154
155         return backend->connect(cl);
156 }
157
158 void uclient_free(struct uclient *cl)
159 {
160         struct uclient_url *url = cl->url;
161
162         if (cl->backend->free)
163                 cl->backend->free(cl);
164         else
165                 free(cl);
166
167         free(url);
168 }
169
170 int uclient_write(struct uclient *cl, char *buf, int len)
171 {
172         if (!cl->backend->write)
173                 return -1;
174
175         return cl->backend->write(cl, buf, len);
176 }
177
178 int uclient_request(struct uclient *cl)
179 {
180         if (!cl->backend->request)
181                 return -1;
182
183         return cl->backend->request(cl);
184 }
185
186 int uclient_read(struct uclient *cl, char *buf, int len)
187 {
188         if (!cl->backend->read)
189                 return -1;
190
191         return cl->backend->read(cl, buf, len);
192 }
193
194 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
195 {
196         struct uclient *cl = container_of(timeout, struct uclient, timeout);
197
198         if (cl->error_code && cl->cb->error)
199                 cl->cb->error(cl, cl->error_code);
200         else if (cl->eof && cl->cb->data_eof)
201                 cl->cb->data_eof(cl);
202 }
203
204 static void uclient_backend_change_state(struct uclient *cl)
205 {
206         cl->timeout.cb = __uclient_backend_change_state;
207         uloop_timeout_set(&cl->timeout, 1);
208 }
209
210 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
211 {
212         if (cl->error_code)
213                 return;
214
215         cl->error_code = code;
216         uclient_backend_change_state(cl);
217 }
218
219 void __hidden uclient_backend_set_eof(struct uclient *cl)
220 {
221         if (cl->eof || cl->error_code)
222                 return;
223
224         cl->eof = true;
225         uclient_backend_change_state(cl);
226 }
227
228 void __hidden uclient_backend_reset_state(struct uclient *cl)
229 {
230         cl->eof = false;
231         cl->error_code = 0;
232         uloop_timeout_cancel(&cl->timeout);
233 }