summaryrefslogtreecommitdiff
path: root/tests/multipart-test.c
blob: 8bcc4cd451e0c14e72b41e915a0bdfe5fafa8123 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2011 Collabora Ltd.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "test-utils.h"

#define READ_BUFFER_SIZE 8192

typedef enum {
	NO_MULTIPART,
	SYNC_MULTIPART,
	ASYNC_MULTIPART,
	ASYNC_MULTIPART_SMALL_READS
} MultipartMode;

char *buffer;
SoupSession *session;
char *base_uri_string;
SoupURI *base_uri;
SoupMultipartInputStream *multipart;
unsigned passes;


/* This payload contains 4 different responses.
 *
 * First, a text/html response with a Content-Length (31);
 * Second, a response lacking Content-Type with Content-Length (11);
 * Third, a text/css response with no Content-Length;
 * Fourth, same as the third, but with different content;
 */
const char *payload = \
	"--cut-here\r\n" \
	"Content-Type: text/html\n"
	"Content-Length: 30\r\n" \
	"\r\n" \
	"<html><body>Hey!</body></html>" \
	"\r\n--cut-here\r\n" \
	"Content-Length: 10\r\n" \
	"\r\n" \
	"soup rocks" \
	"\r\n--cut-here\r\n" \
	"Content-Type: text/css\r\n" \
	"\r\n" \
	".soup { before: rocks; }" \
	"\r\n--cut-here\n" /* Tests boundary ending in a single \n. */ \
	"Content-Type: text/css\r\n" \
	"\r\n" \
	"#soup { background-color: black; }" \
        "\r\n--cut-here--";

static void
server_callback (SoupServer *server, SoupMessage *msg,
		 const char *path, GHashTable *query,
		 SoupClientContext *context, gpointer data)
{
	if (msg->method != SOUP_METHOD_GET) {
		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
		return;
	}

	soup_message_set_status (msg, SOUP_STATUS_OK);

	soup_message_headers_append (msg->response_headers,
				     "Content-Type", "multipart/x-mixed-replace; boundary=cut-here");

	soup_message_body_append (msg->response_body,
				  SOUP_MEMORY_STATIC,
				  payload,
				  strlen (payload));

	soup_message_body_complete (msg->response_body);
}

static void
content_sniffed (SoupMessage *msg, char *content_type, GHashTable *params, int *sniffed_count)
{
	*sniffed_count = *sniffed_count + 1;
	debug_printf (2, "  content-sniffed -> %s\n", content_type);
}

static void
check_is_next (gboolean is_next)
{
	if (!is_next) {
		debug_printf (1, "  expected a header, but there are no more headers\n");
		errors++;
	}
}

static void
got_headers (SoupMessage *msg, int *headers_count)
{
	SoupMessageHeadersIter iter;
	gboolean is_next;
	const char* name, *value;

	*headers_count = *headers_count + 1;

	soup_message_headers_iter_init (&iter, msg->response_headers);

	is_next = soup_message_headers_iter_next (&iter, &name, &value);
	check_is_next (is_next);

	if (g_str_equal (name, "Date")) {
		is_next = soup_message_headers_iter_next (&iter, &name, &value);
		check_is_next (is_next);
	}

	if (!g_str_equal (name, "Content-Type")) {
		debug_printf (1, "  expected `Content-Type' got %s\n", name);
		errors++;
	}

	if (!g_str_equal (value, "multipart/x-mixed-replace; boundary=cut-here")) {
		debug_printf (1, "  expected `multipart/x-mixed-replace; boundary=cut-here' got %s\n", value);
		errors++;
	}
}

static void
read_cb (GObject *source, GAsyncResult *asyncResult, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	GInputStream *stream = G_INPUT_STREAM (source);
	GError *error = NULL;
	gssize bytes_read = g_input_stream_read_finish (stream, asyncResult, &error);
	if (error) {
		debug_printf (1, "  failed read: %s\n", error->message);
		errors++;

		g_main_loop_quit (loop);
		return;
	}

	if (!bytes_read) {
		g_input_stream_close (stream, NULL, &error);

		if (error) {
			debug_printf (1, "  failed close: %s\n", error->message);
			errors++;
		}

		g_main_loop_quit (loop);
		return;
	}

	g_input_stream_read_async (stream, buffer, READ_BUFFER_SIZE,
				   G_PRIORITY_DEFAULT, NULL,
				   read_cb, data);
}

