[Curl] Move response related features into ResourceResponse
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Sep 2017 06:57:12 +0000 (06:57 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Sep 2017 06:57:12 +0000 (06:57 +0000)
https://bugs.webkit.org/show_bug.cgi?id=174654

Patch by Basuke Suzuki <Basuke.Suzuki@sony.com> on 2017-09-13
Reviewed by Alex Christensen.

* platform/Curl.cmake:
* platform/network/curl/ResourceHandleCurlDelegate.cpp:
(WebCore::ResourceHandleCurlDelegate::didReceiveAllHeaders):
(WebCore::ResourceHandleCurlDelegate::didReceiveHeader):
(WebCore::isHttpRedirect): Deleted.
(WebCore::isHttpAuthentication): Deleted.
(WebCore::isHttpNotModified): Deleted.
(WebCore::isAppendableHeader): Deleted.
(WebCore::ResourceHandleCurlDelegate::didReceiveHeaderLine): Deleted.
* platform/network/curl/ResourceHandleCurlDelegate.h:
* platform/network/curl/ResourceResponse.h:
(WebCore::ResourceResponse::platformSuggestedFilename const): Deleted.
* platform/network/curl/ResourceResponseCurl.cpp: Added.
(WebCore::ResourceResponse::isAppendableHeader):
(WebCore::ResourceResponse::appendHTTPHeaderField):
(WebCore::ResourceResponse::setStatusLine):
(WebCore::ResourceResponse::platformSuggestedFilename const):
(WebCore::ResourceResponse::isRedirection const):
(WebCore::ResourceResponse::isNotModified const):
(WebCore::ResourceResponse::isUnauthorized const):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@222012 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/Curl.cmake
Source/WebCore/platform/network/curl/ResourceHandleCurlDelegate.cpp
Source/WebCore/platform/network/curl/ResourceHandleCurlDelegate.h
Source/WebCore/platform/network/curl/ResourceResponse.h
Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp [new file with mode: 0644]

index c7915a3..5f68f9b 100644 (file)
@@ -1,3 +1,31 @@
+2017-09-13  Basuke Suzuki  <Basuke.Suzuki@sony.com>
+
+        [Curl] Move response related features into ResourceResponse
+        https://bugs.webkit.org/show_bug.cgi?id=174654
+
+        Reviewed by Alex Christensen.
+
+        * platform/Curl.cmake:
+        * platform/network/curl/ResourceHandleCurlDelegate.cpp:
+        (WebCore::ResourceHandleCurlDelegate::didReceiveAllHeaders):
+        (WebCore::ResourceHandleCurlDelegate::didReceiveHeader):
+        (WebCore::isHttpRedirect): Deleted.
+        (WebCore::isHttpAuthentication): Deleted.
+        (WebCore::isHttpNotModified): Deleted.
+        (WebCore::isAppendableHeader): Deleted.
+        (WebCore::ResourceHandleCurlDelegate::didReceiveHeaderLine): Deleted.
+        * platform/network/curl/ResourceHandleCurlDelegate.h:
+        * platform/network/curl/ResourceResponse.h:
+        (WebCore::ResourceResponse::platformSuggestedFilename const): Deleted.
+        * platform/network/curl/ResourceResponseCurl.cpp: Added.
+        (WebCore::ResourceResponse::isAppendableHeader):
+        (WebCore::ResourceResponse::appendHTTPHeaderField):
+        (WebCore::ResourceResponse::setStatusLine):
+        (WebCore::ResourceResponse::platformSuggestedFilename const):
+        (WebCore::ResourceResponse::isRedirection const):
+        (WebCore::ResourceResponse::isNotModified const):
+        (WebCore::ResourceResponse::isUnauthorized const):
+
 2017-09-13  Zalan Bujtas  <zalan@apple.com>
 
         Switch multicolumn's spanner map from raw over to weak pointers.
index 6138311..bc12239 100644 (file)
@@ -16,6 +16,7 @@ list(APPEND WebCore_SOURCES
     platform/network/curl/ProxyServerCurl.cpp
     platform/network/curl/ResourceHandleCurl.cpp
     platform/network/curl/ResourceHandleCurlDelegate.cpp
+    platform/network/curl/ResourceResponseCurl.cpp
     platform/network/curl/SSLHandle.cpp
     platform/network/curl/SocketStreamHandleImplCurl.cpp
     platform/network/curl/SynchronousLoaderClientCurl.cpp
index 2b01d6a..7f30136 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "CredentialStorage.h"
 #include "CurlCacheManager.h"
+#include "HTTPParsers.h"
 #include "MIMETypeRegistry.h"
 #include "MultipartHandle.h"
 #include "ResourceHandle.h"
@@ -402,98 +403,6 @@ inline static bool isHttpInfo(int statusCode)
     return 100 <= statusCode && statusCode < 200;
 }
 
-inline static bool isHttpRedirect(int statusCode)
-{
-    return 300 <= statusCode && statusCode < 400 && statusCode != 304;
-}
-
-inline static bool isHttpAuthentication(int statusCode)
-{
-    return statusCode == 401;
-}
-
-inline static bool isHttpNotModified(int statusCode)
-{
-    return statusCode == 304;
-}
-
-static bool isAppendableHeader(const String &key)
-{
-    static const char* appendableHeaders[] = {
-        "access-control-allow-headers",
-        "access-control-allow-methods",
-        "access-control-allow-origin",
-        "access-control-expose-headers",
-        "allow",
-        "cache-control",
-        "connection",
-        "content-encoding",
-        "content-language",
-        "if-match",
-        "if-none-match",
-        "keep-alive",
-        "pragma",
-        "proxy-authenticate",
-        "public",
-        "server",
-        "set-cookie",
-        "te",
-        "trailer",
-        "transfer-encoding",
-        "upgrade",
-        "user-agent",
-        "vary",
-        "via",
-        "warning",
-        "www-authenticate"
-    };
-
-    // Custom headers start with 'X-', and need no further checking.
-    if (key.startsWith("x-", /* caseSensitive */ false))
-        return true;
-
-    for (auto& header : appendableHeaders) {
-        if (equalIgnoringASCIICase(key, header))
-            return true;
-    }
-
-    return false;
-}
-
-void ResourceHandleCurlDelegate::didReceiveHeaderLine(const String& header)
-{
-    ASSERT(isMainThread());
-
-    auto splitPosition = header.find(":");
-    if (splitPosition != notFound) {
-        String key = header.left(splitPosition).stripWhiteSpace();
-        String value = header.substring(splitPosition + 1).stripWhiteSpace();
-
-        if (isAppendableHeader(key))
-            response().addHTTPHeaderField(key, value);
-        else
-            response().setHTTPHeaderField(key, value);
-    } else if (header.startsWith("HTTP", false)) {
-        // This is the first line of the response.
-        // Extract the http status text from this.
-        //
-        // If the FOLLOWLOCATION option is enabled for the curl handle then
-        // curl will follow the redirections internally. Thus this header callback
-        // will be called more than one time with the line starting "HTTP" for one job.
-        long httpCode = 0;
-        m_curlHandle.getResponseCode(httpCode);
-
-        String httpCodeString = String::number(httpCode);
-        int statusCodePos = header.find(httpCodeString);
-
-        if (statusCodePos != notFound) {
-            // The status text is after the status code.
-            String status = header.substring(statusCodePos + httpCodeString.length());
-            response().setHTTPStatusText(status.stripWhiteSpace());
-        }
-    }
-}
-
 void ResourceHandleCurlDelegate::didReceiveAllHeaders(long httpCode, long long contentLength)
 {
     ASSERT(isMainThread());
@@ -512,7 +421,7 @@ void ResourceHandleCurlDelegate::didReceiveAllHeaders(long httpCode, long long c
     }
 
     // HTTP redirection
-    if (isHttpRedirect(httpCode)) {
+    if (response().isRedirection()) {
         String location = response().httpHeaderField(HTTPHeaderName::Location);
         if (!location.isEmpty()) {
             URL newURL = URL(m_firstRequest.url(), location);
@@ -527,7 +436,7 @@ void ResourceHandleCurlDelegate::didReceiveAllHeaders(long httpCode, long long c
 
             return;
         }
-    } else if (isHttpAuthentication(httpCode)) {
+    } else if (response().isUnauthorized()) {
         ProtectionSpace protectionSpace;
         if (getProtectionSpace(response(), protectionSpace)) {
             Credential credential;
@@ -542,7 +451,7 @@ void ResourceHandleCurlDelegate::didReceiveAllHeaders(long httpCode, long long c
     response().setResponseFired(true);
 
     if (m_handle->client()) {
-        if (isHttpNotModified(httpCode)) {
+        if (response().isNotModified()) {
             const String& url = m_firstRequest.url().string();
             if (CurlCacheManager::getInstance().getCachedResponse(url, response())) {
                 if (m_addedCacheValidationHeaders) {
@@ -878,13 +787,17 @@ size_t ResourceHandleCurlDelegate::didReceiveHeader(String&& header)
             });
         }
     } else {
+        // If the FOLLOWLOCATION option is enabled for the curl handle then
+        // curl will follow the redirections internally. Thus this header callback
+        // will be called more than one time with the line starting "HTTP" for one job.
         if (isMainThread())
-            didReceiveHeaderLine(header);
+            response().appendHTTPHeaderField(header);
         else {
-            callOnMainThread([protectedThis = makeRef(*this), header = header.isolatedCopy() ] {
+            callOnMainThread([protectedThis = makeRef(*this), copyHeader = header.isolatedCopy() ] {
                 if (!protectedThis->m_handle)
                     return;
-                protectedThis->didReceiveHeaderLine(header);
+
+                protectedThis->response().appendHTTPHeaderField(copyHeader);
             });
         }
     }
index 65688b7..3bed49a 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2017 NAVER Corp. All rights reserved.
+ * Copyright (C) 2017 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -74,7 +75,6 @@ private:
     void setupAuthentication();
     bool getProtectionSpace(const ResourceResponse&, ProtectionSpace&);
 
-    void didReceiveHeaderLine(const String&);
     void didReceiveAllHeaders(long httpCode, long long contentLength);
     void didReceiveContentData(ThreadSafeDataBuffer);
     void handleLocalReceiveResponse();
index 2a4232c..c7ba54a 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006 Apple Inc.  All rights reserved.
+ * Copyright (C) 2017 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -25,9 +26,6 @@
 
 #pragma once
 
-#include "HTTPHeaderNames.h"
-#include "HTTPParsers.h"
-
 #include "ResourceResponseBase.h"
 
 typedef struct _CFURLResponse* CFURLResponseRef;
@@ -50,16 +48,22 @@ public:
     void setResponseFired(bool fired) { m_responseFired = fired; }
     bool responseFired() { return m_responseFired; }
 
+    void appendHTTPHeaderField(const String&);
+
+    bool isRedirection() const;
+    bool isNotModified() const;
+    bool isUnauthorized() const;
+
     // Needed for compatibility.
     CFURLResponseRef cfURLResponse() const { return 0; }
 
 private:
     friend class ResourceResponseBase;
 
-    String platformSuggestedFilename() const
-    {
-        return filenameFromHTTPContentDisposition(httpHeaderField(HTTPHeaderName::ContentDisposition));
-    }
+    static bool isAppendableHeader(const String &key);
+    String platformSuggestedFilename() const;
+
+    void setStatusLine(const String&);
 
     bool m_responseFired;
 };
diff --git a/Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp b/Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp
new file mode 100644 (file)
index 0000000..e93a5e4
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2017 Sony Interactive Entertainment Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if USE(CURL)
+#include "ResourceResponse.h"
+
+#include "HTTPParsers.h"
+
+namespace WebCore {
+
+bool ResourceResponse::isAppendableHeader(const String &key)
+{
+    static const char* appendableHeaders[] = {
+        "access-control-allow-headers",
+        "access-control-allow-methods",
+        "access-control-allow-origin",
+        "access-control-expose-headers",
+        "allow",
+        "cache-control",
+        "connection",
+        "content-encoding",
+        "content-language",
+        "if-match",
+        "if-none-match",
+        "keep-alive",
+        "pragma",
+        "proxy-authenticate",
+        "public",
+        "server",
+        "set-cookie",
+        "te",
+        "trailer",
+        "transfer-encoding",
+        "upgrade",
+        "user-agent",
+        "vary",
+        "via",
+        "warning",
+        "www-authenticate"
+    };
+
+    // Custom headers start with 'X-', and need no further checking.
+    if (key.startsWith("x-", /* caseSensitive */ false))
+        return true;
+
+    for (auto& header : appendableHeaders) {
+        if (equalIgnoringASCIICase(key, header))
+            return true;
+    }
+
+    return false;
+}
+
+void ResourceResponse::appendHTTPHeaderField(const String& header)
+{
+    int splitPosistion = header.find(":");
+    if (splitPosistion != notFound) {
+        String key = header.left(splitPosistion).stripWhiteSpace();
+        String value = header.substring(splitPosistion + 1).stripWhiteSpace();
+
+        if (isAppendableHeader(key))
+            addHTTPHeaderField(key, value);
+        else
+            setHTTPHeaderField(key, value);
+    } else if (header.startsWith("HTTP", false)) {
+        // This is the first line of the response.
+        setStatusLine(header);
+    }
+}
+
+void ResourceResponse::setStatusLine(const String& header)
+{
+    String statusLine = header.stripWhiteSpace();
+
+    int httpVersionEndPosition = statusLine.find(" ");
+    int statusCodeEndPosition = notFound;
+
+    // Extract the http version
+    if (httpVersionEndPosition != notFound) {
+        String httpVersion = statusLine.left(httpVersionEndPosition);
+        setHTTPVersion(httpVersion.stripWhiteSpace());
+
+        statusLine = statusLine.substring(httpVersionEndPosition + 1).stripWhiteSpace();
+        statusCodeEndPosition = statusLine.find(" ");
+    }
+
+    // Extract the http status text
+    if (statusCodeEndPosition != notFound) {
+        String statusText = statusLine.substring(statusCodeEndPosition + 1);
+        setHTTPStatusText(statusText.stripWhiteSpace());
+    }
+}
+
+String ResourceResponse::platformSuggestedFilename() const
+{
+    return filenameFromHTTPContentDisposition(httpHeaderField(HTTPHeaderName::ContentDisposition));
+}
+
+bool ResourceResponse::isRedirection() const
+{
+    auto statusCode = httpStatusCode();
+    return (300 <= statusCode) && (statusCode < 400) && (statusCode != 304);
+}
+
+bool ResourceResponse::isNotModified() const
+{
+    return (httpStatusCode() == 304);
+}
+
+bool ResourceResponse::isUnauthorized() const
+{
+    return (httpStatusCode() == 401);
+}
+
+}
+
+#endif