Title: [230973] trunk/Source/WebCore
Revision
230973
Author
commit-qu...@webkit.org
Date
2018-04-24 14:54:49 -0700 (Tue, 24 Apr 2018)

Log Message

[Curl] Extract proxy settings into a separate class to hold advanced information.
https://bugs.webkit.org/show_bug.cgi?id=184714

It was simplely structure to hold proxy setting. To support advanced feature of proxy
such as authentication, more inteligent object is required to store intermediate state
or errors. That's why we've introduced new class for that purpose.

Patch by Basuke Suzuki <basuke.suz...@sony.com> on 2018-04-24
Reviewed by Youenn Fablet.

No new tests because there's no new behavior.

* platform/Curl.cmake:
* platform/network/curl/CurlContext.cpp:
(WebCore::CurlHandle::enableProxyIfExists):
(WebCore::CurlContext::ProxyInfo::url const): Deleted.
(WebCore::CurlContext::setProxyInfo): Deleted.
* platform/network/curl/CurlContext.h:
(WebCore::CurlContext::proxySettings const):
(WebCore::CurlContext::setProxySettings):
(WebCore::CurlContext::setProxyUserPass):
(WebCore::CurlContext::proxyInfo const): Deleted.
(WebCore::CurlContext::setProxyInfo): Deleted.
* platform/network/curl/CurlProxySettings.cpp: Added.
(WebCore::CurlProxySettings::CurlProxySettings):
(WebCore::CurlProxySettings::rebuildUrl):
(WebCore::CurlProxySettings::setUserPass):
(WebCore::protocolIsInSocksFamily):
(WebCore::getProxyPort):
(WebCore::createProxyUrl):
* platform/network/curl/CurlProxySettings.h: Added.
(WebCore::CurlProxySettings::CurlProxySettings):
(WebCore::CurlProxySettings::isEmpty const):
(WebCore::CurlProxySettings::mode const):
(WebCore::CurlProxySettings::url const):
(WebCore::CurlProxySettings::ignoreHosts const):
(WebCore::CurlProxySettings::user const):
(WebCore::CurlProxySettings::password const):
* platform/network/curl/CurlRequest.cpp:
(WebCore::CurlRequest::didReceiveHeader):
* platform/network/curl/CurlResponse.h:
(WebCore::CurlResponse::isolatedCopy const):
* platform/network/curl/ResourceResponseCurl.cpp:
(WebCore::ResourceResponse::ResourceResponse):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (230972 => 230973)


--- trunk/Source/WebCore/ChangeLog	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/ChangeLog	2018-04-24 21:54:49 UTC (rev 230973)
@@ -1,3 +1,49 @@
+2018-04-24  Basuke Suzuki  <basuke.suz...@sony.com>
+
+        [Curl] Extract proxy settings into a separate class to hold advanced information.
+        https://bugs.webkit.org/show_bug.cgi?id=184714
+
+        It was simplely structure to hold proxy setting. To support advanced feature of proxy
+        such as authentication, more inteligent object is required to store intermediate state
+        or errors. That's why we've introduced new class for that purpose.
+
+        Reviewed by Youenn Fablet.
+
+        No new tests because there's no new behavior.
+
+        * platform/Curl.cmake:
+        * platform/network/curl/CurlContext.cpp:
+        (WebCore::CurlHandle::enableProxyIfExists):
+        (WebCore::CurlContext::ProxyInfo::url const): Deleted.
+        (WebCore::CurlContext::setProxyInfo): Deleted.
+        * platform/network/curl/CurlContext.h:
+        (WebCore::CurlContext::proxySettings const):
+        (WebCore::CurlContext::setProxySettings):
+        (WebCore::CurlContext::setProxyUserPass):
+        (WebCore::CurlContext::proxyInfo const): Deleted.
+        (WebCore::CurlContext::setProxyInfo): Deleted.
+        * platform/network/curl/CurlProxySettings.cpp: Added.
+        (WebCore::CurlProxySettings::CurlProxySettings):
+        (WebCore::CurlProxySettings::rebuildUrl):
+        (WebCore::CurlProxySettings::setUserPass):
+        (WebCore::protocolIsInSocksFamily):
+        (WebCore::getProxyPort):
+        (WebCore::createProxyUrl):
+        * platform/network/curl/CurlProxySettings.h: Added.
+        (WebCore::CurlProxySettings::CurlProxySettings):
+        (WebCore::CurlProxySettings::isEmpty const):
+        (WebCore::CurlProxySettings::mode const):
+        (WebCore::CurlProxySettings::url const):
+        (WebCore::CurlProxySettings::ignoreHosts const):
+        (WebCore::CurlProxySettings::user const):
+        (WebCore::CurlProxySettings::password const):
+        * platform/network/curl/CurlRequest.cpp:
+        (WebCore::CurlRequest::didReceiveHeader):
+        * platform/network/curl/CurlResponse.h:
+        (WebCore::CurlResponse::isolatedCopy const):
+        * platform/network/curl/ResourceResponseCurl.cpp:
+        (WebCore::ResourceResponse::ResourceResponse):
+
 2018-04-24  Youenn Fablet  <you...@apple.com>
 
         Throw in case of PeerConnection created for detached documents

