From da5eed4dba97d624ba6772e4d28f4c1ae0e7fac3 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Tue, 6 Mar 2007 20:42:09 +0100 Subject: [PATCH] decompress_data: count bytes written to the pipe Count bytes written to the pipe and decompress when the pipe is full. This fixes the issue with blogs.msdn.com. It also should give better performance because of fewer calls to read_encoded. --- src/network/connection.c | 1 + src/network/connection.h | 2 ++ src/protocol/http/http.c | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/network/connection.c b/src/network/connection.c index 8ab89ac8..250c62cc 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -378,6 +378,7 @@ shutdown_connection_stream(struct connection *conn) if (conn->stream_pipes[1] >= 0) close(conn->stream_pipes[1]); conn->stream_pipes[0] = conn->stream_pipes[1] = -1; + conn->stream_pipes_written = 0; } static void diff --git a/src/network/connection.h b/src/network/connection.h index c9398e50..e2781b33 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -53,6 +53,8 @@ struct connection { int tries; timer_id_T timer; int stream_pipes[2]; + /* The number of bytes actually in the pipe. */ + int stream_pipes_written; unsigned int running:1; unsigned int unrestartable:1; diff --git a/src/protocol/http/http.c b/src/protocol/http/http.c index 52d41c18..5e357260 100644 --- a/src/protocol/http/http.c +++ b/src/protocol/http/http.c @@ -1016,11 +1016,13 @@ decompress_data(struct connection *conn, unsigned char *data, int len, *new_len = 0; /* new_len must be zero if we would ever return NULL */ - if (conn->stream_pipes[0] == -1 - && (c_pipe(conn->stream_pipes) < 0 + if (conn->stream_pipes[0] == -1) { + if (c_pipe(conn->stream_pipes) < 0 || set_nonblocking_fd(conn->stream_pipes[0]) < 0 - || set_nonblocking_fd(conn->stream_pipes[1]) < 0)) { - return NULL; + || set_nonblocking_fd(conn->stream_pipes[1]) < 0) { + return NULL; + } + conn->stream_pipes_written = 0; } do { @@ -1029,13 +1031,15 @@ decompress_data(struct connection *conn, unsigned char *data, int len, if (state == NORMAL) { /* ... we aren't finishing yet. */ int written; + int to_write = PIPE_BUF - conn->stream_pipes_written; written = safe_write(conn->stream_pipes[1], data, - len > PIPE_BUF ? PIPE_BUF : len); + len > to_write ? to_write : len); if (written > 0) { data += written; len -= written; + conn->stream_pipes_written += written; /* In non-keep-alive connections http->length == -1, so the test below */ if (*length_of_block > 0) @@ -1045,6 +1049,8 @@ decompress_data(struct connection *conn, unsigned char *data, int len, if (!http->length) { /* That's all, folks - let's finish this. */ state = FINISHING; + } else if (!len) { + return output; } } } @@ -1059,6 +1065,7 @@ decompress_data(struct connection *conn, unsigned char *data, int len, if (!output) break; did_read = read_encoded(conn->stream, output + *new_len, BIG_READ); + conn->stream_pipes_written = 0; if (did_read > 0) *new_len += did_read; else if (did_read == -1) {