[SOUP] Use a webkit subdirectory for the disk cache
[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/text/CString.h>
41 #include <wtf/text/StringBuilder.h>
42
43 #if PLATFORM(EFL)
44 #include "ProxyResolverSoup.h"
45 #endif
46
47 namespace WebCore {
48
49 #if !LOG_DISABLED
50 inline static void soupLogPrinter(SoupLogger*, SoupLoggerLogLevel, char direction, const char* data, gpointer)
51 {
52     LOG(Network, "%c %s", direction, data);
53 }
54 #endif
55
56 SoupNetworkSession& SoupNetworkSession::defaultSession()
57 {
58     static SoupNetworkSession networkSession(soupCookieJar());
59     return networkSession;
60 }
61
62 std::unique_ptr<SoupNetworkSession> SoupNetworkSession::createPrivateBrowsingSession()
63 {
64     return std::unique_ptr<SoupNetworkSession>(new SoupNetworkSession(soupCookieJar()));
65 }
66
67 std::unique_ptr<SoupNetworkSession> SoupNetworkSession::createTestingSession()
68 {
69     GRefPtr<SoupCookieJar> cookieJar = adoptGRef(createPrivateBrowsingCookieJar());
70     return std::unique_ptr<SoupNetworkSession>(new SoupNetworkSession(cookieJar.get()));
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* session, 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(session, 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 = 35;
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     setupLogger();
117
118     g_signal_connect(m_soupSession.get(), "authenticate", G_CALLBACK(authenticateCallback), nullptr);
119 #if ENABLE(WEB_TIMING) && !SOUP_CHECK_VERSION(2, 49, 91)
120     g_signal_connect(m_soupSession.get(), "request-started", G_CALLBACK(requestStartedCallback), nullptr);
121 #endif
122 }
123
124 SoupNetworkSession::SoupNetworkSession(SoupSession* soupSession)
125     : m_soupSession(soupSession)
126 {
127     setupLogger();
128 }
129
130 SoupNetworkSession::~SoupNetworkSession()
131 {
132 }
133
134 void SoupNetworkSession::setupLogger()
135 {
136 #if !LOG_DISABLED
137     if (LogNetwork.state != WTFLogChannelOn || soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_LOGGER))
138         return;
139
140     GRefPtr<SoupLogger> logger = adoptGRef(soup_logger_new(SOUP_LOGGER_LOG_BODY, -1));
141     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(logger.get()));
142     soup_logger_set_printer(logger.get(), soupLogPrinter, nullptr, nullptr);
143 #endif
144 }
145
146 void SoupNetworkSession::setCookieJar(SoupCookieJar* jar)
147 {
148     if (SoupCookieJar* currentJar = cookieJar())
149         soup_session_remove_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(currentJar));
150     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(jar));
151 }
152
153 SoupCookieJar* SoupNetworkSession::cookieJar() const
154 {
155     return SOUP_COOKIE_JAR(soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_COOKIE_JAR));
156 }
157
158 void SoupNetworkSession::setCache(SoupCache* cache)
159 {
160     ASSERT(!soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_CACHE));
161     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(cache));
162 }
163
164 SoupCache* SoupNetworkSession::cache() const
165 {
166     SoupSessionFeature* soupCache = soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_CACHE);
167     return soupCache ? SOUP_CACHE(soupCache) : nullptr;
168 }
169
170 static inline bool stringIsNumeric(const char* str)
171 {
172     while (*str) {
173         if (!g_ascii_isdigit(*str))
174             return false;
175         str++;
176     }
177     return true;
178 }
179
180 void SoupNetworkSession::clearCache(const String& cacheDirectory)
181 {
182     CString cachePath = fileSystemRepresentation(cacheDirectory);
183     GUniquePtr<char> cacheFile(g_build_filename(cachePath.data(), "soup.cache2", nullptr));
184     if (!g_file_test(cacheFile.get(), G_FILE_TEST_IS_REGULAR))
185         return;
186
187     GUniquePtr<GDir> dir(g_dir_open(cachePath.data(), 0, nullptr));
188     if (!dir)
189         return;
190
191     while (const char* name = g_dir_read_name(dir.get())) {
192         if (!g_str_has_prefix(name, "soup.cache") && !stringIsNumeric(name))
193             continue;
194
195         GUniquePtr<gchar> filename(g_build_filename(cachePath.data(), name, nullptr));
196         if (g_file_test(filename.get(), G_FILE_TEST_IS_REGULAR))
197             g_unlink(filename.get());
198     }
199 }
200
201 void SoupNetworkSession::setSSLPolicy(SSLPolicy flags)
202 {
203     g_object_set(m_soupSession.get(),
204         SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, flags & SSLUseSystemCAFile ? TRUE : FALSE,
205         SOUP_SESSION_SSL_STRICT, flags & SSLStrict ? TRUE : FALSE,
206         nullptr);
207 }
208
209 SoupNetworkSession::SSLPolicy SoupNetworkSession::sslPolicy() const
210 {
211     gboolean useSystemCAFile, strict;
212     g_object_get(m_soupSession.get(),
213         SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, &useSystemCAFile,
214         SOUP_SESSION_SSL_STRICT, &strict,
215         nullptr);
216
217     SSLPolicy flags = 0;
218     if (useSystemCAFile)
219         flags |= SSLUseSystemCAFile;
220     if (strict)
221         flags |= SSLStrict;
222     return flags;
223 }
224
225 void SoupNetworkSession::setHTTPProxy(const char* httpProxy, const char* httpProxyExceptions)
226 {
227 #if PLATFORM(EFL)
228     // Only for EFL because GTK port uses the default resolver, which uses GIO's proxy resolver.
229     if (!httpProxy) {
230         soup_session_remove_feature_by_type(m_soupSession.get(), SOUP_TYPE_PROXY_URI_RESOLVER);
231         return;
232     }
233
234     GRefPtr<SoupProxyURIResolver> resolver = adoptGRef(soupProxyResolverWkNew(httpProxy, httpProxyExceptions));
235     soup_session_add_feature(m_soupSession.get(), SOUP_SESSION_FEATURE(resolver.get()));
236 #else
237     UNUSED_PARAM(httpProxy);
238     UNUSED_PARAM(httpProxyExceptions);
239 #endif
240 }
241
242 char* SoupNetworkSession::httpProxy() const
243 {
244 #if PLATFORM(EFL)
245     SoupSessionFeature* soupResolver = soup_session_get_feature(m_soupSession.get(), SOUP_TYPE_PROXY_URI_RESOLVER);
246     if (!soupResolver)
247         return nullptr;
248
249     GUniqueOutPtr<SoupURI> uri;
250     g_object_get(soupResolver, SOUP_PROXY_RESOLVER_WK_PROXY_URI, &uri.outPtr(), nullptr);
251
252     return uri ? soup_uri_to_string(uri.get(), FALSE) : nullptr;
253 #else
254     return nullptr;
255 #endif
256 }
257
258 void SoupNetworkSession::setupHTTPProxyFromEnvironment()
259 {
260 #if PLATFORM(EFL)
261     const char* httpProxy = getenv("http_proxy");
262     if (!httpProxy)
263         return;
264
265     setHTTPProxy(httpProxy, getenv("no_proxy"));
266 #endif
267 }
268
269 static CString buildAcceptLanguages(const Vector<String>& languages)
270 {
271     size_t languagesCount = languages.size();
272
273     // Ignore "C" locale.
274     size_t cLocalePosition = languages.find("c");
275     if (cLocalePosition != notFound)
276         languagesCount--;
277
278     // Fallback to "en" if the list is empty.
279     if (!languagesCount)
280         return "en";
281
282     // Calculate deltas for the quality values.
283     int delta;
284     if (languagesCount < 10)
285         delta = 10;
286     else if (languagesCount < 20)
287         delta = 5;
288     else
289         delta = 1;
290
291     // Set quality values for each language.
292     StringBuilder builder;
293     for (size_t i = 0; i < languages.size(); ++i) {
294         if (i == cLocalePosition)
295             continue;
296
297         if (i)
298             builder.appendLiteral(", ");
299
300         builder.append(languages[i]);
301
302         int quality = 100 - i * delta;
303         if (quality > 0 && quality < 100) {
304             char buffer[8];
305             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
306             builder.append(String::format(";q=%s", buffer));
307         }
308     }
309
310     return builder.toString().utf8();
311 }
312
313 void SoupNetworkSession::setAcceptLanguages(const Vector<String>& languages)
314 {
315     g_object_set(m_soupSession.get(), "accept-language", buildAcceptLanguages(languages).data(), nullptr);
316 }
317
318 } // namespace WebCore
319
320 #endif