Modified: trunk/Source/WebCore/platform/Curl.cmake (230972 => 230973)


--- trunk/Source/WebCore/platform/Curl.cmake	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/Curl.cmake	2018-04-24 21:54:49 UTC (rev 230973)
@@ -16,6 +16,7 @@
     platform/network/curl/CurlDownload.cpp
     platform/network/curl/CurlFormDataStream.cpp
     platform/network/curl/CurlMultipartHandle.cpp
+    platform/network/curl/CurlProxySettings.cpp
     platform/network/curl/CurlRequest.cpp
     platform/network/curl/CurlRequestScheduler.cpp
     platform/network/curl/CurlResourceHandleDelegate.cpp

Modified: trunk/Source/WebCore/platform/network/curl/CurlContext.cpp (230972 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlContext.cpp	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/network/curl/CurlContext.cpp	2018-04-24 21:54:49 UTC (rev 230973)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2013 Apple Inc.  All rights reserved.
- * Copyright (C) 2017 Sony Interactive Entertainment Inc.
+ * Copyright (C) 2018 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -138,34 +138,6 @@
     curl_easy_cleanup(curl);
 }
 
-// Proxy =======================
-
-const String CurlContext::ProxyInfo::url() const
-{
-    String userPass;
-    if (username.length() || password.length())
-        userPass = username + ":" + password + "@";
-
-    return String("http://") + userPass + host + ":" + String::number(port);
-}
-
-void CurlContext::setProxyInfo(const String& host,
-    unsigned long port,
-    CurlProxyType type,
-    const String& username,
-    const String& password)
-{
-    ProxyInfo info;
-
-    info.host = host;
-    info.port = port;
-    info.type = type;
-    info.username = username;
-    info.password = password;
-
-    setProxyInfo(info);
-}
-
 bool CurlContext::isHttp2Enabled() const
 {
     curl_version_info_data* data = ""
@@ -512,11 +484,26 @@
 
 void CurlHandle::enableProxyIfExists()
 {
-    auto& proxy = CurlContext::singleton().proxyInfo();
+    auto& proxy = CurlContext::singleton().proxySettings();
 
-    if (proxy.type != CurlProxyType::Invalid) {
+    switch (proxy.mode()) {
+    case CurlProxySettings::Mode::Default :
+        // For the proxy set by environment variable
+        if (!proxy.user().isEmpty())
+            curl_easy_setopt(m_handle, CURLOPT_PROXYUSERNAME, proxy.user().utf8().data());
+        if (!proxy.password().isEmpty())
+            curl_easy_setopt(m_handle, CURLOPT_PROXYPASSWORD, proxy.password().utf8().data());
+        curl_easy_setopt(m_handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
+        break;
+    case CurlProxySettings::Mode::NoProxy :
+        // Disable the use of a proxy, even if there is an environment variable set for it.
+        curl_easy_setopt(m_handle, CURLOPT_PROXY, "");
+        break;
+    case CurlProxySettings::Mode::Custom :
         curl_easy_setopt(m_handle, CURLOPT_PROXY, proxy.url().utf8().data());
-        curl_easy_setopt(m_handle, CURLOPT_PROXYTYPE, proxy.type);
+        curl_easy_setopt(m_handle, CURLOPT_NOPROXY, proxy.ignoreHosts().utf8().data());
+        curl_easy_setopt(m_handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
+        break;
     }
 }
 

Modified: trunk/Source/WebCore/platform/network/curl/CurlContext.h (230972 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlContext.h	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/network/curl/CurlContext.h	2018-04-24 21:54:49 UTC (rev 230973)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2013 Apple Inc.  All rights reserved.
- * Copyright (C) 2017 Sony Interactive Entertainment Inc.
+ * Copyright (C) 2018 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -26,6 +26,7 @@
 
 #pragma once
 
+#include "CurlProxySettings.h"
 #include "CurlSSLHandle.h"
 #include "URL.h"
 
@@ -44,15 +45,6 @@
 
 namespace WebCore {
 
-enum class CurlProxyType {
-    Invalid = -1,
-    HTTP = CURLPROXY_HTTP,
-    Socks4 = CURLPROXY_SOCKS4,
-    Socks4A = CURLPROXY_SOCKS4A,
-    Socks5 = CURLPROXY_SOCKS5,
-    Socks5Hostname = CURLPROXY_SOCKS5_HOSTNAME
-};
-
 // Values taken from http://www.browserscope.org/ following
 // the rule "Do What Every Other Modern Browser Is Doing".
 const long CurlDefaultMaxConnects { -1 }; // -1 : Does not set CURLMOPT_MAXCONNECTS
@@ -102,16 +94,6 @@
     WTF_MAKE_NONCOPYABLE(CurlContext);
     friend NeverDestroyed<CurlContext>;
 public:
-    struct ProxyInfo {
-        String host;
-        unsigned long port;
-        CurlProxyType type { CurlProxyType::Invalid };
-        String username;
-        String password;
-
-        const String url() const;
-    };
-
     WEBCORE_EXPORT static CurlContext& singleton();
 
     virtual ~CurlContext();
@@ -121,9 +103,9 @@
     CurlRequestScheduler& scheduler() { return *m_scheduler; }
 
     // Proxy
-    const ProxyInfo& proxyInfo() const { return m_proxy; }
-    void setProxyInfo(const ProxyInfo& info) { m_proxy = info;  }
-    void setProxyInfo(const String& host = emptyString(), unsigned long port = 0, CurlProxyType = CurlProxyType::HTTP, const String& username = emptyString(), const String& password = emptyString());
+    const CurlProxySettings& proxySettings() const { return m_proxySettings; }
+    void setProxySettings(const CurlProxySettings& settings) { m_proxySettings = settings; }
+    void setProxyUserPass(const String& user, const String& password) { m_proxySettings.setUserPass(user, password); }
 
     // SSL
     CurlSSLHandle& sslHandle() { return m_sslHandle; }
@@ -144,7 +126,7 @@
     CurlContext();
     void initShareHandle();
 
-    ProxyInfo m_proxy;
+    CurlProxySettings m_proxySettings;
     CurlShareHandle m_shareHandle;
     CurlSSLHandle m_sslHandle;
     std::unique_ptr<CurlRequestScheduler> m_scheduler;

Added: trunk/Source/WebCore/platform/network/curl/CurlProxySettings.cpp (0 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlProxySettings.cpp	                        (rev 0)
+++ trunk/Source/WebCore/platform/network/curl/CurlProxySettings.cpp	2018-04-24 21:54:49 UTC (rev 230973)
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2018 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"
+#include "CurlProxySettings.h"
+
+#if USE(CURL)
+
+namespace WebCore {
+
+static const uint16_t SocksProxyPort = 1080;
+
+static bool protocolIsInSocksFamily(const URL&);
+static std::optional<uint16_t> getProxyPort(const URL&);
+static std::optional<String> createProxyUrl(const URL&);
+
+CurlProxySettings::CurlProxySettings(URL&& proxyUrl, String&& ignoreHosts)
+    : m_mode(Mode::Custom)
+    , m_url(WTFMove(proxyUrl))
+    , m_ignoreHosts(WTFMove(ignoreHosts))
+{
+    if (m_url.protocol().isEmpty())
+        m_url.setProtocol("http");
+
+    rebuildUrl();
+}
+
+void CurlProxySettings::rebuildUrl()
+{
+    if (auto url = ""
+        m_urlSerializedWithPort = WTFMove(*url);
+}
+
+void CurlProxySettings::setUserPass(const String& user, const String& password)
+{
+    m_url.setUser(user);
+    m_url.setPass(password);
+
+    rebuildUrl();
+}
+
+static bool protocolIsInSocksFamily(const URL& url)
+{
+    return url.protocolIs("socks4") || url.protocolIs("socks4a") || url.protocolIs("socks5") || url.protocolIs("socks5h");
+}
+
+static std::optional<uint16_t> getProxyPort(const URL& url)
+{
+    auto port = url.port();
+    if (port)
+        return port;
+
+    // For Curl port, if port number is not specified for HTTP Proxy protocol, port 80 is used.
+    // This differs for libcurl's default port number for HTTP proxy whose value (1080) is for socks.
+    if (url.protocolIsInHTTPFamily())
+        return defaultPortForProtocol(url.protocol());
+
+    if (protocolIsInSocksFamily(url))
+        return SocksProxyPort;
+
+    return std::nullopt;
+}
+
+static std::optional<String> createProxyUrl(const URL &url)
+{
+    if (url.isEmpty() || url.host().isEmpty())
+        return std::nullopt;
+
+    if (url.protocolIsInHTTPFamily() && !protocolIsInSocksFamily(url))
+        return std::nullopt;
+
+    auto port = getProxyPort(url);
+    if (!port)
+        return std::nullopt;
+
+    auto userpass = (url.hasUsername() || url.hasPassword()) ? makeString(url.user(), ":", url.pass(), "@") : String();
+    return makeString(url.protocol(), "://", userpass, url.host(), ":", String::number(*port));
+}
+
+}
+
+#endif

Added: trunk/Source/WebCore/platform/network/curl/CurlProxySettings.h (0 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlProxySettings.h	                        (rev 0)
+++ trunk/Source/WebCore/platform/network/curl/CurlProxySettings.h	2018-04-24 21:54:49 UTC (rev 230973)
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include "URL.h"
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class CurlProxySettings {
+public:
+    enum class Mode {
+        Default,
+        NoProxy,
+        Custom
+    };
+
+    CurlProxySettings() = default;
+    WEBCORE_EXPORT explicit CurlProxySettings(Mode mode) : m_mode(mode) { }
+    WEBCORE_EXPORT CurlProxySettings(URL&&, String&& ignoreHosts);
+
+    WEBCORE_EXPORT CurlProxySettings(const CurlProxySettings&) = default;
+    WEBCORE_EXPORT CurlProxySettings& operator=(const CurlProxySettings&) = default;
+
+    bool isEmpty() const { return m_mode == Mode::Custom && m_urlSerializedWithPort.isEmpty() && m_ignoreHosts.isEmpty(); }
+
+    Mode mode() const { return m_mode; }
+    const String& url() const { return m_urlSerializedWithPort; }
+    const String& ignoreHosts() const { return m_ignoreHosts; }
+
+    WEBCORE_EXPORT void setUserPass(const String&, const String&);
+    const String user() const { return m_url.user(); }
+    const String password() const { return m_url.pass(); }
+
+private:
+    Mode m_mode { Mode::Default };
+    URL m_url;
+    String m_ignoreHosts;
+
+    // We can't simply use m_url.string() because we need to explicitly indicate the port number
+    // to libcurl. URLParser omit the default port while parsing, but libcurl assume 1080 as a
+    // default HTTP Proxy, not 80, if port number is not in the url.
+    String m_urlSerializedWithPort;
+
+    void rebuildUrl();
+};
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp (230972 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp	2018-04-24 21:54:49 UTC (rev 230973)
@@ -312,6 +312,7 @@
 
     m_response.url = ""
     m_response.statusCode = statusCode;
+    m_response.httpConnectCode = httpConnectCode;
 
     if (auto length = m_curlHandle->getContentLength())
         m_response.expectedContentLength = *length;

Modified: trunk/Source/WebCore/platform/network/curl/CurlResponse.h (230972 => 230973)


--- trunk/Source/WebCore/platform/network/curl/CurlResponse.h	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/network/curl/CurlResponse.h	2018-04-24 21:54:49 UTC (rev 230973)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Sony Interactive Entertainment Inc.
+ * Copyright (C) 2018 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -39,6 +39,7 @@
 
         copy.url = ""
         copy.statusCode = statusCode;
+        copy.httpConnectCode = httpConnectCode;
         copy.expectedContentLength = expectedContentLength;
 
         for (const auto& header : headers)
@@ -53,6 +54,7 @@
 
     URL url;
     long statusCode { 0 };
+    long httpConnectCode { 0 };
     long long expectedContentLength { 0 };
     Vector<String> headers;
 

Modified: trunk/Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp (230972 => 230973)


--- trunk/Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp	2018-04-24 21:52:50 UTC (rev 230972)
+++ trunk/Source/WebCore/platform/network/curl/ResourceResponseCurl.cpp	2018-04-24 21:54:49 UTC (rev 230973)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Sony Interactive Entertainment Inc.
+ * Copyright (C) 2018 Sony Interactive Entertainment Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -80,7 +80,7 @@
 ResourceResponse::ResourceResponse(const CurlResponse& response)
     : ResourceResponseBase(response.url, "", response.expectedContentLength, "")
 {
-    setHTTPStatusCode(response.statusCode);
+    setHTTPStatusCode(response.statusCode ? response.statusCode : response.httpConnectCode);
 
     for (const auto& header : response.headers)
         appendHTTPHeaderField(header);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to