]> arthur.barton.de Git - netatalk.git/blobdiff - libevent/sample/http-server.c
Merge product-2-2
[netatalk.git] / libevent / sample / http-server.c
index f9e84d1c9e79bdee245d2fd4670ed05d7a1730fe..fb45579260f39697d2bb820fc9e66778bfe25669 100644 (file)
@@ -19,6 +19,9 @@
 #include <windows.h>
 #include <io.h>
 #include <fcntl.h>
+#ifndef S_ISDIR
+#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
+#endif
 #else
 #include <sys/stat.h>
 #include <sys/socket.h>
 
 #ifdef _EVENT_HAVE_NETINET_IN_H
 #include <netinet/in.h>
+# ifdef _XOPEN_SOURCE_EXTENDED
+#  include <arpa/inet.h>
+# endif
 #endif
 
+/* Compatibility for possible missing IPv6 declarations */
+#include "../util-internal.h"
+
 #ifdef WIN32
 #define stat _stat
 #define fstat _fstat
@@ -124,7 +133,8 @@ dump_request_cb(struct evhttp_request *req, void *arg)
                int n;
                char cbuf[128];
                n = evbuffer_remove(buf, cbuf, sizeof(buf)-1);
-               fwrite(cbuf, 1, n, stdout);
+               if (n > 0)
+                       (void) fwrite(cbuf, 1, n, stdout);
        }
        puts(">>>");
 
@@ -138,7 +148,7 @@ dump_request_cb(struct evhttp_request *req, void *arg)
 static void
 send_document_cb(struct evhttp_request *req, void *arg)
 {
-       struct evbuffer *evb;
+       struct evbuffer *evb = NULL;
        const char *docroot = arg;
        const char *uri = evhttp_request_get_uri(req);
        struct evhttp_uri *decoded = NULL;
@@ -170,6 +180,8 @@ send_document_cb(struct evhttp_request *req, void *arg)
 
        /* We need to decode it, to see what path the user really wanted. */
        decoded_path = evhttp_uridecode(path, 0, NULL);
+       if (decoded_path == NULL)
+               goto err;
        /* Don't allow any ".."s in the path, to avoid exposing stuff outside
         * of the docroot.  This test is both overzealous and underzealous:
         * it forbids aceptable paths like "/this/one..here", but it doesn't
@@ -223,7 +235,6 @@ send_document_cb(struct evhttp_request *req, void *arg)
                if (!(d = opendir(whole_path)))
                        goto err;
 #endif
-               close(fd);
 
                evbuffer_add_printf(evb, "<html>\n <head>\n"
                    "  <title>%s</title>\n"
@@ -280,18 +291,20 @@ send_document_cb(struct evhttp_request *req, void *arg)
        }
 
        evhttp_send_reply(req, 200, "OK", evb);
-       evbuffer_free(evb);
-       return;
+       goto done;
 err:
        evhttp_send_error(req, 404, "Document was not found");
        if (fd>=0)
                close(fd);
+done:
        if (decoded)
                evhttp_uri_free(decoded);
        if (decoded_path)
                free(decoded_path);
        if (whole_path)
                free(whole_path);
+       if (evb)
+               evbuffer_free(evb);
 }
 
 static void