2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
29 #include <libubox/usock.h>
35 static int run_server(void)
44 static void uh_config_parse(void)
46 const char *path
= conf
.file
;
54 path
= "/etc/httpd.conf";
60 memset(line
, 0, sizeof(line
));
62 while (fgets(line
, sizeof(line
) - 1, c
)) {
63 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))
69 uh_auth_add(line
, col1
, col2
);
70 } else if (!strncmp(line
, "I:", 2)) {
71 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
72 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
75 uh_index_add(strdup(col1
));
76 } else if (!strncmp(line
, "E404:", 5)) {
77 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
78 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
81 conf
.error_handler
= strdup(col1
);
84 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
)) {
85 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
86 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
87 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
90 if (!uh_interpreter_add(col1
, col2
))
92 "Unable to add interpreter %s for extension %s: "
93 "Out of memory\n", col2
, col1
102 static int add_listener_arg(char *arg
, bool tls
)
108 s
= strrchr(arg
, ':');
115 return uh_socket_bind(host
, port
, tls
);
118 static int usage(const char *name
)
121 "Usage: %s -p [addr:]port -h docroot\n"
122 " -f Do not fork to background\n"
123 " -c file Configuration file, default is '/etc/httpd.conf'\n"
124 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
126 " -s [addr:]port Like -p but provide HTTPS on this port\n"
127 " -C file ASN.1 server certificate file\n"
128 " -K file ASN.1 server private key file\n"
130 " -h directory Specify the document root, default is '.'\n"
131 " -E string Use given virtual URL as 404 error handler\n"
132 " -I string Use given filename as index for directories, multiple allowed\n"
133 " -S Do not follow symbolic links outside of the docroot\n"
134 " -D Do not allow directory listings, send 403 instead\n"
135 " -R Enable RFC1918 filter\n"
136 " -n count Maximum allowed number of concurrent requests\n"
138 " -l string URL prefix for Lua handler, default is '/lua'\n"
139 " -L file Lua handler script, omit to disable Lua\n"
142 " -u string URL prefix for HTTP/JSON handler\n"
143 " -U file Override ubus socket path\n"
145 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
146 " -i .ext=path Use interpreter at path for files with the given extension\n"
147 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
148 " -T seconds Network timeout in seconds, default is 30\n"
149 " -d string URL decode given string\n"
150 " -r string Specify basic auth realm\n"
151 " -m string MD5 crypt given string\n"
157 static void init_defaults(void)
159 conf
.script_timeout
= 60;
160 conf
.network_timeout
= 30;
161 conf
.http_keepalive
= 20;
162 conf
.max_requests
= 3;
163 conf
.realm
= "Protected Area";
164 conf
.cgi_prefix
= "/cgi-bin";
165 conf
.cgi_path
= "/sbin:/usr/sbin:/bin:/usr/bin";
167 uh_index_add("index.html");
168 uh_index_add("index.htm");
169 uh_index_add("default.html");
170 uh_index_add("default.htm");
173 static void fixup_prefix(char *str
)
180 len
= strlen(str
) - 1;
182 while (len
> 0 && str
[len
] == '/')
188 int main(int argc
, char **argv
)
196 BUILD_BUG_ON(sizeof(uh_buf
) < PATH_MAX
);
198 uh_dispatch_add(&cgi_dispatch
);
200 signal(SIGPIPE
, SIG_IGN
);
202 while ((ch
= getopt(argc
, argv
, "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) != -1) {
210 bound
+= add_listener_arg(optarg
, tls
);
214 if (!realpath(optarg
, uh_buf
)) {
215 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
216 optarg
, strerror(errno
));
219 conf
.docroot
= strdup(uh_buf
);
223 if (optarg
[0] != '/') {
224 fprintf(stderr
, "Error: Invalid error handler: %s\n",
228 conf
.error_handler
= optarg
;
232 if (optarg
[0] == '/') {
233 fprintf(stderr
, "Error: Invalid index page: %s\n",
237 uh_index_add(optarg
);
241 conf
.no_symlinks
= 1;
245 conf
.no_dirlists
= 1;
249 conf
.rfc1918_filter
= 1;
253 conf
.max_requests
= atoi(optarg
);
257 fixup_prefix(optarg
);
258 conf
.cgi_prefix
= optarg
;
262 port
= strchr(optarg
, '=');
263 if (optarg
[0] != '.' || !port
) {
264 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
270 uh_interpreter_add(optarg
, port
);
274 conf
.script_timeout
= atoi(optarg
);
278 conf
.network_timeout
= atoi(optarg
);
282 conf
.tcp_keepalive
= atoi(optarg
);
290 port
= alloca(strlen(optarg
) + 1);
294 /* "decode" plus to space to retain compat */
295 for (opt
= 0; optarg
[opt
]; opt
++)
296 if (optarg
[opt
] == '+')
299 /* opt now contains strlen(optarg) -- no need to re-scan */
300 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
301 fprintf(stderr
, "uhttpd: invalid encoding\n");
308 /* basic auth realm */
315 printf("%s\n", crypt(optarg
, "$1$"));
325 return usage(argv
[0]);
332 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
336 /* fork (if not disabled) */
348 cur_fd
= open("/dev/null", O_WRONLY
);