xref: /libevent-2.1.12/sample/http-server.c (revision 4eb281c8)
1 /*
2   A trivial static http webserver using Libevent's evhttp.
3 
4   This is not the best code in the world, and it does some fairly stupid stuff
5   that you would never want to do in a production webserver. Caveat hackor!
6 
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 
16 #ifdef WIN32
17 #include <winsock2.h>
18 #include <ws2tcpip.h>
19 #include <windows.h>
20 #include <io.h>
21 #include <fcntl.h>
22 #else
23 #include <sys/stat.h>
24 #include <sys/socket.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #endif
30 
31 #include <event2/event.h>
32 #include <event2/http.h>
33 #include <event2/buffer.h>
34 #include <event2/util.h>
35 #include <event2/keyvalq_struct.h>
36 
37 #ifdef _EVENT_HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 # ifdef _XOPEN_SOURCE_EXTENDED
40 #  include <arpa/inet.h>
41 # endif
42 #endif
43 
44 /* Compatibility for possible missing IPv6 declarations */
45 #include "../util-internal.h"
46 
47 #ifdef WIN32
48 #define stat _stat
49 #define fstat _fstat
50 #define open _open
51 #define close _close
52 #define O_RDONLY _O_RDONLY
53 #endif
54 
55 char uri_root[512];
56 
57 static const struct table_entry {
58 	const char *extension;
59 	const char *content_type;
60 } content_type_table[] = {
61 	{ "txt", "text/plain" },
62 	{ "c", "text/plain" },
63 	{ "h", "text/plain" },
64 	{ "html", "text/html" },
65 	{ "htm", "text/htm" },
66 	{ "css", "text/css" },
67 	{ "gif", "image/gif" },
68 	{ "jpg", "image/jpeg" },
69 	{ "jpeg", "image/jpeg" },
70 	{ "png", "image/png" },
71 	{ "pdf", "application/pdf" },
72 	{ "ps", "application/postsript" },
73 	{ NULL, NULL },
74 };
75 
76 /* Try to guess a good content-type for 'path' */
77 static const char *
78 guess_content_type(const char *path)
79 {
80 	const char *last_period, *extension;
81 	const struct table_entry *ent;
82 	last_period = strrchr(path, '.');
83 	if (!last_period || strchr(last_period, '/'))
84 		goto not_found; /* no exension */
85 	extension = last_period + 1;
86 	for (ent = &content_type_table[0]; ent->extension; ++ent) {
87 		if (!evutil_ascii_strcasecmp(ent->extension, extension))
88 			return ent->content_type;
89 	}
90 
91 not_found:
92 	return "application/misc";
93 }
94 
95 /* Callback used for the /dump URI, and for every non-GET request:
96  * dumps all information to stdout and gives back a trivial 200 ok */
97 static void
98 dump_request_cb(struct evhttp_request *req, void *arg)
99 {
100 	const char *cmdtype;
101 	struct evkeyvalq *headers;
102 	struct evkeyval *header;
103 	struct evbuffer *buf;
104 
105 	switch (evhttp_request_get_command(req)) {
106 	case EVHTTP_REQ_GET: cmdtype = "GET"; break;
107 	case EVHTTP_REQ_POST: cmdtype = "POST"; break;
108 	case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
109 	case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
110 	case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
111 	case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
112 	case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
113 	case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
114 	case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
115 	default: cmdtype = "unknown"; break;
116 	}
117 
118 	printf("Received a %s request for %s\nHeaders:\n",
119 	    cmdtype, evhttp_request_get_uri(req));
120 
121 	headers = evhttp_request_get_input_headers(req);
122 	for (header = headers->tqh_first; header;
123 	    header = header->next.tqe_next) {
124 		printf("  %s: %s\n", header->key, header->value);
125 	}
126 
127 	buf = evhttp_request_get_input_buffer(req);
128 	puts("Input data: <<<");
129 	while (evbuffer_get_length(buf)) {
130 		int n;
131 		char cbuf[128];
132 		n = evbuffer_remove(buf, cbuf, sizeof(buf)-1);
133 		fwrite(cbuf, 1, n, stdout);
134 	}
135 	puts(">>>");
136 
137 	evhttp_send_reply(req, 200, "OK", NULL);
138 }
139 
140 /* This callback gets invoked when we get any http request that doesn't match
141  * any other callback.  Like any evhttp server callback, it has a simple job:
142  * it must eventually call evhttp_send_error() or evhttp_send_reply().
143  */
144 static void
145 send_document_cb(struct evhttp_request *req, void *arg)
146 {
147 	struct evbuffer *evb;
148 	const char *docroot = arg;
149 	const char *uri = evhttp_request_get_uri(req);
150 	struct evhttp_uri *decoded = NULL;
151 	const char *path;
152 	char *decoded_path;
153 	char *whole_path = NULL;
154 	size_t len;
155 	int fd = -1;
156 	struct stat st;
157 
158 	if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
159 		dump_request_cb(req, arg);
160 		return;
161 	}
162 
163 	printf("Got a GET request for <%s>\n",  uri);
164 
165 	/* Decode the URI */
166 	decoded = evhttp_uri_parse(uri);
167 	if (!decoded) {
168 		printf("It's not a good URI. Sending BADREQUEST\n");
169 		evhttp_send_error(req, HTTP_BADREQUEST, 0);
170 		return;
171 	}
172 
173 	/* Let's see what path the user asked for. */
174 	path = evhttp_uri_get_path(decoded);
175 	if (!path) path = "/";
176 
177 	/* We need to decode it, to see what path the user really wanted. */
178 	decoded_path = evhttp_uridecode(path, 0, NULL);
179 	/* Don't allow any ".."s in the path, to avoid exposing stuff outside
180 	 * of the docroot.  This test is both overzealous and underzealous:
181 	 * it forbids aceptable paths like "/this/one..here", but it doesn't
182 	 * do anything to prevent symlink following." */
183 	if (strstr(decoded_path, ".."))
184 		goto err;
185 
186 	len = strlen(decoded_path)+strlen(docroot)+2;
187 	if (!(whole_path = malloc(len))) {
188 		perror("malloc");
189 		goto err;
190 	}
191 	evutil_snprintf(whole_path, len, "%s/%s", docroot, decoded_path);
192 
193 	if (stat(whole_path, &st)<0) {
194 		goto err;
195 	}
196 
197 	/* This holds the content we're sending. */
198 	evb = evbuffer_new();
199 
200 	if (S_ISDIR(st.st_mode)) {
201 		/* If it's a directory, read the comments and make a little
202 		 * index page */
203 #ifdef WIN32
204 		HANDLE d;
205 		WIN32_FIND_DATAA ent;
206 		char *pattern;
207 		size_t dirlen;
208 #else
209 		DIR *d;
210 		struct dirent *ent;
211 #endif
212 		const char *trailing_slash = "";
213 
214 		if (!strlen(path) || path[strlen(path)-1] != '/')
215 			trailing_slash = "/";
216 
217 #ifdef WIN32
218 		dirlen = strlen(whole_path);
219 		pattern = malloc(dirlen+3);
220 		memcpy(pattern, whole_path, dirlen);
221 		pattern[dirlen] = '\\';
222 		pattern[dirlen+1] = '*';
223 		pattern[dirlen+2] = '\0';
224 		d = FindFirstFileA(pattern, &ent);
225 		free(pattern);
226 		if (d == INVALID_HANDLE_VALUE)
227 			goto err;
228 #else
229 		if (!(d = opendir(whole_path)))
230 			goto err;
231 #endif
232 		close(fd);
233 
234 		evbuffer_add_printf(evb, "<html>\n <head>\n"
235 		    "  <title>%s</title>\n"
236 		    "  <base href='%s%s%s'>\n"
237 		    " </head>\n"
238 		    " <body>\n"
239 		    "  <h1>%s</h1>\n"
240 		    "  <ul>\n",
241 		    decoded_path, /* XXX html-escape this. */
242 		    uri_root, path, /* XXX html-escape this? */
243 		    trailing_slash,
244 		    decoded_path /* XXX html-escape this */);
245 #ifdef WIN32
246 		do {
247 			const char *name = ent.cFileName;
248 #else
249 		while ((ent = readdir(d))) {
250 			const char *name = ent->d_name;
251 #endif
252 			evbuffer_add_printf(evb,
253 			    "    <li><a href=\"%s\">%s</a>\n",
254 			    name, name);/* XXX escape this */
255 #ifdef WIN32
256 		} while (FindNextFileA(d, &ent));
257 #else
258 		}
259 #endif
260 		evbuffer_add_printf(evb, "</ul></body></html>\n");
261 #ifdef WIN32
262 		CloseHandle(d);
263 #else
264 		closedir(d);
265 #endif
266 		evhttp_add_header(evhttp_request_get_output_headers(req),
267 		    "Content-Type", "text/html");
268 	} else {
269 		/* Otherwise it's a file; add it to the buffer to get
270 		 * sent via sendfile */
271 		const char *type = guess_content_type(decoded_path);
272 		if ((fd = open(whole_path, O_RDONLY)) < 0) {
273 			perror("open");
274 			goto err;
275 		}
276 
277 		if (fstat(fd, &st)<0) {
278 			/* Make sure the length still matches, now that we
279 			 * opened the file :/ */
280 			perror("fstat");
281 			goto err;
282 		}
283 		evhttp_add_header(evhttp_request_get_output_headers(req),
284 		    "Content-Type", type);
285 		evbuffer_add_file(evb, fd, 0, st.st_size);
286 	}
287 
288 	evhttp_send_reply(req, 200, "OK", evb);
289 	evbuffer_free(evb);
290 	return;
291 err:
292 	evhttp_send_error(req, 404, "Document was not found");
293 	if (fd>=0)
294 		close(fd);
295 	if (decoded)
296 		evhttp_uri_free(decoded);
297 	if (decoded_path)
298 		free(decoded_path);
299 	if (whole_path)
300 		free(whole_path);
301 }
302 
303 static void
304 syntax(void)
305 {
306 	fprintf(stdout, "Syntax: http-server <docroot>\n");
307 }
308 
309 int
310 main(int argc, char **argv)
311 {
312 	struct event_base *base;
313 	struct evhttp *http;
314 	struct evhttp_bound_socket *handle;
315 
316 	unsigned short port = 0;
317 #ifdef WIN32
318 	WSADATA WSAData;
319 	WSAStartup(0x101, &WSAData);
320 #else
321 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
322 		return (1);
323 #endif
324 	if (argc < 2) {
325 		syntax();
326 		return 1;
327 	}
328 
329 	base = event_base_new();
330 	if (!base) {
331 		fprintf(stderr, "Couldn't create an event_base: exiting\n");
332 		return 1;
333 	}
334 
335 	/* Create a new evhttp object to handle requests. */
336 	http = evhttp_new(base);
337 	if (!http) {
338 		fprintf(stderr, "couldn't create evhttp. Exiting.\n");
339 		return 1;
340 	}
341 
342 	/* The /dump URI will dump all requests to stdout and say 200 ok. */
343 	evhttp_set_cb(http, "/dump", dump_request_cb, NULL);
344 
345 	/* We want to accept arbitrary requests, so we need to set a "generic"
346 	 * cb.  We can also add callbacks for specific paths. */
347 	evhttp_set_gencb(http, send_document_cb, argv[1]);
348 
349 	/* Now we tell the evhttp what port to listen on */
350 	handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port);
351 	if (!handle) {
352 		fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
353 		    (int)port);
354 		return 1;
355 	}
356 
357 	{
358 		/* Extract and display the address we're listening on. */
359 		struct sockaddr_storage ss;
360 		evutil_socket_t fd;
361 		ev_socklen_t socklen = sizeof(ss);
362 		char addrbuf[128];
363 		void *inaddr;
364 		const char *addr;
365 		int got_port = -1;
366 		fd = evhttp_bound_socket_get_fd(handle);
367 		memset(&ss, 0, sizeof(ss));
368 		if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
369 			perror("getsockname() failed");
370 			return 1;
371 		}
372 		if (ss.ss_family == AF_INET) {
373 			got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
374 			inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
375 		} else if (ss.ss_family == AF_INET6) {
376 			got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
377 			inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
378 		} else {
379 			fprintf(stderr, "Weird address family %d\n",
380 			    ss.ss_family);
381 			return 1;
382 		}
383 		addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
384 		    sizeof(addrbuf));
385 		if (addr) {
386 			printf("Listening on %s:%d\n", addr, got_port);
387 			evutil_snprintf(uri_root, sizeof(uri_root),
388 			    "http://%s:%d",addr,got_port);
389 		} else {
390 			fprintf(stderr, "evutil_inet_ntop failed\n");
391 			return 1;
392 		}
393 	}
394 
395 	event_base_dispatch(base);
396 
397 	return 0;
398 }
399