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