6f51e7716df1303b6a1b002b76ad2860c3644e73
[WebKit-https.git] / Source / WebCore / platform / network / soup / SoupNetworkSession.cpp
1 /*
2  * Copyright (C) 2014 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if USE(SOUP)
29
30 #include "SoupNetworkSession.h"
31
32 #include "AuthenticationChallenge.h"
33 #include "CookieJarSoup.h"
34 #include "FileSystem.h"
35 #include "GUniquePtrSoup.h"
36 #include "Logging.h"
37 #include "ResourceHandle.h"
38 #include <glib/gstdio.h>
39 #include <libsoup/soup.h>
40 #include <wtf/NeverDestroyed.h>
41 #include <wtf/text/CString.h>
42 #include <wtf/text/StringBuilder.h>
43
44 namespace WebCore {
45
46 #if !LOG_DISABLED
47 inline static void soupLogPrinter(SoupLogger*, SoupLoggerLogLevel, char direction, const char* data, gpointer)
48 {
49     LOG(Network, "%c %s", direction, data);
50 }
51 #endif
52
53 SoupNetworkSession& SoupNetworkSession::defaultSession()
54 {
55     static NeverDestroyed<SoupNetworkSession> networkSession(soupCookieJar());
56     return networkSession;
57 }
58
59 std::unique_ptr<SoupNetworkSession> SoupNetworkSession::createPrivateBrowsingSession()
60 {
61     return std::unique_ptr<SoupNetworkSession>(new SoupNetworkSession(soupCookieJar()));
62 }
63
64 std::unique_ptr<SoupNetworkSession> SoupNetworkSession::createTestingSession()
65 {
66     auto cookieJar = adoptGRef(createPrivateBrowsingCookieJar());
67     auto newSoupSession = std::unique_ptr<SoupNetworkSession>(new SoupNetworkSession(cookieJar.get()));
68     // FIXME: Creating a testing session is losing soup session values set when initializing the network process.
69     g_object_set(newSoupSession->soupSession(), "accept-language", "en-us", nullptr);
70     return newSoupSession;
71 }
72
73 std::unique_ptr<SoupNetworkSession> SoupNetworkSession::createForSoupSession(SoupSession* soupSession)
74 {
75     return std::unique_ptr<SoupNetworkSession>(new SoupNetworkSession(soupSession));
76 }
77
78 static void authenticateCallback(SoupSession*, SoupMessage* soupMessage, SoupAuth* soupAuth, gboolean retrying)
79 {
80     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
81     if (!handle)
82         return;
83     handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(soupMessage, soupAuth, retrying, handle.get()));
84 }
85
86 #if ENABLE(WEB_TIMING) && !SOUP_CHECK_VERSION(2, 49, 91)
87 static void requestStartedCallback(SoupSession*, SoupMessage* soupMessage, SoupSocket*, gpointer)
88 {
89     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
90     if (!handle)
91         return;
92     handle->didStartRequest();
93 }
94 #endif
95
96 SoupNetworkSession::SoupNetworkSession(SoupCookieJar* cookieJar)
97     : m_soupSession(adoptGRef(soup_session_async_new()))
98 {
99     // Values taken from http://www.browserscope.org/ following
100     // the rule "Do What Every Other Modern Browser Is Doing". They seem
101     // to significantly improve page loading time compared to soup's
102     // default values.
103     static const int maxConnections = 17;
104     static const int maxConnectionsPerHost = 6;
105
106     g_object_set(m_soupSession.get(),
107         SOUP_SESSION_MAX_CONNS, maxConnections,
108         SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
109         SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
110         SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
111         SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
112         SOUP_SESSION_ADD_FEATURE, cookieJar,
113         SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
114         nullptr);
115
116 #if SOUP_CHECK_VERSION(2, 53, 92)
117     if (soup_auth_negotiate_supported()) {
118         g_object_set(m_soupSession.get(),
119             SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_AUTH_NEGOTIATE,
120             nullptr);
121     }
122 #endif
123
124     setupLogger();
125
126     g_signal_connect(m_soupSession.get(), "authenticate", G_CALLBACK(authenticateCallback), nullptr);
127 #if ENABLE(WEB_TIMING) && !SOUP_CHECK_VERSION(2, 49, 91)
128     g_signal_connect(m_soupSession.get(), "request-started", G_CALLBACK(requestStartedCallback), nullptr);
129 #endif
130 }
131
132 SoupNetworkSession::SoupNetworkSession(SoupSession* soupSession)
133     : m_soupSession(soupSession)
134 {
135     setupLogger();
136 }
137
138 SoupNetworkSession::~SoupNetworkSession()
139 {
140 }
141
142 void SoupNetworkSession::setupLogger()
143 {
144 #if !LOG_DISABLED
145     if (LogNetwork.state != WTFLogChannelOn || soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_LOGGER))
146         return;
147
148     GRefPtr<SoupLogger> logger = adoptGRef(soup_logger_new(SOUP_LOGGER_LOG_BODY, -1));
149     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(logger.get()));
150     soup_logger_set_printer(logger.get(), soupLogPrinter, nullptr, nullptr);
151 #endif
152 }
153
154 void SoupNetworkSession::setCookieJar(SoupCookieJar* jar)
155 {
156     if (SoupCookieJar* currentJar = cookieJar())
157         soup_session_remove_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(currentJar));
158     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(jar));
159 }
160
161 SoupCookieJar* SoupNetworkSession::cookieJar() const
162 {
163     return SOUP_COOKIE_JAR(soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_COOKIE_JAR));
164 }
165
166 static inline bool stringIsNumeric(const char* str)
167 {
168     while (*str) {
169         if (!g_ascii_isdigit(*str))
170             return false;
171         str++;
172     }
173     return true;
174 }
175
176 // Old versions of WebKit created this cache.
177 void SoupNetworkSession::clearOldSoupCache(const String& cacheDirectory)
178 {
179     CString cachePath = fileSystemRepresentation(cacheDirectory);
180     GUniquePtr<char> cacheFile(g_build_filename(cachePath.data(), "soup.cache2", nullptr));
181     if (!g_file_test(cacheFile.get(), G_FILE_TEST_IS_REGULAR))
182         return;
183
184     GUniquePtr<GDir> dir(g_dir_open(cachePath.data(), 0, nullptr));
185     if (!dir)
186         return;
187
188     while (const char* name = g_dir_read_name(dir.get())) {
189         if (!g_str_has_prefix(name, "soup.cache") && !stringIsNumeric(name))
190             continue;
191
192         GUniquePtr<gchar> filename(g_build_filename(cachePath.data(), name, nullptr));
193         if (g_file_test(filename.get(), G_FILE_TEST_IS_REGULAR))
194             g_unlink(filename.get());
195     }
196 }
197
198 void SoupNetworkSession::setSSLPolicy(SSLPolicy flags)
199 {
200     g_object_set(m_soupSession.get(),
201         SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, flags & SSLUseSystemCAFile ? TRUE : FALSE,
202         SOUP_SESSION_SSL_STRICT, flags & SSLStrict ? TRUE : FALSE,
203         nullptr);
204 }
205
206 SoupNetworkSession::SSLPolicy SoupNetworkSession::sslPolicy() const
207 {
208     gboolean useSystemCAFile, strict;
209     g_object_get(m_soupSession.get(),
210         SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, &useSystemCAFile,
211         SOUP_SESSION_SSL_STRICT, &strict,
212         nullptr);
213
214     SSLPolicy flags = 0;
215     if (useSystemCAFile)
216         flags |= SSLUseSystemCAFile;
217     if (strict)
218         flags |= SSLStrict;
219     return flags;
220 }
221
222 void SoupNetworkSession::setHTTPProxy(const char* httpProxy, const char* httpProxyExceptions)
223 {
224 #if PLATFORM(EFL)
225     // Only for EFL because GTK port uses the default resolver, which uses GIO's proxy resolver.
226     GProxyResolver* resolver = nullptr;
227     if (httpProxy) {
228         gchar** ignoreLists = nullptr;
229         if (httpProxyExceptions)
230             ignoreLists =  g_strsplit(httpProxyExceptions, ",", -1);
231
232         resolver = g_simple_proxy_resolver_new(httpProxy, ignoreLists);
233
234         g_strfreev(ignoreLists);
235     }
236
237     g_object_set(m_soupSession.get(), SOUP_SESSION_PROXY_RESOLVER, resolver, nullptr);
238 #else
239     UNUSED_PARAM(httpProxy);
240     UNUSED_PARAM(httpProxyExceptions);
241 #endif
242 }
243
244 void SoupNetworkSession::setupHTTPProxyFromEnvironment()
245 {
246 #if PLATFORM(EFL)
247     const char* httpProxy = getenv("http_proxy");
248     if (!httpProxy)
249         return;
250
251     setHTTPProxy(httpProxy, getenv("no_proxy"));
252 #endif
253 }
254
255 static CString buildAcceptLanguages(const Vector<String>& languages)
256 {
257     size_t languagesCount = languages.size();
258
259     // Ignore "C" locale.
260     size_t cLocalePosition = languages.find("c");
261     if (cLocalePosition != notFound)
262         languagesCount--;
263
264     // Fallback to "en" if the list is empty.
265     if (!languagesCount)
266         return "en";
267
268     // Calculate deltas for the quality values.
269     int delta;
270     if (languagesCount < 10)
271         delta = 10;
272     else if (languagesCount < 20)
273         delta = 5;
274     else
275         delta = 1;
276
277     // Set quality values for each language.
278     StringBuilder builder;
279     for (size_t i = 0; i < languages.size(); ++i) {
280         if (i == cLocalePosition)
281             continue;
282
283         if (i)
284             builder.appendLiteral(", ");
285
286         builder.append(languages[i]);
287
288         int quality = 100 - i * delta;
289         if (quality > 0 && quality < 100) {
290             char buffer[8];
291             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
292             builder.append(String::format(";q=%s", buffer));
293         }
294     }
295
296     return builder.toString().utf8();
297 }
298
299 void SoupNetworkSession::setAcceptLanguages(const Vector<String>& languages)
300 {
301     g_object_set(m_soupSession.get(), "accept-language", buildAcceptLanguages(languages).data(), nullptr);
302 }
303
304 } // namespace WebCore
305
306 #endif