From 54665ff4093f4517104fe36c1b58f9a339710adf Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 23 Sep 2012 21:46:01 +0900 Subject: [PATCH] Removed static std::string constant in HttpHeader This change also removes inspection of Proxy-Connection header field when checking whether the remote endpoint keeps connection open. --- src/HttpHeader.cc | 14 +++++++------- src/HttpHeader.h | 9 +++------ src/HttpResponse.cc | 26 ++++---------------------- src/HttpServer.cc | 23 +++++++---------------- src/HttpServer.h | 6 +----- test/HttpHeaderTest.cc | 4 ++-- test/HttpResponseTest.cc | 20 ++------------------ 7 files changed, 26 insertions(+), 76 deletions(-) diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 165ce546..b934f1bd 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -41,13 +41,6 @@ namespace aria2 { -const std::string HttpHeader::HTTP_1_1 = "HTTP/1.1"; -const std::string HttpHeader::CLOSE = "close"; -const std::string HttpHeader::KEEP_ALIVE = "keep-alive"; -const std::string HttpHeader::CHUNKED = "chunked"; -const std::string HttpHeader::GZIP = "gzip"; -const std::string HttpHeader::DEFLATE = "deflate"; - HttpHeader::HttpHeader() {} HttpHeader::~HttpHeader() {} @@ -259,6 +252,13 @@ bool HttpHeader::fieldContains(int hdKey, const char* value) return false; } +bool HttpHeader::isKeepAlive() const +{ + const std::string& connection = find(CONNECTION); + return !util::strieq(connection, "close") && + (version_ == "HTTP/1.1" || util::strieq(connection, "keep-alive")); +} + namespace { const char* INTERESTING_HEADER_NAMES[] = { "accept-encoding", diff --git a/src/HttpHeader.h b/src/HttpHeader.h index 0d4eb7ba..353065c3 100644 --- a/src/HttpHeader.h +++ b/src/HttpHeader.h @@ -158,12 +158,9 @@ public: // assumes the values of the header field is delimited by ','. bool fieldContains(int hdKey, const char* value); - static const std::string HTTP_1_1; - static const std::string CLOSE; - static const std::string KEEP_ALIVE; - static const std::string CHUNKED; - static const std::string GZIP; - static const std::string DEFLATE; + // Returns true if the headers indicate that the remote endpoint + // keeps connection open. + bool isKeepAlive() const; }; int idInterestingHeader(const char* hdName); diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index cb1b61fd..182de9e3 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -197,7 +197,7 @@ SharedHandle HttpResponse::getTransferEncodingStreamFilter() const // TODO Transfer-Encoding header field can contains multiple tokens. We should // parse the field and retrieve each token. if(isTransferEncodingSpecified()) { - if(getTransferEncoding() == HttpHeader::CHUNKED) { + if(util::strieq(getTransferEncoding(), "chunked")) { filter.reset(new ChunkedDecodingStreamFilter()); } } @@ -218,8 +218,8 @@ SharedHandle HttpResponse::getContentEncodingStreamFilter() const { SharedHandle filter; #ifdef HAVE_ZLIB - if(getContentEncoding() == HttpHeader::GZIP || - getContentEncoding() == HttpHeader::DEFLATE) { + if(util::strieq(getContentEncoding(), "gzip") || + util::strieq(getContentEncoding(), "deflate")) { filter.reset(new GZipDecodingStreamFilter()); } #endif // HAVE_ZLIB @@ -288,25 +288,7 @@ Time HttpResponse::getLastModifiedTime() const bool HttpResponse::supportsPersistentConnection() const { - const std::string& connection = httpHeader_->find(HttpHeader::CONNECTION); - const std::string& version = httpHeader_->getVersion(); - const std::string& proxyConn = - httpHeader_->find(HttpHeader::PROXY_CONNECTION); - return - util::strifind(connection.begin(), - connection.end(), - HttpHeader::CLOSE.begin(), - HttpHeader::CLOSE.end()) == connection.end() && - (version == HttpHeader::HTTP_1_1 || - util::strifind(connection.begin(), - connection.end(), - HttpHeader::KEEP_ALIVE.begin(), - HttpHeader::KEEP_ALIVE.end()) != connection.end()) && - (!httpRequest_->isProxyRequestSet() || - util::strifind(proxyConn.begin(), - proxyConn.end(), - HttpHeader::KEEP_ALIVE.begin(), - HttpHeader::KEEP_ALIVE.end()) != proxyConn.end()); + return httpHeader_->isKeepAlive(); } namespace { diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 53145037..b7f467b1 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -71,7 +71,6 @@ HttpServer::HttpServer reqType_(RPC_TYPE_NONE), keepAlive_(true), gzip_(false), - acceptsPersistentConnection_(true), acceptsGZip_(false) {} @@ -156,19 +155,6 @@ SharedHandle HttpServer::receiveRequest() } headerProcessor_->clear(); - const std::string& connection = - lastRequestHeader_->find(HttpHeader::CONNECTION); - acceptsPersistentConnection_ = - util::strifind(connection.begin(), - connection.end(), - HttpHeader::CLOSE.begin(), - HttpHeader::CLOSE.end()) == connection.end() && - (lastRequestHeader_->getVersion() == HttpHeader::HTTP_1_1 || - util::strifind(connection.begin(), - connection.end(), - HttpHeader::KEEP_ALIVE.begin(), - HttpHeader::KEEP_ALIVE.end()) != connection.end()); - std::vector acceptEncodings; const std::string& acceptEnc = lastRequestHeader_->find(HttpHeader::ACCEPT_ENCODING); @@ -177,8 +163,7 @@ SharedHandle HttpServer::receiveRequest() acceptsGZip_ = false; for(std::vector::const_iterator i = acceptEncodings.begin(), eoi = acceptEncodings.end(); i != eoi; ++i) { - if(util::strieq((*i).first, (*i).second, - HttpHeader::GZIP.begin(), HttpHeader::GZIP.end())) { + if(util::strieq((*i).first, (*i).second, "gzip")) { acceptsGZip_ = true; break; } @@ -392,4 +377,10 @@ std::string HttpServer::createQuery() const } } +bool HttpServer::supportsPersistentConnection() const +{ + return keepAlive_ && + lastRequestHeader_ && lastRequestHeader_->isKeepAlive(); +} + } // namespace aria2 diff --git a/src/HttpServer.h b/src/HttpServer.h index 995102a0..c5b01530 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -80,7 +80,6 @@ private: bool gzip_; std::string username_; std::string password_; - bool acceptsPersistentConnection_; bool acceptsGZip_; std::string allowOrigin_; public: @@ -138,10 +137,7 @@ public: bool sendBufferIsEmpty() const; - bool supportsPersistentConnection() const - { - return keepAlive_ && acceptsPersistentConnection_; - } + bool supportsPersistentConnection() const; bool supportsGZip() const { diff --git a/test/HttpHeaderTest.cc b/test/HttpHeaderTest.cc index 1899a09f..056d4f10 100644 --- a/test/HttpHeaderTest.cc +++ b/test/HttpHeaderTest.cc @@ -138,7 +138,7 @@ void HttpHeaderTest::testClearField() { HttpHeader h; h.setStatusCode(200); - h.setVersion(HttpHeader::HTTP_1_1); + h.setVersion("HTTP/1.1"); h.put(HttpHeader::LINK, "Bar"); CPPUNIT_ASSERT_EQUAL(std::string("Bar"), h.find(HttpHeader::LINK)); @@ -147,7 +147,7 @@ void HttpHeaderTest::testClearField() CPPUNIT_ASSERT_EQUAL(std::string(""), h.find(HttpHeader::LINK)); CPPUNIT_ASSERT_EQUAL(200, h.getStatusCode()); - CPPUNIT_ASSERT_EQUAL(std::string(HttpHeader::HTTP_1_1), h.getVersion()); + CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), h.getVersion()); } void HttpHeaderTest::testFieldContains() diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc index 3289b462..9ed6f180 100644 --- a/test/HttpResponseTest.cc +++ b/test/HttpResponseTest.cc @@ -581,20 +581,12 @@ void HttpResponseTest::testSupportsPersistentConnection() httpRequest->setProxyRequest(proxyRequest); httpHeader->setVersion("HTTP/1.1"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->put(HttpHeader::CONNECTION, "close"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->clearField(); - httpHeader->put(HttpHeader::CONNECTION, "keep-alive"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->clearField(); - httpHeader->put(HttpHeader::PROXY_CONNECTION, "keep-alive"); CPPUNIT_ASSERT(httpResponse.supportsPersistentConnection()); httpHeader->put(HttpHeader::CONNECTION, "close"); CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); httpHeader->clearField(); - httpHeader->put(HttpHeader::PROXY_CONNECTION, "close"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); + httpHeader->put(HttpHeader::CONNECTION, "keep-alive"); + CPPUNIT_ASSERT(httpResponse.supportsPersistentConnection()); httpHeader->clearField(); httpHeader->setVersion("HTTP/1.0"); @@ -603,16 +595,8 @@ void HttpResponseTest::testSupportsPersistentConnection() CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); httpHeader->clearField(); httpHeader->put(HttpHeader::CONNECTION, "keep-alive"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->put(HttpHeader::PROXY_CONNECTION, "keep-alive"); CPPUNIT_ASSERT(httpResponse.supportsPersistentConnection()); httpHeader->clearField(); - httpHeader->put(HttpHeader::PROXY_CONNECTION, "keep-alive"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->clearField(); - httpHeader->put(HttpHeader::PROXY_CONNECTION, "close"); - CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection()); - httpHeader->clearField(); } void HttpResponseTest::testGetMetalinKHttpEntries()