summaryrefslogtreecommitdiff
path: root/commands.c
blob: 21b07035c5e8b4d5dbc930306865781a95faf3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "commands.h"
#include "music.h"

#include <glib.h>
#include <string.h>
#include <gio/gio.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 && data[1] != NULL);

	gchar *dirname = g_strdup(data[1]);
	g_strfreev(data);

	struct directory *directory = music_find_dir(dirname);
	g_assert(directory != NULL);

	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_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_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) {
	GError *error = NULL;
	gchar **data = g_strsplit(cmd, " ", 2);
	g_assert(data[0] != NULL && data[1] != NULL);

	gchar *path = g_strdup(data[1]);
	g_strfreev(data);

	struct file *f = music_find_file(path);

	if(f == NULL) {
		g_warning("couldn't find %s", path);
		send_404(connection);
		goto commands_get_raw_free_path;
	}

	GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
	GFile *file = g_file_new_for_path(path);
	GFileInputStream *is = g_file_read(file, NULL, &error);

	if(is == NULL) {
		g_warning(error->message);
		g_error_free(error);
		goto commands_get_raw_file_unref;
	}

	GFileInfo *fi = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
			G_FILE_QUERY_INFO_NONE, NULL, &error);

	if(fi == NULL) {
		g_warning(error->message);
		g_error_free(error);
		goto commands_get_raw_file_unref;
	}

	goffset filesize = g_file_info_get_size(fi);
	g_object_unref(fi);

	GString *string = g_string_new(NULL);
	g_string_append(string, "HTTP/1.1 200 OK\r\n");
	g_string_append(string, "content-type: octet-stream\r\n");
	g_string_append_printf(string, "content-length: %lu\r\n", filesize);
	g_string_append(string, "\r\n");

	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);
		goto commands_get_raw_file_unref;
	}

	g_string_free(string, TRUE);

	gssize size = g_output_stream_splice(os, (GInputStream*)is,
			G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE ||
			G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
			NULL, &error);
	if(size == -1) {
		g_warning(error->message);
	} else {
		g_debug("wrote %lu bytes of file data", size);
	}

commands_get_raw_file_unref:

	g_object_unref(is);
	g_object_unref(file);

commands_get_raw_free_path:

	g_free(path);
}

void commands_handle(GSocketConnection *connection, const gchar *cmd) {
	g_debug("handling command string %s", cmd);
	if(g_ascii_strncasecmp(cmd, "/list", 5) == 0) {
		commands_list(connection, cmd);
	} else if(g_ascii_strncasecmp(cmd, "/get_raw", 8) == 0) {
		commands_get_raw(connection, cmd);
	} else {
		g_warning("no command handlers found");
	}
}