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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*/
/* Kill SoupRequester-related deprecation warnings */
#define SOUP_VERSION_MIN_REQUIRED SOUP_VERSION_2_40
#include "test-utils.h"
SoupServer *server;
GMainLoop *loop;
char buf[1024];
SoupBuffer *response, *auth_response;
#define REDIRECT_HTML_BODY "<html><body>Try again</body></html>\r\n"
#define AUTH_HTML_BODY "<html><body>Unauthorized</body></html>\r\n"
static void
get_index (void)
{
char *contents;
gsize length;
GError *error = NULL;
if (!g_file_get_contents (SRCDIR "/index.txt", &contents, &length, &error)) {
g_printerr ("Could not read index.txt: %s\n",
error->message);
exit (1);
}
response = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length);
auth_response = soup_buffer_new (SOUP_MEMORY_STATIC,
AUTH_HTML_BODY,
strlen (AUTH_HTML_BODY));
}
static void
server_callback (SoupServer *server, SoupMessage *msg,
const char *path, GHashTable *query,
SoupClientContext *context, gpointer data)
{
gboolean chunked = FALSE;
int i;
if (strcmp (path, "/auth") == 0) {
soup_message_set_status (msg, SOUP_STATUS_UNAUTHORIZED);
soup_message_set_response (msg, "text/html",
SOUP_MEMORY_STATIC,
AUTH_HTML_BODY,
strlen (AUTH_HTML_BODY));
soup_message_headers_append (msg->response_headers,
"WWW-Authenticate",
"Basic: realm=\"requester-test\"");
return;
} else if (strcmp (path, "/foo") == 0) {
soup_message_set_redirect (msg, SOUP_STATUS_FOUND, "/");
/* Make the response HTML so if we sniff that instead of the
* real body, we'll notice.
*/
soup_message_set_response (msg, "text/html",
SOUP_MEMORY_STATIC,
REDIRECT_HTML_BODY,
strlen (REDIRECT_HTML_BODY));
return;
} else if (strcmp (path, "/chunked") == 0) {
chunked = TRUE;
} else if (strcmp (path, "/non-persistent") == 0) {
soup_message_headers_append (msg->response_headers,
"Connection", "close");
}
soup_message_set_status (msg, SOUP_STATUS_OK);
if (chunked) {
soup_message_headers_set_encoding (msg->response_headers,
SOUP_ENCODING_CHUNKED);
for (i = 0; i < response->length; i += 8192) {
SoupBuffer *tmp;
tmp = soup_buffer_new_subbuffer (response, i,
MIN (8192, response->length - i));
soup_message_body_append_buffer (msg->response_body, tmp);
soup_buffer_free (tmp);
}
soup_message_body_complete (msg->response_body);
} else
soup_message_body_append_buffer (msg->response_body, response);
}
typedef struct {
GString *body;
gboolean cancel;
} RequestData;
static void
stream_closed (GObject *source, GAsyncResult *res, gpointer user_data)
{
GInputStream *stream = G_INPUT_STREAM (source);
GError *error = NULL;
if (!g_input_stream_close_finish (stream, res, &error)) {
debug_printf (1, " close failed: %s\n", error->message);
g_error_free (error);
errors++;
}
g_main_loop_quit (loop);
g_object_unref (stream);
}
static void
test_read_ready (GObject *source, GAsyncResult *res, gpointer user_data)
{
GInputStream *stream = G_INPUT_STREAM (source);
RequestData *data = user_data;
GString *body = data->body;
GError *error = NULL;
gsize nread;
nread = g_input_stream_read_finish (stream, res, &error);
if (nread == -1) {
debug_printf (1, " read_async failed: %s\n", error->message);
g_error_free (error);
errors++;
g_input_stream_close (stream, NULL, NULL);
g_object_unref (stream);
g_main_loop_quit (loop);
return;
} else if (nread == 0) {
g_input_stream_close_async (stream,
G_PRIORITY_DEFAULT, NULL,
stream_closed, NULL);
return;
}
g_string_append_len (body, buf, nread);
g_input_stream_read_async (stream, buf, sizeof (buf),
G_PRIORITY_DEFAULT, NULL,
test_read_ready, data);
}
static void
auth_test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
{
SoupRequestHTTP *http = SOUP_REQUEST_HTTP (source);
RequestData *data = user_data;
GInputStream *stream;
GError *error = NULL;
const char *content_type;
stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
if (!stream) {
debug_printf (1, " send_async failed: %s\n", error->message);
g_clear_error (&error);
errors++;
g_main_loop_quit (loop);
return;
}
if (http->status_code != SOUP_STATUS_UNAUTHORIZED) {
debug_printf (1, " GET failed: %d %s\n", http->status_code,
http->reason_phrase);
errors++;
g_main_loop_quit (loop);
return;
}
content_type = soup_request_get_content_type (SOUP_REQUEST (source));
if (g_strcmp0 (content_type, "text/html") != 0) {
debug_printf (1, " failed to sniff Content-Type: got %s\n",
content_type ? content_type : "(NULL)");
errors++;
}
g_input_stream_read_async (stream, buf, sizeof (buf),
G_PRIORITY_DEFAULT, NULL,
test_read_ready, data);
}
static void
test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
{
RequestData *data = user_data;
GInputStream *stream;
GError *error = NULL;
const char *content_type;
stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
if (data->cancel) {
if (stream) {
debug_printf (1, " send_async succeeded??\n");
errors++;
g_input_stream_close (stream, NULL, NULL);
g_object_unref (stream);
} else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
debug_printf (1, " send_async failed with wrong error: %s\n", error->message);
errors++;
}
g_clear_error (&error);
g_main_loop_quit (loop);
return;
} else {
if (!stream) {
debug_printf (1, " send_async failed: %s\n", error->message);
errors++;
g_main_loop_quit (loop);
g_clear_error (&error);
return;
}
}
content_type = soup_request_get_content_type (SOUP_REQUEST (source));
if (g_strcmp0 (content_type, "text/plain") != 0) {
debug_printf (1, " failed to sniff Content-Type: got %s\n",
content_type ? content_type : "(NULL)");
errors++;
}
g_input_stream_read_async (stream, buf, sizeof (buf),
G_PRIORITY_DEFAULT, NULL,
test_read_ready, data);
}
static void
cancel_message (SoupMessage *msg, gpointer session)
{
soup_session_cancel_message (session, msg, SOUP_STATUS_FORBIDDEN);
}
static void
request_started (SoupSession *session, SoupMessage *msg,
SoupSocket *socket, gpointer user_data)
{
SoupSocket **save_socket = user_data;
g_clear_object (save_socket);
*save_socket = g_object_ref (socket);
}
static void
do_async_test (SoupSession *session, SoupURI *uri,
GAsyncReadyCallback callback, guint expected_status,
SoupBuffer *expected_response,
gboolean persistent, gboolean cancel)
{
SoupRequester *requester;
SoupRequest *request;
guint started_id;
SoupSocket *socket = NULL;
SoupMessage *msg;
RequestData data;
if (SOUP_IS_SESSION_ASYNC (session))
requester = SOUP_REQUESTER (soup_session_get_feature (session, SOUP_TYPE_REQUESTER));
else
requester = NULL;
data.body = g_string_new (NULL);
data.cancel = cancel;
if (requester)
request = soup_requester_request_uri (requester, uri, NULL);
else
request = soup_session_request_uri (session, uri, NULL);
if (cancel) {
msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
g_signal_connect (msg, "got-headers",
G_CALLBACK (cancel_message), session);
g_object_unref (msg);
}
started_id = g_signal_connect (session, "request-started",
G_CALLBACK (request_started),
&socket);
soup_request_send_async (request, NULL, callback, &data);
loop = g_main_loop_new (soup_session_get_async_context (session), TRUE);
g_main_loop_run (loop);
g_main_loop_unref (loop);
g_signal_handler_disconnect (session, started_id);
if (SOUP_REQUEST_HTTP (request)->status_code != expected_status) {
debug_printf (1, " GET failed: %d %s (expected %d)\n",
msg->status_code, msg->reason_phrase,
expected_status);
g_object_unref (socket);
g_object_unref (request);
errors++;
return;
}
g_object_unref (request);
if (!expected_response) {
if (data.body->len) {
debug_printf (1, " body length mismatch: expected 0, got %d\n",
(int)data.body->len);
errors++;
}
} else if (data.body->len != expected_response->length) {
debug_printf (1, " body length mismatch: expected %d, got %d\n",
(int)expected_response->length, (int)data.body->len);
errors++;
} else if (memcmp (data.body->str, expected_response->data,
expected_response->length) != 0) {
debug_printf (1, " body data mismatch\n");
errors++;
}
if (persistent) {
if (!soup_socket_is_connected (socket)) {
debug_printf (1, " socket not still connected!\n");
errors++;
}
} else {
if (soup_socket_is_connected (socket)) {
debug_printf (1, " socket still connected!\n");
errors++;
}
}
g_object_unref (socket);
g_string_free (data.body, TRUE);
}
static void
do_test_for_thread_and_context (SoupSession *session, const char *base_uri)
{
SoupRequester *requester;
SoupURI *uri;
if (SOUP_IS_SESSION_ASYNC (session)) {
requester = soup_requester_new ();
soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
g_object_unref (requester);
}
soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);
debug_printf (1, " basic test\n");
uri = soup_uri_new (base_uri);
do_async_test (session, uri, test_sent,
SOUP_STATUS_OK, response,
TRUE, FALSE);
soup_uri_free (uri);
debug_printf (1, " chunked test\n");
uri = soup_uri_new (base_uri);
soup_uri_set_path (uri, "/chunked");
do_async_test (session, uri, test_sent,
SOUP_STATUS_OK, response,
TRUE, FALSE);
soup_uri_free (uri);
debug_printf (1, " auth test\n");
uri = soup_uri_new (base_uri);
soup_uri_set_path (uri, "/auth");
do_async_test (session, uri, auth_test_sent,
SOUP_STATUS_UNAUTHORIZED, auth_response,
TRUE, FALSE);
soup_uri_free (uri);
debug_printf (1, " non-persistent test\n");
uri = soup_uri_new (base_uri);
soup_uri_set_path (uri, "/non-persistent");
do_async_test (session, uri, test_sent,
SOUP_STATUS_OK, response,
FALSE, FALSE);
soup_uri_free (uri);
debug_printf (1, " cancellation test\n");
uri = soup_uri_new (base_uri);
soup_uri_set_path (uri, "/");
do_async_test (session, uri, test_sent,
SOUP_STATUS_FORBIDDEN, NULL,
FALSE, TRUE);
soup_uri_free (uri);
}
static void
do_simple_test (const char *uri, gboolean plain_session)
{
SoupSession *session;
debug_printf (1, "Simple streaming test with %s\n",
plain_session ? "SoupSession" : "SoupSessionAsync");
session = soup_test_session_new (plain_session ? SOUP_TYPE_SESSION : SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
NULL);
do_test_for_thread_and_context (session, uri);
soup_test_session_abort_unref (session);
}
static void
do_test_with_context_and_type (const char *uri, gboolean plain_session)
{
GMainContext *async_context;
SoupSession *session;
async_context = g_main_context_new ();
g_main_context_push_thread_default (async_context);
session = soup_test_session_new (plain_session ? SOUP_TYPE_SESSION : SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_ASYNC_CONTEXT, async_context,
SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
NULL);
do_test_for_thread_and_context (session, uri);
soup_test_session_abort_unref (session);
g_main_context_pop_thread_default (async_context);
g_main_context_unref (async_context);
}
static gpointer
do_test_with_context (gpointer uri)
{
do_test_with_context_and_type (uri, FALSE);
return NULL;
}
static gpointer
do_plain_test_with_context (gpointer uri)
{
do_test_with_context_and_type (uri, TRUE);
return NULL;
}
static void
do_context_test (const char *uri, gboolean plain_session)
{
debug_printf (1, "Streaming with a non-default-context with %s\n",
plain_session ? "SoupSession" : "SoupSessionAsync");
if (plain_session)
do_plain_test_with_context ((gpointer)uri);
else
do_test_with_context ((gpointer)uri);
}
static void
do_thread_test (const char *uri, gboolean plain_session)
{
GThread *thread;
debug_printf (1, "Streaming in another thread with %s\n",
plain_session ? "SoupSession" : "SoupSessionAsync");
thread = g_thread_new ("do_test_with_context",
plain_session ? do_plain_test_with_context : do_test_with_context,
(gpointer)uri);
g_thread_join (thread);
}
static void
do_sync_request (SoupSession *session, SoupRequest *request,
guint expected_status, SoupBuffer *expected_response,
gboolean persistent, gboolean cancel)
{
GInputStream *in;
SoupMessage *msg;
GError *error = NULL;
GString *body;
char buf[1024];
gssize nread;
guint started_id;
SoupSocket *socket = NULL;
if (cancel) {
msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
g_signal_connect (msg, "got-headers",
G_CALLBACK (cancel_message), session);
g_object_unref (msg);
}
started_id = g_signal_connect (session, "request-started",
G_CALLBACK (request_started),
&socket);
in = soup_request_send (request, NULL, &error);
g_signal_handler_disconnect (session, started_id);
if (cancel) {
if (in) {
debug_printf (1, " send succeeded??\n");
errors++;
g_input_stream_close (in, NULL, NULL);
g_object_unref (in);
} else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
debug_printf (1, " send failed with wrong error: %s\n", error->message);
errors++;
}
g_clear_error (&error);
g_object_unref (socket);
return;
} else if (!in) {
debug_printf (1, " soup_request_send failed: %s\n",
error->message);
g_clear_error (&error);
g_object_unref (socket);
errors++;
return;
}
if (SOUP_REQUEST_HTTP (request)->status_code != expected_status) {
debug_printf (1, " GET failed: %d %s\n", msg->status_code,
msg->reason_phrase);
g_object_unref (in);
g_object_unref (socket);
errors++;
return;
}
body = g_string_new (NULL);
do {
nread = g_input_stream_read (in, buf, sizeof (buf),
NULL, &error);
if (nread == -1) {
debug_printf (1, " g_input_stream_read failed: %s\n",
error->message);
g_clear_error (&error);
errors++;
break;
}
g_string_append_len (body, buf, nread);
} while (nread > 0);
if (!g_input_stream_close (in, NULL, &error)) {
debug_printf (1, " g_input_stream_close failed: %s\n",
error->message);
g_clear_error (&error);
errors++;
}
g_object_unref (in);
if (!expected_response) {
if (body->len) {
debug_printf (1, " body length mismatch: expected 0, got %d\n",
(int)body->len);
errors++;
}
} else if (body->len != expected_response->length) {
debug_printf (1, " body length mismatch: expected %d, got %d\n",
(int)expected_response->length, (int)body->len);
errors++;
} else if (memcmp (body->str, expected_response->data, body->len) != 0) {
debug_printf (1, " body data mismatch\n");
errors++;
}
if (persistent) {
if (!soup_socket_is_connected (socket)) {
debug_printf (1, " socket not still connected!\n");
errors++;
}
} else {
if (soup_socket_is_connected (socket)) {
debug_printf (1, " socket still connected!\n");
errors++;
}
}
g_object_unref (socket);
g_string_free (body, TRUE);
}
static void
do_sync_test (const char *uri_string, gboolean plain_session)
{
SoupSession *session;
SoupRequester *requester;
SoupRequest *request;
SoupURI *uri;
debug_printf (1, "Sync streaming with %s\n",
plain_session ? "SoupSession" : "SoupSessionSync");
session = soup_test_session_new (plain_session ? SOUP_TYPE_SESSION : SOUP_TYPE_SESSION_SYNC, NULL);
if (!plain_session) {
requester = soup_requester_new ();
soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
g_object_unref (requester);
}
uri = soup_uri_new (uri_string);
debug_printf (1, " basic test\n");
if (plain_session)
request = soup_session_request_uri (session, uri, NULL);
else
request = soup_requester_request_uri (requester, uri, NULL);
do_sync_request (session, request,
SOUP_STATUS_OK, response,
TRUE, FALSE);
g_object_unref (request);
debug_printf (1, " chunked test\n");
soup_uri_set_path (uri, "/chunked");
if (plain_session)
request = soup_session_request_uri (session, uri, NULL);
else
request = soup_requester_request_uri (requester, uri, NULL);
do_sync_request (session, request,
SOUP_STATUS_OK, response,
TRUE, FALSE);
g_object_unref (request);
debug_printf (1, " auth test\n");
soup_uri_set_path (uri, "/auth");
if (plain_session)
request = soup_session_request_uri (session, uri, NULL);
else
request = soup_requester_request_uri (requester, uri, NULL);
do_sync_request (session, request,
SOUP_STATUS_UNAUTHORIZED, auth_response,
TRUE, FALSE);
g_object_unref (request);
debug_printf (1, " non-persistent test\n");
soup_uri_set_path (uri, "/non-persistent");
if (plain_session)
request = soup_session_request_uri (session, uri, NULL);
else
request = soup_requester_request_uri (requester, uri, NULL);
do_sync_request (session, request,
SOUP_STATUS_OK, response,
FALSE, FALSE);
g_object_unref (request);
debug_printf (1, " cancel test\n");
soup_uri_set_path (uri, "/");
if (plain_session)
request = soup_session_request_uri (session, uri, NULL);
else
request = soup_requester_request_uri (requester, uri, NULL);
do_sync_request (session, request,
SOUP_STATUS_FORBIDDEN, NULL,
TRUE, TRUE);
g_object_unref (request);
soup_test_session_abort_unref (session);
soup_uri_free (uri);
}
int
main (int argc, char **argv)
{
char *uri;
test_init (argc, argv, NULL);
get_index ();
server = soup_test_server_new (TRUE);
soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
uri = g_strdup_printf ("http://127.0.0.1:%u/foo", soup_server_get_port (server));
do_simple_test (uri, FALSE);
do_thread_test (uri, FALSE);
do_context_test (uri, FALSE);
do_sync_test (uri, FALSE);
do_simple_test (uri, TRUE);
do_thread_test (uri, TRUE);
do_context_test (uri, TRUE);
do_sync_test (uri, TRUE);
g_free (uri);
soup_buffer_free (response);
soup_buffer_free (auth_response);
soup_test_server_quit_unref (server);
test_cleanup ();
return errors != 0;
}
|