summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2012-12-04 11:41:29 -0500
committerDan Winship <danw@gnome.org>2012-12-04 11:45:12 -0500
commit3f716d732b8473ffbb68daf192958dbe1702aa39 (patch)
treeee8b10aace34c45e103a37c96ded8da9f01fad20 /tests
parent9457c058330b394c7e531479c368bcacb998a01d (diff)
downloadlibsoup-3f716d732b8473ffbb68daf192958dbe1702aa39.tar.gz
libsoup-3f716d732b8473ffbb68daf192958dbe1702aa39.tar.bz2
libsoup-3f716d732b8473ffbb68daf192958dbe1702aa39.zip
tests/simple-httpd: use GMappedFile rather than mmap
(fixing a fd leak in the process)
Diffstat (limited to 'tests')
-rw-r--r--tests/simple-httpd.c57
1 files changed, 12 insertions, 45 deletions
diff --git a/tests/simple-httpd.c b/tests/simple-httpd.c
index 0f240ca8..75b7cd4a 100644
--- a/tests/simple-httpd.c
+++ b/tests/simple-httpd.c
@@ -15,25 +15,6 @@
#include <getopt.h>
#endif
-#ifdef HAVE_MMAP
-#include <sys/mman.h>
-#endif
-
-#ifdef HAVE_MMAP
-struct mapping {
- void *start;
- size_t length;
-};
-
-static void
-free_mapping (gpointer data)
-{
- struct mapping *mapping = data;
- munmap (mapping->start, mapping->length);
- g_slice_free (struct mapping, mapping);
-}
-#endif
-
static int
compare_strings (gconstpointer a, gconstpointer b)
{
@@ -91,7 +72,6 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path)
{
char *slash;
struct stat st;
- int fd;
if (stat (path, &st) == -1) {
if (errno == EPERM)
@@ -134,40 +114,27 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path)
return;
}
- fd = open (path, O_RDONLY);
- if (fd == -1) {
- soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
- return;
- }
-
if (msg->method == SOUP_METHOD_GET) {
-#ifdef HAVE_MMAP
- struct mapping *mapping = g_slice_new (struct mapping);
+ GMappedFile *mapping;
SoupBuffer *buffer;
- mapping->start = mmap (NULL, st.st_size, PROT_READ,
- MAP_PRIVATE, fd, 0);
- mapping->length = st.st_size;
- buffer = soup_buffer_new_with_owner (mapping->start,
- mapping->length,
- mapping, free_mapping);
+ mapping = g_mapped_file_new (path, FALSE, NULL);
+ if (!mapping) {
+ soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
+ g_mapped_file_get_length (mapping),
+ mapping, (GDestroyNotify)g_mapped_file_unref);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
-#else
- char *buf;
-
- buf = g_malloc (st.st_size);
- read (fd, buf, st.st_size);
- close (fd);
- soup_message_body_append (msg->response_body, SOUP_MEMORY_TAKE,
- buf, st.st_size);
-#endif
} else /* msg->method == SOUP_METHOD_HEAD */ {
char *length;
/* We could just use the same code for both GET and
- * HEAD. But we'll optimize and avoid the extra
- * malloc.
+ * HEAD (soup-message-server-io.c will fix things up).
+ * But we'll optimize and avoid the extra I/O.
*/
length = g_strdup_printf ("%lu", (gulong)st.st_size);
soup_message_headers_append (msg->response_headers,