Browse Source

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.
Tatsuhiro Tsujikawa 13 years ago
parent
commit
54665ff409
7 changed files with 25 additions and 75 deletions
  1. 7 7
      src/HttpHeader.cc
  2. 3 6
      src/HttpHeader.h
  3. 4 22
      src/HttpResponse.cc
  4. 7 16
      src/HttpServer.cc
  5. 1 5
      src/HttpServer.h
  6. 2 2
      test/HttpHeaderTest.cc
  7. 1 17
      test/HttpResponseTest.cc

+ 7 - 7
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",

+ 3 - 6
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);

+ 4 - 22
src/HttpResponse.cc

@@ -197,7 +197,7 @@ SharedHandle<StreamFilter> 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<StreamFilter> HttpResponse::getContentEncodingStreamFilter() const
 {
   SharedHandle<StreamFilter> 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 {

+ 7 - 16
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<HttpHeader> 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<Scip> acceptEncodings;
     const std::string& acceptEnc =
       lastRequestHeader_->find(HttpHeader::ACCEPT_ENCODING);
@@ -177,8 +163,7 @@ SharedHandle<HttpHeader> HttpServer::receiveRequest()
     acceptsGZip_ = false;
     for(std::vector<Scip>::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

+ 1 - 5
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
   {

+ 2 - 2
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()

+ 1 - 17
test/HttpResponseTest.cc

@@ -581,20 +581,12 @@ void HttpResponseTest::testSupportsPersistentConnection()
   httpRequest->setProxyRequest(proxyRequest);
 
   httpHeader->setVersion("HTTP/1.1");
-  CPPUNIT_ASSERT(!httpResponse.supportsPersistentConnection());
+  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->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()