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
);
27 static bool client_done
= false;
30 struct config conf
= {};
32 const char * const http_versions
[] = {
33 [UH_HTTP_VER_0_9
] = "HTTP/0.9",
34 [UH_HTTP_VER_1_0
] = "HTTP/1.0",
35 [UH_HTTP_VER_1_1
] = "HTTP/1.1",
38 const char * const http_methods
[] = {
39 [UH_HTTP_MSG_GET
] = "GET",
40 [UH_HTTP_MSG_POST
] = "POST",
41 [UH_HTTP_MSG_HEAD
] = "HEAD",
42 [UH_HTTP_MSG_OPTIONS
] = "OPTIONS",
45 void uh_http_header(struct client
*cl
, int code
, const char *summary
)
47 struct http_request
*r
= &cl
->request
;
48 struct blob_attr
*cur
;
49 const char *enc
= "Transfer-Encoding: chunked\r\n";
55 if (!uh_use_chunked(cl
))
58 if (r
->connection_close
)
59 conn
= "Connection: close";
61 conn
= "Connection: Keep-Alive";
63 ustream_printf(cl
->us
, "%s %03i %s\r\n%s\r\n%s",
64 http_versions
[cl
->request
.version
],
65 code
, summary
, conn
, enc
);
67 if (!r
->connection_close
)
68 ustream_printf(cl
->us
, "Keep-Alive: timeout=%d\r\n", conf
.http_keepalive
);
70 blobmsg_for_each_attr(cur
, cl
->hdr_response
.head
, rem
)
71 ustream_printf(cl
->us
, "%s: %s\r\n", blobmsg_name(cur
),
72 blobmsg_get_string(cur
));
75 static void uh_connection_close(struct client
*cl
)
77 cl
->state
= CLIENT_STATE_CLOSE
;
79 ustream_state_change(cl
->us
);
82 static void uh_dispatch_done(struct client
*cl
)
84 if (cl
->dispatch
.free
)
85 cl
->dispatch
.free(cl
);
86 if (cl
->dispatch
.req_free
)
87 cl
->dispatch
.req_free(cl
);
90 static void client_timeout(struct uloop_timeout
*timeout
)
92 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
94 cl
->state
= CLIENT_STATE_CLOSE
;
95 uh_connection_close(cl
);
98 static void uh_set_client_timeout(struct client
*cl
, int timeout
)
100 cl
->timeout
.cb
= client_timeout
;
101 uloop_timeout_set(&cl
->timeout
, timeout
* 1000);
104 static void uh_keepalive_poll_cb(struct uloop_timeout
*timeout
)
106 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
107 int sec
= cl
->requests
> 0 ? conf
.http_keepalive
: conf
.network_timeout
;
109 uh_set_client_timeout(cl
, sec
);
110 cl
->us
->notify_read(cl
->us
, 0);
113 static void uh_poll_connection(struct client
*cl
)
115 cl
->timeout
.cb
= uh_keepalive_poll_cb
;
116 uloop_timeout_set(&cl
->timeout
, 1);
119 void uh_request_done(struct client
*cl
)
122 uh_dispatch_done(cl
);
123 blob_buf_init(&cl
->hdr_response
, 0);
124 memset(&cl
->dispatch
, 0, sizeof(cl
->dispatch
));
126 if (!conf
.http_keepalive
|| cl
->request
.connection_close
)
127 return uh_connection_close(cl
);
129 cl
->state
= CLIENT_STATE_INIT
;
131 uh_poll_connection(cl
);
135 uh_client_error(struct client
*cl
, int code
, const char *summary
, const char *fmt
, ...)
139 uh_http_header(cl
, code
, summary
);
140 ustream_printf(cl
->us
, "Content-Type: text/html\r\n\r\n");
142 uh_chunk_printf(cl
, "<h1>%s</h1>", summary
);
146 uh_chunk_vprintf(cl
, fmt
, arg
);
153 static void uh_header_error(struct client
*cl
, int code
, const char *summary
)
155 uh_client_error(cl
, code
, summary
, NULL
);
156 uh_connection_close(cl
);
159 static int find_idx(const char * const *list
, int max
, const char *str
)
163 for (i
= 0; i
< max
; i
++)
164 if (!strcmp(list
[i
], str
))
170 static int client_parse_request(struct client
*cl
, char *data
)
172 struct http_request
*req
= &cl
->request
;
173 char *type
, *path
, *version
;
174 int h_method
, h_version
;
176 type
= strtok(data
, " ");
177 path
= strtok(NULL
, " ");
178 version
= strtok(NULL
, " ");
179 if (!type
|| !path
|| !version
)
180 return CLIENT_STATE_DONE
;
182 blobmsg_add_string(&cl
->hdr
, "URL", path
);
184 memset(&cl
->request
, 0, sizeof(cl
->request
));
185 h_method
= find_idx(http_methods
, ARRAY_SIZE(http_methods
), type
);
186 h_version
= find_idx(http_versions
, ARRAY_SIZE(http_versions
), version
);
187 if (h_method
< 0 || h_version
< 0) {
188 req
->version
= UH_HTTP_VER_1_0
;
189 return CLIENT_STATE_DONE
;
192 req
->method
= h_method
;
193 req
->version
= h_version
;
194 if (req
->version
< UH_HTTP_VER_1_1
|| req
->method
== UH_HTTP_MSG_POST
||
195 !conf
.http_keepalive
)
196 req
->connection_close
= true;
198 return CLIENT_STATE_HEADER
;
201 static bool client_init_cb(struct client
*cl
, char *buf
, int len
)
205 newline
= strstr(buf
, "\r\n");
209 if (newline
== buf
) {
210 ustream_consume(cl
->us
, 2);
215 blob_buf_init(&cl
->hdr
, 0);
216 cl
->state
= client_parse_request(cl
, buf
);
217 ustream_consume(cl
->us
, newline
+ 2 - buf
);
218 if (cl
->state
== CLIENT_STATE_DONE
)
219 uh_header_error(cl
, 400, "Bad Request");
224 static bool rfc1918_filter_check(struct client
*cl
)
226 if (!conf
.rfc1918_filter
)
229 if (!uh_addr_rfc1918(&cl
->peer_addr
) || uh_addr_rfc1918(&cl
->srv_addr
))
232 uh_client_error(cl
, 403, "Forbidden",
233 "Rejected request from RFC1918 IP "
234 "to public server address");
238 static bool tls_redirect_check(struct client
*cl
)
241 struct blob_attr
*cur
;
242 char *ptr
, *url
= NULL
, *host
= NULL
;
244 if (cl
->tls
|| !conf
.tls_redirect
)
247 if ((port
= uh_first_tls_port(cl
->srv_addr
.family
)) == -1)
250 blob_for_each_attr(cur
, cl
->hdr
.head
, rem
) {
251 if (!strcmp(blobmsg_name(cur
), "host"))
252 host
= blobmsg_get_string(cur
);
254 if (!strcmp(blobmsg_name(cur
), "URL"))
255 url
= blobmsg_get_string(cur
);
264 if ((ptr
= strchr(host
, ']')) != NULL
)
266 else if ((ptr
= strchr(host
, ':')) != NULL
)
269 cl
->request
.disable_chunked
= true;
270 cl
->request
.connection_close
= true;
272 uh_http_header(cl
, 307, "Temporary Redirect");
275 ustream_printf(cl
->us
, "Location: https://%s:%d%s\r\n\r\n", host
, port
, url
);
277 ustream_printf(cl
->us
, "Location: https://%s%s\r\n\r\n", host
, url
);
284 static void client_header_complete(struct client
*cl
)
286 struct http_request
*r
= &cl
->request
;
288 if (!rfc1918_filter_check(cl
))
291 if (!tls_redirect_check(cl
))
295 ustream_printf(cl
->us
, "HTTP/1.1 100 Continue\r\n\r\n");
299 if (r
->method
!= UH_HTTP_MSG_POST
)
304 r
->connection_close
= true;
310 uh_handle_request(cl
);
313 static void client_parse_header(struct client
*cl
, char *data
)
315 struct http_request
*r
= &cl
->request
;
321 uloop_timeout_cancel(&cl
->timeout
);
322 cl
->state
= CLIENT_STATE_DATA
;
323 client_header_complete(cl
);
327 val
= uh_split_header(data
);
329 cl
->state
= CLIENT_STATE_DONE
;
333 for (name
= data
; *name
; name
++)
335 *name
= tolower(*name
);
337 if (!strcmp(data
, "expect")) {
338 if (!strcasecmp(val
, "100-continue"))
339 r
->expect_cont
= true;
341 uh_header_error(cl
, 412, "Precondition Failed");
344 } else if (!strcmp(data
, "content-length")) {
345 r
->content_length
= strtoul(val
, &err
, 0);
347 uh_header_error(cl
, 400, "Bad Request");
350 } else if (!strcmp(data
, "transfer-encoding")) {
351 if (!strcmp(val
, "chunked"))
352 r
->transfer_chunked
= true;
353 } else if (!strcmp(data
, "connection")) {
354 if (!strcasecmp(val
, "close"))
355 r
->connection_close
= true;
356 } else if (!strcmp(data
, "user-agent")) {
359 if (strstr(val
, "Opera"))
361 else if ((str
= strstr(val
, "MSIE ")) != NULL
) {
362 r
->ua
= UH_UA_MSIE_NEW
;
363 if (str
[5] && str
[6] == '.') {
366 if (strstr(str
, "SV1"))
371 r
->ua
= UH_UA_MSIE_OLD
;
376 else if (strstr(val
, "Chrome/"))
377 r
->ua
= UH_UA_CHROME
;
378 else if (strstr(val
, "Safari/") && strstr(val
, "Mac OS X"))
379 r
->ua
= UH_UA_SAFARI
;
380 else if (strstr(val
, "Gecko/"))
382 else if (strstr(val
, "Konqueror"))
383 r
->ua
= UH_UA_KONQUEROR
;
387 blobmsg_add_string(&cl
->hdr
, data
, val
);
389 cl
->state
= CLIENT_STATE_HEADER
;
392 void client_poll_post_data(struct client
*cl
)
394 struct dispatch
*d
= &cl
->dispatch
;
395 struct http_request
*r
= &cl
->request
;
399 if (cl
->state
== CLIENT_STATE_DONE
)
407 buf
= ustream_get_read_buf(cl
->us
, &len
);
414 cur_len
= min(r
->content_length
, len
);
420 cur_len
= d
->data_send(cl
, buf
, cur_len
);
422 r
->content_length
-= cur_len
;
423 ustream_consume(cl
->us
, cur_len
);
427 if (!r
->transfer_chunked
)
430 if (r
->transfer_chunked
> 1)
433 sep
= strstr(buf
+ offset
, "\r\n");
439 r
->content_length
= strtoul(buf
+ offset
, &sep
, 16);
440 r
->transfer_chunked
++;
441 ustream_consume(cl
->us
, sep
+ 2 - buf
);
443 /* invalid chunk length */
445 r
->content_length
= 0;
446 r
->transfer_chunked
= 0;
450 /* empty chunk == eof */
451 if (!r
->content_length
) {
452 r
->transfer_chunked
= false;
457 buf
= ustream_get_read_buf(cl
->us
, &len
);
458 if (!r
->content_length
&& !r
->transfer_chunked
&&
459 cl
->state
!= CLIENT_STATE_DONE
) {
460 if (cl
->dispatch
.data_done
)
461 cl
->dispatch
.data_done(cl
);
463 cl
->state
= CLIENT_STATE_DONE
;
467 static bool client_data_cb(struct client
*cl
, char *buf
, int len
)
469 client_poll_post_data(cl
);
473 static bool client_header_cb(struct client
*cl
, char *buf
, int len
)
478 newline
= strstr(buf
, "\r\n");
483 client_parse_header(cl
, buf
);
484 line_len
= newline
+ 2 - buf
;
485 ustream_consume(cl
->us
, line_len
);
486 if (cl
->state
== CLIENT_STATE_DATA
)
487 return client_data_cb(cl
, newline
+ 2, len
- line_len
);
492 typedef bool (*read_cb_t
)(struct client
*cl
, char *buf
, int len
);
493 static read_cb_t read_cbs
[] = {
494 [CLIENT_STATE_INIT
] = client_init_cb
,
495 [CLIENT_STATE_HEADER
] = client_header_cb
,
496 [CLIENT_STATE_DATA
] = client_data_cb
,
499 void uh_client_read_cb(struct client
*cl
)
501 struct ustream
*us
= cl
->us
;
507 str
= ustream_get_read_buf(us
, &len
);
511 if (cl
->state
>= array_size(read_cbs
) || !read_cbs
[cl
->state
])
514 if (!read_cbs
[cl
->state
](cl
, str
, len
)) {
515 if (len
== us
->r
.buffer_len
&&
516 cl
->state
!= CLIENT_STATE_DATA
)
517 uh_header_error(cl
, 413, "Request Entity Too Large");
520 } while (!client_done
);
523 static void client_close(struct client
*cl
)
526 cl
->state
= CLIENT_STATE_CLEANUP
;
532 uh_dispatch_done(cl
);
533 uloop_timeout_cancel(&cl
->timeout
);
535 uh_tls_client_detach(cl
);
536 ustream_free(&cl
->sfd
.stream
);
537 close(cl
->sfd
.fd
.fd
);
539 blob_buf_free(&cl
->hdr
);
540 blob_buf_free(&cl
->hdr_response
);
543 uh_unblock_listeners();
546 void uh_client_notify_state(struct client
*cl
)
548 struct ustream
*s
= cl
->us
;
550 if (!s
->write_error
&& cl
->state
!= CLIENT_STATE_CLEANUP
) {
551 if (cl
->state
== CLIENT_STATE_DATA
)
554 if (!s
->eof
|| s
->w
.data_bytes
)
558 return client_close(cl
);
561 static void client_ustream_read_cb(struct ustream
*s
, int bytes
)
563 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
565 uh_client_read_cb(cl
);
568 static void client_ustream_write_cb(struct ustream
*s
, int bytes
)
570 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
572 if (cl
->dispatch
.write_cb
)
573 cl
->dispatch
.write_cb(cl
);
576 static void client_notify_state(struct ustream
*s
)
578 struct client
*cl
= container_of(s
, struct client
, sfd
.stream
);
580 uh_client_notify_state(cl
);
583 static void set_addr(struct uh_addr
*addr
, void *src
)
585 struct sockaddr_in
*sin
= src
;
586 struct sockaddr_in6
*sin6
= src
;
588 addr
->family
= sin
->sin_family
;
589 if (addr
->family
== AF_INET
) {
590 addr
->port
= ntohs(sin
->sin_port
);
591 memcpy(&addr
->in
, &sin
->sin_addr
, sizeof(addr
->in
));
593 addr
->port
= ntohs(sin6
->sin6_port
);
594 memcpy(&addr
->in6
, &sin6
->sin6_addr
, sizeof(addr
->in6
));
598 bool uh_accept_client(int fd
, bool tls
)
600 static struct client
*next_client
;
604 static int client_id
= 0;
605 struct sockaddr_in6 addr
;
608 next_client
= calloc(1, sizeof(*next_client
));
613 sfd
= accept(fd
, (struct sockaddr
*) &addr
, &sl
);
617 set_addr(&cl
->peer_addr
, &addr
);
619 getsockname(sfd
, (struct sockaddr
*) &addr
, &sl
);
620 set_addr(&cl
->srv_addr
, &addr
);
622 cl
->us
= &cl
->sfd
.stream
;
624 uh_tls_client_attach(cl
);
626 cl
->us
->notify_read
= client_ustream_read_cb
;
627 cl
->us
->notify_write
= client_ustream_write_cb
;
628 cl
->us
->notify_state
= client_notify_state
;
631 cl
->us
->string_data
= true;
632 ustream_fd_init(&cl
->sfd
, sfd
);
634 uh_poll_connection(cl
);
635 list_add_tail(&cl
->list
, &clients
);
639 cl
->id
= client_id
++;
645 void uh_close_fds(void)
650 uh_close_listen_fds();
651 list_for_each_entry(cl
, &clients
, list
) {
652 close(cl
->sfd
.fd
.fd
);
653 if (cl
->dispatch
.close_fds
)
654 cl
->dispatch
.close_fds(cl
);