2008-10-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

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
pull/1/head
Tatsuhiro Tsujikawa 2008-09-30 15:54:50 +00:00
parent 91e4a887c6
commit c682371a58
7 changed files with 67 additions and 18 deletions

View File

@ -1,3 +1,15 @@
2008-10-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
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 <tujikawa at rednoah dot com>
Fixed compile error with debian mingw32 cross compiler(4.2.1).

View File

@ -33,6 +33,9 @@
*/
/* copyright --> */
#include "HttpConnection.h"
#include <sstream>
#include "Util.h"
#include "message.h"
#include "prefs.h"
@ -49,7 +52,6 @@
#include "Socket.h"
#include "Option.h"
#include "CookieStorage.h"
#include <sstream>
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> 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);

View File

@ -36,11 +36,13 @@
#define _D_HTTP_CONNECTION_H_
#include "common.h"
#include "SharedHandle.h"
#include "SocketBuffer.h"
#include <string>
#include <deque>
#include "SharedHandle.h"
#include "SocketBuffer.h"
namespace aria2 {
class HttpRequest;

View File

@ -33,6 +33,9 @@
*/
/* copyright --> */
#include "HttpResponse.h"
#include <deque>
#include "Request.h"
#include "Segment.h"
#include "HttpRequest.h"
@ -52,7 +55,6 @@
# include "GZipDecoder.h"
#endif // HAVE_LIBZ
#include "CookieStorage.h"
#include <deque>
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

View File

@ -36,9 +36,11 @@
#define _D_HTTP_RESPONSE_H_
#include "common.h"
#include <stdint.h>
#include "SharedHandle.h"
#include "TimeA2.h"
#include <stdint.h>
namespace aria2 {
@ -117,6 +119,8 @@ public:
time_t getRetryAfter() const;
Time getLastModifiedTime() const;
bool supportsPersistentConnection() const;
};
typedef SharedHandle<HttpResponse> HttpResponseHandle;

View File

@ -93,6 +93,13 @@ bool HttpResponseCommand::executeInternal()
httpResponse->validateResponse();
httpResponse->retrieveCookie();
SharedHandle<HttpHeader> 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();

View File

@ -1,3 +1,7 @@
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "HttpResponse.h"
#include "prefs.h"
#include "PiecedSegment.h"
@ -10,8 +14,6 @@
#include "Decoder.h"
#include "DlRetryEx.h"
#include "CookieStorage.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
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> 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