diff options
| -rw-r--r-- | commands.c | 170 | ||||
| -rw-r--r-- | commands.h | 7 | ||||
| -rw-r--r-- | httpd_commands.c | 183 | ||||
| -rw-r--r-- | httpd_commands.h | 8 | ||||
| -rw-r--r-- | server_commands.c | 73 | ||||
| -rw-r--r-- | server_commands.h | 9 | 
6 files changed, 225 insertions, 225 deletions
| @@ -1,12 +1,5 @@ -#include "commands.h" +#include "server_commands.h"  #include "music.h" -#include "transcode.h" -#include "decoder.h" -#include "encoder.h" - -#include <glib.h> -#include <string.h> -#include <gio/gio.h>  static void send_404(GSocketConnection *connection) {  	GError *error = NULL; @@ -22,162 +15,59 @@ static void send_404(GSocketConnection *connection) {  	}  } -static void commands_get_raw(GSocketConnection *connection, const gchar *cmd) { +static void commands_list(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); - -	gchar *full_path = music_get_full_path(path); -	GFile *file = g_file_new_for_path(full_path); -	g_free(full_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; +	for(gint i = 0; data[i]; i++) { +		g_debug("\tdata[%d] = %s", i, data[i]);  	} +	g_assert(data[0] != NULL); -	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: application/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); +	gchar *dirname; +	if(data[1] != NULL) { +		dirname = g_strdup(data[1]);  	} else { -		g_debug("wrote %lu bytes of file data", size); +		dirname = "/";  	} -commands_get_raw_file_unref: - -	g_object_unref(is); -	g_object_unref(file); - -commands_get_raw_free_path: - -	g_free(path); -} - -static void commands_get_mp3(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); +	struct directory *directory = music_find_dir(dirname); +	if(directory == NULL) { +		g_warning("couldn't find directory %s", dirname);  		send_404(connection); -		goto commands_get_mp3_free_path; +		return;  	} -	GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); - -	gchar *full_path = music_get_full_path(path); -	GFile *file = g_file_new_for_path(full_path); -	g_free(full_path); - -	GFileInputStream *is = g_file_read(file, NULL, &error); +	GString *string = g_string_new(NULL); -	if(is == NULL) { -		g_warning(error->message); -		g_error_free(error); -		goto commands_get_mp3_file_unref; +	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);  	} -	GString *string = g_string_new(NULL); -	g_string_append(string, "HTTP/1.1 200 OK\r\n"); -	g_string_append(string, "content-type: application/octet-stream\r\n"); -	g_string_append(string, "\r\n"); +	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) { +			&error) == FALSE) {  		g_warning(error->message);  		g_error_free(error); -		g_string_free(string, TRUE); -		goto commands_get_mp3_file_unref;  	}  	g_string_free(string, TRUE); - -	const struct decoder_plugin *decoder = decoder_find(path); -	const struct encoder_plugin *encoder = encoder_get("lame"); - -	if(decoder == NULL) { -		g_warning("no suitable decoder found"); -		goto commands_get_mp3_file_unref; -	} else { -		g_debug("using decoder %s", decoder->name); -	} - -	if(encoder == NULL) { -		g_warning("no encoder found"); -		goto commands_get_mp3_file_unref; -	} - -	transcode((GInputStream*)is, decoder, os, encoder); - -commands_get_mp3_file_unref: - -	g_object_unref(is); -	g_object_unref(file); - -commands_get_mp3_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, "/get_raw", 8) == 0) { -		commands_get_raw(connection, cmd); -	} else if(g_ascii_strncasecmp(cmd, "/get_mp3", 8) == 0) { -		commands_get_mp3(connection, cmd); +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_warning("no command handlers found"); +		g_debug("unknown command");  	}  } @@ -1,8 +1,9 @@ -#ifndef _COMMANDS_H_ -#define _COMMANDS_H_ +#ifndef _SERVER_COMMANDS_H_ +#define _SERVER_COMMANDS_H_ +#include <glib.h>  #include <gio/gio.h> -void commands_handle(GSocketConnection *connection, const gchar *cmd); +void server_commands_handle(GSocketConnection *connection, const gchar *cmd);  #endif diff --git a/httpd_commands.c b/httpd_commands.c new file mode 100644 index 0000000..c77d7c8 --- /dev/null +++ b/httpd_commands.c @@ -0,0 +1,183 @@ +#include "commands.h" +#include "music.h" +#include "transcode.h" +#include "decoder.h" +#include "encoder.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_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); + +	gchar *full_path = music_get_full_path(path); +	GFile *file = g_file_new_for_path(full_path); +	g_free(full_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: application/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); +} + +static void commands_get_mp3(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_mp3_free_path; +	} + +	GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); + +	gchar *full_path = music_get_full_path(path); +	GFile *file = g_file_new_for_path(full_path); +	g_free(full_path); + +	GFileInputStream *is = g_file_read(file, NULL, &error); + +	if(is == NULL) { +		g_warning(error->message); +		g_error_free(error); +		goto commands_get_mp3_file_unref; +	} + +	GString *string = g_string_new(NULL); +	g_string_append(string, "HTTP/1.1 200 OK\r\n"); +	g_string_append(string, "content-type: application/octet-stream\r\n"); +	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_mp3_file_unref; +	} + +	g_string_free(string, TRUE); + +	const struct decoder_plugin *decoder = decoder_find(path); +	const struct encoder_plugin *encoder = encoder_get("lame"); + +	if(decoder == NULL) { +		g_warning("no suitable decoder found"); +		goto commands_get_mp3_file_unref; +	} else { +		g_debug("using decoder %s", decoder->name); +	} + +	if(encoder == NULL) { +		g_warning("no encoder found"); +		goto commands_get_mp3_file_unref; +	} + +	transcode((GInputStream*)is, decoder, os, encoder); + +commands_get_mp3_file_unref: + +	g_object_unref(is); +	g_object_unref(file); + +commands_get_mp3_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, "/get_raw", 8) == 0) { +		commands_get_raw(connection, cmd); +	} else if(g_ascii_strncasecmp(cmd, "/get_mp3", 8) == 0) { +		commands_get_mp3(connection, cmd); +	} else { +		g_warning("no command handlers found"); +	} +} diff --git a/httpd_commands.h b/httpd_commands.h new file mode 100644 index 0000000..7b90f95 --- /dev/null +++ b/httpd_commands.h @@ -0,0 +1,8 @@ +#ifndef _COMMANDS_H_ +#define _COMMANDS_H_ + +#include <gio/gio.h> + +void commands_handle(GSocketConnection *connection, const gchar *cmd); + +#endif diff --git a/server_commands.c b/server_commands.c deleted file mode 100644 index 4442a1b..0000000 --- a/server_commands.c +++ /dev/null @@ -1,73 +0,0 @@ -#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"); -	} -} diff --git a/server_commands.h b/server_commands.h deleted file mode 100644 index 3a073b3..0000000 --- a/server_commands.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _SERVER_COMMANDS_H_ -#define _SERVER_COMMANDS_H_ - -#include <glib.h> -#include <gio/gio.h> - -void server_commands_handle(GSocketConnection *connection, const gchar *cmd); - -#endif | 
