diff --git a/ChangeLog b/ChangeLog index 07d7c088..99f35911 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-10-01 Tatsuhiro Tsujikawa + + Don't set Request::supportsPersistentConnection() in HttpConnection + since this code is also used in AbstractProxyResponseCommand. + Moved those code to HttpResponse. + * src/HttpResponse.cc + * src/HttpResponse.h + * src/HttpConnection.cc + * src/HttpConnection.h + * src/HttpResponseCommand.cc + * test/HttpResponseTest.cc + 2008-09-30 Tatsuhiro Tsujikawa Fixed compile error with debian mingw32 cross compiler(4.2.1). diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index 26bb50cd..833e6e6d 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "HttpConnection.h" + +#include + #include "Util.h" #include "message.h" #include "prefs.h" @@ -49,7 +52,6 @@ #include "Socket.h" #include "Option.h" #include "CookieStorage.h" -#include namespace aria2 { @@ -141,19 +143,8 @@ HttpResponseHandle HttpConnection::receiveResponse() size -= putbackDataLength; socket->readData(buf, size); - // OK, we got all headers. logger->info(MSG_RECEIVE_RESPONSE, cuid, proc->getHeaderString().c_str()); - // Disable persistent connection if: - // Connection: close is received or the remote server is not HTTP/1.1. - // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive. SharedHandle httpHeader = proc->getHttpResponseHeader(); - if(Util::toLower(httpHeader->getFirst(HttpHeader::CONNECTION)).find(HttpHeader::CLOSE) != std::string::npos - || httpHeader->getVersion() != HttpHeader::HTTP_1_1) { - entry->getHttpRequest()->getRequest()->supportsPersistentConnection(false); - } else { - entry->getHttpRequest()->getRequest()->supportsPersistentConnection(true); - } - HttpResponseHandle httpResponse(new HttpResponse()); httpResponse->setCuid(cuid); httpResponse->setHttpHeader(httpHeader); diff --git a/src/HttpConnection.h b/src/HttpConnection.h index 0584d2aa..87577fea 100644 --- a/src/HttpConnection.h +++ b/src/HttpConnection.h @@ -36,11 +36,13 @@ #define _D_HTTP_CONNECTION_H_ #include "common.h" -#include "SharedHandle.h" -#include "SocketBuffer.h" + #include #include +#include "SharedHandle.h" +#include "SocketBuffer.h" + namespace aria2 { class HttpRequest; diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 98468b54..e77e696e 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "HttpResponse.h" + +#include + #include "Request.h" #include "Segment.h" #include "HttpRequest.h" @@ -52,7 +55,6 @@ # include "GZipDecoder.h" #endif // HAVE_LIBZ #include "CookieStorage.h" -#include namespace aria2 { @@ -254,4 +256,11 @@ Time HttpResponse::getLastModifiedTime() const return Time::parseHTTPDate(httpHeader->getFirst(HttpHeader::LAST_MODIFIED)); } +bool HttpResponse::supportsPersistentConnection() const +{ + return Util::toLower(httpHeader->getFirst(HttpHeader::CONNECTION)). + find(HttpHeader::CLOSE) == std::string::npos + && httpHeader->getVersion() == HttpHeader::HTTP_1_1; +} + } // namespace aria2 diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 8d156768..b12a6f44 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -36,9 +36,11 @@ #define _D_HTTP_RESPONSE_H_ #include "common.h" + +#include + #include "SharedHandle.h" #include "TimeA2.h" -#include namespace aria2 { @@ -117,6 +119,8 @@ public: time_t getRetryAfter() const; Time getLastModifiedTime() const; + + bool supportsPersistentConnection() const; }; typedef SharedHandle HttpResponseHandle; diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index b5025b52..e8cedc95 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -93,6 +93,13 @@ bool HttpResponseCommand::executeInternal() httpResponse->validateResponse(); httpResponse->retrieveCookie(); + SharedHandle httpHeader = httpResponse->getHttpHeader(); + // Disable persistent connection if: + // Connection: close is received or the remote server is not HTTP/1.1. + // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive. + req->supportsPersistentConnection + (httpResponse->supportsPersistentConnection()); + if(httpResponse->getResponseStatus() >= HttpHeader::S300) { if(httpResponse->getResponseStatus() == HttpHeader::S404) { _requestGroup->increaseAndValidateFileNotFoundCount(); diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc index 6a2ffbcf..db1bcee4 100644 --- a/test/HttpResponseTest.cc +++ b/test/HttpResponseTest.cc @@ -1,3 +1,7 @@ +#include + +#include + #include "HttpResponse.h" #include "prefs.h" #include "PiecedSegment.h" @@ -10,8 +14,6 @@ #include "Decoder.h" #include "DlRetryEx.h" #include "CookieStorage.h" -#include -#include namespace aria2 { @@ -42,6 +44,7 @@ class HttpResponseTest : public CppUnit::TestFixture { CPPUNIT_TEST(testHasRetryAfter); CPPUNIT_TEST(testProcessRedirect); CPPUNIT_TEST(testRetrieveCookie); + CPPUNIT_TEST(testSupportsPersistentConnection); CPPUNIT_TEST_SUITE_END(); private: @@ -72,6 +75,7 @@ public: void testHasRetryAfter(); void testProcessRedirect(); void testRetrieveCookie(); + void testSupportsPersistentConnection(); }; @@ -486,4 +490,24 @@ void HttpResponseTest::testRetrieveCookie() CPPUNIT_ASSERT_EQUAL(std::string("k3=v3"), (*(st->begin()+1)).toString()); } +void HttpResponseTest::testSupportsPersistentConnection() +{ + HttpResponse httpResponse; + SharedHandle httpHeader(new HttpHeader()); + httpResponse.setHttpHeader(httpHeader); + + httpHeader->setVersion("HTTP/1.1"); + CPPUNIT_ASSERT(httpResponse.supportsPersistentConnection()); + + httpHeader->setVersion("HTTP/1.0"); + CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); + + httpHeader->setVersion("HTTP/1.1"); + httpHeader->put("Connection", "close"); + CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); + httpHeader->clearField(); + httpHeader->put("Connection", "keep-alive"); + CPPUNIT_ASSERT(httpResponse.supportsPersistentConnection()); +} + } // namespace aria2