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.
22 #define _XOPEN_SOURCE 700
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
32 #include <libubox/usock.h>
38 static int run_server(void)
47 static void uh_config_parse(void)
49 const char *path
= conf
.file
;
57 path
= "/etc/httpd.conf";
63 memset(line
, 0, sizeof(line
));
65 while (fgets(line
, sizeof(line
) - 1, c
)) {
66 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
)) {
67 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
68 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
69 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
72 uh_auth_add(line
, col1
, col2
);
73 } else if (!strncmp(line
, "I:", 2)) {
74 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
75 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
78 uh_index_add(strdup(col1
));
79 } else if (!strncmp(line
, "E404:", 5)) {
80 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
81 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
84 conf
.error_handler
= strdup(col1
);
87 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
)) {
88 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
89 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
90 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
93 if (!uh_interpreter_add(col1
, col2
))
95 "Unable to add interpreter %s for extension %s: "
96 "Out of memory\n", col2
, col1
105 static int add_listener_arg(char *arg
, bool tls
)
111 s
= strrchr(arg
, ':');
118 return uh_socket_bind(host
, port
, tls
);
121 static int usage(const char *name
)
124 "Usage: %s -p [addr:]port -h docroot\n"
125 " -f Do not fork to background\n"
126 " -c file Configuration file, default is '/etc/httpd.conf'\n"
127 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
129 " -s [addr:]port Like -p but provide HTTPS on this port\n"
130 " -C file ASN.1 server certificate file\n"
131 " -K file ASN.1 server private key file\n"
133 " -h directory Specify the document root, default is '.'\n"
134 " -E string Use given virtual URL as 404 error handler\n"
135 " -I string Use given filename as index for directories, multiple allowed\n"
136 " -S Do not follow symbolic links outside of the docroot\n"
137 " -D Do not allow directory listings, send 403 instead\n"
138 " -R Enable RFC1918 filter\n"
139 " -n count Maximum allowed number of concurrent requests\n"
141 " -l string URL prefix for Lua handler, default is '/lua'\n"
142 " -L file Lua handler script, omit to disable Lua\n"
145 " -u string URL prefix for HTTP/JSON handler\n"
146 " -U file Override ubus socket path\n"
148 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
149 " -i .ext=path Use interpreter at path for files with the given extension\n"
150 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
151 " -T seconds Network timeout in seconds, default is 30\n"
152 " -d string URL decode given string\n"
153 " -r string Specify basic auth realm\n"
154 " -m string MD5 crypt given string\n"
160 static void init_defaults(void)
162 conf
.script_timeout
= 60;
163 conf
.network_timeout
= 30;
164 conf
.http_keepalive
= 20;
165 conf
.max_requests
= 3;
166 conf
.realm
= "Protected Area";
167 conf
.cgi_prefix
= "/cgi-bin";
168 conf
.cgi_path
= "/sbin:/usr/sbin:/bin:/usr/bin";
170 uh_index_add("index.html");
171 uh_index_add("index.htm");
172 uh_index_add("default.html");
173 uh_index_add("default.htm");
176 static void fixup_prefix(char *str
)
183 len
= strlen(str
) - 1;
185 while (len
> 0 && str
[len
] == '/')
191 int main(int argc
, char **argv
)
199 BUILD_BUG_ON(sizeof(uh_buf
) < PATH_MAX
);
201 uh_dispatch_add(&cgi_dispatch
);
203 signal(SIGPIPE
, SIG_IGN
);
205 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) {
213 bound
+= add_listener_arg(optarg
, tls
);
217 if (!realpath(optarg
, uh_buf
)) {
218 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
219 optarg
, strerror(errno
));
222 conf
.docroot
= strdup(uh_buf
);
226 if (optarg
[0] != '/') {
227 fprintf(stderr
, "Error: Invalid error handler: %s\n",
231 conf
.error_handler
= optarg
;
235 if (optarg
[0] == '/') {
236 fprintf(stderr
, "Error: Invalid index page: %s\n",
240 uh_index_add(optarg
);
244 conf
.no_symlinks
= 1;
248 conf
.no_dirlists
= 1;
252 conf
.rfc1918_filter
= 1;
256 conf
.max_requests
= atoi(optarg
);
260 fixup_prefix(optarg
);
261 conf
.cgi_prefix
= optarg
;
265 port
= strchr(optarg
, '=');
266 if (optarg
[0] != '.' || !port
) {
267 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
273 uh_interpreter_add(optarg
, port
);
277 conf
.script_timeout
= atoi(optarg
);
281 conf
.network_timeout
= atoi(optarg
);
285 conf
.tcp_keepalive
= atoi(optarg
);
293 port
= alloca(strlen(optarg
) + 1);
297 /* "decode" plus to space to retain compat */
298 for (opt
= 0; optarg
[opt
]; opt
++)
299 if (optarg
[opt
] == '+')
302 /* opt now contains strlen(optarg) -- no need to re-scan */
303 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
304 fprintf(stderr
, "uhttpd: invalid encoding\n");
311 /* basic auth realm */
318 printf("%s\n", crypt(optarg
, "$1$"));
328 return usage(argv
[0]);
335 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
339 /* fork (if not disabled) */
351 cur_fd
= open("/dev/null", O_WRONLY
);