aboutsummaryrefslogtreecommitdiff
path: root/server.c
blob: 1a54ef6df8f2f3a717d5f0fdc7022046818e52e1 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <libsoup/soup.h>

#include "rodisc.h"

/** Buffer size for a single read() call.
 * Any request larger than this will be served in chunks. */
#define MAX_BUFFER_SIZE (32*1024*1024UL)

#define RESPONSE_SERVER_HEADER "RODisc/1.0"

typedef struct {
	RODisc *disc;
	gsize start;
	gsize length;
	gpointer buffer;
	SoupMessage *msg;
	GInputStream *stream;
	GCancellable *cancel;
} RODiscReadOp;

static SoupServer *server;
static unsigned int server_port = 0;

static void server_disc_cb(SoupServer *server, SoupMessage *msg, const char *path,
						   GHashTable *query, SoupClientContext *client, gpointer user_data);

static inline RODiscReadOp* server_disc_read_op_new()
{
	RODiscReadOp *op = g_slice_new(RODiscReadOp);
	return op;
}

static void server_disc_read_op_free(RODiscReadOp *op)
{
	if (op->stream) g_object_unref(op->stream);
	if (op->buffer) g_free(op->buffer);
	if (op->cancel) g_object_unref(op->cancel);
	g_slice_free(RODiscReadOp, op);
}

static void server_disc_finished_cb(SoupMessage *msg, gpointer user_data)
{
	RODiscReadOp *op = (RODiscReadOp*) user_data;
	g_warn_if_fail(op->msg == msg);

	g_debug(" request finished");

	if (op->cancel) {
		// Cancel pending operation if any
		g_cancellable_cancel(op->cancel);
		g_object_unref(op->cancel);
		op->cancel = NULL;
	}

	server_disc_read_op_free(op);
}

static void server_disc_read_cb(GInputStream *stream, GAsyncResult *res, gpointer user_data)
{
	RODiscReadOp *op = (RODiscReadOp*) user_data;
	GError *error = NULL;
	gssize size = g_input_stream_read_finish(stream, res, &error);
	if (size == -1) {
		if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
			g_debug(" request canceled");
			return;
		}

		g_warning("Failed to read from %s: %s",
		          op->disc->file_path, error->message);
		soup_message_set_status(op->msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
		soup_server_unpause_message(server, op->msg);
		g_error_free(error);
		return;
	}

	g_debug(" read %ld bytes of %lu bytes", size, op->length);
	g_warn_if_fail(size > 0);

	if (size >= op->length || size == 0) {
		// We have read everything we needed, so this finishes the request!
		g_debug(" finishing request");
		g_object_unref(op->cancel);
		op->cancel = NULL;
		op->length = 0;

		soup_message_body_append(op->msg->response_body, SOUP_MEMORY_TAKE,
								 op->buffer, size);
		soup_message_body_complete(op->msg->response_body);
		soup_server_unpause_message(server, op->msg);

		op->buffer = NULL; // Passed ownership of buffer to libsoup
		// 'op' will be freed when 'finished' signal @msg is triggered
	} else {
		soup_message_body_append(op->msg->response_body, SOUP_MEMORY_COPY,
								 op->buffer, size);
		soup_server_unpause_message(server, op->msg);

		// Launch the read for the remaining bytes
		op->start += size;
		op->length -= size;

		gsize read_size = MIN(op->length, MAX_BUFFER_SIZE);
		g_debug(" going to read %lu bytes", read_size);
		g_return_if_fail(read_size > 0);

		g_input_stream_read_async(op->stream, op->buffer, read_size,
								  G_PRIORITY_DEFAULT, op->cancel,
								  (GAsyncReadyCallback) server_disc_read_cb, op);
	}
}

