diff options
-rw-r--r-- | commands.c | 28 |
1 files changed, 23 insertions, 5 deletions
@@ -6,6 +6,7 @@ #include <gio/gio.h> 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) { |