[libsoup] soup-message-io: fix up async body close semantics



commit aa42dd5588eaf617264cc0c9a0984b8c79f22603
Author: Dan Winship <danw gnome org>
Date:   Mon Mar 2 18:39:27 2015 -0500

    soup-message-io: fix up async body close semantics
    
    add13ea0 fixed soup-message-io to do an async close of
    io->body_ostream, so that in cases where it was necessary to write out
    the final "0" chunk, and the stream was not immediately writable, it
    wouldn't block.
    
    However, it was emitting :wrote-body and potentially :finished before
    the close finished in this case, which made it difficult to ensure
    that the last request on a session was cleaned up correctly in the
    test programs.
    
    Fix this by adding in a new IO_STATE_BODY_FLUSH to do this step before
    proceeding to IO_STATE_FINISHING. But also, to simplify things, just
    do the sync close if we're not doing chunked encoding, since it's
    guaranteed to not block in that case.
    
    (Also revert the changes that had been made to continue-test for that
    commit, since they are no longer needed.)

 libsoup/soup-message-io.c |   19 +++++++++++--------
 tests/continue-test.c     |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 8 deletions(-)
---
diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c
index 021a5ec..bec3e34 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -36,6 +36,7 @@ typedef enum {
        SOUP_MESSAGE_IO_STATE_BODY_START,
        SOUP_MESSAGE_IO_STATE_BODY,
        SOUP_MESSAGE_IO_STATE_BODY_DATA,
+       SOUP_MESSAGE_IO_STATE_BODY_FLUSH,
        SOUP_MESSAGE_IO_STATE_BODY_DONE,
        SOUP_MESSAGE_IO_STATE_FINISHING,
        SOUP_MESSAGE_IO_STATE_DONE
@@ -486,7 +487,7 @@ io_write (SoupMessage *msg, gboolean blocking,
                if (!io->write_length &&
                    io->write_encoding != SOUP_ENCODING_EOF &&
                    io->write_encoding != SOUP_ENCODING_CHUNKED) {
-                       io->write_state = SOUP_MESSAGE_IO_STATE_BODY_DONE;
+                       io->write_state = SOUP_MESSAGE_IO_STATE_BODY_FLUSH;
                        break;
                }
 
@@ -498,7 +499,7 @@ io_write (SoupMessage *msg, gboolean blocking,
                                return FALSE;
                        }
                        if (!io->write_chunk->length) {
-                               io->write_state = SOUP_MESSAGE_IO_STATE_BODY_DONE;
+                               io->write_state = SOUP_MESSAGE_IO_STATE_BODY_FLUSH;
                                break;
                        }
                }
@@ -528,7 +529,7 @@ io_write (SoupMessage *msg, gboolean blocking,
        case SOUP_MESSAGE_IO_STATE_BODY_DATA:
                io->written = 0;
                if (io->write_chunk->length == 0) {
-                       io->write_state = SOUP_MESSAGE_IO_STATE_BODY_DONE;
+                       io->write_state = SOUP_MESSAGE_IO_STATE_BODY_FLUSH;
                        break;
                }
 
@@ -544,9 +545,9 @@ io_write (SoupMessage *msg, gboolean blocking,
                break;
 
 
-       case SOUP_MESSAGE_IO_STATE_BODY_DONE:
+       case SOUP_MESSAGE_IO_STATE_BODY_FLUSH:
                if (io->body_ostream) {
-                       if (blocking) {
+                       if (blocking || io->write_encoding != SOUP_ENCODING_CHUNKED) {
                                if (!g_output_stream_close (io->body_ostream, cancellable, error))
                                        return FALSE;
                                g_clear_object (&io->body_ostream);
@@ -562,11 +563,13 @@ io_write (SoupMessage *msg, gboolean blocking,
                        }
                }
 
+               io->write_state = SOUP_MESSAGE_IO_STATE_BODY_DONE;
+               break;
+
+
+       case SOUP_MESSAGE_IO_STATE_BODY_DONE:
                io->write_state = SOUP_MESSAGE_IO_STATE_FINISHING;
                soup_message_wrote_body (msg);
-
-               if (io->async_close_wait)
-                       return TRUE;
                break;
 
 
diff --git a/tests/continue-test.c b/tests/continue-test.c
index 1bf0774..5e0c660 100644
--- a/tests/continue-test.c
+++ b/tests/continue-test.c
@@ -44,6 +44,7 @@ EVENT_HANDLER (got_body)
 EVENT_HANDLER (wrote_informational)
 EVENT_HANDLER (wrote_headers)
 EVENT_HANDLER (wrote_body)
+EVENT_HANDLER (finished)
 
 static void
 do_message (const char *path, gboolean long_body,
@@ -89,6 +90,8 @@ do_message (const char *path, gboolean long_body,
                          G_CALLBACK (wrote_headers), "client");
        g_signal_connect (msg, "wrote_body",
                          G_CALLBACK (wrote_body), "client");
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (finished), "client");
 
        events = NULL;
        session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
@@ -155,8 +158,10 @@ do_test_unauth_short_noexpect_nopass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_CREATED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -170,8 +175,10 @@ do_test_unauth_long_noexpect_nopass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -187,8 +194,10 @@ do_test_unauth_short_expect_nopass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_CREATED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -200,8 +209,10 @@ do_test_unauth_long_expect_nopass (void)
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -215,8 +226,10 @@ do_test_auth_short_noexpect_nopass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -230,8 +243,10 @@ do_test_auth_long_noexpect_nopass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -243,8 +258,10 @@ do_test_auth_short_expect_nopass (void)
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -256,8 +273,10 @@ do_test_auth_long_expect_nopass (void)
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -271,6 +290,7 @@ do_test_auth_short_noexpect_pass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
                    "client-wrote_headers",
@@ -279,8 +299,10 @@ do_test_auth_short_noexpect_pass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_CREATED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -294,6 +316,7 @@ do_test_auth_long_noexpect_pass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
                    "client-wrote_headers",
@@ -302,8 +325,10 @@ do_test_auth_long_noexpect_pass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -315,6 +340,7 @@ do_test_auth_short_expect_pass (void)
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
                    "client-wrote_headers",
@@ -325,8 +351,10 @@ do_test_auth_short_expect_pass (void)
                    "server-got_body",
                    "server-wrote_headers", SOUP_STATUS_CREATED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -338,14 +366,17 @@ do_test_auth_long_expect_pass (void)
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
                    "client-wrote_headers",
                    "server-got_headers",
                    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
                    "server-wrote_body",
+                   "server-finished",
                    "client-got_headers",
                    "client-got_body",
+                   "client-finished",
                    NULL);
 }
 
@@ -392,6 +423,8 @@ request_started (SoupServer *server, SoupMessage *msg,
                          G_CALLBACK (wrote_headers), "server");
        g_signal_connect (msg, "wrote_body",
                          G_CALLBACK (wrote_body), "server");
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (finished), "server");
 }
 
 static gboolean


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]