static void
no_multipart_handling_cb (GObject *source, GAsyncResult *res, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	SoupRequest *request = SOUP_REQUEST (source);
	GError *error = NULL;
	GInputStream* in;

	in = soup_request_send_finish (request, res, &error);

	if (error) {
		debug_printf (1, "  failed send: %s\n", error->message);
		errors++;

		g_main_loop_quit (loop);
		return;
	}

	g_input_stream_read_async (in, buffer, READ_BUFFER_SIZE,
				   G_PRIORITY_DEFAULT, NULL,
				   read_cb, data);
}

static void
multipart_close_part_cb (GObject *source, GAsyncResult *res, gpointer data)
{
	GInputStream *in = G_INPUT_STREAM (source);
	GError *error = NULL;

	g_input_stream_close_finish (in, res, &error);
	if (error) {
		debug_printf (1, "  error closing stream: %s\n", error->message);
		errors++;
	}
}

static void multipart_next_part_cb (GObject *source,
				    GAsyncResult *res,
				    gpointer data);

static void
check_read (gsize nread, unsigned passes)
{
	switch (passes) {
	case 0:
		if (nread != 30) {
			debug_printf (1, "  expected to read 30 bytes, got: %d\n", (int)nread);
			errors++;
		}
		break;
	case 1:
		if (nread != 10) {
			debug_printf (1, "  expected to read 10 bytes, got: %d\n", (int)nread);
			errors++;
		}
		break;
	case 2:
		if (nread != 24) {
			debug_printf (1, "  expected to read 24 bytes, got: %d\n", (int)nread);
			errors++;
		}
		break;
	case 3:
		if (nread != 34) {
			debug_printf (1, "  expected to read 34 bytes, got: %d\n", (int)nread);
			errors++;
		}
		break;
	default:
		debug_printf (1, "  unexpected read of size: %d\n", (int)nread);
		errors++;
	}
}

static void
multipart_read_cb (GObject *source, GAsyncResult *asyncResult, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	GInputStream *in = G_INPUT_STREAM (source);
	GError *error = NULL;
	static gssize bytes_read_for_part = 0;
	gssize bytes_read;

	bytes_read = g_input_stream_read_finish (in, asyncResult, &error);

	if (error) {
		debug_printf (1, "  failed read: %s\n", error->message);
		errors++;

		g_input_stream_close_async (in, G_PRIORITY_DEFAULT, NULL,
					    multipart_close_part_cb, NULL);

		g_main_loop_quit (loop);
		return;
	}

	/* Read 0 bytes - try to start reading another part. */
	if (!bytes_read) {
		check_read (bytes_read_for_part, passes);
		bytes_read_for_part = 0;
		passes++;

		g_input_stream_close_async (in, G_PRIORITY_DEFAULT, NULL,
					    multipart_close_part_cb, NULL);

		soup_multipart_input_stream_next_part_async (multipart, G_PRIORITY_DEFAULT, NULL,
							     multipart_next_part_cb, data);
		return;
	}

	bytes_read_for_part += bytes_read;
	g_input_stream_read_async (in, buffer, READ_BUFFER_SIZE,
				   G_PRIORITY_DEFAULT, NULL,
				   multipart_read_cb, data);
}

