From d6721f09b07d0c680edf734df4a1148c1c991f61 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sat, 2 May 2009 21:36:09 +0200 Subject: [PATCH] Add sokoke_get_res_server --- midori/sokoke.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ midori/sokoke.h | 5 ++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/midori/sokoke.c b/midori/sokoke.c index bb421d80..69f2d675 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -971,3 +971,50 @@ sokoke_remove_path (const gchar* path, g_rmdir (path); return TRUE; } + +static void +res_server_handler_cb (SoupServer* res_server, + SoupMessage* msg, + const gchar* path, + GHashTable* query, + SoupClientContext* client, + gpointer data) +{ + if (g_str_has_prefix (path, "/res")) + { + gchar* filename = g_strconcat (DATADIR "/midori", path, NULL); + gchar* contents; + guint length; + + if (g_file_get_contents (filename, &contents, &length, NULL)) + { + /* FIXME: Support any MIME type */ + soup_message_set_response (msg, "image/png", SOUP_MEMORY_TAKE, + contents, length); + soup_message_set_status (msg, 401); + } + else + soup_message_set_status (msg, 404); + g_free (filename); + } + else + { + soup_message_set_status (msg, 404); + } +} + +SoupServer* +sokoke_get_res_server (void) +{ + static SoupServer* res_server = NULL; + + if (G_UNLIKELY (!res_server)) + { + res_server = soup_server_new ("port", SOUP_ADDRESS_ANY_PORT, NULL); + soup_server_add_handler (res_server, "/", + res_server_handler_cb, NULL, NULL); + soup_server_run_async (res_server); + } + + return res_server; +} diff --git a/midori/sokoke.h b/midori/sokoke.h index d750f2f3..e960e087 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -16,7 +16,7 @@ #include #include - +#include #include gchar* @@ -154,4 +154,7 @@ gboolean sokoke_remove_path (const gchar* path, gboolean ignore_errors); +SoupServer* +sokoke_get_res_server (void); + #endif /* !__SOKOKE_H__ */