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>
34 static int run_server(void)
43 static void uh_config_parse(void)
45 const char *path
= conf
.file
;
53 path
= "/etc/httpd.conf";
59 memset(line
, 0, sizeof(line
));
61 while (fgets(line
, sizeof(line
) - 1, c
)) {
62 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
)) {
63 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
64 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
65 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
68 uh_auth_add(line
, col1
, col2
);
69 } else if (!strncmp(line
, "I:", 2)) {
70 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
71 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
74 uh_index_add(strdup(col1
));
75 } else if (!strncmp(line
, "E404:", 5)) {
76 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
77 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
80 conf
.error_handler
= strdup(col1
);
83 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
)) {
84 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
85 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
86 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
89 if (!uh_interpreter_add(col1
, col2
))
91 "Unable to add interpreter %s for extension %s: "
92 "Out of memory\n", col2
, col1
101 static void add_listener_arg(char *arg
, bool tls
)
107 s
= strrchr(arg
, ':');
113 uh_socket_bind(host
, port
, tls
);
116 static int usage(const char *name
)
118 fprintf(stderr
, "Usage: %s -p <port>\n", name
);
122 static void init_defaults(void)
124 conf
.network_timeout
= 30;
125 conf
.http_keepalive
= 0; /* fixme */
126 conf
.max_requests
= 3;
128 uh_index_add("index.html");
129 uh_index_add("index.htm");
130 uh_index_add("default.html");
131 uh_index_add("default.htm");
134 int main(int argc
, char **argv
)
142 signal(SIGPIPE
, SIG_IGN
);
144 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) {
152 add_listener_arg(optarg
, tls
);
156 if (!realpath(optarg
, conf
.docroot
)) {
157 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
158 optarg
, strerror(errno
));
164 if (optarg
[0] != '/') {
165 fprintf(stderr
, "Error: Invalid error handler: %s\n",
169 conf
.error_handler
= optarg
;
173 if (optarg
[0] == '/') {
174 fprintf(stderr
, "Error: Invalid index page: %s\n",
178 uh_index_add(optarg
);
182 conf
.no_symlinks
= 1;
186 conf
.no_dirlists
= 1;
190 conf
.rfc1918_filter
= 1;
194 conf
.max_requests
= atoi(optarg
);
198 conf
.script_timeout
= atoi(optarg
);
202 conf
.network_timeout
= atoi(optarg
);
206 conf
.tcp_keepalive
= atoi(optarg
);
214 port
= alloca(strlen(optarg
) + 1);
218 /* "decode" plus to space to retain compat */
219 for (opt
= 0; optarg
[opt
]; opt
++)
220 if (optarg
[opt
] == '+')
223 /* opt now contains strlen(optarg) -- no need to re-scan */
224 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
225 fprintf(stderr
, "uhttpd: invalid encoding\n");
232 /* basic auth realm */
239 printf("%s\n", crypt(optarg
, "$1$"));
249 return usage(argv
[0]);
255 /* fork (if not disabled) */
267 cur_fd
= open("/dev/null", O_WRONLY
);