properly terminate POST requests
[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_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
136 {
137 const struct uclient_backend *backend = cl->backend;
138 struct uclient_url *url = cl->url;
139
140 url = uclient_get_url(url_str, auth_str);
141 if (!url)
142 return -1;
143
144 if (url->backend != cl->backend)
145 return -1;
146
147 free(cl->url);
148 cl->url = url;
149
150 if (backend->update_url)
151 backend->update_url(cl);
152
153 return 0;
154 }
155
156 int uclient_connect(struct uclient *cl)
157 {
158 return cl->backend->connect(cl);
159 }
160
161 void uclient_free(struct uclient *cl)
162 {
163 struct uclient_url *url = cl->url;
164
165 if (cl->backend->free)
166 cl->backend->free(cl);
167 else
168 free(cl);
169
170 free(url);
171 }
172
173 int uclient_write(struct uclient *cl, char *buf, int len)
174 {
175 if (!cl->backend->write)
176 return -1;
177
178 return cl->backend->write(cl, buf, len);
179 }
180
181 int uclient_request(struct uclient *cl)
182 {
183 if (!cl->backend->request)
184 return -1;
185
186 return cl->backend->request(cl);
187 }
188
189 int uclient_read(struct uclient *cl, char *buf, int len)
190 {
191 if (!cl->backend->read)
192 return -1;
193
194 return cl->backend->read(cl, buf, len);
195 }
196
197 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
198 {
199 struct uclient *cl = container_of(timeout, struct uclient, timeout);
200
201 if (cl->error_code && cl->cb->error)
202 cl->cb->error(cl, cl->error_code);
203 else if (cl->eof && cl->cb->data_eof)
204 cl->cb->data_eof(cl);
205 }
206
207 static void uclient_backend_change_state(struct uclient *cl)
208 {
209 cl->timeout.cb = __uclient_backend_change_state;
210 uloop_timeout_set(&cl->timeout, 1);
211 }
212
213 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
214 {
215 if (cl->error_code)
216 return;
217
218 cl->error_code = code;
219 uclient_backend_change_state(cl);
220 }
221
222 void __hidden uclient_backend_set_eof(struct uclient *cl)
223 {
224 if (cl->eof || cl->error_code)
225 return;
226
227 cl->eof = true;
228 uclient_backend_change_state(cl);
229 }
230
231 void __hidden uclient_backend_reset_state(struct uclient *cl)
232 {
233 cl->eof = false;
234 cl->error_code = 0;
235 uloop_timeout_cancel(&cl->timeout);
236 }