static void
check_headers (SoupMultipartInputStream* multipart, unsigned passes)
{
	SoupMessageHeaders *headers;
	SoupMessageHeadersIter iter;
	gboolean is_next;
	const char *name, *value;

	headers = soup_multipart_input_stream_get_headers (multipart);
	soup_message_headers_iter_init (&iter, headers);

	switch (passes) {
	case 0:
		is_next = soup_message_headers_iter_next (&iter, &name, &value);
		check_is_next (is_next);

		if (!g_str_equal (name, "Content-Type")) {
			debug_printf (1, "  [0] expected `Content-Type' got %s\n", name);
			errors++;
		}

		if (!g_str_equal (value, "text/html")) {
			debug_printf (1, "  [0] expected `text/html' got %s\n", value);
			errors++;
		}

		is_next = soup_message_headers_iter_next (&iter, &name, &value);
		check_is_next (is_next);

		if (!g_str_equal (name, "Content-Length")) {
			debug_printf (1, "  [0] expected `Content-Length' got %s\n", name);
			errors++;
		}

		if (!g_str_equal (value, "30")) {
			debug_printf (1, "  [0] expected `30' got %s\n", value);
			errors++;
		}

		break;
	case 1:
		is_next = soup_message_headers_iter_next (&iter, &name, &value);
		check_is_next (is_next);

		if (!g_str_equal (name, "Content-Length")) {
			debug_printf (1, "  [1] expected `Content-Length' got %s\n", name);
			errors++;
		}

		if (!g_str_equal (value, "10")) {
			debug_printf (1, "  [1] expected `10' got %s\n", value);
			errors++;
		}

		break;
	case 2:
	case 3:
		is_next = soup_message_headers_iter_next (&iter, &name, &value);
		check_is_next (is_next);

		if (!g_str_equal (name, "Content-Type")) {
			debug_printf (1, "  [%d] expected `Content-Type' got %s\n", passes, name);
			errors++;
		}

		if (!g_str_equal (value, "text/css")) {
			debug_printf (1, "  [%d] expected `text/html' got %s\n", passes, value);
			errors++;
		}

		break;
	default:
		debug_printf (1, "  unexpected part received\n");
		break;
	}
}

static void
multipart_next_part_cb (GObject *source, GAsyncResult *res, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	GError *error = NULL;
	GInputStream *in;
	gsize read_size = READ_BUFFER_SIZE;

	g_assert (SOUP_MULTIPART_INPUT_STREAM (source) == multipart);

	in = soup_multipart_input_stream_next_part_finish (multipart, res, &error);

	if (error) {
		debug_printf (1, "  failed next part: %s\n", error->message);
		errors++;

		g_main_loop_quit (loop);
		return;
	}

	if (!in) {
		if (passes != 4) {
			debug_printf (1, "  expected 4 parts, got %u\n", passes);
			errors++;
		}

		g_main_loop_quit (loop);
		return;
	}

	check_headers (multipart, passes);

	if (g_object_get_data (G_OBJECT (multipart), "multipart-small-reads"))
		read_size = 4;

	g_input_stream_read_async (in, buffer, read_size,
				   G_PRIORITY_DEFAULT, NULL,
				   multipart_read_cb, data);
}

static void
multipart_handling_cb (GObject *source, GAsyncResult *res, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	SoupRequest *request = SOUP_REQUEST (source);
	GError *error = NULL;
	GInputStream *in;
	SoupMessage *message;

	in = soup_request_send_finish (request, res, &error);
	message = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
	multipart = soup_multipart_input_stream_new (message, in);

	if (error) {
		debug_printf (1, "  failed send: %s\n", error->message);
		errors++;

		g_main_loop_quit (loop);
		return;
	}

	if (g_object_get_data (source, "multipart-small-reads"))
		g_object_set_data (G_OBJECT (multipart), "multipart-small-reads", GINT_TO_POINTER(1));

	soup_multipart_input_stream_next_part_async (multipart, G_PRIORITY_DEFAULT, NULL,
						     multipart_next_part_cb, data);
}

static void
sync_multipart_handling_cb (GObject *source, GAsyncResult *res, gpointer data)
{
	GMainLoop *loop = (GMainLoop*)data;
	SoupRequest *request = SOUP_REQUEST (source);
	GError *error = NULL;
	GInputStream *in;
	SoupMessage *message;
	char buffer[READ_BUFFER_SIZE];
	gsize bytes_read;

	in = soup_request_send_finish (request, res, &error);
	message = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
	multipart = soup_multipart_input_stream_new (message, in);

	if (error) {
		debug_printf (1, "  failed send: %s\n", error->message);
		errors++;

		g_main_loop_quit (loop);
		return;
	}

	while (TRUE) {
		in = soup_multipart_input_stream_next_part (multipart, NULL, &error);

		if (error) {
			debug_printf (1, "  failed sync next part: %s\n", error->message);
			errors++;
			g_clear_error (&error);
			break;
		}

		if (!in)
			break;

		check_headers (multipart, passes);

		g_input_stream_read_all (in, (void*)buffer, sizeof (buffer), &bytes_read, NULL, &error);

		if (error) {
			debug_printf (1, "  failed sync read: %s\n", error->message);
			errors++;
			g_clear_error (&error);
			break;
		}

		check_read (bytes_read, passes);

		passes++;
	}

	if (passes != 4) {
		debug_printf (1, "  expected 4 parts, got %u\n", passes);
		errors++;
	}

	g_main_loop_quit (loop);

}

