1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright 2007-2012 Red Hat, Inc.
8 #include <libsoup/soup.h>
10 #include "test-utils.h"
17 forget_close (SoupMessage *msg, gpointer user_data)
19 soup_message_headers_remove (msg->response_headers, "Connection");
23 close_socket (SoupMessage *msg, gpointer user_data)
25 SoupSocket *sock = user_data;
28 /* Actually calling soup_socket_disconnect() here would cause
29 * us to leak memory, so just shutdown the socket instead.
31 sockfd = soup_socket_get_fd (sock);
33 shutdown (sockfd, SD_SEND);
35 shutdown (sockfd, SHUT_WR);
38 /* Then add the missing data to the message now, so SoupServer
39 * can clean up after itself properly.
41 soup_message_body_append (msg->response_body, SOUP_MEMORY_STATIC,
46 timeout_socket (SoupSocket *sock, gpointer user_data)
48 soup_socket_disconnect (sock);
52 timeout_request_started (SoupServer *server, SoupMessage *msg,
53 SoupClientContext *client, gpointer user_data)
56 GMainContext *context = soup_server_get_async_context (server);
59 sock = soup_client_context_get_socket (client);
60 readable = g_signal_connect (sock, "readable",
61 G_CALLBACK (timeout_socket), NULL);
62 while (soup_socket_is_connected (sock))
63 g_main_context_iteration (context, TRUE);
64 g_signal_handler_disconnect (sock, readable);
65 g_signal_handlers_disconnect_by_func (server, timeout_request_started, NULL);
69 setup_timeout_persistent (SoupServer *server, SoupSocket *sock)
74 /* In order for the test to work correctly, we have to
75 * close the connection *after* the client side writes
76 * the request. To ensure that this happens reliably,
77 * regardless of thread scheduling, we:
79 * 1. Try to read off the socket now, knowing it will
80 * fail (since the client is waiting for us to
81 * return a response). This will cause it to
82 * emit "readable" later.
83 * 2. Connect to the server's request-started signal.
84 * 3. Run an inner main loop from that signal handler
85 * until the socket emits "readable". (If we don't
86 * do this then it's possible the client's next
87 * request would be ready before we returned to
88 * the main loop, and so the signal would never be
90 * 4. Close the socket.
93 soup_socket_read (sock, buf, 1, &nread, NULL, NULL);
94 g_signal_connect (server, "request-started",
95 G_CALLBACK (timeout_request_started), NULL);
99 server_callback (SoupServer *server, SoupMessage *msg,
100 const char *path, GHashTable *query,
101 SoupClientContext *context, gpointer data)
103 /* The way this gets used in the tests, we don't actually
104 * need to hold it through the whole function, so it's simpler
105 * to just release it right away.
107 g_mutex_lock (&server_mutex);
108 g_mutex_unlock (&server_mutex);
110 if (msg->method != SOUP_METHOD_GET && msg->method != SOUP_METHOD_POST) {
111 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
115 if (g_str_has_prefix (path, "/content-length/")) {
116 gboolean too_long = strcmp (path, "/content-length/long") == 0;
117 gboolean no_close = strcmp (path, "/content-length/noclose") == 0;
119 soup_message_set_status (msg, SOUP_STATUS_OK);
120 soup_message_set_response (msg, "text/plain",
121 SOUP_MEMORY_STATIC, "foobar", 6);
123 soup_message_headers_set_content_length (msg->response_headers, 9);
124 soup_message_headers_append (msg->response_headers,
125 "Connection", "close");
130 /* soup-message-io will wait for us to add
131 * another chunk after the first, to fill out
132 * the declared Content-Length. Instead, we
133 * forcibly close the socket at that point.
135 sock = soup_client_context_get_socket (context);
136 g_signal_connect (msg, "wrote-chunk",
137 G_CALLBACK (close_socket), sock);
138 } else if (no_close) {
139 /* Remove the 'Connection: close' after writing
140 * the headers, so that when we check it after
141 * writing the body, we'll think we aren't
142 * supposed to close it.
144 g_signal_connect (msg, "wrote-headers",
145 G_CALLBACK (forget_close), NULL);
150 if (!strcmp (path, "/timeout-persistent")) {
153 sock = soup_client_context_get_socket (context);
154 setup_timeout_persistent (server, sock);
157 soup_message_set_status (msg, SOUP_STATUS_OK);
158 soup_message_set_response (msg, "text/plain",
159 SOUP_MEMORY_STATIC, "index", 5);
164 do_content_length_framing_test (void)
166 SoupSession *session;
168 SoupURI *request_uri;
169 goffset declared_length;
171 debug_printf (1, "\nInvalid Content-Length framing tests\n");
173 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
175 debug_printf (1, " Content-Length larger than message body length\n");
176 request_uri = soup_uri_new_with_base (base_uri, "/content-length/long");
177 msg = soup_message_new_from_uri ("GET", request_uri);
178 soup_session_send_message (session, msg);
179 if (msg->status_code != SOUP_STATUS_OK) {
180 debug_printf (1, " Unexpected response: %d %s\n",
181 msg->status_code, msg->reason_phrase);
184 declared_length = soup_message_headers_get_content_length (msg->response_headers);
185 debug_printf (2, " Content-Length: %lu, body: %s\n",
186 (gulong)declared_length, msg->response_body->data);
187 if (msg->response_body->length >= declared_length) {
188 debug_printf (1, " Body length %lu >= declared length %lu\n",
189 (gulong)msg->response_body->length,
190 (gulong)declared_length);
194 soup_uri_free (request_uri);
195 g_object_unref (msg);
197 debug_printf (1, " Server claims 'Connection: close' but doesn't\n");
198 request_uri = soup_uri_new_with_base (base_uri, "/content-length/noclose");
199 msg = soup_message_new_from_uri ("GET", request_uri);
200 soup_session_send_message (session, msg);
201 if (msg->status_code != SOUP_STATUS_OK) {
202 debug_printf (1, " Unexpected response: %d %s\n",
203 msg->status_code, msg->reason_phrase);
206 declared_length = soup_message_headers_get_content_length (msg->response_headers);
207 debug_printf (2, " Content-Length: %lu, body: %s\n",
208 (gulong)declared_length, msg->response_body->data);
209 if (msg->response_body->length != declared_length) {
210 debug_printf (1, " Body length %lu != declared length %lu\n",
211 (gulong)msg->response_body->length,
212 (gulong)declared_length);
216 soup_uri_free (request_uri);
217 g_object_unref (msg);
219 soup_test_session_abort_unref (session);
223 request_started_socket_collector (SoupSession *session, SoupMessage *msg,
224 SoupSocket *socket, gpointer user_data)
226 SoupSocket **sockets = user_data;
229 debug_printf (2, " msg %p => socket %p\n", msg, socket);
230 for (i = 0; i < 4; i++) {
232 /* We ref the socket to make sure that even if
233 * it gets disconnected, it doesn't get freed,
234 * since our checks would get messed up if the
235 * slice allocator reused the same address for
236 * two consecutive sockets.
238 sockets[i] = g_object_ref (socket);
243 debug_printf (1, " socket queue overflowed!\n");
245 soup_session_cancel_message (session, msg, SOUP_STATUS_CANCELLED);
249 do_timeout_test_for_session (SoupSession *session)
252 SoupSocket *sockets[4] = { NULL, NULL, NULL, NULL };
253 SoupURI *timeout_uri;
256 g_signal_connect (session, "request-started",
257 G_CALLBACK (request_started_socket_collector),
260 debug_printf (1, " First message\n");
261 timeout_uri = soup_uri_new_with_base (base_uri, "/timeout-persistent");
262 msg = soup_message_new_from_uri ("GET", timeout_uri);
263 soup_uri_free (timeout_uri);
264 soup_session_send_message (session, msg);
265 if (msg->status_code != SOUP_STATUS_OK) {
266 debug_printf (1, " Unexpected response: %d %s\n",
267 msg->status_code, msg->reason_phrase);
271 debug_printf (1, " Message was retried??\n");
273 sockets[1] = sockets[2] = sockets[3] = NULL;
275 g_object_unref (msg);
277 debug_printf (1, " Second message\n");
278 msg = soup_message_new_from_uri ("GET", base_uri);
279 soup_session_send_message (session, msg);
280 if (msg->status_code != SOUP_STATUS_OK) {
281 debug_printf (1, " Unexpected response: %d %s\n",
282 msg->status_code, msg->reason_phrase);
285 if (sockets[1] != sockets[0]) {
286 debug_printf (1, " Message was not retried on existing connection\n");
288 } else if (!sockets[2]) {
289 debug_printf (1, " Message was not retried after disconnect\n");
291 } else if (sockets[2] == sockets[1]) {
292 debug_printf (1, " Message was retried on closed connection??\n");
294 } else if (sockets[3]) {
295 debug_printf (1, " Message was retried again??\n");
298 g_object_unref (msg);
300 for (i = 0; sockets[i]; i++)
301 g_object_unref (sockets[i]);
305 do_persistent_connection_timeout_test (void)
307 SoupSession *session;
309 debug_printf (1, "\nUnexpected timing out of persistent connections\n");
311 debug_printf (1, " Async session\n");
312 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
313 do_timeout_test_for_session (session);
314 soup_test_session_abort_unref (session);
316 debug_printf (1, " Sync session\n");
317 session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
318 do_timeout_test_for_session (session);
319 soup_test_session_abort_unref (session);
322 static GMainLoop *max_conns_loop;
323 static int msgs_done;
324 static guint quit_loop_timeout;
326 #define TEST_CONNS (MAX_CONNS * 2)
329 idle_start_server (gpointer data)
331 g_mutex_unlock (&server_mutex);
336 quit_loop (gpointer data)
338 quit_loop_timeout = 0;
339 g_main_loop_quit (max_conns_loop);
344 max_conns_request_started (SoupSession *session, SoupMessage *msg,
345 SoupSocket *socket, gpointer user_data)
347 if (++msgs_done == MAX_CONNS) {
348 if (quit_loop_timeout)
349 g_source_remove (quit_loop_timeout);
350 quit_loop_timeout = g_timeout_add (100, quit_loop, NULL);
355 max_conns_message_complete (SoupSession *session, SoupMessage *msg, gpointer user_data)
357 if (++msgs_done == TEST_CONNS)
358 g_main_loop_quit (max_conns_loop);
362 do_max_conns_test_for_session (SoupSession *session)
364 SoupMessage *msgs[TEST_CONNS];
367 max_conns_loop = g_main_loop_new (NULL, TRUE);
369 g_mutex_lock (&server_mutex);
371 g_signal_connect (session, "request-started",
372 G_CALLBACK (max_conns_request_started), NULL);
374 for (i = 0; i < TEST_CONNS; i++) {
375 msgs[i] = soup_message_new_from_uri ("GET", base_uri);
376 g_object_ref (msgs[i]);
377 soup_session_queue_message (session, msgs[i],
378 max_conns_message_complete, NULL);
381 g_main_loop_run (max_conns_loop);
382 if (msgs_done != MAX_CONNS) {
383 debug_printf (1, " Queued %d connections out of max %d?",
384 msgs_done, MAX_CONNS);
387 g_signal_handlers_disconnect_by_func (session, max_conns_request_started, NULL);
390 g_idle_add (idle_start_server, NULL);
391 quit_loop_timeout = g_timeout_add (1000, quit_loop, NULL);
392 g_main_loop_run (max_conns_loop);
394 for (i = 0; i < TEST_CONNS; i++) {
395 if (!SOUP_STATUS_IS_SUCCESSFUL (msgs[i]->status_code)) {
396 debug_printf (1, " Message %d failed? %d %s\n",
397 i, msgs[i]->status_code,
398 msgs[i]->reason_phrase ? msgs[i]->reason_phrase : "-");
403 if (msgs_done != TEST_CONNS) {
404 /* Clean up so we don't get a spurious "Leaked
407 for (i = 0; i < TEST_CONNS; i++)
408 soup_session_cancel_message (session, msgs[i], SOUP_STATUS_CANCELLED);
409 g_main_loop_run (max_conns_loop);
412 g_main_loop_unref (max_conns_loop);
413 if (quit_loop_timeout)
414 g_source_remove (quit_loop_timeout);
416 for (i = 0; i < TEST_CONNS; i++)
417 g_object_unref (msgs[i]);
421 do_max_conns_test (void)
423 SoupSession *session;
425 debug_printf (1, "\nExceeding max-conns\n");
427 debug_printf (1, " Async session\n");
428 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
429 SOUP_SESSION_MAX_CONNS, MAX_CONNS,
431 do_max_conns_test_for_session (session);
432 soup_test_session_abort_unref (session);
434 debug_printf (1, " Sync session\n");
435 session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
436 SOUP_SESSION_MAX_CONNS, MAX_CONNS,
438 do_max_conns_test_for_session (session);
439 soup_test_session_abort_unref (session);
445 np_request_started (SoupSession *session, SoupMessage *msg,
446 SoupSocket *socket, gpointer user_data)
448 SoupSocket **save_socket = user_data;
450 *save_socket = g_object_ref (socket);
454 np_request_unqueued (SoupSession *session, SoupMessage *msg,
457 SoupSocket *socket = *(SoupSocket **)user_data;
459 if (soup_socket_is_connected (socket)) {
460 debug_printf (1, " socket is still connected\n");
464 g_main_loop_quit (loop);
468 do_non_persistent_test_for_session (SoupSession *session)
471 SoupSocket *socket = NULL;
473 loop = g_main_loop_new (NULL, FALSE);
475 g_signal_connect (session, "request-started",
476 G_CALLBACK (np_request_started),
478 g_signal_connect (session, "request-unqueued",
479 G_CALLBACK (np_request_unqueued),
482 msg = soup_message_new_from_uri ("GET", base_uri);
483 soup_message_headers_append (msg->request_headers, "Connection", "close");
485 soup_session_queue_message (session, msg, NULL, NULL);
486 g_main_loop_run (loop);
488 if (msg->status_code != SOUP_STATUS_OK) {
489 debug_printf (1, " Unexpected response: %d %s\n",
490 msg->status_code, msg->reason_phrase);
493 g_object_unref (msg);
497 do_non_persistent_connection_test (void)
499 SoupSession *session;
501 debug_printf (1, "\nNon-persistent connections are closed immediately\n");
503 debug_printf (1, " Async session\n");
504 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
505 do_non_persistent_test_for_session (session);
506 soup_test_session_abort_unref (session);
508 debug_printf (1, " Sync session\n");
509 session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
510 do_non_persistent_test_for_session (session);
511 soup_test_session_abort_unref (session);
515 do_non_idempotent_test_for_session (SoupSession *session)
518 SoupSocket *sockets[4] = { NULL, NULL, NULL, NULL };
521 g_signal_connect (session, "request-started",
522 G_CALLBACK (request_started_socket_collector),
525 debug_printf (2, " GET\n");
526 msg = soup_message_new_from_uri ("GET", base_uri);
527 soup_session_send_message (session, msg);
528 if (msg->status_code != SOUP_STATUS_OK) {
529 debug_printf (1, " Unexpected response: %d %s\n",
530 msg->status_code, msg->reason_phrase);
534 debug_printf (1, " Message was retried??\n");
536 sockets[1] = sockets[2] = sockets[3] = NULL;
538 g_object_unref (msg);
540 debug_printf (2, " POST\n");
541 msg = soup_message_new_from_uri ("POST", base_uri);
542 soup_session_send_message (session, msg);
543 if (msg->status_code != SOUP_STATUS_OK) {
544 debug_printf (1, " Unexpected response: %d %s\n",
545 msg->status_code, msg->reason_phrase);
548 if (sockets[1] == sockets[0]) {
549 debug_printf (1, " Message was sent on existing connection!\n");
553 debug_printf (1, " Too many connections used...\n");
556 g_object_unref (msg);
558 for (i = 0; sockets[i]; i++)
559 g_object_unref (sockets[i]);
563 do_non_idempotent_connection_test (void)
565 SoupSession *session;
567 debug_printf (1, "\nNon-idempotent methods are always sent on new connections\n");
569 debug_printf (1, " Async session\n");
570 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
571 do_non_idempotent_test_for_session (session);
572 soup_test_session_abort_unref (session);
574 debug_printf (1, " Sync session\n");
575 session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
576 do_non_idempotent_test_for_session (session);
577 soup_test_session_abort_unref (session);
581 main (int argc, char **argv)
583 test_init (argc, argv, NULL);
585 server = soup_test_server_new (TRUE);
586 soup_server_add_handler (server, NULL, server_callback, "http", NULL);
587 base_uri = soup_uri_new ("http://127.0.0.1/");
588 soup_uri_set_port (base_uri, soup_server_get_port (server));
590 do_content_length_framing_test ();
591 do_persistent_connection_timeout_test ();
592 do_max_conns_test ();
593 do_non_persistent_connection_test ();
594 do_non_idempotent_connection_test ();
596 soup_uri_free (base_uri);
597 soup_test_server_quit_unref (server);