ipc/chromium/src/third_party/libevent/sample/http-server.c

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial