1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Red Hat, Inc.
13 #include <libsoup/soup.h>
15 #include "test-utils.h"
19 SoupBuffer *chunks[3];
20 int next, nwrote, nfreed;
25 error_chunk_allocator (SoupMessage *msg, gsize max_len, gpointer user_data)
27 /* This should never be called, because there is no response body. */
28 debug_printf (1, " error_chunk_allocator called!\n");
30 return soup_buffer_new (SOUP_MEMORY_TAKE, g_malloc (100), 100);
34 write_next_chunk (SoupMessage *msg, gpointer user_data)
36 PutTestData *ptd = user_data;
38 debug_printf (2, " writing chunk %d\n", ptd->next);
40 if (ptd->streaming && ptd->next > 0 && ptd->chunks[ptd->next - 1]) {
41 debug_printf (1, " error: next chunk requested before last one freed!\n");
45 if (ptd->next < G_N_ELEMENTS (ptd->chunks)) {
46 soup_message_body_append_buffer (msg->request_body,
47 ptd->chunks[ptd->next]);
48 soup_buffer_free (ptd->chunks[ptd->next]);
51 soup_message_body_complete (msg->request_body);
52 soup_session_unpause_message (ptd->session, msg);
55 /* This is not a supported part of the API. Use SOUP_MESSAGE_CAN_REBUILD
59 write_next_chunk_streaming_hack (SoupMessage *msg, gpointer user_data)
61 PutTestData *ptd = user_data;
64 debug_printf (2, " freeing chunk at %d\n", ptd->nfreed);
65 chunk = soup_message_body_get_chunk (msg->request_body, ptd->nfreed);
67 ptd->nfreed += chunk->length;
68 soup_message_body_wrote_chunk (msg->request_body, chunk);
69 soup_buffer_free (chunk);
71 debug_printf (1, " error: written chunk does not exist!\n");
74 write_next_chunk (msg, user_data);
78 wrote_body_data (SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
80 PutTestData *ptd = user_data;
82 debug_printf (2, " wrote_body_data, %d bytes\n",
84 ptd->nwrote += chunk->length;
88 clear_buffer_ptr (gpointer data)
90 SoupBuffer **buffer_ptr = data;
92 debug_printf (2, " clearing chunk\n");
94 (*buffer_ptr)->length = 0;
95 g_free ((char *)(*buffer_ptr)->data);
98 debug_printf (2, " chunk is already clear!\n");
103 /* Put a chunk containing @text into *@buffer, set up so that it will
104 * clear out *@buffer when the chunk is freed, allowing us to make sure
105 * the set_accumulate(FALSE) is working.
108 make_put_chunk (SoupBuffer **buffer, const char *text)
110 *buffer = soup_buffer_new_with_owner (g_strdup (text), strlen (text),
111 buffer, clear_buffer_ptr);
115 setup_request_body (PutTestData *ptd)
117 make_put_chunk (&ptd->chunks[0], "one\r\n");
118 make_put_chunk (&ptd->chunks[1], "two\r\n");
119 make_put_chunk (&ptd->chunks[2], "three\r\n");
120 ptd->next = ptd->nwrote = ptd->nfreed = 0;
124 restarted_streaming (SoupMessage *msg, gpointer user_data)
126 PutTestData *ptd = user_data;
128 debug_printf (2, " --restarting--\n");
130 /* We're streaming, and we had to restart. So the data need
133 setup_request_body (ptd);
135 /* The 302 redirect will turn it into a GET request and
136 * reset the body encoding back to "NONE". Fix that.
138 soup_message_headers_set_encoding (msg->request_headers,
139 SOUP_ENCODING_CHUNKED);
140 msg->method = SOUP_METHOD_PUT;
144 restarted_streaming_hack (SoupMessage *msg, gpointer user_data)
146 restarted_streaming (msg, user_data);
147 soup_message_body_truncate (msg->request_body);
151 HACKY_STREAMING = (1 << 0),
152 PROPER_STREAMING = (1 << 1),
157 do_request_test (SoupSession *session, SoupURI *base_uri, RequestTestFlags flags)
159 SoupURI *uri = base_uri;
162 const char *client_md5, *server_md5;
166 debug_printf (1, "PUT");
167 if (flags & HACKY_STREAMING)
168 debug_printf (1, " w/ hacky streaming");
169 else if (flags & PROPER_STREAMING)
170 debug_printf (1, " w/ proper streaming");
171 if (flags & RESTART) {
172 debug_printf (1, " and restart");
173 uri = soup_uri_copy (base_uri);
174 soup_uri_set_path (uri, "/redirect");
176 debug_printf (1, "\n");
178 ptd.session = session;
179 setup_request_body (&ptd);
180 ptd.streaming = flags & (HACKY_STREAMING | PROPER_STREAMING);
182 check = g_checksum_new (G_CHECKSUM_MD5);
184 for (i = 0; i < 3; i++) {
185 g_checksum_update (check, (guchar *)ptd.chunks[i]->data,
186 ptd.chunks[i]->length);
187 length += ptd.chunks[i]->length;
189 client_md5 = g_checksum_get_string (check);
191 msg = soup_message_new_from_uri ("PUT", uri);
192 soup_message_headers_set_encoding (msg->request_headers, SOUP_ENCODING_CHUNKED);
193 soup_message_body_set_accumulate (msg->request_body, FALSE);
194 soup_message_set_chunk_allocator (msg, error_chunk_allocator, NULL, NULL);
195 if (flags & HACKY_STREAMING) {
196 g_signal_connect (msg, "wrote_chunk",
197 G_CALLBACK (write_next_chunk_streaming_hack), &ptd);
198 if (flags & RESTART) {
199 g_signal_connect (msg, "restarted",
200 G_CALLBACK (restarted_streaming_hack), &ptd);
203 g_signal_connect (msg, "wrote_chunk",
204 G_CALLBACK (write_next_chunk), &ptd);
207 if (flags & PROPER_STREAMING) {
208 soup_message_set_flags (msg, SOUP_MESSAGE_CAN_REBUILD);
209 if (flags & RESTART) {
210 g_signal_connect (msg, "restarted",
211 G_CALLBACK (restarted_streaming), &ptd);
215 g_signal_connect (msg, "wrote_headers",
216 G_CALLBACK (write_next_chunk), &ptd);
217 g_signal_connect (msg, "wrote_body_data",
218 G_CALLBACK (wrote_body_data), &ptd);
219 soup_session_send_message (session, msg);
221 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
222 debug_printf (1, " message failed: %d %s\n",
223 msg->status_code, msg->reason_phrase);
227 if (msg->request_body->data) {
228 debug_printf (1, " msg->request_body set!\n");
231 if (msg->request_body->length != length || length != ptd.nwrote) {
232 debug_printf (1, " sent length mismatch: %d vs %d vs %d\n",
233 (int)msg->request_body->length, length, ptd.nwrote);
237 server_md5 = soup_message_headers_get_one (msg->response_headers,
239 if (!server_md5 || strcmp (client_md5, server_md5) != 0) {
240 debug_printf (1, " client/server data mismatch: %s vs %s\n",
241 client_md5, server_md5 ? server_md5 : "(null)");
245 g_object_unref (msg);
246 g_checksum_free (check);
253 SoupBuffer *current_chunk;
259 chunk_allocator (SoupMessage *msg, gsize max_len, gpointer user_data)
261 GetTestData *gtd = user_data;
263 debug_printf (2, " allocating chunk\n");
265 if (gtd->current_chunk) {
266 debug_printf (1, " error: next chunk allocated before last one freed!\n");
269 gtd->current_chunk = soup_buffer_new_with_owner (g_malloc (6), 6,
272 return gtd->current_chunk;
276 got_chunk (SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
278 GetTestData *gtd = user_data;
280 debug_printf (2, " got chunk, %d bytes\n",
282 if (chunk != gtd->current_chunk) {
283 debug_printf (1, "chunk mismatch! %p vs %p\n",
284 chunk, gtd->current_chunk);
287 g_checksum_update (gtd->check, (guchar *)chunk->data, chunk->length);
288 gtd->length += chunk->length;
292 do_response_test (SoupSession *session, SoupURI *base_uri)
296 const char *client_md5, *server_md5;
298 debug_printf (1, "GET\n");
300 gtd.current_chunk = NULL;
302 gtd.check = g_checksum_new (G_CHECKSUM_MD5);
304 msg = soup_message_new_from_uri ("GET", base_uri);
305 soup_message_body_set_accumulate (msg->response_body, FALSE);
306 soup_message_set_chunk_allocator (msg, chunk_allocator, >d, NULL);
307 g_signal_connect (msg, "got_chunk",
308 G_CALLBACK (got_chunk), >d);
309 soup_session_send_message (session, msg);
311 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
312 debug_printf (1, " message failed: %d %s\n",
313 msg->status_code, msg->reason_phrase);
317 if (msg->response_body->data) {
318 debug_printf (1, " msg->response_body set!\n");
321 if (soup_message_headers_get_content_length (msg->response_headers) != gtd.length) {
322 debug_printf (1, " received length mismatch: %d vs %d\n",
323 (int)soup_message_headers_get_content_length (msg->response_headers), gtd.length);
327 client_md5 = g_checksum_get_string (gtd.check);
328 server_md5 = soup_message_headers_get_one (msg->response_headers,
330 if (!server_md5 || strcmp (client_md5, server_md5) != 0) {
331 debug_printf (1, " client/server data mismatch: %s vs %s\n",
332 client_md5, server_md5 ? server_md5 : "(null)");
336 g_object_unref (msg);
337 g_checksum_free (gtd.check);
340 /* Make sure TEMPORARY buffers are handled properly with non-accumulating
341 * message bodies. Part of https://bugs.webkit.org/show_bug.cgi?id=18343
345 temp_test_wrote_chunk (SoupMessage *msg, gpointer session)
349 chunk = soup_message_body_get_chunk (msg->request_body, 5);
351 /* When the bug is present, the second chunk will also be
352 * discarded after the first is written, which will cause
353 * the I/O to stall since soup-message-io will think it's
354 * done, but it hasn't written Content-Length bytes yet.
357 debug_printf (1, " Lost second chunk!\n");
359 soup_session_abort (session);
361 soup_buffer_free (chunk);
363 g_signal_handlers_disconnect_by_func (msg, temp_test_wrote_chunk, session);
367 do_temporary_test (SoupSession *session, SoupURI *base_uri)
371 const char *server_md5;
373 debug_printf (1, "PUT w/ temporary buffers\n");
375 msg = soup_message_new_from_uri ("PUT", base_uri);
376 soup_message_body_append (msg->request_body, SOUP_MEMORY_TEMPORARY,
378 soup_message_body_append (msg->request_body, SOUP_MEMORY_STATIC,
380 soup_message_body_set_accumulate (msg->request_body, FALSE);
382 client_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5,
383 "one\r\ntwo\r\n", 10);
384 g_signal_connect (msg, "wrote_chunk",
385 G_CALLBACK (temp_test_wrote_chunk), session);
386 soup_session_send_message (session, msg);
388 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
389 debug_printf (1, " message failed: %d %s\n",
390 msg->status_code, msg->reason_phrase);
394 server_md5 = soup_message_headers_get_one (msg->response_headers,
396 if (!server_md5 || strcmp (client_md5, server_md5) != 0) {
397 debug_printf (1, " client/server data mismatch: %s vs %s\n",
398 client_md5, server_md5 ? server_md5 : "(null)");
403 g_object_unref (msg);
407 do_chunk_tests (SoupURI *base_uri)
409 SoupSession *session;
411 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
412 do_request_test (session, base_uri, 0);
413 debug_printf (2, "\n\n");
414 do_request_test (session, base_uri, PROPER_STREAMING);
415 debug_printf (2, "\n\n");
416 do_request_test (session, base_uri, PROPER_STREAMING | RESTART);
417 debug_printf (2, "\n\n");
418 do_request_test (session, base_uri, HACKY_STREAMING);
419 debug_printf (2, "\n\n");
420 do_request_test (session, base_uri, HACKY_STREAMING | RESTART);
421 debug_printf (2, "\n\n");
422 do_response_test (session, base_uri);
423 debug_printf (2, "\n\n");
424 do_temporary_test (session, base_uri);
425 soup_test_session_abort_unref (session);
429 server_callback (SoupServer *server, SoupMessage *msg,
430 const char *path, GHashTable *query,
431 SoupClientContext *context, gpointer data)
433 SoupMessageBody *md5_body;
436 if (g_str_has_prefix (path, "/redirect")) {
437 soup_message_set_redirect (msg, SOUP_STATUS_FOUND, "/");
441 if (msg->method == SOUP_METHOD_GET) {
442 soup_message_set_response (msg, "text/plain",
444 "three\r\ntwo\r\none\r\n",
445 strlen ("three\r\ntwo\r\none\r\n"));
446 soup_buffer_free (soup_message_body_flatten (msg->response_body));
447 md5_body = msg->response_body;
448 soup_message_set_status (msg, SOUP_STATUS_OK);
449 } else if (msg->method == SOUP_METHOD_PUT) {
450 soup_message_set_status (msg, SOUP_STATUS_CREATED);
451 md5_body = msg->request_body;
453 soup_message_set_status (msg, SOUP_STATUS_METHOD_NOT_ALLOWED);
457 md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
458 (guchar *)md5_body->data,
460 soup_message_headers_append (msg->response_headers,
466 main (int argc, char **argv)
473 test_init (argc, argv, NULL);
475 server = soup_test_server_new (TRUE);
476 soup_server_add_handler (server, NULL,
477 server_callback, NULL, NULL);
478 port = soup_server_get_port (server);
480 loop = g_main_loop_new (NULL, TRUE);
482 base_uri = soup_uri_new ("http://127.0.0.1");
483 soup_uri_set_port (base_uri, port);
484 do_chunk_tests (base_uri);
485 soup_uri_free (base_uri);
487 g_main_loop_unref (loop);
488 soup_test_server_quit_unref (server);