6d23283d9f3f1270efca79cef441b68b183b573c
2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <libubox/blobmsg.h>
26 static LIST_HEAD(clients
);
29 struct config conf
= {};
31 const char * const http_versions
[] = {
32 [UH_HTTP_VER_0_9
] = "HTTP/0.9",
33 [UH_HTTP_VER_1_0
] = "HTTP/1.0",
34 [UH_HTTP_VER_1_1
] = "HTTP/1.1",
37 const char * const http_methods
[] = {
38 [UH_HTTP_MSG_GET
] = "GET",
39 [UH_HTTP_MSG_POST
] = "POST",
40 [UH_HTTP_MSG_HEAD
] = "HEAD",
43 void uh_http_header(struct client
*cl
, int code
, const char *summary
)
45 struct http_request
*r
= &cl
->request
;
46 const char *enc
= "Transfer-Encoding: chunked\r\n";
49 if (!uh_use_chunked(cl
))
52 if (r
->connection_close
)
53 conn
= "Connection: close";
55 conn
= "Connection: Keep-Alive";
57 ustream_printf(cl
->us
, "%s %03i %s\r\n%s\r\n%s",
58 http_versions
[cl
->request
.version
],
59 code
, summary
, conn
, enc
);
61 if (!r
->connection_close
)
62 ustream_printf(cl
->us
, "Keep-Alive: timeout=%d\r\n", conf
.http_keepalive
);
65 static void uh_connection_close(struct client
*cl
)
67 cl
->state
= CLIENT_STATE_CLOSE
;
69 ustream_state_change(cl
->us
);
72 static void uh_dispatch_done(struct client
*cl
)
74 if (cl
->dispatch
.free
)
75 cl
->dispatch
.free(cl
);
76 if (cl
->dispatch
.req_free
)
77 cl
->dispatch
.req_free(cl
);
80 static void client_timeout(struct uloop_timeout
*timeout
)
82 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
84 cl
->state
= CLIENT_STATE_CLOSE
;
85 uh_connection_close(cl
);
88 static void uh_set_client_timeout(struct client
*cl
, int timeout
)
90 cl
->timeout
.cb
= client_timeout
;
91 uloop_timeout_set(&cl
->timeout
, timeout
* 1000);
94 static void uh_keepalive_poll_cb(struct uloop_timeout
*timeout
)
96 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
97 int sec
= cl
->requests
> 0 ? conf
.http_keepalive
: conf
.network_timeout
;
99 uh_set_client_timeout(cl
, sec
);
100 cl
->us
->notify_read(cl
->us
, 0);
103 static void uh_poll_connection(struct client
*cl
)
105 cl
->timeout
.cb
= uh_keepalive_poll_cb
;
106 uloop_timeout_set(&cl
->timeout
, 1);
109 void uh_request_done(struct client
*cl
)
112 uh_dispatch_done(cl
);
113 memset(&cl
->dispatch
, 0, sizeof(cl
->dispatch
));
115 if (!conf
.http_keepalive
|| cl
->request
.connection_close
)
116 return uh_connection_close(cl
);
118 cl
->state
= CLIENT_STATE_INIT
;
120 uh_poll_connection(cl
);
124 uh_client_error(struct client
*cl
, int code
, const char *summary
, const char *fmt
, ...)
128 uh_http_header(cl
, code
, summary
);
129 ustream_printf(cl
->us
, "Content-Type: text/html\r\n\r\n");
131 uh_chunk_printf(cl
, "<h1>%s</h1>", summary
);
135 uh_chunk_vprintf(cl
, fmt
, arg
);
142 static void uh_header_error(struct client
*cl
, int code
, const char *summary
)
144 uh_client_error(cl
, code
, summary
, NULL
);
145 uh_connection_close(cl
);
148 static int find_idx(const char * const *list
, int max
, const char *str
)
152 for (i
= 0; i
< max
; i
++)
153 if (!strcmp(list
[i
], str
))
159 static int client_parse_request(struct client
*cl
, char *data
)
161 struct http_request
*req
= &cl
->request
;
162 char *type
, *path
, *version
;
163 int h_method
, h_version
;
165 type
= strtok(data
, " ");
166 path
= strtok(NULL
, " ");
167 version
= strtok(NULL
, " ");
168 if (!type
|| !path
|| !version
)
169 return CLIENT_STATE_DONE
;
171 blobmsg_add_string(&cl
->hdr
, "URL", path
);
173 memset(&cl
->request
, 0, sizeof(cl
->request
));
174 h_method
= find_idx(http_methods
, ARRAY_SIZE(http_methods
), type
);
175 h_version
= find_idx(http_versions
, ARRAY_SIZE(http_versions
), version
);
176 if (h_method
< 0 || h_version
< 0) {
177 req
->version
= UH_HTTP_VER_1_0
;
178 return CLIENT_STATE_DONE
;
181 req
->method
= h_method
;
182 req
->version
= h_version
;
183 if (req
->version
< UH_HTTP_VER_1_1
|| !conf
.http_keepalive
)
184 req
->connection_close
= true;
186 return CLIENT_STATE_HEADER
;
189 static bool client_init_cb(struct client
*cl
, char *buf
, int len
)
193 newline
= strstr(buf
, "\r\n");
201 blob_buf_init(&cl
->hdr
, 0);
202 cl
->state
= client_parse_request(cl
, buf
);
203 ustream_consume(cl
->us
, newline
+ 2 - buf
);
204 if (cl
->state
== CLIENT_STATE_DONE
)
205 uh_header_error(cl
, 400, "Bad Request");
210 static bool rfc1918_filter_check(struct client
*cl
)
212 if (!conf
.rfc1918_filter
)
215 if (!uh_addr_rfc1918(&cl
->peer_addr
) || uh_addr_rfc1918(&cl
->srv_addr
))
218 uh_client_error(cl
, 403, "Forbidden",
219 "Rejected request from RFC1918 IP "
220 "to public server address");
224 static void client_header_complete(struct client
*cl
)
226 struct http_request
*r
= &cl
->request
;
228 if (!rfc1918_filter_check(cl
))
232 ustream_printf(cl
->us
, "HTTP/1.1 100 Continue\r\n\r\n");
236 if (r
->method
!= UH_HTTP_MSG_POST
)
241 r
->connection_close
= true;
247 uh_handle_request(cl
);
250 static void client_parse_header(struct client
*cl
, char *data
)
252 struct http_request
*r
= &cl
->request
;
258 uloop_timeout_cancel(&cl
->timeout
);
259 cl
->state
= CLIENT_STATE_DATA
;
260 client_header_complete(cl
);
264 val
= uh_split_header(data
);
266 cl
->state
= CLIENT_STATE_DONE
;
270 for (name
= data
; *name
; name
++)
272 *name
= tolower(*name
);
274 if (!strcmp(data
, "expect")) {
275 if (!strcasecmp(val
, "100-continue"))
276 r
->expect_cont
= true;
278 uh_header_error(cl
, 412, "Precondition Failed");
281 } else if (!strcmp(data
, "content-length")) {
282 r
->content_length
= strtoul(val
, &err
, 0);
284 uh_header_error(cl
, 400, "Bad Request");
287 } else if (!strcmp(data
, "transfer-encoding")) {
288 if (!strcmp(val
, "chunked"))
289 r
->transfer_chunked
= true;
290 } else if (!strcmp(data
, "connection")) {
291 if (!strcasecmp(val
, "close"))
292 r
->connection_close
= true;
293 else if (!strcasecmp(val
, "keep-alive"))
294 r
->connection_close
= false;
295 } else if (!strcmp(data
, "user-agent")) {
298 if (strstr(val
, "Opera"))
300 else if ((str
= strstr(val
, "MSIE ")) != NULL
) {
301 r
->ua
= UH_UA_MSIE_NEW
;
302 if (str
[5] && str
[6] == '.') {
305 if (strstr(str
, "SV1"))
310 r
->ua
= UH_UA_MSIE_OLD
;
314 } else if (strstr(val
, "Safari/") && strstr(val
, "Mac OS X"))
315 r
->ua
= UH_UA_SAFARI
;
316 else if (strstr(val
, "Chrome/"))
317 r
->ua
= UH_UA_CHROME
;
318 else if (strstr(val
, "Gecko/"))
320 else if (strstr(val
, "Konqueror"))
321 r
->ua
= UH_UA_KONQUEROR
;
325 blobmsg_add_string(&cl
->hdr
, data
, val
);
327 cl
->state
= CLIENT_STATE_HEADER
;
330 void client_poll_post_data(struct client
*cl
)
332 struct dispatch
*d
= &cl
->dispatch
;
333 struct http_request
*r
= &cl
->request
;
337 if (cl
->state
== CLIENT_STATE_DONE
)
345 buf
= ustream_get_read_buf(cl
->us
, &len
);
352 cur_len
= min(r
->content_length
, len
);
358 cur_len
= d
->data_send(cl
, buf
, cur_len
);
360 r
->content_length
-= cur_len
;
361 ustream_consume(cl
->us
, cur_len
);
365 if (!r
->transfer_chunked
)
368 if (r
->transfer_chunked
> 1)
371 sep
= strstr(buf
+ offset
, "\r\n");
377 r
->content_length
= strtoul(buf
+ offset
, &sep
, 16);
378 r
->transfer_chunked
++;
379 ustream_consume(cl
->us
, sep
+ 2 - buf
);
381 /* invalid chunk length */
383 r
->content_length
= 0;
384 r
->transfer_chunked
= 0;
388 /* empty chunk == eof */
389 if (!r
->content_length
) {
390 r
->transfer_chunked
= false;
395 buf
= ustream_get_read_buf(cl
->us
, &len
);
396 if (!r
->content_length
&& !r
->transfer_chunked
&&
397 cl
->state
!= CLIENT_STATE_DONE
) {
398 if (cl
->dispatch
.data_done
)
399 cl
->dispatch
.data_done(cl
);
401 cl
->state
= CLIENT_STATE_DONE
;
405 static bool client_data_cb(struct client
*cl
, char *buf
, int len
)
407 client_poll_post_data(cl
);
411 static bool client_header_cb(struct client
*cl
, char *buf
, int len
)
416 newline
= strstr(buf
, "\r\n");
421 client_parse_header(cl
, buf
);
422 line_len
= newline
+ 2 - buf
;
423 ustream_consume(cl
->us
, line_len
);
424 if (cl
->state
== CLIENT_STATE_DATA
)
425 return client_data_cb(cl
, newline
+ 2, len
- line_len
);
430 typedef bool (*read_cb_t
)(struct client
*cl
, char *buf
, int len
);
431 static read_cb_t read_cbs
[] = {
432 [CLIENT_STATE_INIT
] = client_init_cb
,
433 [CLIENT_STATE_HEADER
] = client_header_cb
,
434 [CLIENT_STATE_DATA
] = client_data_cb
,
437 void uh_client_read_cb(struct client
*cl
)
439 struct ustream
*us
= cl
->us
;
444 str
= ustream_get_read_buf(us
, &len
);
448 if (cl
->state
>= array_size(read_cbs
) || !read_cbs
[cl
->state
])
451 if (!read_cbs
[cl
->state
](cl
, str
, len
)) {
452 if (len
== us
->r
.buffer_len
&&
453 cl
->state
!= CLIENT_STATE_DATA
)
454 uh_header_error(cl
, 413, "Request Entity Too Large");
460 static void client_close(struct client
*cl
)
463 uh_dispatch_done(cl
);
464 uloop_timeout_cancel(&cl
->timeout
);
466 uh_tls_client_detach(cl
);
467 ustream_free(&cl
->sfd
.stream
);
468 close(cl
->sfd
.fd
.fd
);
470 blob_buf_free(&cl
->hdr
);
473 uh_unblock_listeners();
476 void uh_client_notify_state(struct client
*cl
)
478 struct ustream
*s
= cl
->us
;
480 if (!s
->write_error
) {
481 if (cl
->state
== CLIENT_STATE_DATA
)
484 if (!s
->eof
|| s
->w
.data_bytes
)
488 return client_close(cl
);
491 static void client_ustream_read_cb(struct ustream
*s
, int bytes
)
493 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
495 uh_client_read_cb(cl
);
498 static void client_ustream_write_cb(struct ustream
*s
, int bytes
)
500 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
502 if (cl
->dispatch
.write_cb
)
503 cl
->dispatch
.write_cb(cl
);
506 static void client_notify_state(struct ustream
*s
)
508 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
510 uh_client_notify_state(cl
);
513 static void set_addr(struct uh_addr
*addr
, void *src
)
515 struct sockaddr_in
*sin
= src
;
516 struct sockaddr_in6
*sin6
= src
;
518 addr
->family
= sin
->sin_family
;
519 if (addr
->family
== AF_INET
) {
520 addr
->port
= ntohs(sin
->sin_port
);
521 memcpy(&addr
->in
, &sin
->sin_addr
, sizeof(addr
->in
));
523 addr
->port
= ntohs(sin6
->sin6_port
);
524 memcpy(&addr
->in6
, &sin6
->sin6_addr
, sizeof(addr
->in6
));
528 bool uh_accept_client(int fd
, bool tls
)
530 static struct client
*next_client
;
534 static int client_id
= 0;
535 struct sockaddr_in6 addr
;
538 next_client
= calloc(1, sizeof(*next_client
));
543 sfd
= accept(fd
, (struct sockaddr
*) &addr
, &sl
);
547 set_addr(&cl
->peer_addr
, &addr
);
549 getsockname(sfd
, (struct sockaddr
*) &addr
, &sl
);
550 set_addr(&cl
->srv_addr
, &addr
);
552 cl
->us
= &cl
->sfd
.stream
;
554 uh_tls_client_attach(cl
);
556 cl
->us
->notify_read
= client_ustream_read_cb
;
557 cl
->us
->notify_write
= client_ustream_write_cb
;
558 cl
->us
->notify_state
= client_notify_state
;
561 cl
->us
->string_data
= true;
562 ustream_fd_init(&cl
->sfd
, sfd
);
564 uh_poll_connection(cl
);
565 list_add_tail(&cl
->list
, &clients
);
569 cl
->id
= client_id
++;
575 void uh_close_fds(void)
580 uh_close_listen_fds();
581 list_for_each_entry(cl
, &clients
, list
) {
582 close(cl
->sfd
.fd
.fd
);
583 if (cl
->dispatch
.close_fds
)
584 cl
->dispatch
.close_fds(cl
);