Commit 53a3dd24 authored by Chun-wei Fan's avatar Chun-wei Fan

examples/simple-httpd.c: Make it work better for Windows

Replace dirent calls and POSIX file API calls with GLib calls, so that
the code will build and work better across different platforms.

https://bugzilla.gnome.org/show_bug.cgi?id=758759
parent 846295ab
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
* Copyright (C) 2001-2003, Ximian, Inc. * Copyright (C) 2001-2003, Ximian, Inc.
*/ */
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -11,6 +10,7 @@ ...@@ -11,6 +10,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <libsoup/soup.h> #include <libsoup/soup.h>
#include <glib/gstdio.h>
static int static int
compare_strings (gconstpointer a, gconstpointer b) compare_strings (gconstpointer a, gconstpointer b)
...@@ -27,22 +27,22 @@ get_directory_listing (const char *path) ...@@ -27,22 +27,22 @@ get_directory_listing (const char *path)
GPtrArray *entries; GPtrArray *entries;
GString *listing; GString *listing;
char *escaped; char *escaped;
DIR *dir; GDir *dir;
struct dirent *dent; const gchar *d_name;
int i; int i;
entries = g_ptr_array_new (); entries = g_ptr_array_new ();
dir = opendir (path); dir = g_dir_open (path, 0, NULL);
if (dir) { if (dir) {
while ((dent = readdir (dir))) { while ((d_name = g_dir_read_name (dir))) {
if (!strcmp (dent->d_name, ".") || if (!strcmp (d_name, ".") ||
(!strcmp (dent->d_name, "..") && (!strcmp (d_name, "..") &&
!strcmp (path, "./"))) !strcmp (path, "./")))
continue; continue;
escaped = g_markup_escape_text (dent->d_name, -1); escaped = g_markup_escape_text (d_name, -1);
g_ptr_array_add (entries, escaped); g_ptr_array_add (entries, escaped);
} }
closedir (dir); g_dir_close (dir);
} }
g_ptr_array_sort (entries, (GCompareFunc)compare_strings); g_ptr_array_sort (entries, (GCompareFunc)compare_strings);
...@@ -68,9 +68,9 @@ static void ...@@ -68,9 +68,9 @@ static void
do_get (SoupServer *server, SoupMessage *msg, const char *path) do_get (SoupServer *server, SoupMessage *msg, const char *path)
{ {
char *slash; char *slash;
struct stat st; GStatBuf st;
if (stat (path, &st) == -1) { if (g_stat (path, &st) == -1) {
if (errno == EPERM) if (errno == EPERM)
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
else if (errno == ENOENT) else if (errno == ENOENT)
...@@ -80,7 +80,7 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path) ...@@ -80,7 +80,7 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path)
return; return;
} }
if (S_ISDIR (st.st_mode)) { if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
GString *listing; GString *listing;
char *index_path; char *index_path;
...@@ -96,7 +96,7 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path) ...@@ -96,7 +96,7 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path)
} }
index_path = g_strdup_printf ("%s/index.html", path); index_path = g_strdup_printf ("%s/index.html", path);
if (stat (index_path, &st) != -1) { if (g_stat (path, &st) != -1) {
do_get (server, msg, index_path); do_get (server, msg, index_path);
g_free (index_path); g_free (index_path);
return; return;
...@@ -146,18 +146,18 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path) ...@@ -146,18 +146,18 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path)
static void static void
do_put (SoupServer *server, SoupMessage *msg, const char *path) do_put (SoupServer *server, SoupMessage *msg, const char *path)
{ {
struct stat st; GStatBuf st;
FILE *f; FILE *f;
gboolean created = TRUE; gboolean created = TRUE;
if (stat (path, &st) != -1) { if (g_stat (path, &st) != -1) {
const char *match = soup_message_headers_get_one (msg->request_headers, "If-None-Match"); const char *match = soup_message_headers_get_one (msg->request_headers, "If-None-Match");
if (match && !strcmp (match, "*")) { if (match && !strcmp (match, "*")) {
soup_message_set_status (msg, SOUP_STATUS_CONFLICT); soup_message_set_status (msg, SOUP_STATUS_CONFLICT);
return; return;
} }
if (!S_ISREG (st.st_mode)) { if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
return; return;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment