add uh_split_header()
[project/uhttpd.git] / client.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include <libubox/blobmsg.h>
21 #include <ctype.h>
22
23 #include "uhttpd.h"
24
25 static LIST_HEAD(clients);
26
27 int n_clients = 0;
28 struct config conf = {};
29
30 const char * const http_versions[] = {
31 [UH_HTTP_VER_0_9] = "HTTP/0.9",
32 [UH_HTTP_VER_1_0] = "HTTP/1.0",
33 [UH_HTTP_VER_1_1] = "HTTP/1.1",
34 };
35
36 const char * const http_methods[] = {
37 [UH_HTTP_MSG_GET] = "GET",
38 [UH_HTTP_MSG_POST] = "POST",
39 [UH_HTTP_MSG_HEAD] = "HEAD",
40 };
41
42 void uh_http_header(struct client *cl, int code, const char *summary)
43 {
44 const char *enc = "Transfer-Encoding: chunked\r\n";
45 const char *conn;
46
47 if (!uh_use_chunked(cl))
48 enc = "";
49
50 if (cl->request.version != UH_HTTP_VER_1_1)
51 conn = "Connection: close";
52 else
53 conn = "Connection: keep-alive";
54
55 ustream_printf(cl->us, "%s %03i %s\r\n%s\r\n%s",
56 http_versions[cl->request.version],
57 code, summary, conn, enc);
58 }
59
60 static void uh_connection_close(struct client *cl)
61 {
62 cl->state = CLIENT_STATE_DONE;
63 cl->us->eof = true;
64 ustream_state_change(cl->us);
65 }
66
67 static void uh_dispatch_done(struct client *cl)
68 {
69 if (cl->dispatch.free)
70 cl->dispatch.free(cl);
71 }
72
73 void uh_request_done(struct client *cl)
74 {
75 uh_chunk_eof(cl);
76 uh_dispatch_done(cl);
77 cl->us->notify_write = NULL;
78 memset(&cl->dispatch, 0, sizeof(cl->dispatch));
79
80 if (cl->request.version != UH_HTTP_VER_1_1 || !conf.http_keepalive) {
81 uh_connection_close(cl);
82 return;
83 }
84
85 cl->state = CLIENT_STATE_INIT;
86 uloop_timeout_set(&cl->timeout, conf.http_keepalive * 1000);
87 }
88
89 void __printf(4, 5)
90 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...)
91 {
92 va_list arg;
93
94 uh_http_header(cl, code, summary);
95 ustream_printf(cl->us, "Content-Type: text/html\r\n\r\n");
96
97 uh_chunk_printf(cl, "<h1>%s</h1>", summary);
98
99 if (fmt) {
100 va_start(arg, fmt);
101 uh_chunk_vprintf(cl, fmt, arg);
102 va_end(arg);
103 }
104
105 uh_request_done(cl);
106 }
107
108 static void uh_header_error(struct client *cl, int code, const char *summary)
109 {
110 uh_client_error(cl, code, summary, NULL);
111 uh_connection_close(cl);
112 }
113
114 static void client_timeout(struct uloop_timeout *timeout)
115 {
116 struct client *cl = container_of(timeout, struct client, timeout);
117
118 cl->state = CLIENT_STATE_CLOSE;
119 uh_connection_close(cl);
120 }
121
122 static int find_idx(const char * const *list, int max, const char *str)
123 {
124 int i;
125
126 for (i = 0; i < max; i++)
127 if (!strcmp(list[i], str))
128 return i;
129
130 return -1;
131 }
132
133 static int client_parse_request(struct client *cl, char *data)
134 {
135 struct http_request *req = &cl->request;
136 char *type, *path, *version;
137
138 type = strtok(data, " ");
139 path = strtok(NULL, " ");
140 version = strtok(NULL, " ");
141 if (!type || !path || !version)
142 return CLIENT_STATE_DONE;
143
144 req->url = path;
145 req->method = find_idx(http_methods, ARRAY_SIZE(http_methods), type);
146 if (req->method < 0)
147 return CLIENT_STATE_DONE;
148
149 req->version = find_idx(http_versions, ARRAY_SIZE(http_versions), version);
150 if (cl->request.version < 0)
151 return CLIENT_STATE_DONE;
152
153 return CLIENT_STATE_HEADER;
154 }
155
156 static bool client_init_cb(struct client *cl, char *buf, int len)
157 {
158 char *newline;
159
160 newline = strstr(buf, "\r\n");
161 if (!newline)
162 return false;
163
164 *newline = 0;
165 blob_buf_init(&cl->hdr, 0);
166 blobmsg_add_string(&cl->hdr, "REQUEST", buf);
167 ustream_consume(cl->us, newline + 2 - buf);
168 cl->state = client_parse_request(cl, (char *) blobmsg_data(blob_data(cl->hdr.head)));
169 if (cl->state == CLIENT_STATE_DONE)
170 uh_header_error(cl, 400, "Bad Request");
171
172 return true;
173 }
174
175 static void client_header_complete(struct client *cl)
176 {
177 uh_handle_file_request(cl);
178 }
179
180 static int client_parse_header(struct client *cl, char *data)
181 {
182 char *name;
183 char *val;
184
185 if (!*data) {
186 uloop_timeout_cancel(&cl->timeout);
187 client_header_complete(cl);
188 return CLIENT_STATE_DATA;
189 }
190
191 val = uh_split_header(data);
192 if (!val)
193 return CLIENT_STATE_DONE;
194
195 for (name = data; *name; name++)
196 if (isupper(*name))
197 *name = tolower(*name);
198
199 blobmsg_add_string(&cl->hdr, data, val);
200
201 return CLIENT_STATE_HEADER;
202 }
203
204 static bool client_data_cb(struct client *cl, char *buf, int len)
205 {
206 return false;
207 }
208
209 static bool client_header_cb(struct client *cl, char *buf, int len)
210 {
211 char *newline;
212 int line_len;
213
214 newline = strstr(buf, "\r\n");
215 if (!newline)
216 return false;
217
218 *newline = 0;
219 cl->state = client_parse_header(cl, buf);
220 line_len = newline + 2 - buf;
221 ustream_consume(cl->us, line_len);
222 if (cl->state == CLIENT_STATE_DATA)
223 client_data_cb(cl, newline + 2, len - line_len);
224
225 return true;
226 }
227
228 typedef bool (*read_cb_t)(struct client *cl, char *buf, int len);
229 static read_cb_t read_cbs[] = {
230 [CLIENT_STATE_INIT] = client_init_cb,
231 [CLIENT_STATE_HEADER] = client_header_cb,
232 [CLIENT_STATE_DATA] = client_data_cb,
233 };
234
235 static void client_read_cb(struct client *cl)
236 {
237 struct ustream *us = cl->us;
238 char *str;
239 int len;
240
241 do {
242 str = ustream_get_read_buf(us, &len);
243 if (!str)
244 break;
245
246 if (cl->state >= array_size(read_cbs) || !read_cbs[cl->state])
247 break;
248
249 if (!read_cbs[cl->state](cl, str, len)) {
250 if (len == us->r.buffer_len)
251 uh_header_error(cl, 413, "Request Entity Too Large");
252 break;
253 }
254 } while(1);
255 }
256
257 static void client_close(struct client *cl)
258 {
259 uh_dispatch_done(cl);
260 uloop_timeout_cancel(&cl->timeout);
261 ustream_free(&cl->sfd.stream);
262 close(cl->sfd.fd.fd);
263 list_del(&cl->list);
264 blob_buf_free(&cl->hdr);
265 free(cl);
266
267 uh_unblock_listeners();
268 }
269
270 static void client_ustream_read_cb(struct ustream *s, int bytes)
271 {
272 struct client *cl = container_of(s, struct client, sfd);
273
274 client_read_cb(cl);
275 }
276
277 static void client_ustream_write_cb(struct ustream *s, int bytes)
278 {
279 struct client *cl = container_of(s, struct client, sfd);
280
281 if (cl->dispatch.write_cb)
282 cl->dispatch.write_cb(cl);
283 }
284
285 static void client_notify_state(struct ustream *s)
286 {
287 struct client *cl = container_of(s, struct client, sfd);
288
289 if (cl->state == CLIENT_STATE_CLOSE ||
290 (s->eof && !s->w.data_bytes) || s->write_error)
291 return client_close(cl);
292 }
293
294 void uh_accept_client(int fd)
295 {
296 static struct client *next_client;
297 struct client *cl;
298 unsigned int sl;
299 int sfd;
300 static int client_id = 0;
301
302 if (!next_client)
303 next_client = calloc(1, sizeof(*next_client));
304
305 cl = next_client;
306
307 sl = sizeof(cl->peeraddr);
308 sfd = accept(fd, (struct sockaddr *) &cl->peeraddr, &sl);
309 if (sfd < 0)
310 return;
311
312 sl = sizeof(cl->servaddr);
313 getsockname(fd, (struct sockaddr *) &cl->servaddr, &sl);
314 cl->us = &cl->sfd.stream;
315 cl->us->string_data = true;
316 cl->us->notify_read = client_ustream_read_cb;
317 cl->us->notify_write = client_ustream_write_cb;
318 cl->us->notify_state = client_notify_state;
319 ustream_fd_init(&cl->sfd, sfd);
320
321 cl->timeout.cb = client_timeout;
322 uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
323
324 list_add_tail(&cl->list, &clients);
325
326 next_client = NULL;
327 n_clients++;
328 cl->id = client_id++;
329 }
330
331 void uh_close_fds(void)
332 {
333 struct client *cl;
334
335 uloop_done();
336 uh_close_listen_fds();
337 list_for_each_entry(cl, &clients, list) {
338 close(cl->sfd.fd.fd);
339 if (cl->dispatch.close_fds)
340 cl->dispatch.close_fds(cl);
341 }
342 }