static void server_disc_perform_read_range(SoupMessage *msg, RODisc *disc, goffset start, goffset end)
{
	GError *error = NULL;

	g_debug("Opening %s", disc->file_path);

	// File is opened on every read because of asynchronous reads
	GFileInputStream * stream = g_file_read(disc->file, NULL, &error);

	if (!stream) {
		g_warning("Could not open file '%s' for reading: %s",
		          disc->file_path, error->message);
		soup_message_set_status(msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
		g_error_free(error);
		return;
	}

	gsize size = (end - start) + 1;
	g_debug(" reading range %lu-%lu size %lu", start, end, size);
	g_warn_if_fail(size > 0);

	if (start != 0 &&
			!g_seekable_seek(G_SEEKABLE(stream), start, G_SEEK_SET, NULL, &error)) {
		g_warning("Could not seek in file '%s': %s",
				  disc->file_path, error->message);
		soup_message_set_status(msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
		g_error_free(error);
		return;
	}

	gsize read_size = MIN(size, MAX_BUFFER_SIZE);
	g_debug(" going to read %lu bytes", read_size);

	RODiscReadOp *op = server_disc_read_op_new();
	op->disc = disc;
	op->start = start;
	op->length = size;
	op->buffer = g_malloc(read_size);
	op->msg = msg;
	op->stream = G_INPUT_STREAM(stream);
	op->cancel = g_cancellable_new();

	g_signal_connect(msg, "finished",
					 G_CALLBACK(server_disc_finished_cb), op);

	g_input_stream_read_async(op->stream, op->buffer, read_size,
							  G_PRIORITY_DEFAULT, op->cancel,
	                          (GAsyncReadyCallback) server_disc_read_cb, op);
}

static void server_disc_perform_read(SoupMessage *msg, RODisc *disc)
{
    server_disc_perform_read_range(msg, disc, 0, disc->size - 1);
}

static void server_disc_cb(SoupServer *server, SoupMessage *msg, const char *path,
                           GHashTable *query, SoupClientContext *client, gpointer user_data)
{

	RODisc *disc = (RODisc*) user_data;
	if (!g_str_has_suffix(path,".dmg")) {
		g_debug("Not found (%s)", path);
		soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
		return;
	}
	if (msg->method == SOUP_METHOD_HEAD) {
		g_debug("Head on %s", path);
		soup_message_set_status(msg, SOUP_STATUS_OK);
		soup_message_headers_set_content_length(msg->response_headers, disc->size);
		soup_message_headers_set_content_type(msg->response_headers, RODISC_IMAGE_MIME, NULL);
		soup_message_headers_replace(msg->response_headers, "Accept-Ranges", "bytes");
	} else if (msg->method == SOUP_METHOD_GET) {
		g_debug("Get on %s", path);
		soup_message_headers_set_content_type(msg->response_headers, RODISC_IMAGE_MIME, NULL);
		soup_message_headers_replace(msg->response_headers, "Accept-Ranges", "bytes");
		soup_message_body_set_accumulate(msg->response_body, FALSE);
		SoupRange *ranges;
		int length;
		if (soup_message_headers_get_ranges(msg->request_headers, disc->size, &ranges, &length)) {
			if (length != 1) {
				g_warning("Multi-range not yet supported");
				soup_message_set_status(msg, SOUP_STATUS_INVALID_RANGE);
				return;
			}
			goffset start = ranges[0].start, end = ranges[0].end;
			soup_message_set_status(msg, SOUP_STATUS_PARTIAL_CONTENT);
			soup_message_headers_set_content_range(msg->response_headers,
			                                       start, end,
			                                       disc->size);
			soup_message_headers_free_ranges(msg->request_headers, ranges);
			soup_server_pause_message(server, msg);
			server_disc_perform_read_range(msg, disc, start, end);
		} else {
			soup_message_headers_set_content_length(msg->response_headers, disc->size);
			soup_message_set_status(msg, SOUP_STATUS_OK);
			soup_server_pause_message(server, msg);
			server_disc_perform_read(msg, disc);
		}
	} else {
		soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
		g_warning("Unimplemented method on %s", path);
	}
}

static void server_cb(SoupServer *server, SoupMessage *msg, const char *path,
                      GHashTable *query, SoupClientContext *client, gpointer user_data)
{
	g_debug("Unknown path requested: %s", path);
	soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
}

bool server_start()
{
	GError *error = NULL;

	server = soup_server_new(SOUP_SERVER_SERVER_HEADER, RESPONSE_SERVER_HEADER, NULL);
	if (!server) {
		g_printerr("Could not create HTTP server\n");
		return false;
	}

	soup_server_add_handler(server, NULL, server_cb, NULL, NULL);

	if (!soup_server_listen_all(server, 0, SOUP_SERVER_LISTEN_IPV4_ONLY, &error)) {
		g_printerr("Could not start HTTP server: %s\n", error->message);
		g_error_free(error);
		return false;
	}

	// Fetch the server port
	GSList *uris = soup_server_get_uris(server);
	g_warn_if_fail(g_slist_length(uris) == 1);
	for (GSList *l = uris; l; l = l->next) {
		SoupURI *uri = l->data;
		char *str = soup_uri_to_string(uri, FALSE);
		g_message("Listening on %s", str);
		g_free(str);
		server_port = soup_uri_get_port(uri);
	}
	g_slist_free_full(uris, (GDestroyNotify)soup_uri_free);

	if (server_port == 0) {
		g_printerr("Could not get HTTP server port\n");
		return false;
	}

	return true;
}

void server_stop()
{
	g_object_unref(server);
}

void server_register(RODisc *disc)
{
	soup_server_add_handler(server, disc->uri, server_disc_cb, disc, NULL);
}

void server_unregister(RODisc *disc)
{
	soup_server_remove_handler(server, disc->uri);
}

unsigned int server_get_port()
{
	return server_port;
}