82553a56847a4dae7b526c34a2396657e556e8cd
[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 *next = 0;
106 url->port = next + 1;
107 }
108 }
109
110 return url;
111
112 free:
113 free(url);
114 return NULL;
115 }
116
117 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
118 {
119 struct uclient *cl;
120 struct uclient_url *url;
121
122 url = uclient_get_url(url_str, auth_str);
123 if (!url)
124 return NULL;
125
126 cl = url->backend->alloc();
127 if (!cl)
128 return NULL;
129
130 cl->backend = url->backend;
131 cl->cb = cb;
132 cl->url = url;
133
134 return cl;
135 }
136
137 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
138 {
139 const struct uclient_backend *backend = cl->backend;
140 struct uclient_url *url = cl->url;
141
142 url = uclient_get_url(url_str, auth_str);
143 if (!url)
144 return -1;
145
146 if (url->backend != cl->backend)
147 return -1;
148
149 free(cl->url);
150 cl->url = url;
151
152 if (backend->update_url)
153 backend->update_url(cl);
154
155 return 0;
156 }
157
158 int uclient_connect(struct uclient *cl)
159 {
160 return cl->backend->connect(cl);
161 }
162
163 void uclient_free(struct uclient *cl)
164 {
165 struct uclient_url *url = cl->url;
166
167 if (cl->backend->free)
168 cl->backend->free(cl);
169 else
170 free(cl);
171
172 free(url);
173 }
174
175 int uclient_write(struct uclient *cl, char *buf, int len)
176 {
177 if (!cl->backend->write)
178 return -1;
179
180 return cl->backend->write(cl, buf, len);
181 }
182
183 int uclient_request(struct uclient *cl)
184 {
185 if (!cl->backend->request)
186 return -1;
187
188 return cl->backend->request(cl);
189 }
190
191 int uclient_read(struct uclient *cl, char *buf, int len)
192 {
193 if (!cl->backend->read)
194 return -1;
195
196 return cl->backend->read(cl, buf, len);
197 }
198
199 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
200 {
201 struct uclient *cl = container_of(timeout, struct uclient, timeout);
202
203 if (cl->error_code && cl->cb->error)
204 cl->cb->error(cl, cl->error_code);
205 else if (cl->eof && cl->cb->data_eof)
206 cl->cb->data_eof(cl);
207 }
208
209 static void uclient_backend_change_state(struct uclient *cl)
210 {
211 cl->timeout.cb = __uclient_backend_change_state;
212 uloop_timeout_set(&cl->timeout, 1);
213 }
214
215 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
216 {
217 if (cl->error_code)
218 return;
219
220 cl->error_code = code;
221 uclient_backend_change_state(cl);
222 }
223
224 void __hidden uclient_backend_set_eof(struct uclient *cl)
225 {
226 if (cl->eof || cl->error_code)
227 return;
228
229 cl->eof = true;
230 uclient_backend_change_state(cl);
231 }
232
233 void __hidden uclient_backend_reset_state(struct uclient *cl)
234 {
235 cl->eof = false;
236 cl->error_code = 0;
237 uloop_timeout_cancel(&cl->timeout);
238 }