1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert   A trivial static http webserver using Libevent's evhttp.
3*a466cc55SCy Schubert 
4*a466cc55SCy Schubert   This is not the best code in the world, and it does some fairly stupid stuff
5*a466cc55SCy Schubert   that you would never want to do in a production webserver. Caveat hackor!
6*a466cc55SCy Schubert 
7*a466cc55SCy Schubert  */
8*a466cc55SCy Schubert 
9*a466cc55SCy Schubert /* Compatibility for possible missing IPv6 declarations */
10*a466cc55SCy Schubert #include "../util-internal.h"
11*a466cc55SCy Schubert 
12*a466cc55SCy Schubert #include <stdio.h>
13*a466cc55SCy Schubert #include <stdlib.h>
14*a466cc55SCy Schubert #include <string.h>
15*a466cc55SCy Schubert 
16*a466cc55SCy Schubert #include <sys/types.h>
17*a466cc55SCy Schubert #include <sys/stat.h>
18*a466cc55SCy Schubert 
19*a466cc55SCy Schubert #ifdef _WIN32
20*a466cc55SCy Schubert #include <winsock2.h>
21*a466cc55SCy Schubert #include <ws2tcpip.h>
22*a466cc55SCy Schubert #include <windows.h>
23*a466cc55SCy Schubert #include <getopt.h>
24*a466cc55SCy Schubert #include <io.h>
25*a466cc55SCy Schubert #include <fcntl.h>
26*a466cc55SCy Schubert #ifndef S_ISDIR
27*a466cc55SCy Schubert #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
28*a466cc55SCy Schubert #endif
29*a466cc55SCy Schubert #else /* !_WIN32 */
30*a466cc55SCy Schubert #include <sys/stat.h>
31*a466cc55SCy Schubert #include <sys/socket.h>
32*a466cc55SCy Schubert #include <fcntl.h>
33*a466cc55SCy Schubert #include <unistd.h>
34*a466cc55SCy Schubert #include <dirent.h>
35*a466cc55SCy Schubert #endif /* _WIN32 */
36*a466cc55SCy Schubert #include <signal.h>
37*a466cc55SCy Schubert 
38*a466cc55SCy Schubert #ifdef EVENT__HAVE_SYS_UN_H
39*a466cc55SCy Schubert #include <sys/un.h>
40*a466cc55SCy Schubert #endif
41*a466cc55SCy Schubert #ifdef EVENT__HAVE_AFUNIX_H
42*a466cc55SCy Schubert #include <afunix.h>
43*a466cc55SCy Schubert #endif
44*a466cc55SCy Schubert 
45*a466cc55SCy Schubert #include <event2/event.h>
46*a466cc55SCy Schubert #include <event2/http.h>
47*a466cc55SCy Schubert #include <event2/listener.h>
48*a466cc55SCy Schubert #include <event2/buffer.h>
49*a466cc55SCy Schubert #include <event2/util.h>
50*a466cc55SCy Schubert #include <event2/keyvalq_struct.h>
51*a466cc55SCy Schubert 
52*a466cc55SCy Schubert #ifdef _WIN32
53*a466cc55SCy Schubert #include <event2/thread.h>
54*a466cc55SCy Schubert #endif /* _WIN32 */
55*a466cc55SCy Schubert 
56*a466cc55SCy Schubert #ifdef EVENT__HAVE_NETINET_IN_H
57*a466cc55SCy Schubert #include <netinet/in.h>
58*a466cc55SCy Schubert # ifdef _XOPEN_SOURCE_EXTENDED
59*a466cc55SCy Schubert #  include <arpa/inet.h>
60*a466cc55SCy Schubert # endif
61*a466cc55SCy Schubert #endif
62*a466cc55SCy Schubert 
63*a466cc55SCy Schubert #ifdef _WIN32
64*a466cc55SCy Schubert #ifndef stat
65*a466cc55SCy Schubert #define stat _stat
66*a466cc55SCy Schubert #endif
67*a466cc55SCy Schubert #ifndef fstat
68*a466cc55SCy Schubert #define fstat _fstat
69*a466cc55SCy Schubert #endif
70*a466cc55SCy Schubert #ifndef open
71*a466cc55SCy Schubert #define open _open
72*a466cc55SCy Schubert #endif
73*a466cc55SCy Schubert #ifndef close
74*a466cc55SCy Schubert #define close _close
75*a466cc55SCy Schubert #endif
76*a466cc55SCy Schubert #ifndef O_RDONLY
77*a466cc55SCy Schubert #define O_RDONLY _O_RDONLY
78*a466cc55SCy Schubert #endif
79*a466cc55SCy Schubert #endif /* _WIN32 */
80*a466cc55SCy Schubert 
81*a466cc55SCy Schubert char uri_root[512];
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert static const struct table_entry {
84*a466cc55SCy Schubert 	const char *extension;
85*a466cc55SCy Schubert 	const char *content_type;
86*a466cc55SCy Schubert } content_type_table[] = {
87*a466cc55SCy Schubert 	{ "txt", "text/plain" },
88*a466cc55SCy Schubert 	{ "c", "text/plain" },
89*a466cc55SCy Schubert 	{ "h", "text/plain" },
90*a466cc55SCy Schubert 	{ "html", "text/html" },
91*a466cc55SCy Schubert 	{ "htm", "text/htm" },
92*a466cc55SCy Schubert 	{ "css", "text/css" },
93*a466cc55SCy Schubert 	{ "gif", "image/gif" },
94*a466cc55SCy Schubert 	{ "jpg", "image/jpeg" },
95*a466cc55SCy Schubert 	{ "jpeg", "image/jpeg" },
96*a466cc55SCy Schubert 	{ "png", "image/png" },
97*a466cc55SCy Schubert 	{ "pdf", "application/pdf" },
98*a466cc55SCy Schubert 	{ "ps", "application/postscript" },
99*a466cc55SCy Schubert 	{ NULL, NULL },
100*a466cc55SCy Schubert };
101*a466cc55SCy Schubert 
102*a466cc55SCy Schubert struct options {
103*a466cc55SCy Schubert 	int port;
104*a466cc55SCy Schubert 	int iocp;
105*a466cc55SCy Schubert 	int verbose;
106*a466cc55SCy Schubert 
107*a466cc55SCy Schubert 	int unlink;
108*a466cc55SCy Schubert 	const char *unixsock;
109*a466cc55SCy Schubert 	const char *docroot;
110*a466cc55SCy Schubert };
111*a466cc55SCy Schubert 
112*a466cc55SCy Schubert /* Try to guess a good content-type for 'path' */
113*a466cc55SCy Schubert static const char *
guess_content_type(const char * path)114*a466cc55SCy Schubert guess_content_type(const char *path)
115*a466cc55SCy Schubert {
116*a466cc55SCy Schubert 	const char *last_period, *extension;
117*a466cc55SCy Schubert 	const struct table_entry *ent;
118*a466cc55SCy Schubert 	last_period = strrchr(path, '.');
119*a466cc55SCy Schubert 	if (!last_period || strchr(last_period, '/'))
120*a466cc55SCy Schubert 		goto not_found; /* no exension */
121*a466cc55SCy Schubert 	extension = last_period + 1;
122*a466cc55SCy Schubert 	for (ent = &content_type_table[0]; ent->extension; ++ent) {
123*a466cc55SCy Schubert 		if (!evutil_ascii_strcasecmp(ent->extension, extension))
124*a466cc55SCy Schubert 			return ent->content_type;
125*a466cc55SCy Schubert 	}
126*a466cc55SCy Schubert 
127*a466cc55SCy Schubert not_found:
128*a466cc55SCy Schubert 	return "application/misc";
129*a466cc55SCy Schubert }
130*a466cc55SCy Schubert 
131*a466cc55SCy Schubert /* Callback used for the /dump URI, and for every non-GET request:
132*a466cc55SCy Schubert  * dumps all information to stdout and gives back a trivial 200 ok */
133*a466cc55SCy Schubert static void
dump_request_cb(struct evhttp_request * req,void * arg)134*a466cc55SCy Schubert dump_request_cb(struct evhttp_request *req, void *arg)
135*a466cc55SCy Schubert {
136*a466cc55SCy Schubert 	const char *cmdtype;
137*a466cc55SCy Schubert 	struct evkeyvalq *headers;
138*a466cc55SCy Schubert 	struct evkeyval *header;
139*a466cc55SCy Schubert 	struct evbuffer *buf;
140*a466cc55SCy Schubert 
141*a466cc55SCy Schubert 	switch (evhttp_request_get_command(req)) {
142*a466cc55SCy Schubert 	case EVHTTP_REQ_GET: cmdtype = "GET"; break;
143*a466cc55SCy Schubert 	case EVHTTP_REQ_POST: cmdtype = "POST"; break;
144*a466cc55SCy Schubert 	case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
145*a466cc55SCy Schubert 	case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
146*a466cc55SCy Schubert 	case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
147*a466cc55SCy Schubert 	case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
148*a466cc55SCy Schubert 	case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
149*a466cc55SCy Schubert 	case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
150*a466cc55SCy Schubert 	case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
151*a466cc55SCy Schubert 	default: cmdtype = "unknown"; break;
152*a466cc55SCy Schubert 	}
153*a466cc55SCy Schubert 
154*a466cc55SCy Schubert 	printf("Received a %s request for %s\nHeaders:\n",
155*a466cc55SCy Schubert 	    cmdtype, evhttp_request_get_uri(req));
156*a466cc55SCy Schubert 
157*a466cc55SCy Schubert 	headers = evhttp_request_get_input_headers(req);
158*a466cc55SCy Schubert 	for (header = headers->tqh_first; header;
159*a466cc55SCy Schubert 	    header = header->next.tqe_next) {
160*a466cc55SCy Schubert 		printf("  %s: %s\n", header->key, header->value);
161*a466cc55SCy Schubert 	}
162*a466cc55SCy Schubert 
163*a466cc55SCy Schubert 	buf = evhttp_request_get_input_buffer(req);
164*a466cc55SCy Schubert 	puts("Input data: <<<");
165*a466cc55SCy Schubert 	while (evbuffer_get_length(buf)) {
166*a466cc55SCy Schubert 		int n;
167*a466cc55SCy Schubert 		char cbuf[128];
168*a466cc55SCy Schubert 		n = evbuffer_remove(buf, cbuf, sizeof(cbuf));
169*a466cc55SCy Schubert 		if (n > 0)
170*a466cc55SCy Schubert 			(void) fwrite(cbuf, 1, n, stdout);
171*a466cc55SCy Schubert 	}
172*a466cc55SCy Schubert 	puts(">>>");
173*a466cc55SCy Schubert 
174*a466cc55SCy Schubert 	evhttp_send_reply(req, 200, "OK", NULL);
175*a466cc55SCy Schubert }
176*a466cc55SCy Schubert 
177*a466cc55SCy Schubert /* This callback gets invoked when we get any http request that doesn't match
178*a466cc55SCy Schubert  * any other callback.  Like any evhttp server callback, it has a simple job:
179*a466cc55SCy Schubert  * it must eventually call evhttp_send_error() or evhttp_send_reply().
180*a466cc55SCy Schubert  */
181*a466cc55SCy Schubert static void
send_document_cb(struct evhttp_request * req,void * arg)182*a466cc55SCy Schubert send_document_cb(struct evhttp_request *req, void *arg)
183*a466cc55SCy Schubert {
184*a466cc55SCy Schubert 	struct evbuffer *evb = NULL;
185*a466cc55SCy Schubert 	struct options *o = arg;
186*a466cc55SCy Schubert 	const char *uri = evhttp_request_get_uri(req);
187*a466cc55SCy Schubert 	struct evhttp_uri *decoded = NULL;
188*a466cc55SCy Schubert 	const char *path;
189*a466cc55SCy Schubert 	char *decoded_path;
190*a466cc55SCy Schubert 	char *whole_path = NULL;
191*a466cc55SCy Schubert 	size_t len;
192*a466cc55SCy Schubert 	int fd = -1;
193*a466cc55SCy Schubert 	struct stat st;
194*a466cc55SCy Schubert 
195*a466cc55SCy Schubert 	if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
196*a466cc55SCy Schubert 		dump_request_cb(req, arg);
197*a466cc55SCy Schubert 		return;
198*a466cc55SCy Schubert 	}
199*a466cc55SCy Schubert 
200*a466cc55SCy Schubert 	printf("Got a GET request for <%s>\n",  uri);
201*a466cc55SCy Schubert 
202*a466cc55SCy Schubert 	/* Decode the URI */
203*a466cc55SCy Schubert 	decoded = evhttp_uri_parse(uri);
204*a466cc55SCy Schubert 	if (!decoded) {
205*a466cc55SCy Schubert 		printf("It's not a good URI. Sending BADREQUEST\n");
206*a466cc55SCy Schubert 		evhttp_send_error(req, HTTP_BADREQUEST, 0);
207*a466cc55SCy Schubert 		return;
208*a466cc55SCy Schubert 	}
209*a466cc55SCy Schubert 
210*a466cc55SCy Schubert 	/* Let's see what path the user asked for. */
211*a466cc55SCy Schubert 	path = evhttp_uri_get_path(decoded);
212*a466cc55SCy Schubert 	if (!path) path = "/";
213*a466cc55SCy Schubert 
214*a466cc55SCy Schubert 	/* We need to decode it, to see what path the user really wanted. */
215*a466cc55SCy Schubert 	decoded_path = evhttp_uridecode(path, 0, NULL);
216*a466cc55SCy Schubert 	if (decoded_path == NULL)
217*a466cc55SCy Schubert 		goto err;
218*a466cc55SCy Schubert 	/* Don't allow any ".."s in the path, to avoid exposing stuff outside
219*a466cc55SCy Schubert 	 * of the docroot.  This test is both overzealous and underzealous:
220*a466cc55SCy Schubert 	 * it forbids aceptable paths like "/this/one..here", but it doesn't
221*a466cc55SCy Schubert 	 * do anything to prevent symlink following." */
222*a466cc55SCy Schubert 	if (strstr(decoded_path, ".."))
223*a466cc55SCy Schubert 		goto err;
224*a466cc55SCy Schubert 
225*a466cc55SCy Schubert 	len = strlen(decoded_path)+strlen(o->docroot)+2;
226*a466cc55SCy Schubert 	if (!(whole_path = malloc(len))) {
227*a466cc55SCy Schubert 		perror("malloc");
228*a466cc55SCy Schubert 		goto err;
229*a466cc55SCy Schubert 	}
230*a466cc55SCy Schubert 	evutil_snprintf(whole_path, len, "%s/%s", o->docroot, decoded_path);
231*a466cc55SCy Schubert 
232*a466cc55SCy Schubert 	if (stat(whole_path, &st)<0) {
233*a466cc55SCy Schubert 		goto err;
234*a466cc55SCy Schubert 	}
235*a466cc55SCy Schubert 
236*a466cc55SCy Schubert 	/* This holds the content we're sending. */
237*a466cc55SCy Schubert 	evb = evbuffer_new();
238*a466cc55SCy Schubert 
239*a466cc55SCy Schubert 	if (S_ISDIR(st.st_mode)) {
240*a466cc55SCy Schubert 		/* If it's a directory, read the comments and make a little
241*a466cc55SCy Schubert 		 * index page */
242*a466cc55SCy Schubert #ifdef _WIN32
243*a466cc55SCy Schubert 		HANDLE d;
244*a466cc55SCy Schubert 		WIN32_FIND_DATAA ent;
245*a466cc55SCy Schubert 		char *pattern;
246*a466cc55SCy Schubert 		size_t dirlen;
247*a466cc55SCy Schubert #else
248*a466cc55SCy Schubert 		DIR *d;
249*a466cc55SCy Schubert 		struct dirent *ent;
250*a466cc55SCy Schubert #endif
251*a466cc55SCy Schubert 		const char *trailing_slash = "";
252*a466cc55SCy Schubert 
253*a466cc55SCy Schubert 		if (!strlen(path) || path[strlen(path)-1] != '/')
254*a466cc55SCy Schubert 			trailing_slash = "/";
255*a466cc55SCy Schubert 
256*a466cc55SCy Schubert #ifdef _WIN32
257*a466cc55SCy Schubert 		dirlen = strlen(whole_path);
258*a466cc55SCy Schubert 		pattern = malloc(dirlen+3);
259*a466cc55SCy Schubert 		memcpy(pattern, whole_path, dirlen);
260*a466cc55SCy Schubert 		pattern[dirlen] = '\\';
261*a466cc55SCy Schubert 		pattern[dirlen+1] = '*';
262*a466cc55SCy Schubert 		pattern[dirlen+2] = '\0';
263*a466cc55SCy Schubert 		d = FindFirstFileA(pattern, &ent);
264*a466cc55SCy Schubert 		free(pattern);
265*a466cc55SCy Schubert 		if (d == INVALID_HANDLE_VALUE)
266*a466cc55SCy Schubert 			goto err;
267*a466cc55SCy Schubert #else
268*a466cc55SCy Schubert 		if (!(d = opendir(whole_path)))
269*a466cc55SCy Schubert 			goto err;
270*a466cc55SCy Schubert #endif
271*a466cc55SCy Schubert 
272*a466cc55SCy Schubert 		evbuffer_add_printf(evb,
273*a466cc55SCy Schubert                     "<!DOCTYPE html>\n"
274*a466cc55SCy Schubert                     "<html>\n <head>\n"
275*a466cc55SCy Schubert                     "  <meta charset='utf-8'>\n"
276*a466cc55SCy Schubert 		    "  <title>%s</title>\n"
277*a466cc55SCy Schubert 		    "  <base href='%s%s'>\n"
278*a466cc55SCy Schubert 		    " </head>\n"
279*a466cc55SCy Schubert 		    " <body>\n"
280*a466cc55SCy Schubert 		    "  <h1>%s</h1>\n"
281*a466cc55SCy Schubert 		    "  <ul>\n",
282*a466cc55SCy Schubert 		    decoded_path, /* XXX html-escape this. */
283*a466cc55SCy Schubert 		    path, /* XXX html-escape this? */
284*a466cc55SCy Schubert 		    trailing_slash,
285*a466cc55SCy Schubert 		    decoded_path /* XXX html-escape this */);
286*a466cc55SCy Schubert #ifdef _WIN32
287*a466cc55SCy Schubert 		do {
288*a466cc55SCy Schubert 			const char *name = ent.cFileName;
289*a466cc55SCy Schubert #else
290*a466cc55SCy Schubert 		while ((ent = readdir(d))) {
291*a466cc55SCy Schubert 			const char *name = ent->d_name;
292*a466cc55SCy Schubert #endif
293*a466cc55SCy Schubert 			evbuffer_add_printf(evb,
294*a466cc55SCy Schubert 			    "    <li><a href=\"%s\">%s</a>\n",
295*a466cc55SCy Schubert 			    name, name);/* XXX escape this */
296*a466cc55SCy Schubert #ifdef _WIN32
297*a466cc55SCy Schubert 		} while (FindNextFileA(d, &ent));
298*a466cc55SCy Schubert #else
299*a466cc55SCy Schubert 		}
300*a466cc55SCy Schubert #endif
301*a466cc55SCy Schubert 		evbuffer_add_printf(evb, "</ul></body></html>\n");
302*a466cc55SCy Schubert #ifdef _WIN32
303*a466cc55SCy Schubert 		FindClose(d);
304*a466cc55SCy Schubert #else
305*a466cc55SCy Schubert 		closedir(d);
306*a466cc55SCy Schubert #endif
307*a466cc55SCy Schubert 		evhttp_add_header(evhttp_request_get_output_headers(req),
308*a466cc55SCy Schubert 		    "Content-Type", "text/html");
309*a466cc55SCy Schubert 	} else {
310*a466cc55SCy Schubert 		/* Otherwise it's a file; add it to the buffer to get
311*a466cc55SCy Schubert 		 * sent via sendfile */
312*a466cc55SCy Schubert 		const char *type = guess_content_type(decoded_path);
313*a466cc55SCy Schubert 		if ((fd = open(whole_path, O_RDONLY)) < 0) {
314*a466cc55SCy Schubert 			perror("open");
315*a466cc55SCy Schubert 			goto err;
316*a466cc55SCy Schubert 		}
317*a466cc55SCy Schubert 
318*a466cc55SCy Schubert 		if (fstat(fd, &st)<0) {
319*a466cc55SCy Schubert 			/* Make sure the length still matches, now that we
320*a466cc55SCy Schubert 			 * opened the file :/ */
321*a466cc55SCy Schubert 			perror("fstat");
322*a466cc55SCy Schubert 			goto err;
323*a466cc55SCy Schubert 		}
324*a466cc55SCy Schubert 		evhttp_add_header(evhttp_request_get_output_headers(req),
325*a466cc55SCy Schubert 		    "Content-Type", type);
326*a466cc55SCy Schubert 		evbuffer_add_file(evb, fd, 0, st.st_size);
327*a466cc55SCy Schubert 	}
328*a466cc55SCy Schubert 
329*a466cc55SCy Schubert 	evhttp_send_reply(req, 200, "OK", evb);
330*a466cc55SCy Schubert 	goto done;
331*a466cc55SCy Schubert err:
332*a466cc55SCy Schubert 	evhttp_send_error(req, 404, "Document was not found");
333*a466cc55SCy Schubert 	if (fd>=0)
334*a466cc55SCy Schubert 		close(fd);
335*a466cc55SCy Schubert done:
336*a466cc55SCy Schubert 	if (decoded)
337*a466cc55SCy Schubert 		evhttp_uri_free(decoded);
338*a466cc55SCy Schubert 	if (decoded_path)
339*a466cc55SCy Schubert 		free(decoded_path);
340*a466cc55SCy Schubert 	if (whole_path)
341*a466cc55SCy Schubert 		free(whole_path);
342*a466cc55SCy Schubert 	if (evb)
343*a466cc55SCy Schubert 		evbuffer_free(evb);
344*a466cc55SCy Schubert }
345*a466cc55SCy Schubert 
346*a466cc55SCy Schubert static void
print_usage(FILE * out,const char * prog,int exit_code)347*a466cc55SCy Schubert print_usage(FILE *out, const char *prog, int exit_code)
348*a466cc55SCy Schubert {
349*a466cc55SCy Schubert 	fprintf(out,
350*a466cc55SCy Schubert 		"Syntax: %s [ OPTS ] <docroot>\n"
351*a466cc55SCy Schubert 		" -p      - port\n"
352*a466cc55SCy Schubert 		" -U      - bind to unix socket\n"
353*a466cc55SCy Schubert 		" -u      - unlink unix socket before bind\n"
354*a466cc55SCy Schubert 		" -I      - IOCP\n"
355*a466cc55SCy Schubert 		" -v      - verbosity, enables libevent debug logging too\n", prog);
356*a466cc55SCy Schubert 	exit(exit_code);
357*a466cc55SCy Schubert }
358*a466cc55SCy Schubert static struct options
parse_opts(int argc,char ** argv)359*a466cc55SCy Schubert parse_opts(int argc, char **argv)
360*a466cc55SCy Schubert {
361*a466cc55SCy Schubert 	struct options o;
362*a466cc55SCy Schubert 	int opt;
363*a466cc55SCy Schubert 
364*a466cc55SCy Schubert 	memset(&o, 0, sizeof(o));
365*a466cc55SCy Schubert 
366*a466cc55SCy Schubert 	while ((opt = getopt(argc, argv, "hp:U:uIv")) != -1) {
367*a466cc55SCy Schubert 		switch (opt) {
368*a466cc55SCy Schubert 			case 'p': o.port = atoi(optarg); break;
369*a466cc55SCy Schubert 			case 'U': o.unixsock = optarg; break;
370*a466cc55SCy Schubert 			case 'u': o.unlink = 1; break;
371*a466cc55SCy Schubert 			case 'I': o.iocp = 1; break;
372*a466cc55SCy Schubert 			case 'v': ++o.verbose; break;
373*a466cc55SCy Schubert 			case 'h': print_usage(stdout, argv[0], 0); break;
374*a466cc55SCy Schubert 			default : fprintf(stderr, "Unknown option %c\n", opt); break;
375*a466cc55SCy Schubert 		}
376*a466cc55SCy Schubert 	}
377*a466cc55SCy Schubert 
378*a466cc55SCy Schubert 	if (optind >= argc || (argc - optind) > 1) {
379*a466cc55SCy Schubert 		print_usage(stdout, argv[0], 1);
380*a466cc55SCy Schubert 	}
381*a466cc55SCy Schubert 	o.docroot = argv[optind];
382*a466cc55SCy Schubert 
383*a466cc55SCy Schubert 	return o;
384*a466cc55SCy Schubert }
385*a466cc55SCy Schubert 
386*a466cc55SCy Schubert static void
do_term(int sig,short events,void * arg)387*a466cc55SCy Schubert do_term(int sig, short events, void *arg)
388*a466cc55SCy Schubert {
389*a466cc55SCy Schubert 	struct event_base *base = arg;
390*a466cc55SCy Schubert 	event_base_loopbreak(base);
391*a466cc55SCy Schubert 	fprintf(stderr, "Got %i, Terminating\n", sig);
392*a466cc55SCy Schubert }
393*a466cc55SCy Schubert 
394*a466cc55SCy Schubert static int
display_listen_sock(struct evhttp_bound_socket * handle)395*a466cc55SCy Schubert display_listen_sock(struct evhttp_bound_socket *handle)
396*a466cc55SCy Schubert {
397*a466cc55SCy Schubert 	struct sockaddr_storage ss;
398*a466cc55SCy Schubert 	evutil_socket_t fd;
399*a466cc55SCy Schubert 	ev_socklen_t socklen = sizeof(ss);
400*a466cc55SCy Schubert 	char addrbuf[128];
401*a466cc55SCy Schubert 	void *inaddr;
402*a466cc55SCy Schubert 	const char *addr;
403*a466cc55SCy Schubert 	int got_port = -1;
404*a466cc55SCy Schubert 
405*a466cc55SCy Schubert 	fd = evhttp_bound_socket_get_fd(handle);
406*a466cc55SCy Schubert 	memset(&ss, 0, sizeof(ss));
407*a466cc55SCy Schubert 	if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
408*a466cc55SCy Schubert 		perror("getsockname() failed");
409*a466cc55SCy Schubert 		return 1;
410*a466cc55SCy Schubert 	}
411*a466cc55SCy Schubert 
412*a466cc55SCy Schubert 	if (ss.ss_family == AF_INET) {
413*a466cc55SCy Schubert 		got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
414*a466cc55SCy Schubert 		inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
415*a466cc55SCy Schubert 	} else if (ss.ss_family == AF_INET6) {
416*a466cc55SCy Schubert 		got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
417*a466cc55SCy Schubert 		inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
418*a466cc55SCy Schubert 	}
419*a466cc55SCy Schubert #ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
420*a466cc55SCy Schubert 	else if (ss.ss_family == AF_UNIX) {
421*a466cc55SCy Schubert 		printf("Listening on <%s>\n", ((struct sockaddr_un*)&ss)->sun_path);
422*a466cc55SCy Schubert 		return 0;
423*a466cc55SCy Schubert 	}
424*a466cc55SCy Schubert #endif
425*a466cc55SCy Schubert 	else {
426*a466cc55SCy Schubert 		fprintf(stderr, "Weird address family %d\n",
427*a466cc55SCy Schubert 		    ss.ss_family);
428*a466cc55SCy Schubert 		return 1;
429*a466cc55SCy Schubert 	}
430*a466cc55SCy Schubert 
431*a466cc55SCy Schubert 	addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
432*a466cc55SCy Schubert 	    sizeof(addrbuf));
433*a466cc55SCy Schubert 	if (addr) {
434*a466cc55SCy Schubert 		printf("Listening on %s:%d\n", addr, got_port);
435*a466cc55SCy Schubert 		evutil_snprintf(uri_root, sizeof(uri_root),
436*a466cc55SCy Schubert 		    "http://%s:%d",addr,got_port);
437*a466cc55SCy Schubert 	} else {
438*a466cc55SCy Schubert 		fprintf(stderr, "evutil_inet_ntop failed\n");
439*a466cc55SCy Schubert 		return 1;
440*a466cc55SCy Schubert 	}
441*a466cc55SCy Schubert 
442*a466cc55SCy Schubert 	return 0;
443*a466cc55SCy Schubert }
444*a466cc55SCy Schubert 
445*a466cc55SCy Schubert int
main(int argc,char ** argv)446*a466cc55SCy Schubert main(int argc, char **argv)
447*a466cc55SCy Schubert {
448*a466cc55SCy Schubert 	struct event_config *cfg = NULL;
449*a466cc55SCy Schubert 	struct event_base *base = NULL;
450*a466cc55SCy Schubert 	struct evhttp *http = NULL;
451*a466cc55SCy Schubert 	struct evhttp_bound_socket *handle = NULL;
452*a466cc55SCy Schubert 	struct evconnlistener *lev = NULL;
453*a466cc55SCy Schubert 	struct event *term = NULL;
454*a466cc55SCy Schubert 	struct options o = parse_opts(argc, argv);
455*a466cc55SCy Schubert 	int ret = 0;
456*a466cc55SCy Schubert 
457*a466cc55SCy Schubert #ifdef _WIN32
458*a466cc55SCy Schubert 	{
459*a466cc55SCy Schubert 		WORD wVersionRequested;
460*a466cc55SCy Schubert 		WSADATA wsaData;
461*a466cc55SCy Schubert 		wVersionRequested = MAKEWORD(2, 2);
462*a466cc55SCy Schubert 		WSAStartup(wVersionRequested, &wsaData);
463*a466cc55SCy Schubert 	}
464*a466cc55SCy Schubert #else
465*a466cc55SCy Schubert 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
466*a466cc55SCy Schubert 		ret = 1;
467*a466cc55SCy Schubert 		goto err;
468*a466cc55SCy Schubert 	}
469*a466cc55SCy Schubert #endif
470*a466cc55SCy Schubert 
471*a466cc55SCy Schubert 	setbuf(stdout, NULL);
472*a466cc55SCy Schubert 	setbuf(stderr, NULL);
473*a466cc55SCy Schubert 
474*a466cc55SCy Schubert 	/** Read env like in regress */
475*a466cc55SCy Schubert 	if (o.verbose || getenv("EVENT_DEBUG_LOGGING_ALL"))
476*a466cc55SCy Schubert 		event_enable_debug_logging(EVENT_DBG_ALL);
477*a466cc55SCy Schubert 
478*a466cc55SCy Schubert 	cfg = event_config_new();
479*a466cc55SCy Schubert #ifdef _WIN32
480*a466cc55SCy Schubert 	if (o.iocp) {
481*a466cc55SCy Schubert #ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
482*a466cc55SCy Schubert 		evthread_use_windows_threads();
483*a466cc55SCy Schubert 		event_config_set_num_cpus_hint(cfg, 8);
484*a466cc55SCy Schubert #endif
485*a466cc55SCy Schubert 		event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
486*a466cc55SCy Schubert 	}
487*a466cc55SCy Schubert #endif
488*a466cc55SCy Schubert 
489*a466cc55SCy Schubert 	base = event_base_new_with_config(cfg);
490*a466cc55SCy Schubert 	if (!base) {
491*a466cc55SCy Schubert 		fprintf(stderr, "Couldn't create an event_base: exiting\n");
492*a466cc55SCy Schubert 		ret = 1;
493*a466cc55SCy Schubert 	}
494*a466cc55SCy Schubert 	event_config_free(cfg);
495*a466cc55SCy Schubert 	cfg = NULL;
496*a466cc55SCy Schubert 
497*a466cc55SCy Schubert 	/* Create a new evhttp object to handle requests. */
498*a466cc55SCy Schubert 	http = evhttp_new(base);
499*a466cc55SCy Schubert 	if (!http) {
500*a466cc55SCy Schubert 		fprintf(stderr, "couldn't create evhttp. Exiting.\n");
501*a466cc55SCy Schubert 		ret = 1;
502*a466cc55SCy Schubert 	}
503*a466cc55SCy Schubert 
504*a466cc55SCy Schubert 	/* The /dump URI will dump all requests to stdout and say 200 ok. */
505*a466cc55SCy Schubert 	evhttp_set_cb(http, "/dump", dump_request_cb, NULL);
506*a466cc55SCy Schubert 
507*a466cc55SCy Schubert 	/* We want to accept arbitrary requests, so we need to set a "generic"
508*a466cc55SCy Schubert 	 * cb.  We can also add callbacks for specific paths. */
509*a466cc55SCy Schubert 	evhttp_set_gencb(http, send_document_cb, &o);
510*a466cc55SCy Schubert 
511*a466cc55SCy Schubert 	if (o.unixsock) {
512*a466cc55SCy Schubert #ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
513*a466cc55SCy Schubert 		struct sockaddr_un addr;
514*a466cc55SCy Schubert 
515*a466cc55SCy Schubert 		if (o.unlink && (unlink(o.unixsock) && errno != ENOENT)) {
516*a466cc55SCy Schubert 			perror(o.unixsock);
517*a466cc55SCy Schubert 			ret = 1;
518*a466cc55SCy Schubert 			goto err;
519*a466cc55SCy Schubert 		}
520*a466cc55SCy Schubert 
521*a466cc55SCy Schubert 		addr.sun_family = AF_UNIX;
522*a466cc55SCy Schubert 		strcpy(addr.sun_path, o.unixsock);
523*a466cc55SCy Schubert 
524*a466cc55SCy Schubert 		lev = evconnlistener_new_bind(base, NULL, NULL,
525*a466cc55SCy Schubert 			LEV_OPT_CLOSE_ON_FREE, -1,
526*a466cc55SCy Schubert 			(struct sockaddr *)&addr, sizeof(addr));
527*a466cc55SCy Schubert 		if (!lev) {
528*a466cc55SCy Schubert 			perror("Cannot create listener");
529*a466cc55SCy Schubert 			ret = 1;
530*a466cc55SCy Schubert 			goto err;
531*a466cc55SCy Schubert 		}
532*a466cc55SCy Schubert 
533*a466cc55SCy Schubert 		handle = evhttp_bind_listener(http, lev);
534*a466cc55SCy Schubert 		if (!handle) {
535*a466cc55SCy Schubert 			fprintf(stderr, "couldn't bind to %s. Exiting.\n", o.unixsock);
536*a466cc55SCy Schubert 			ret = 1;
537*a466cc55SCy Schubert 			goto err;
538*a466cc55SCy Schubert 		}
539*a466cc55SCy Schubert #else /* !EVENT__HAVE_STRUCT_SOCKADDR_UN */
540*a466cc55SCy Schubert 		fprintf(stderr, "-U is not supported on this platform. Exiting.\n");
541*a466cc55SCy Schubert 		ret = 1;
542*a466cc55SCy Schubert 		goto err;
543*a466cc55SCy Schubert #endif /* EVENT__HAVE_STRUCT_SOCKADDR_UN */
544*a466cc55SCy Schubert 	}
545*a466cc55SCy Schubert 	else {
546*a466cc55SCy Schubert 		handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port);
547*a466cc55SCy Schubert 		if (!handle) {
548*a466cc55SCy Schubert 			fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port);
549*a466cc55SCy Schubert 			ret = 1;
550*a466cc55SCy Schubert 			goto err;
551*a466cc55SCy Schubert 		}
552*a466cc55SCy Schubert 	}
553*a466cc55SCy Schubert 
554*a466cc55SCy Schubert 	if (display_listen_sock(handle)) {
555*a466cc55SCy Schubert 		ret = 1;
556*a466cc55SCy Schubert 		goto err;
557*a466cc55SCy Schubert 	}
558*a466cc55SCy Schubert 
559*a466cc55SCy Schubert 	term = evsignal_new(base, SIGINT, do_term, base);
560*a466cc55SCy Schubert 	if (!term)
561*a466cc55SCy Schubert 		goto err;
562*a466cc55SCy Schubert 	if (event_add(term, NULL))
563*a466cc55SCy Schubert 		goto err;
564*a466cc55SCy Schubert 
565*a466cc55SCy Schubert 	event_base_dispatch(base);
566*a466cc55SCy Schubert 
567*a466cc55SCy Schubert #ifdef _WIN32
568*a466cc55SCy Schubert 	WSACleanup();
569*a466cc55SCy Schubert #endif
570*a466cc55SCy Schubert 
571*a466cc55SCy Schubert err:
572*a466cc55SCy Schubert 	if (cfg)
573*a466cc55SCy Schubert 		event_config_free(cfg);
574*a466cc55SCy Schubert 	if (http)
575*a466cc55SCy Schubert 		evhttp_free(http);
576*a466cc55SCy Schubert 	if (term)
577*a466cc55SCy Schubert 		event_free(term);
578*a466cc55SCy Schubert 	if (base)
579*a466cc55SCy Schubert 		event_base_free(base);
580*a466cc55SCy Schubert 
581*a466cc55SCy Schubert 	return ret;
582*a466cc55SCy Schubert }
583