summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-16 16:09:01 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-16 16:09:01 +0200
commit5c0ca3c4474c331d306729b174f3645aa80ee1b7 (patch)
tree21928c520b6f52ca595bcd03137fbf2c92f5201b
parentb57481d1dfcc9a479290f4b1849c210f951c03f2 (diff)
Send proper HTTP headers in commands_list().
-rw-r--r--commands.c28
1 files 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 <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) {