summaryrefslogtreecommitdiff
path: root/httpd.c
blob: 6e957305e73e8a474657ef823b1157560cd19812 (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
#include "httpd.h"
#include "httpd_commands.h"

#include <gio/gio.h>
#include <string.h>

GSocketService *ss = NULL;

static gboolean service_incoming(GSocketService *service,
		GSocketConnection *connection, GObject *source_object,
		gpointer user_data) {
	GSocket *socket;
	GError *error = NULL;

	g_debug("incoming connection to service");

	socket = g_socket_connection_get_socket(connection);
	if(socket == NULL) {
		g_warning("g_socket_connection_get_socket() returned NULL");
		return FALSE;
	}

	GString *string = g_string_new(NULL);
	gboolean done = FALSE;
	gssize tot = 0;

	while(done == FALSE) {
		gchar buffer[0x400];
		gssize len = g_socket_receive(socket, buffer, 0x400, NULL, &error);
		if(len == -1) {
			g_warning(error->message);
			g_error_free(error);
			g_string_free(string, TRUE);
			return FALSE;
		}
		tot += len;
		g_string_append_len(string, buffer, len);

		/* TODO: find a more sane way to stop reading */
		if(string->len >= 4 && g_strcmp0(string->str + string->len - 4, "\r\n\r\n") == 0) {
			done = TRUE;
		}
	}
	g_debug("read %lu bytes", tot);
	g_debug("HTTP header:\n%s", string->str);

	/* split headers */
	gchar **data = g_strsplit(string->str, "\r\n", 0);

	g_string_free(string, TRUE);

	gchar **firstline = g_strsplit(data[0], " ", 0);
	/* this sets the first character of the last string in
	 * firstline to \0 (HTTP version) */
	firstline[g_strv_length(firstline)-1] = '\0';
	/* now join from the second string to get the request path */
	gchar *path_escaped = g_strjoinv(" ", firstline + 1);
	gchar *path = g_uri_unescape_string(path_escaped, NULL);
	g_free(path_escaped);

	g_strfreev(firstline);

	for(gint i = 1; strlen(data[i]); i++) {
		g_debug("data[%d] = %s", i, data[i]);
	}

	g_strfreev(data);

	httpd_commands_handle(connection, path);

	g_free(path);

	if(g_socket_close(socket, &error) == FALSE) {
		g_warning(error->message);
		g_error_free(error);
	}

	return FALSE;
}

gboolean httpd_start() {
	ss = g_threaded_socket_service_new(10);

	g_socket_listener_add_inet_port((GSocketListener*)ss, 8000, NULL, NULL);

	g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL);
	g_socket_service_start(ss);

	return TRUE;
}

void httpd_stop() {
	g_socket_service_stop(ss);
	g_object_unref(ss);
	ss = NULL;
}