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
);
78 static void client_timeout(struct uloop_timeout
*timeout
)
80 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
82 cl
->state
= CLIENT_STATE_CLOSE
;
83 uh_connection_close(cl
);
86 static void uh_set_client_timeout(struct client
*cl
, int timeout
)
88 cl
->timeout
.cb
= client_timeout
;
89 uloop_timeout_set(&cl
->timeout
, timeout
* 1000);
92 static void uh_keepalive_poll_cb(struct uloop_timeout
*timeout
)
94 struct client
*cl
= container_of(timeout
, struct client
, timeout
);
95 int sec
= cl
->requests
> 0 ? conf
.http_keepalive
: conf
.network_timeout
;
97 uh_set_client_timeout(cl
, sec
);
98 cl
->us
->notify_read(cl
->us
, 0);
101 static void uh_poll_connection(struct client
*cl
)
103 cl
->timeout
.cb
= uh_keepalive_poll_cb
;
104 uloop_timeout_set(&cl
->timeout
, 1);
107 void uh_request_done(struct client
*cl
)
110 uh_dispatch_done(cl
);
111 cl
->us
->notify_write
= NULL
;
112 memset(&cl
->dispatch
, 0, sizeof(cl
->dispatch
));
114 if (!conf
.http_keepalive
|| cl
->request
.connection_close
)
115 return uh_connection_close(cl
);
117 cl
->state
= CLIENT_STATE_INIT
;
119 uh_poll_connection(cl
);
123 uh_client_error(struct client
*cl
, int code
, const char *summary
, const char *fmt
, ...)
127 uh_http_header(cl
, code
, summary
);
128 ustream_printf(cl
->us
, "Content-Type: text/html\r\n\r\n");
130 uh_chunk_printf(cl
, "<h1>%s</h1>", summary
);
134 uh_chunk_vprintf(cl
, fmt
, arg
);
141 static void uh_header_error(struct client
*cl
, int code
, const char *summary
)
143 uh_client_error(cl
, code
, summary
, NULL
);
144 uh_connection_close(cl
);
147 static int find_idx(const char * const *list
, int max
, const char *str
)
151 for (i
= 0; i
< max
; i
++)
152 if (!strcmp(list
[i
], str
))
158 static int client_parse_request(struct client
*cl
, char *data
)
160 struct http_request
*req
= &cl
->request
;
161 char *type
, *path
, *version
;
162 int h_method
, h_version
;
164 type
= strtok(data
, " ");
165 path
= strtok(NULL
, " ");
166 version
= strtok(NULL
, " ");
167 if (!type
|| !path
|| !version
)
168 return CLIENT_STATE_DONE
;
170 blobmsg_add_string(&cl
->hdr
, "URL", path
);
172 memset(&cl
->request
, 0, sizeof(cl
->request
));
173 h_method
= find_idx(http_methods
, ARRAY_SIZE(http_methods
), type
);
174 h_version
= find_idx(http_versions
, ARRAY_SIZE(http_versions
), version
);
175 if (h_method
< 0 || h_version
< 0) {
176 req
->version
= UH_HTTP_VER_1_0
;
177 return CLIENT_STATE_DONE
;
180 req
->method
= h_method
;
181 req
->version
= h_version
;
182 if (req
->version
< UH_HTTP_VER_1_1
|| !conf
.http_keepalive
)
183 req
->connection_close
= true;
185 return CLIENT_STATE_HEADER
;
188 static bool client_init_cb(struct client
*cl
, char *buf
, int len
)
192 newline
= strstr(buf
, "\r\n");
197 blob_buf_init(&cl
->hdr
, 0);
198 cl
->state
= client_parse_request(cl
, buf
);
199 ustream_consume(cl
->us
, newline
+ 2 - buf
);
200 if (cl
->state
== CLIENT_STATE_DONE
)
201 uh_header_error(cl
, 400, "Bad Request");
206 static bool rfc1918_filter_check(struct client
*cl
)
208 if (!conf
.rfc1918_filter
)
211 if (!uh_addr_rfc1918(&cl
->peer_addr
) || uh_addr_rfc1918(&cl
->srv_addr
))
214 uh_client_error(cl
, 403, "Forbidden",
215 "Rejected request from RFC1918 IP "
216 "to public server address");
220 static void client_header_complete(struct client
*cl
)
222 struct http_request
*r
= &cl
->request
;
224 if (!rfc1918_filter_check(cl
))
228 ustream_printf(cl
->us
, "HTTP/1.1 100 Continue\r\n\r\n");
232 if (r
->method
!= UH_HTTP_MSG_POST
)
237 r
->connection_close
= true;
243 uh_handle_request(cl
);
246 static void client_parse_header(struct client
*cl
, char *data
)
248 struct http_request
*r
= &cl
->request
;
254 uloop_timeout_cancel(&cl
->timeout
);
255 cl
->state
= CLIENT_STATE_DATA
;
256 client_header_complete(cl
);
260 val
= uh_split_header(data
);
262 cl
->state
= CLIENT_STATE_DONE
;
266 for (name
= data
; *name
; name
++)
268 *name
= tolower(*name
);
270 if (!strcmp(data
, "expect")) {
271 if (!strcasecmp(val
, "100-continue"))
272 r
->expect_cont
= true;
274 uh_header_error(cl
, 412, "Precondition Failed");
277 } else if (!strcmp(data
, "content-length")) {
278 r
->content_length
= strtoul(val
, &err
, 0);
280 uh_header_error(cl
, 400, "Bad Request");
283 } else if (!strcmp(data
, "transfer-encoding")) {
284 if (!strcmp(val
, "chunked"))
285 r
->transfer_chunked
= true;
286 } else if (!strcmp(data
, "connection")) {
287 if (!strcasecmp(val
, "close"))
288 r
->connection_close
= true;
289 else if (!strcasecmp(val
, "keep-alive"))
290 r
->connection_close
= false;
291 } else if (!strcmp(data
, "user-agent")) {
294 if (strstr(val
, "Opera"))
296 else if ((str
= strstr(val
, "MSIE ")) != NULL
) {
297 r
->ua
= UH_UA_MSIE_NEW
;
298 if (str
[5] && str
[6] == '.') {
301 if (strstr(str
, "SV1"))
306 r
->ua
= UH_UA_MSIE_OLD
;
310 } else if (strstr(val
, "Safari/") && strstr(val
, "Mac OS X"))
311 r
->ua
= UH_UA_SAFARI
;
312 else if (strstr(val
, "Chrome/"))
313 r
->ua
= UH_UA_CHROME
;
314 else if (strstr(val
, "Gecko/"))
316 else if (strstr(val
, "Konqueror"))
317 r
->ua
= UH_UA_KONQUEROR
;
321 blobmsg_add_string(&cl
->hdr
, data
, val
);
323 cl
->state
= CLIENT_STATE_HEADER
;
326 void client_poll_post_data(struct client
*cl
)
328 struct dispatch
*d
= &cl
->dispatch
;
329 struct http_request
*r
= &cl
->request
;
333 if (cl
->state
== CLIENT_STATE_DONE
)
341 buf
= ustream_get_read_buf(cl
->us
, &len
);
348 cur_len
= min(r
->content_length
, len
);
354 cur_len
= d
->data_send(cl
, buf
, cur_len
);
356 r
->content_length
-= cur_len
;
357 ustream_consume(cl
->us
, cur_len
);
361 if (!r
->transfer_chunked
)
364 if (r
->transfer_chunked
> 1)
367 sep
= strstr(buf
+ offset
, "\r\n");
373 r
->content_length
= strtoul(buf
+ offset
, &sep
, 16);
374 r
->transfer_chunked
++;
375 ustream_consume(cl
->us
, sep
+ 2 - buf
);
377 /* invalid chunk length */
379 r
->content_length
= 0;
380 r
->transfer_chunked
= 0;
384 /* empty chunk == eof */
385 if (!r
->content_length
) {
386 r
->transfer_chunked
= false;
391 buf
= ustream_get_read_buf(cl
->us
, &len
);
392 if (!r
->content_length
&& !r
->transfer_chunked
&&
393 cl
->state
!= CLIENT_STATE_DONE
) {
394 if (cl
->dispatch
.data_done
)
395 cl
->dispatch
.data_done(cl
);
397 cl
->state
= CLIENT_STATE_DONE
;
401 static bool client_data_cb(struct client
*cl
, char *buf
, int len
)
403 client_poll_post_data(cl
);
407 static bool client_header_cb(struct client
*cl
, char *buf
, int len
)
412 newline
= strstr(buf
, "\r\n");
417 client_parse_header(cl
, buf
);
418 line_len
= newline
+ 2 - buf
;
419 ustream_consume(cl
->us
, line_len
);
420 if (cl
->state
== CLIENT_STATE_DATA
)
421 return client_data_cb(cl
, newline
+ 2, len
- line_len
);
426 typedef bool (*read_cb_t
)(struct client
*cl
, char *buf
, int len
);
427 static read_cb_t read_cbs
[] = {
428 [CLIENT_STATE_INIT
] = client_init_cb
,
429 [CLIENT_STATE_HEADER
] = client_header_cb
,
430 [CLIENT_STATE_DATA
] = client_data_cb
,
433 void uh_client_read_cb(struct client
*cl
)
435 struct ustream
*us
= cl
->us
;
440 str
= ustream_get_read_buf(us
, &len
);
444 if (cl
->state
>= array_size(read_cbs
) || !read_cbs
[cl
->state
])
447 if (!read_cbs
[cl
->state
](cl
, str
, len
)) {
448 if (len
== us
->r
.buffer_len
&&
449 cl
->state
!= CLIENT_STATE_DATA
)
450 uh_header_error(cl
, 413, "Request Entity Too Large");
456 static void client_close(struct client
*cl
)
459 uh_dispatch_done(cl
);
460 uloop_timeout_cancel(&cl
->timeout
);
462 uh_tls_client_detach(cl
);
463 ustream_free(&cl
->sfd
.stream
);
464 close(cl
->sfd
.fd
.fd
);
466 blob_buf_free(&cl
->hdr
);
469 uh_unblock_listeners();
472 void uh_client_notify_state(struct client
*cl
)
474 struct ustream
*s
= cl
->us
;
476 if (!s
->write_error
) {
477 if (cl
->state
== CLIENT_STATE_DATA
)
480 if (!s
->eof
|| s
->w
.data_bytes
)
484 return client_close(cl
);
487 static void client_ustream_read_cb(struct ustream
*s
, int bytes
)
489 struct client
*cl
= container_of(s
, struct client
, sfd
);
491 uh_client_read_cb(cl
);
494 static void client_ustream_write_cb(struct ustream
*s
, int bytes
)
496 struct client
*cl
= container_of(s
, struct client
, sfd
);
498 if (cl
->dispatch
.write_cb
)
499 cl
->dispatch
.write_cb(cl
);
502 static void client_notify_state(struct ustream
*s
)
504 struct client
*cl
= container_of(s
, struct client
, sfd
);
506 uh_client_notify_state(cl
);
509 static void set_addr(struct uh_addr
*addr
, void *src
)
511 struct sockaddr_in
*sin
= src
;
512 struct sockaddr_in6
*sin6
= src
;
514 addr
->family
= sin
->sin_family
;
515 if (addr
->family
== AF_INET
) {
516 addr
->port
= ntohs(sin
->sin_port
);
517 memcpy(&addr
->in
, &sin
->sin_addr
, sizeof(addr
->in
));
519 addr
->port
= ntohs(sin6
->sin6_port
);
520 memcpy(&addr
->in6
, &sin6
->sin6_addr
, sizeof(addr
->in6
));
524 bool uh_accept_client(int fd
, bool tls
)
526 static struct client
*next_client
;
530 static int client_id
= 0;
531 struct sockaddr_in6 addr
;
534 next_client
= calloc(1, sizeof(*next_client
));
539 sfd
= accept(fd
, (struct sockaddr
*) &addr
, &sl
);
543 set_addr(&cl
->peer_addr
, &addr
);
545 getsockname(sfd
, (struct sockaddr
*) &addr
, &sl
);
546 set_addr(&cl
->srv_addr
, &addr
);
548 cl
->us
= &cl
->sfd
.stream
;
550 uh_tls_client_attach(cl
);
552 cl
->us
->notify_read
= client_ustream_read_cb
;
553 cl
->us
->notify_write
= client_ustream_write_cb
;
554 cl
->us
->notify_state
= client_notify_state
;
557 cl
->us
->string_data
= true;
558 ustream_fd_init(&cl
->sfd
, sfd
);
560 uh_poll_connection(cl
);
561 list_add_tail(&cl
->list
, &clients
);
565 cl
->id
= client_id
++;
570 void uh_close_fds(void)
575 uh_close_listen_fds();
576 list_for_each_entry(cl
, &clients
, list
) {
577 close(cl
->sfd
.fd
.fd
);
578 if (cl
->dispatch
.close_fds
)
579 cl
->dispatch
.close_fds(cl
);