2 * uhttpd - Tiny single-threaded httpd - Main component
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #define _XOPEN_SOURCE 500 /* crypt() */
22 #include "uhttpd-utils.h"
23 #include "uhttpd-file.h"
26 #include "uhttpd-cgi.h"
30 #include "uhttpd-lua.h"
34 #include "uhttpd-tls.h"
40 static void uh_sigterm(int sig
)
45 static void uh_config_parse(struct config
*conf
)
53 const char *path
= conf
->file
? conf
->file
: "/etc/httpd.conf";
56 if ((c
= fopen(path
, "r")) != NULL
)
58 memset(line
, 0, sizeof(line
));
60 while (fgets(line
, sizeof(line
) - 1, c
))
62 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
))
64 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
65 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
66 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
71 if (!uh_auth_add(line
, col1
, col2
))
74 "Notice: No password set for user %s, ignoring "
75 "authentication on %s\n", col1
, line
79 else if (!strncmp(line
, "I:", 2))
81 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
82 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
87 conf
->index_file
= strdup(col1
);
89 else if (!strncmp(line
, "E404:", 5))
91 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
92 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
97 conf
->error_handler
= strdup(col1
);
100 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
))
102 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
103 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
104 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
109 if (!uh_interpreter_add(col1
, col2
))
112 "Unable to add interpreter %s for extension %s: "
113 "Out of memory\n", col2
, col1
124 static void uh_listener_cb(struct uloop_fd
*u
, unsigned int events
);
126 static int uh_socket_bind(fd_set
*serv_fds
, int *max_fd
,
127 const char *host
, const char *port
,
128 struct addrinfo
*hints
, int do_tls
,
136 int tcp_ka_idl
, tcp_ka_int
, tcp_ka_cnt
;
138 struct listener
*l
= NULL
;
139 struct addrinfo
*addrs
= NULL
, *p
= NULL
;
141 if ((status
= getaddrinfo(host
, port
, hints
, &addrs
)) != 0)
143 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(status
));
146 /* try to bind a new socket to each found address */
147 for (p
= addrs
; p
; p
= p
->ai_next
)
150 if ((sock
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)) == -1)
156 /* "address already in use" */
157 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
)))
159 perror("setsockopt()");
164 if (conf
->tcp_keepalive
> 0)
168 tcp_ka_int
= conf
->tcp_keepalive
;
170 if (setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
, &yes
, sizeof(yes
)) ||
171 setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
, &tcp_ka_idl
, sizeof(tcp_ka_idl
)) ||
172 setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
, &tcp_ka_int
, sizeof(tcp_ka_int
)) ||
173 setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
, &tcp_ka_cnt
, sizeof(tcp_ka_cnt
)))
175 fprintf(stderr
, "Notice: Unable to enable TCP keep-alive: %s\n",
180 /* required to get parallel v4 + v6 working */
181 if (p
->ai_family
== AF_INET6
)
183 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &yes
, sizeof(yes
)) == -1)
185 perror("setsockopt()");
191 if (bind(sock
, p
->ai_addr
, p
->ai_addrlen
) == -1)
198 if (listen(sock
, UH_LIMIT_CLIENTS
) == -1)
204 /* add listener to global list */
205 if (!(l
= uh_listener_add(sock
, conf
)))
207 fprintf(stderr
, "uh_listener_add(): Failed to allocate memory\n");
213 l
->tls
= do_tls
? conf
->tls
: NULL
;
216 /* add socket to server fd set */
217 FD_SET(sock
, serv_fds
);
219 *max_fd
= max(*max_fd
, sock
);
221 l
->fd
.cb
= uh_listener_cb
;
222 uloop_fd_add(&l
->fd
, ULOOP_READ
);
237 static struct http_request
* uh_http_header_parse(struct client
*cl
,
238 char *buffer
, int buflen
)
240 char *method
= buffer
;
242 char *version
= NULL
;
244 char *headers
= NULL
;
245 char *hdrname
= NULL
;
246 char *hdrdata
= NULL
;
251 struct http_request
*req
= &cl
->request
;
254 /* terminate initial header line */
255 if ((headers
= strfind(buffer
, buflen
, "\r\n", 2)) != NULL
)
257 buffer
[buflen
-1] = 0;
262 /* find request path */
263 if ((path
= strchr(buffer
, ' ')) != NULL
)
266 /* find http version */
267 if ((path
!= NULL
) && ((version
= strchr(path
, ' ')) != NULL
))
272 if (strcmp(method
, "GET") && strcmp(method
, "HEAD") && strcmp(method
, "POST"))
275 uh_http_response(cl
, 405, "Method Not Allowed");
283 req
->method
= UH_HTTP_MSG_GET
;
287 req
->method
= UH_HTTP_MSG_HEAD
;
291 req
->method
= UH_HTTP_MSG_POST
;
297 if (!path
|| !strlen(path
))
299 /* malformed request */
300 uh_http_response(cl
, 400, "Bad Request");
309 if ((version
== NULL
) || (strcmp(version
, "HTTP/0.9") &&
310 strcmp(version
, "HTTP/1.0") && strcmp(version
, "HTTP/1.1")))
312 /* unsupported version */
313 uh_http_response(cl
, 400, "Bad Request");
318 req
->version
= strtof(&version
[5], NULL
);
321 D("SRV: %s %s HTTP/%.1f\n",
322 (req
->method
== UH_HTTP_MSG_POST
) ? "POST" :
323 (req
->method
== UH_HTTP_MSG_GET
) ? "GET" : "HEAD",
324 req
->url
, req
->version
);
326 /* process header fields */
327 for (i
= (int)(headers
- buffer
); i
< buflen
; i
++)
329 /* found eol and have name + value, push out header tuple */
330 if (hdrname
&& hdrdata
&& (buffer
[i
] == '\r' || buffer
[i
] == '\n'))
335 if ((hdrcount
+ 1) < array_size(req
->headers
))
337 D("SRV: HTTP: %s: %s\n", hdrname
, hdrdata
);
339 req
->headers
[hdrcount
++] = hdrname
;
340 req
->headers
[hdrcount
++] = hdrdata
;
342 hdrname
= hdrdata
= NULL
;
348 D("SRV: HTTP: header too big (too many headers)\n");
349 uh_http_response(cl
, 413, "Request Entity Too Large");
354 /* have name but no value and found a colon, start of value */
355 else if (hdrname
&& !hdrdata
&&
356 ((i
+1) < buflen
) && (buffer
[i
] == ':'))
359 hdrdata
= &buffer
[i
+1];
361 while ((hdrdata
+ 1) < (buffer
+ buflen
) && *hdrdata
== ' ')
365 /* have no name and found [A-Za-z], start of name */
366 else if (!hdrname
&& isalpha(buffer
[i
]))
368 hdrname
= &buffer
[i
];
373 req
->redirect_status
= 200;
377 /* Malformed request */
378 uh_http_response(cl
, 400, "Bad Request");
383 static struct http_request
* uh_http_header_recv(struct client
*cl
)
385 char *bufptr
= cl
->httpbuf
.buf
;
388 ssize_t blen
= sizeof(cl
->httpbuf
.buf
)-1;
391 memset(bufptr
, 0, sizeof(cl
->httpbuf
.buf
));
396 ensure_out(rlen
= uh_tcp_recv(cl
, bufptr
, blen
));
397 D("SRV: Client(%d) peek(%d) = %d\n", cl
->fd
.fd
, blen
, rlen
);
401 D("SRV: Client(%d) dead [%s]\n", cl
->fd
.fd
, strerror(errno
));
408 if ((idxptr
= strfind(cl
->httpbuf
.buf
, sizeof(cl
->httpbuf
.buf
),
411 /* header read complete ... */
412 cl
->httpbuf
.ptr
= idxptr
+ 4;
413 cl
->httpbuf
.len
= bufptr
- cl
->httpbuf
.ptr
;
415 return uh_http_header_parse(cl
, cl
->httpbuf
.buf
,
416 (cl
->httpbuf
.ptr
- cl
->httpbuf
.buf
));
420 /* request entity too large */
421 D("SRV: HTTP: header too big (buffer exceeded)\n");
422 uh_http_response(cl
, 413, "Request Entity Too Large");
428 #if defined(HAVE_LUA) || defined(HAVE_CGI)
429 static int uh_path_match(const char *prefix
, const char *url
)
431 if ((strstr(url
, prefix
) == url
) &&
432 ((prefix
[strlen(prefix
)-1] == '/') ||
433 (strlen(url
) == strlen(prefix
)) ||
434 (url
[strlen(prefix
)] == '/')))
443 static bool uh_dispatch_request(struct client
*cl
, struct http_request
*req
)
445 struct path_info
*pin
;
446 struct interpreter
*ipr
= NULL
;
447 struct config
*conf
= cl
->server
->conf
;
451 if (conf
->lua_state
&&
452 uh_path_match(conf
->lua_prefix
, req
->url
))
454 return conf
->lua_request(cl
, conf
->lua_state
);
461 if (conf
->ubus_state
&&
462 uh_path_match(conf
->ubus_prefix
, req
->url
))
464 return conf
->ubus_request(cl
, conf
->ubus_state
);
469 /* dispatch request */
470 if ((pin
= uh_path_lookup(cl
, req
->url
)) != NULL
)
473 if (!pin
->redirected
&& uh_auth_check(cl
, req
, pin
))
476 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
477 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
479 return uh_cgi_request(cl
, pin
, ipr
);
482 return uh_file_request(cl
, pin
);
489 /* Try to invoke an error handler */
490 if ((pin
= uh_path_lookup(cl
, conf
->error_handler
)) != NULL
)
493 if (uh_auth_check(cl
, req
, pin
))
495 req
->redirect_status
= 404;
497 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
498 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
500 return uh_cgi_request(cl
, pin
, ipr
);
503 return uh_file_request(cl
, pin
);
510 uh_http_sendhf(cl
, 404, "Not Found", "No such file or directory");
517 static void uh_client_cb(struct uloop_fd
*u
, unsigned int events
);
519 static void uh_listener_cb(struct uloop_fd
*u
, unsigned int events
)
522 struct listener
*serv
;
526 serv
= container_of(u
, struct listener
, fd
);
529 /* defer client if maximum number of requests is exceeded */
530 if (serv
->n_clients
>= conf
->max_requests
)
533 /* handle new connections */
534 if ((new_fd
= accept(u
->fd
, NULL
, 0)) != -1)
536 D("SRV: Server(%d) accept => Client(%d)\n", u
->fd
, new_fd
);
538 /* add to global client list */
539 if ((cl
= uh_client_add(new_fd
, serv
)) != NULL
)
541 /* add client socket to global fdset */
542 uloop_fd_add(&cl
->fd
, ULOOP_READ
);
545 /* setup client tls context */
548 if (conf
->tls_accept(cl
) < 1)
550 D("SRV: Client(%d) SSL handshake failed, drop\n", new_fd
);
552 /* remove from global client list */
553 uh_client_remove(cl
);
559 cl
->fd
.cb
= uh_client_cb
;
563 /* insufficient resources */
566 fprintf(stderr
, "uh_client_add(): Cannot allocate memory\n");
572 static void uh_pipe_cb(struct uloop_fd
*u
, unsigned int events
)
574 struct client
*cl
= container_of(u
, struct client
, pipe
);
576 D("SRV: Client(%d) pipe(%d) readable\n", cl
->fd
.fd
, cl
->pipe
.fd
);
578 uh_client_cb(&cl
->fd
, ULOOP_WRITE
);
581 static void uh_child_cb(struct uloop_process
*p
, int rv
)
583 struct client
*cl
= container_of(p
, struct client
, proc
);
585 D("SRV: Client(%d) child(%d) is dead\n", cl
->fd
.fd
, cl
->proc
.pid
);
589 uh_client_cb(&cl
->fd
, ULOOP_READ
| ULOOP_WRITE
);
592 static void uh_kill9_cb(struct uloop_timeout
*t
)
594 struct client
*cl
= container_of(t
, struct client
, timeout
);
596 if (!kill(cl
->proc
.pid
, 0))
598 D("SRV: Client(%d) child(%d) kill(SIGKILL)...\n",
599 cl
->fd
.fd
, cl
->proc
.pid
);
601 kill(cl
->proc
.pid
, SIGKILL
);
605 static void uh_timeout_cb(struct uloop_timeout
*t
)
607 struct client
*cl
= container_of(t
, struct client
, timeout
);
609 D("SRV: Client(%d) child(%d) timed out\n", cl
->fd
.fd
, cl
->proc
.pid
);
611 if (!kill(cl
->proc
.pid
, 0))
613 D("SRV: Client(%d) child(%d) kill(SIGTERM)...\n",
614 cl
->fd
.fd
, cl
->proc
.pid
);
616 kill(cl
->proc
.pid
, SIGTERM
);
618 cl
->timeout
.cb
= uh_kill9_cb
;
619 uloop_timeout_set(&cl
->timeout
, 1000);
623 static void uh_client_cb(struct uloop_fd
*u
, unsigned int events
)
628 struct http_request
*req
;
630 cl
= container_of(u
, struct client
, fd
);
631 conf
= cl
->server
->conf
;
633 D("SRV: Client(%d) enter callback\n", u
->fd
);
635 /* undispatched yet */
638 /* we have no headers yet and this was a write event, ignore... */
639 if (!(events
& ULOOP_READ
))
641 D("SRV: Client(%d) ignoring write event before headers\n", u
->fd
);
645 /* attempt to receive and parse headers */
646 if (!(req
= uh_http_header_recv(cl
)))
648 D("SRV: Client(%d) failed to receive header\n", u
->fd
);
649 uh_client_shutdown(cl
);
653 /* process expect headers */
654 foreach_header(i
, req
->headers
)
656 if (strcasecmp(req
->headers
[i
], "Expect"))
659 if (strcasecmp(req
->headers
[i
+1], "100-continue"))
661 D("SRV: Client(%d) unknown expect header (%s)\n",
662 u
->fd
, req
->headers
[i
+1]);
664 uh_http_response(cl
, 417, "Precondition Failed");
665 uh_client_shutdown(cl
);
670 D("SRV: Client(%d) sending HTTP/1.1 100 Continue\n", u
->fd
);
672 uh_http_sendf(cl
, NULL
, "HTTP/1.1 100 Continue\r\n\r\n");
673 cl
->httpbuf
.len
= 0; /* client will re-send the body */
678 /* RFC1918 filtering */
679 if (conf
->rfc1918_filter
&&
680 sa_rfc1918(&cl
->peeraddr
) && !sa_rfc1918(&cl
->servaddr
))
682 uh_http_sendhf(cl
, 403, "Forbidden",
683 "Rejected request from RFC1918 IP "
684 "to public server address");
686 uh_client_shutdown(cl
);
690 /* dispatch request */
691 if (!uh_dispatch_request(cl
, req
))
693 D("SRV: Client(%d) failed to dispach request\n", u
->fd
);
694 uh_client_shutdown(cl
);
698 /* request handler spawned a pipe, register handler */
701 D("SRV: Client(%d) pipe(%d) spawned\n", u
->fd
, cl
->pipe
.fd
);
703 cl
->pipe
.cb
= uh_pipe_cb
;
704 uloop_fd_add(&cl
->pipe
, ULOOP_READ
);
707 /* request handler spawned a child, register handler */
710 D("SRV: Client(%d) child(%d) spawned\n", u
->fd
, cl
->proc
.pid
);
712 cl
->proc
.cb
= uh_child_cb
;
713 uloop_process_add(&cl
->proc
);
715 cl
->timeout
.cb
= uh_timeout_cb
;
716 uloop_timeout_set(&cl
->timeout
, conf
->script_timeout
* 1000);
719 /* header processing complete */
720 D("SRV: Client(%d) dispatched\n", u
->fd
);
721 cl
->dispatched
= true;
726 D("SRV: Client(%d) response callback signalized EOF\n", u
->fd
);
727 uh_client_shutdown(cl
);
733 static inline int uh_inittls(struct config
*conf
)
739 if (conf
->tls
!= NULL
)
742 /* load TLS plugin */
743 if (!(lib
= dlopen("uhttpd_tls.so", RTLD_LAZY
| RTLD_GLOBAL
)))
746 "Notice: Unable to load TLS plugin - disabling SSL support! "
747 "(Reason: %s)\n", dlerror()
754 /* resolve functions */
755 if (!(conf
->tls_init
= dlsym(lib
, "uh_tls_ctx_init")) ||
756 !(conf
->tls_cert
= dlsym(lib
, "uh_tls_ctx_cert")) ||
757 !(conf
->tls_key
= dlsym(lib
, "uh_tls_ctx_key")) ||
758 !(conf
->tls_free
= dlsym(lib
, "uh_tls_ctx_free")) ||
759 !(conf
->tls_accept
= dlsym(lib
, "uh_tls_client_accept")) ||
760 !(conf
->tls_close
= dlsym(lib
, "uh_tls_client_close")) ||
761 !(conf
->tls_recv
= dlsym(lib
, "uh_tls_client_recv")) ||
762 !(conf
->tls_send
= dlsym(lib
, "uh_tls_client_send")))
765 "Error: Failed to lookup required symbols "
766 "in TLS plugin: %s\n", dlerror()
771 /* init SSL context */
772 if (!(conf
->tls
= conf
->tls_init()))
774 fprintf(stderr
, "Error: Failed to initalize SSL context\n");
783 int main (int argc
, char **argv
)
785 /* master file descriptor list */
788 /* working structs */
789 struct addrinfo hints
;
793 /* maximum file descriptor number */
794 int cur_fd
, max_fd
= 0;
816 /* handle SIGPIPE, SIGINT, SIGTERM */
818 sigemptyset(&sa
.sa_mask
);
820 sa
.sa_handler
= SIG_IGN
;
821 sigaction(SIGPIPE
, &sa
, NULL
);
823 sa
.sa_handler
= uh_sigterm
;
824 sigaction(SIGINT
, &sa
, NULL
);
825 sigaction(SIGTERM
, &sa
, NULL
);
827 /* prepare addrinfo hints */
828 memset(&hints
, 0, sizeof(hints
));
829 hints
.ai_family
= AF_UNSPEC
;
830 hints
.ai_socktype
= SOCK_STREAM
;
831 hints
.ai_flags
= AI_PASSIVE
;
834 memset(&conf
, 0, sizeof(conf
));
835 memset(bind
, 0, sizeof(bind
));
839 while ((opt
= getopt(argc
, argv
,
840 "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) > 0)
847 if ((port
= strrchr(optarg
, ':')) != NULL
)
849 if ((optarg
[0] == '[') && (port
> optarg
) && (port
[-1] == ']'))
850 memcpy(bind
, optarg
+ 1,
851 min(sizeof(bind
), (int)(port
- optarg
) - 2));
854 min(sizeof(bind
), (int)(port
- optarg
)));
866 if (uh_inittls(&conf
))
869 "Notice: TLS support is disabled, "
870 "ignoring '-s %s'\n", optarg
880 bound
+= uh_socket_bind(&serv_fds
, &max_fd
,
881 bind
[0] ? bind
: NULL
,
882 port
, &hints
, (opt
== 's'), &conf
);
884 memset(bind
, 0, sizeof(bind
));
890 if (!uh_inittls(&conf
))
892 if (conf
.tls_cert(conf
.tls
, optarg
) < 1)
895 "Error: Invalid certificate file given\n");
906 if (!uh_inittls(&conf
))
908 if (conf
.tls_key(conf
.tls
, optarg
) < 1)
911 "Error: Invalid private key file given\n");
923 if (! realpath(optarg
, conf
.docroot
))
925 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
926 optarg
, strerror(errno
));
933 if ((strlen(optarg
) == 0) || (optarg
[0] != '/'))
935 fprintf(stderr
, "Error: Invalid error handler: %s\n",
939 conf
.error_handler
= optarg
;
944 if ((strlen(optarg
) == 0) || (optarg
[0] == '/'))
946 fprintf(stderr
, "Error: Invalid index page: %s\n",
950 conf
.index_file
= optarg
;
953 /* don't follow symlinks */
955 conf
.no_symlinks
= 1;
958 /* don't list directories */
960 conf
.no_dirlists
= 1;
964 conf
.rfc1918_filter
= 1;
968 conf
.max_requests
= atoi(optarg
);
974 conf
.cgi_prefix
= optarg
;
979 if ((optarg
[0] == '.') && (port
= strchr(optarg
, '=')))
982 uh_interpreter_add(optarg
, port
);
986 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
996 conf
.lua_prefix
= optarg
;
1001 conf
.lua_handler
= optarg
;
1008 conf
.ubus_prefix
= optarg
;
1013 conf
.ubus_socket
= optarg
;
1017 #if defined(HAVE_CGI) || defined(HAVE_LUA)
1018 /* script timeout */
1020 conf
.script_timeout
= atoi(optarg
);
1024 /* network timeout */
1026 conf
.network_timeout
= atoi(optarg
);
1029 /* tcp keep-alive */
1031 conf
.tcp_keepalive
= atoi(optarg
);
1041 if ((port
= malloc(strlen(optarg
)+1)) != NULL
)
1043 /* "decode" plus to space to retain compat */
1044 for (opt
= 0; optarg
[opt
]; opt
++)
1045 if (optarg
[opt
] == '+')
1047 /* opt now contains strlen(optarg) -- no need to re-scan */
1048 memset(port
, 0, opt
+1);
1049 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0)
1050 fprintf(stderr
, "uhttpd: invalid encoding\n");
1058 /* basic auth realm */
1060 conf
.realm
= optarg
;
1065 printf("%s\n", crypt(optarg
, "$1$"));
1076 "Usage: %s -p [addr:]port [-h docroot]\n"
1077 " -f Do not fork to background\n"
1078 " -c file Configuration file, default is '/etc/httpd.conf'\n"
1079 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
1081 " -s [addr:]port Like -p but provide HTTPS on this port\n"
1082 " -C file ASN.1 server certificate file\n"
1083 " -K file ASN.1 server private key file\n"
1085 " -h directory Specify the document root, default is '.'\n"
1086 " -E string Use given virtual URL as 404 error handler\n"
1087 " -I string Use given filename as index page for directories\n"
1088 " -S Do not follow symbolic links outside of the docroot\n"
1089 " -D Do not allow directory listings, send 403 instead\n"
1090 " -R Enable RFC1918 filter\n"
1091 " -n count Maximum allowed number of concurrent requests\n"
1093 " -l string URL prefix for Lua handler, default is '/lua'\n"
1094 " -L file Lua handler script, omit to disable Lua\n"
1097 " -u string URL prefix for HTTP/JSON handler, default is '/ubus'\n"
1098 " -U file Override ubus socket path\n"
1101 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
1102 " -i .ext=path Use interpreter at path for files with the given extension\n"
1104 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1105 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
1107 " -T seconds Network timeout in seconds, default is 30\n"
1108 " -d string URL decode given string\n"
1109 " -r string Specify basic auth realm\n"
1110 " -m string MD5 crypt given string\n"
1119 if ((tls
== 1) && (keys
< 2))
1121 fprintf(stderr
, "Error: Missing private key or certificate file\n");
1128 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
1132 /* default docroot */
1133 if (!conf
.docroot
[0] && !realpath(".", conf
.docroot
))
1135 fprintf(stderr
, "Error: Can not determine default document root: %s\n",
1142 conf
.realm
= "Protected Area";
1145 uh_config_parse(&conf
);
1147 /* default max requests */
1148 if (conf
.max_requests
<= 0)
1149 conf
.max_requests
= 3;
1151 /* default network timeout */
1152 if (conf
.network_timeout
<= 0)
1153 conf
.network_timeout
= 30;
1155 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1156 /* default script timeout */
1157 if (conf
.script_timeout
<= 0)
1158 conf
.script_timeout
= 60;
1162 /* default cgi prefix */
1163 if (!conf
.cgi_prefix
)
1164 conf
.cgi_prefix
= "/cgi-bin";
1168 /* load Lua plugin */
1169 if (!(lib
= dlopen("uhttpd_lua.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1172 "Notice: Unable to load Lua plugin - disabling Lua support! "
1173 "(Reason: %s)\n", dlerror());
1177 /* resolve functions */
1178 if (!(conf
.lua_init
= dlsym(lib
, "uh_lua_init")) ||
1179 !(conf
.lua_close
= dlsym(lib
, "uh_lua_close")) ||
1180 !(conf
.lua_request
= dlsym(lib
, "uh_lua_request")))
1183 "Error: Failed to lookup required symbols "
1184 "in Lua plugin: %s\n", dlerror()
1189 /* init Lua runtime if handler is specified */
1190 if (conf
.lua_handler
)
1192 /* default lua prefix */
1193 if (!conf
.lua_prefix
)
1194 conf
.lua_prefix
= "/lua";
1196 conf
.lua_state
= conf
.lua_init(&conf
);
1202 /* load ubus plugin */
1203 if (!(lib
= dlopen("uhttpd_ubus.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1206 "Notice: Unable to load ubus plugin - disabling ubus support! "
1207 "(Reason: %s)\n", dlerror());
1211 /* resolve functions */
1212 if (!(conf
.ubus_init
= dlsym(lib
, "uh_ubus_init")) ||
1213 !(conf
.ubus_close
= dlsym(lib
, "uh_ubus_close")) ||
1214 !(conf
.ubus_request
= dlsym(lib
, "uh_ubus_request")))
1217 "Error: Failed to lookup required symbols "
1218 "in ubus plugin: %s\n", dlerror()
1223 /* default ubus prefix */
1224 if (!conf
.ubus_prefix
)
1225 conf
.ubus_prefix
= "/ubus";
1227 conf
.ubus_state
= conf
.ubus_init(&conf
);
1231 /* fork (if not disabled) */
1245 if ((cur_fd
= open("/dev/null", O_WRONLY
)) > -1)
1248 if ((cur_fd
= open("/dev/null", O_RDONLY
)) > -1)
1251 if ((cur_fd
= open("/dev/null", O_RDONLY
)) > -1)
1261 /* server main loop */
1265 /* destroy the Lua state */
1266 if (conf
.lua_state
!= NULL
)
1267 conf
.lua_close(conf
.lua_state
);
1271 /* destroy the ubus state */
1272 if (conf
.ubus_state
!= NULL
)
1273 conf
.ubus_close(conf
.ubus_state
);