From 5c0ca3c4474c331d306729b174f3645aa80ee1b7 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 16 Aug 2010 16:09:01 +0200 Subject: Send proper HTTP headers in commands_list(). --- commands.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/commands.c b/commands.c index 08b0366..df9504c 100644 --- a/commands.c +++ b/commands.c @@ -6,6 +6,7 @@ #include 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]); @@ -18,21 +19,38 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { struct directory *directory = music_find_dir(dirname); g_assert(directory != NULL); - GSocket *socket = g_socket_connection_get_socket(connection); + GString *list_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_socket_send(socket, name, strlen(name), NULL, NULL); - g_socket_send(socket, "\n", 1, NULL, NULL); + g_string_append_printf(list_string, "%s\n", name); g_free(name); } for(GSList *node = directory->files; node; node = g_slist_next(node)) { struct file *f = node->data; - g_socket_send(socket, f->name, strlen(f->name), NULL, NULL); - g_socket_send(socket, "\n", 1, NULL, NULL); + g_string_append_printf(list_string, "%s\n", f->name); } + + GString *string = g_string_new(NULL); + + g_string_append(string, "HTTP/1.1 200 OK\r\n"); + g_string_append(string, "content-type: text/plain\r\n"); + g_string_append_printf(string, "content-length: %lu\r\n", list_string->len); + g_string_append(string, "\r\n"); + g_string_append(string, list_string->str); + + g_string_free(list_string, TRUE); + + 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); } static void commands_get_raw(GSocketConnection *connection, const gchar *cmd) { -- cgit v1.2.3