From 5241f71a41f4125281f9215dd30b8fc4dfc546b1 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 20 Aug 2010 14:40:48 +0200 Subject: Added a service for server commands and moved the list command in there. --- server_commands.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 server_commands.c (limited to 'server_commands.c') diff --git a/server_commands.c b/server_commands.c new file mode 100644 index 0000000..4442a1b --- /dev/null +++ b/server_commands.c @@ -0,0 +1,73 @@ +#include "server_commands.h" +#include "music.h" + +static void send_404(GSocketConnection *connection) { + GError *error = NULL; + GString *string = g_string_new(NULL); + g_string_append(string, "HTTP/1.1 404 Not Found\r\n"); + g_string_append(string, "\r\n"); + + GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); + if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL, + &error) == FALSE) { + g_warning(error->message); + g_error_free(error); + } +} + +static void commands_list(GSocketConnection *connection, const gchar *cmd) { + GError *error = NULL; + gchar **data = g_strsplit(cmd, " ", 2); + for(gint i = 0; data[i]; i++) { + g_debug("\tdata[%d] = %s", i, data[i]); + } + g_assert(data[0] != NULL); + + gchar *dirname; + if(data[1] != NULL) { + dirname = g_strdup(data[1]); + } else { + dirname = "/"; + } + + g_strfreev(data); + + struct directory *directory = music_find_dir(dirname); + if(directory == NULL) { + g_warning("couldn't find directory %s", dirname); + send_404(connection); + return; + } + + GString *string = g_string_new(NULL); + + for(GSList *node = directory->sub; node; node = g_slist_next(node)) { + struct directory *d = node->data; + gchar *name = g_path_get_basename(d->path); + g_string_append_printf(string, "%s\n", name); + g_free(name); + } + + for(GSList *node = directory->files; node; node = g_slist_next(node)) { + struct file *f = node->data; + g_string_append_printf(string, "%s\n", f->name); + } + + GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); + if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL, + &error) == FALSE) { + g_warning(error->message); + g_error_free(error); + } + + g_string_free(string, TRUE); +} + +void server_commands_handle(GSocketConnection *connection, const gchar *cmd) { + g_debug(cmd); + if(g_strncasecmp(cmd, "list", 4) == 0) { + commands_list(connection, cmd); + } else { + g_debug("unknown command"); + } +} -- cgit v1.2.3