fix compile errors
[project/uhttpd.git] / main.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23
24 #include <getopt.h>
25 #include <errno.h>
26 #include <netdb.h>
27 #include <signal.h>
28
29 #include <libubox/usock.h>
30
31 #include "uhttpd.h"
32
33
34 static int run_server(void)
35 {
36         uloop_init();
37         uh_setup_listeners();
38         uloop_run();
39
40         return 0;
41 }
42
43 static void add_listener_arg(char *arg, bool tls)
44 {
45         char *host = NULL;
46         char *port = arg;
47         char *s;
48
49         s = strrchr(arg, ':');
50         if (s) {
51                 host = arg;
52                 port = s + 1;
53                 *s = 0;
54         }
55         uh_socket_bind(host, port, tls);
56 }
57
58 static int usage(const char *name)
59 {
60         fprintf(stderr, "Usage: %s -p <port>\n", name);
61         return 1;
62 }
63
64 static void init_defaults(void)
65 {
66         conf.network_timeout = 30;
67         conf.http_keepalive = 0; /* fixme */
68
69         uh_index_add("index.html");
70         uh_index_add("index.htm");
71         uh_index_add("default.html");
72         uh_index_add("default.htm");
73 }
74
75 int main(int argc, char **argv)
76 {
77         int ch;
78
79         init_defaults();
80         signal(SIGPIPE, SIG_IGN);
81
82         while ((ch = getopt(argc, argv, "sp:h:")) != -1) {
83                 bool tls = false;
84                 switch(ch) {
85                 case 's':
86                         tls = true;
87                 case 'p':
88                         add_listener_arg(optarg, tls);
89                         break;
90
91                 case 'h':
92                         /* docroot */
93                         if (!realpath(optarg, conf.docroot)) {
94                                 fprintf(stderr, "Error: Invalid directory %s: %s\n",
95                                                 optarg, strerror(errno));
96                                 exit(1);
97                         }
98                         break;
99                 default:
100                         return usage(argv[0]);
101                 }
102         }
103
104         return run_server();
105 }