9e5a5742639550766b01705ac72d4ec83aaa1ae7
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 uh_ufd_add(&l
->fd
, uh_listener_cb
, ULOOP_READ
);
236 static struct http_request
* uh_http_header_parse(struct client
*cl
,
237 char *buffer
, int buflen
)
239 char *method
= buffer
;
241 char *version
= NULL
;
243 char *headers
= NULL
;
244 char *hdrname
= NULL
;
245 char *hdrdata
= NULL
;
250 struct http_request
*req
= &cl
->request
;
253 /* terminate initial header line */
254 if ((headers
= strfind(buffer
, buflen
, "\r\n", 2)) != NULL
)
256 buffer
[buflen
-1] = 0;
261 /* find request path */
262 if ((path
= strchr(buffer
, ' ')) != NULL
)
265 /* find http version */
266 if ((path
!= NULL
) && ((version
= strchr(path
, ' ')) != NULL
))
271 if (strcmp(method
, "GET") && strcmp(method
, "HEAD") && strcmp(method
, "POST"))
274 uh_http_response(cl
, 405, "Method Not Allowed");
282 req
->method
= UH_HTTP_MSG_GET
;
286 req
->method
= UH_HTTP_MSG_HEAD
;
290 req
->method
= UH_HTTP_MSG_POST
;
296 if (!path
|| !strlen(path
))
298 /* malformed request */
299 uh_http_response(cl
, 400, "Bad Request");
308 if ((version
== NULL
) || (strcmp(version
, "HTTP/0.9") &&
309 strcmp(version
, "HTTP/1.0") && strcmp(version
, "HTTP/1.1")))
311 /* unsupported version */
312 uh_http_response(cl
, 400, "Bad Request");
317 req
->version
= strtof(&version
[5], NULL
);
320 D("SRV: %s %s HTTP/%.1f\n",
321 (req
->method
== UH_HTTP_MSG_POST
) ? "POST" :
322 (req
->method
== UH_HTTP_MSG_GET
) ? "GET" : "HEAD",
323 req
->url
, req
->version
);
325 /* process header fields */
326 for (i
= (int)(headers
- buffer
); i
< buflen
; i
++)
328 /* found eol and have name + value, push out header tuple */
329 if (hdrname
&& hdrdata
&& (buffer
[i
] == '\r' || buffer
[i
] == '\n'))
334 if ((hdrcount
+ 1) < array_size(req
->headers
))
336 D("SRV: HTTP: %s: %s\n", hdrname
, hdrdata
);
338 req
->headers
[hdrcount
++] = hdrname
;
339 req
->headers
[hdrcount
++] = hdrdata
;
341 hdrname
= hdrdata
= NULL
;
347 D("SRV: HTTP: header too big (too many headers)\n");
348 uh_http_response(cl
, 413, "Request Entity Too Large");
353 /* have name but no value and found a colon, start of value */
354 else if (hdrname
&& !hdrdata
&&
355 ((i
+1) < buflen
) && (buffer
[i
] == ':'))
358 hdrdata
= &buffer
[i
+1];
360 while ((hdrdata
+ 1) < (buffer
+ buflen
) && *hdrdata
== ' ')
364 /* have no name and found [A-Za-z], start of name */
365 else if (!hdrname
&& isalpha(buffer
[i
]))
367 hdrname
= &buffer
[i
];
372 req
->redirect_status
= 200;
376 /* Malformed request */
377 uh_http_response(cl
, 400, "Bad Request");
382 static struct http_request
* uh_http_header_recv(struct client
*cl
)
384 char *bufptr
= cl
->httpbuf
.buf
;
387 ssize_t blen
= sizeof(cl
->httpbuf
.buf
)-1;
390 memset(bufptr
, 0, sizeof(cl
->httpbuf
.buf
));
395 ensure_out(rlen
= uh_tcp_recv(cl
, bufptr
, blen
));
396 D("SRV: Client(%d) peek(%d) = %d\n", cl
->fd
.fd
, blen
, rlen
);
400 D("SRV: Client(%d) dead [%s]\n", cl
->fd
.fd
, strerror(errno
));
407 if ((idxptr
= strfind(cl
->httpbuf
.buf
, sizeof(cl
->httpbuf
.buf
),
410 /* header read complete ... */
411 cl
->httpbuf
.ptr
= idxptr
+ 4;
412 cl
->httpbuf
.len
= bufptr
- cl
->httpbuf
.ptr
;
414 return uh_http_header_parse(cl
, cl
->httpbuf
.buf
,
415 (cl
->httpbuf
.ptr
- cl
->httpbuf
.buf
));
419 /* request entity too large */
420 D("SRV: HTTP: header too big (buffer exceeded)\n");
421 uh_http_response(cl
, 413, "Request Entity Too Large");
427 #if defined(HAVE_LUA) || defined(HAVE_CGI)
428 static int uh_path_match(const char *prefix
, const char *url
)
430 if ((strstr(url
, prefix
) == url
) &&
431 ((prefix
[strlen(prefix
)-1] == '/') ||
432 (strlen(url
) == strlen(prefix
)) ||
433 (url
[strlen(prefix
)] == '/')))
442 static bool uh_dispatch_request(struct client
*cl
, struct http_request
*req
)
444 struct path_info
*pin
;
445 struct interpreter
*ipr
= NULL
;
446 struct config
*conf
= cl
->server
->conf
;
450 if (conf
->lua_state
&&
451 uh_path_match(conf
->lua_prefix
, req
->url
))
453 return conf
->lua_request(cl
, conf
->lua_state
);
460 if (conf
->ubus_state
&&
461 uh_path_match(conf
->ubus_prefix
, req
->url
))
463 return conf
->ubus_request(cl
, conf
->ubus_state
);
468 /* dispatch request */
469 if ((pin
= uh_path_lookup(cl
, req
->url
)) != NULL
)
472 if (!pin
->redirected
&& uh_auth_check(cl
, req
, pin
))
475 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
476 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
478 return uh_cgi_request(cl
, pin
, ipr
);
481 return uh_file_request(cl
, pin
);
488 /* Try to invoke an error handler */
489 if ((pin
= uh_path_lookup(cl
, conf
->error_handler
)) != NULL
)
492 if (uh_auth_check(cl
, req
, pin
))
494 req
->redirect_status
= 404;
496 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
497 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
499 return uh_cgi_request(cl
, pin
, ipr
);
502 return uh_file_request(cl
, pin
);
509 uh_http_sendhf(cl
, 404, "Not Found", "No such file or directory");
516 static void uh_socket_cb(struct uloop_fd
*u
, unsigned int events
);
518 static void uh_listener_cb(struct uloop_fd
*u
, unsigned int events
)
521 struct listener
*serv
;
525 serv
= container_of(u
, struct listener
, fd
);
528 /* defer client if maximum number of requests is exceeded */
529 if (serv
->n_clients
>= conf
->max_requests
)
532 /* handle new connections */
533 if ((new_fd
= accept(u
->fd
, NULL
, 0)) != -1)
535 D("SRV: Server(%d) accept => Client(%d)\n", u
->fd
, new_fd
);
537 /* add to global client list */
538 if ((cl
= uh_client_add(new_fd
, serv
)) != NULL
)
540 /* add client socket to global fdset */
541 uh_ufd_add(&cl
->fd
, uh_socket_cb
, ULOOP_READ
);
542 fd_cloexec(cl
->fd
.fd
);
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
);
560 /* insufficient resources */
563 fprintf(stderr
, "uh_client_add(): Cannot allocate memory\n");
569 static void uh_client_cb(struct client
*cl
, unsigned int events
);
571 static void uh_rpipe_cb(struct uloop_fd
*u
, unsigned int events
)
573 struct client
*cl
= container_of(u
, struct client
, rpipe
);
575 D("SRV: Client(%d) rpipe readable\n", cl
->fd
.fd
);
577 uh_client_cb(cl
, ULOOP_WRITE
);
580 static void uh_socket_cb(struct uloop_fd
*u
, unsigned int events
)
582 struct client
*cl
= container_of(u
, struct client
, fd
);
584 D("SRV: Client(%d) socket readable\n", cl
->fd
.fd
);
586 uh_client_cb(cl
, ULOOP_READ
);
589 static void uh_child_cb(struct uloop_process
*p
, int rv
)
591 struct client
*cl
= container_of(p
, struct client
, proc
);
593 D("SRV: Client(%d) child(%d) dead\n", cl
->fd
.fd
, cl
->proc
.pid
);
595 uh_client_cb(cl
, ULOOP_READ
| ULOOP_WRITE
);
598 static void uh_kill9_cb(struct uloop_timeout
*t
)
600 struct client
*cl
= container_of(t
, struct client
, timeout
);
602 if (!kill(cl
->proc
.pid
, 0))
604 D("SRV: Client(%d) child(%d) kill(SIGKILL)...\n",
605 cl
->fd
.fd
, cl
->proc
.pid
);
607 kill(cl
->proc
.pid
, SIGKILL
);
611 static void uh_timeout_cb(struct uloop_timeout
*t
)
613 struct client
*cl
= container_of(t
, struct client
, timeout
);
615 D("SRV: Client(%d) child(%d) timed out\n", cl
->fd
.fd
, cl
->proc
.pid
);
617 if (!kill(cl
->proc
.pid
, 0))
619 D("SRV: Client(%d) child(%d) kill(SIGTERM)...\n",
620 cl
->fd
.fd
, cl
->proc
.pid
);
622 kill(cl
->proc
.pid
, SIGTERM
);
624 cl
->timeout
.cb
= uh_kill9_cb
;
625 uloop_timeout_set(&cl
->timeout
, 1000);
629 static void uh_client_cb(struct client
*cl
, unsigned int events
)
633 struct http_request
*req
;
635 conf
= cl
->server
->conf
;
637 D("SRV: Client(%d) enter callback\n", cl
->fd
.fd
);
639 /* undispatched yet */
642 /* we have no headers yet and this was a write event, ignore... */
643 if (!(events
& ULOOP_READ
))
645 D("SRV: Client(%d) ignoring write event before headers\n", cl
->fd
.fd
);
649 /* attempt to receive and parse headers */
650 if (!(req
= uh_http_header_recv(cl
)))
652 D("SRV: Client(%d) failed to receive header\n", cl
->fd
.fd
);
653 uh_client_shutdown(cl
);
657 /* process expect headers */
658 foreach_header(i
, req
->headers
)
660 if (strcasecmp(req
->headers
[i
], "Expect"))
663 if (strcasecmp(req
->headers
[i
+1], "100-continue"))
665 D("SRV: Client(%d) unknown expect header (%s)\n",
666 cl
->fd
.fd
, req
->headers
[i
+1]);
668 uh_http_response(cl
, 417, "Precondition Failed");
669 uh_client_shutdown(cl
);
674 D("SRV: Client(%d) sending HTTP/1.1 100 Continue\n", cl
->fd
.fd
);
676 uh_http_sendf(cl
, NULL
, "HTTP/1.1 100 Continue\r\n\r\n");
677 cl
->httpbuf
.len
= 0; /* client will re-send the body */
682 /* RFC1918 filtering */
683 if (conf
->rfc1918_filter
&&
684 sa_rfc1918(&cl
->peeraddr
) && !sa_rfc1918(&cl
->servaddr
))
686 uh_http_sendhf(cl
, 403, "Forbidden",
687 "Rejected request from RFC1918 IP "
688 "to public server address");
690 uh_client_shutdown(cl
);
694 /* dispatch request */
695 if (!uh_dispatch_request(cl
, req
))
697 D("SRV: Client(%d) failed to dispach request\n", cl
->fd
.fd
);
698 uh_client_shutdown(cl
);
702 /* request handler spawned a pipe, register handler */
703 if (cl
->rpipe
.fd
> -1)
705 D("SRV: Client(%d) pipe(%d) spawned\n", cl
->fd
.fd
, cl
->rpipe
.fd
);
707 uh_ufd_add(&cl
->rpipe
, uh_rpipe_cb
, ULOOP_READ
);
710 /* request handler spawned a child, register handler */
713 D("SRV: Client(%d) child(%d) spawned\n", cl
->fd
.fd
, cl
->proc
.pid
);
715 cl
->proc
.cb
= uh_child_cb
;
716 uloop_process_add(&cl
->proc
);
718 cl
->timeout
.cb
= uh_timeout_cb
;
719 uloop_timeout_set(&cl
->timeout
, conf
->script_timeout
* 1000);
722 /* header processing complete */
723 D("SRV: Client(%d) dispatched\n", cl
->fd
.fd
);
724 cl
->dispatched
= true;
729 D("SRV: Client(%d) response callback signalized EOF\n", cl
->fd
.fd
);
730 uh_client_shutdown(cl
);
736 static inline int uh_inittls(struct config
*conf
)
742 if (conf
->tls
!= NULL
)
745 /* load TLS plugin */
746 if (!(lib
= dlopen("uhttpd_tls.so", RTLD_LAZY
| RTLD_GLOBAL
)))
749 "Notice: Unable to load TLS plugin - disabling SSL support! "
750 "(Reason: %s)\n", dlerror()
757 /* resolve functions */
758 if (!(conf
->tls_init
= dlsym(lib
, "uh_tls_ctx_init")) ||
759 !(conf
->tls_cert
= dlsym(lib
, "uh_tls_ctx_cert")) ||
760 !(conf
->tls_key
= dlsym(lib
, "uh_tls_ctx_key")) ||
761 !(conf
->tls_free
= dlsym(lib
, "uh_tls_ctx_free")) ||
762 !(conf
->tls_accept
= dlsym(lib
, "uh_tls_client_accept")) ||
763 !(conf
->tls_close
= dlsym(lib
, "uh_tls_client_close")) ||
764 !(conf
->tls_recv
= dlsym(lib
, "uh_tls_client_recv")) ||
765 !(conf
->tls_send
= dlsym(lib
, "uh_tls_client_send")))
768 "Error: Failed to lookup required symbols "
769 "in TLS plugin: %s\n", dlerror()
774 /* init SSL context */
775 if (!(conf
->tls
= conf
->tls_init()))
777 fprintf(stderr
, "Error: Failed to initalize SSL context\n");
786 int main (int argc
, char **argv
)
788 /* master file descriptor list */
791 /* working structs */
792 struct addrinfo hints
;
796 /* maximum file descriptor number */
797 int cur_fd
, max_fd
= 0;
819 /* handle SIGPIPE, SIGINT, SIGTERM */
821 sigemptyset(&sa
.sa_mask
);
823 sa
.sa_handler
= SIG_IGN
;
824 sigaction(SIGPIPE
, &sa
, NULL
);
826 sa
.sa_handler
= uh_sigterm
;
827 sigaction(SIGINT
, &sa
, NULL
);
828 sigaction(SIGTERM
, &sa
, NULL
);
830 /* prepare addrinfo hints */
831 memset(&hints
, 0, sizeof(hints
));
832 hints
.ai_family
= AF_UNSPEC
;
833 hints
.ai_socktype
= SOCK_STREAM
;
834 hints
.ai_flags
= AI_PASSIVE
;
837 memset(&conf
, 0, sizeof(conf
));
838 memset(bind
, 0, sizeof(bind
));
842 while ((opt
= getopt(argc
, argv
,
843 "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) > 0)
850 if ((port
= strrchr(optarg
, ':')) != NULL
)
852 if ((optarg
[0] == '[') && (port
> optarg
) && (port
[-1] == ']'))
853 memcpy(bind
, optarg
+ 1,
854 min(sizeof(bind
), (int)(port
- optarg
) - 2));
857 min(sizeof(bind
), (int)(port
- optarg
)));
869 if (uh_inittls(&conf
))
872 "Notice: TLS support is disabled, "
873 "ignoring '-s %s'\n", optarg
883 bound
+= uh_socket_bind(&serv_fds
, &max_fd
,
884 bind
[0] ? bind
: NULL
,
885 port
, &hints
, (opt
== 's'), &conf
);
887 memset(bind
, 0, sizeof(bind
));
893 if (!uh_inittls(&conf
))
895 if (conf
.tls_cert(conf
.tls
, optarg
) < 1)
898 "Error: Invalid certificate file given\n");
909 if (!uh_inittls(&conf
))
911 if (conf
.tls_key(conf
.tls
, optarg
) < 1)
914 "Error: Invalid private key file given\n");
926 if (! realpath(optarg
, conf
.docroot
))
928 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
929 optarg
, strerror(errno
));
936 if ((strlen(optarg
) == 0) || (optarg
[0] != '/'))
938 fprintf(stderr
, "Error: Invalid error handler: %s\n",
942 conf
.error_handler
= optarg
;
947 if ((strlen(optarg
) == 0) || (optarg
[0] == '/'))
949 fprintf(stderr
, "Error: Invalid index page: %s\n",
953 conf
.index_file
= optarg
;
956 /* don't follow symlinks */
958 conf
.no_symlinks
= 1;
961 /* don't list directories */
963 conf
.no_dirlists
= 1;
967 conf
.rfc1918_filter
= 1;
971 conf
.max_requests
= atoi(optarg
);
977 conf
.cgi_prefix
= optarg
;
982 if ((optarg
[0] == '.') && (port
= strchr(optarg
, '=')))
985 uh_interpreter_add(optarg
, port
);
989 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
999 conf
.lua_prefix
= optarg
;
1004 conf
.lua_handler
= optarg
;
1011 conf
.ubus_prefix
= optarg
;
1016 conf
.ubus_socket
= optarg
;
1020 #if defined(HAVE_CGI) || defined(HAVE_LUA)
1021 /* script timeout */
1023 conf
.script_timeout
= atoi(optarg
);
1027 /* network timeout */
1029 conf
.network_timeout
= atoi(optarg
);
1032 /* tcp keep-alive */
1034 conf
.tcp_keepalive
= atoi(optarg
);
1044 if ((port
= malloc(strlen(optarg
)+1)) != NULL
)
1046 /* "decode" plus to space to retain compat */
1047 for (opt
= 0; optarg
[opt
]; opt
++)
1048 if (optarg
[opt
] == '+')
1050 /* opt now contains strlen(optarg) -- no need to re-scan */
1051 memset(port
, 0, opt
+1);
1052 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0)
1053 fprintf(stderr
, "uhttpd: invalid encoding\n");
1061 /* basic auth realm */
1063 conf
.realm
= optarg
;
1068 printf("%s\n", crypt(optarg
, "$1$"));
1079 "Usage: %s -p [addr:]port [-h docroot]\n"
1080 " -f Do not fork to background\n"
1081 " -c file Configuration file, default is '/etc/httpd.conf'\n"
1082 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
1084 " -s [addr:]port Like -p but provide HTTPS on this port\n"
1085 " -C file ASN.1 server certificate file\n"
1086 " -K file ASN.1 server private key file\n"
1088 " -h directory Specify the document root, default is '.'\n"
1089 " -E string Use given virtual URL as 404 error handler\n"
1090 " -I string Use given filename as index page for directories\n"
1091 " -S Do not follow symbolic links outside of the docroot\n"
1092 " -D Do not allow directory listings, send 403 instead\n"
1093 " -R Enable RFC1918 filter\n"
1094 " -n count Maximum allowed number of concurrent requests\n"
1096 " -l string URL prefix for Lua handler, default is '/lua'\n"
1097 " -L file Lua handler script, omit to disable Lua\n"
1100 " -u string URL prefix for HTTP/JSON handler\n"
1101 " -U file Override ubus socket path\n"
1104 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
1105 " -i .ext=path Use interpreter at path for files with the given extension\n"
1107 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1108 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
1110 " -T seconds Network timeout in seconds, default is 30\n"
1111 " -d string URL decode given string\n"
1112 " -r string Specify basic auth realm\n"
1113 " -m string MD5 crypt given string\n"
1122 if ((tls
== 1) && (keys
< 2))
1124 fprintf(stderr
, "Error: Missing private key or certificate file\n");
1131 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
1135 /* default docroot */
1136 if (!conf
.docroot
[0] && !realpath(".", conf
.docroot
))
1138 fprintf(stderr
, "Error: Can not determine default document root: %s\n",
1145 conf
.realm
= "Protected Area";
1148 uh_config_parse(&conf
);
1150 /* default max requests */
1151 if (conf
.max_requests
<= 0)
1152 conf
.max_requests
= 3;
1154 /* default network timeout */
1155 if (conf
.network_timeout
<= 0)
1156 conf
.network_timeout
= 30;
1158 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1159 /* default script timeout */
1160 if (conf
.script_timeout
<= 0)
1161 conf
.script_timeout
= 60;
1165 /* default cgi prefix */
1166 if (!conf
.cgi_prefix
)
1167 conf
.cgi_prefix
= "/cgi-bin";
1171 /* load Lua plugin */
1172 if (!(lib
= dlopen("uhttpd_lua.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1175 "Notice: Unable to load Lua plugin - disabling Lua support! "
1176 "(Reason: %s)\n", dlerror());
1180 /* resolve functions */
1181 if (!(conf
.lua_init
= dlsym(lib
, "uh_lua_init")) ||
1182 !(conf
.lua_close
= dlsym(lib
, "uh_lua_close")) ||
1183 !(conf
.lua_request
= dlsym(lib
, "uh_lua_request")))
1186 "Error: Failed to lookup required symbols "
1187 "in Lua plugin: %s\n", dlerror()
1192 /* init Lua runtime if handler is specified */
1193 if (conf
.lua_handler
)
1195 /* default lua prefix */
1196 if (!conf
.lua_prefix
)
1197 conf
.lua_prefix
= "/lua";
1199 conf
.lua_state
= conf
.lua_init(&conf
);
1205 /* load ubus plugin */
1206 if (!(lib
= dlopen("uhttpd_ubus.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1209 "Notice: Unable to load ubus plugin - disabling ubus support! "
1210 "(Reason: %s)\n", dlerror());
1212 else if (conf
.ubus_prefix
)
1214 /* resolve functions */
1215 if (!(conf
.ubus_init
= dlsym(lib
, "uh_ubus_init")) ||
1216 !(conf
.ubus_close
= dlsym(lib
, "uh_ubus_close")) ||
1217 !(conf
.ubus_request
= dlsym(lib
, "uh_ubus_request")))
1220 "Error: Failed to lookup required symbols "
1221 "in ubus plugin: %s\n", dlerror()
1226 /* initialize 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
);