static const char*
multipart_mode_to_string (MultipartMode mode)
{
	if (mode == NO_MULTIPART)
		return "NO_MULTIPART";
	else if (mode == SYNC_MULTIPART)
		return "SYNC_MULTIPART";
	else if (mode == ASYNC_MULTIPART_SMALL_READS)
		return "SYNC_MULTIPART_SMALL_READS";

	return "ASYNC_MULTIPART";
}

static void
test_multipart (int headers_expected, int sniffed_expected, MultipartMode multipart_mode)
{
	GError* error = NULL;
	SoupRequest* request = soup_session_request (session, base_uri_string, &error);

	SoupMessage *msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
	GMainLoop *loop = g_main_loop_new (NULL, TRUE);
	int headers_count = 0;
	int sniffed_count = 0;
	GHashTable *params;
	const char *content_type;
	gboolean message_is_multipart = FALSE;

	debug_printf (1, "test_multipart(%s)\n", multipart_mode_to_string (multipart_mode));

	/* This is used to track the number of parts. */
	passes = 0;

	/* Force the server to close the connection. */
	soup_message_headers_append (msg->request_headers,
				     "Connection", "close");

	g_signal_connect (msg, "got_headers",
			  G_CALLBACK (got_headers), &headers_count);

	g_signal_connect (msg, "content-sniffed",
			  G_CALLBACK (content_sniffed), &sniffed_count);

	g_object_ref (msg);

	if (multipart_mode == ASYNC_MULTIPART)
		soup_request_send_async (request, NULL, multipart_handling_cb, loop);
	else if (multipart_mode == ASYNC_MULTIPART_SMALL_READS) {
		g_object_set_data (G_OBJECT (request), "multipart-small-reads", GINT_TO_POINTER(1));
		soup_request_send_async (request, NULL, multipart_handling_cb, loop);
	} else if (multipart_mode == SYNC_MULTIPART)
		soup_request_send_async (request, NULL, sync_multipart_handling_cb, loop);
	else
		soup_request_send_async (request, NULL, no_multipart_handling_cb, loop);

	g_main_loop_run (loop);

	content_type = soup_message_headers_get_content_type (msg->response_headers, &params);

	if (content_type &&
	    g_str_has_prefix (content_type, "multipart/") &&
	    g_hash_table_lookup (params, "boundary")) {
		message_is_multipart = TRUE;
	}
	g_clear_pointer (&params, g_hash_table_unref);

	if (!message_is_multipart) {
		debug_printf (1,
			      "	 Header does not indicate a multipart message!\n");
		errors++;
	}

	if (headers_count != headers_expected) {
		debug_printf (1,
			      "	 expected got_header %d times, got %d!\n",
			      headers_expected, headers_count);
		errors++;
	}

	if (sniffed_count != sniffed_expected) {
		debug_printf (1,
			      "	 expected content_sniffed %d times, got %d!\n",
			      sniffed_expected, sniffed_count);
		errors++;
	}

	g_object_unref (msg);
	g_object_unref (request);
	g_main_loop_unref (loop);
}

int
main (int argc, char **argv)
{
	SoupServer *server;

	test_init (argc, argv, NULL);

	buffer = g_malloc (READ_BUFFER_SIZE);

	server = soup_test_server_new (FALSE);
	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
	base_uri = soup_uri_new ("http://127.0.0.1");
	soup_uri_set_port (base_uri, soup_server_get_port (server));
	base_uri_string = soup_uri_to_string (base_uri, FALSE);

	/* FIXME: I had to raise the number of connections allowed here, otherwise I
	 * was hitting the limit, which indicates some connections are not dying.
	 */
	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
					 "use-thread-context", TRUE,
					 "max-conns", 20,
					 "max-conns-per-host", 20,
					 NULL);
	soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);

	test_multipart (1, 1, NO_MULTIPART);
	test_multipart (1, 1, SYNC_MULTIPART);
	test_multipart (1, 1, ASYNC_MULTIPART);
	test_multipart (1, 1, ASYNC_MULTIPART_SMALL_READS);

	soup_uri_free (base_uri);
	g_free (base_uri_string);

	soup_test_session_abort_unref (session);
	soup_test_server_quit_unref (server);
	test_cleanup ();
	return errors != 0;
}