[SOUP] Add NetworkSession implementation and switch to use it
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / NetworkDataTask.h
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if USE(NETWORK_SESSION)
29
30 #include "DownloadID.h"
31 #include "SandboxExtension.h"
32 #include <WebCore/Credential.h>
33 #include <WebCore/FrameLoaderTypes.h>
34 #include <WebCore/ResourceHandleTypes.h>
35 #include <WebCore/ResourceLoaderOptions.h>
36 #include <WebCore/ResourceRequest.h>
37 #include <WebCore/Timer.h>
38 #include <wtf/Function.h>
39 #include <wtf/RetainPtr.h>
40 #include <wtf/text/WTFString.h>
41
42 #if PLATFORM(COCOA)
43 OBJC_CLASS NSURLSessionDataTask;
44 #endif
45
46 #if USE(SOUP)
47 #include <WebCore/ProtectionSpace.h>
48 #include <WebCore/ResourceResponse.h>
49 #include <wtf/RunLoop.h>
50 #include <wtf/glib/GRefPtr.h>
51 #endif
52
53 namespace WebCore {
54 class AuthenticationChallenge;
55 class Credential;
56 class ResourceError;
57 class ResourceRequest;
58 class ResourceResponse;
59 class SharedBuffer;
60 }
61
62 namespace WebKit {
63
64 class Download;
65 class NetworkSession;
66 class PendingDownload;
67 enum class AuthenticationChallengeDisposition;
68
69 typedef Function<void(const WebCore::ResourceRequest&)> RedirectCompletionHandler;
70 typedef Function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)> ChallengeCompletionHandler;
71 typedef Function<void(WebCore::PolicyAction)> ResponseCompletionHandler;
72
73 class NetworkDataTaskClient {
74 public:
75     virtual void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&) = 0;
76     virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&) = 0;
77     virtual void didReceiveResponseNetworkSession(WebCore::ResourceResponse&&, ResponseCompletionHandler&&) = 0;
78     virtual void didReceiveData(Ref<WebCore::SharedBuffer>&&) = 0;
79     virtual void didCompleteWithError(const WebCore::ResourceError&) = 0;
80     virtual void didBecomeDownload() = 0;
81     virtual void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend) = 0;
82     virtual void wasBlocked() = 0;
83     virtual void cannotShowURL() = 0;
84     
85     virtual ~NetworkDataTaskClient() { }
86 };
87
88 class NetworkDataTask : public RefCounted<NetworkDataTask> {
89     friend class NetworkSession;
90 public:
91     static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::StoredCredentials storedCredentials, WebCore::ContentSniffingPolicy shouldContentSniff, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
92     {
93         return adoptRef(*new NetworkDataTask(session, client, request, storedCredentials, shouldContentSniff, shouldClearReferrerOnHTTPSToHTTPRedirect));
94     }
95     
96     void suspend();
97     void cancel();
98     void resume();
99
100     enum class State {
101         Running,
102         Suspended,
103         Canceling,
104         Completed
105     };
106     State state() const;
107
108     typedef uint64_t TaskIdentifier;
109     
110     ~NetworkDataTask();
111
112 #if PLATFORM(COCOA)
113     void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend);
114     void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&);
115     void didCompleteWithError(const WebCore::ResourceError&);
116     void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&);
117     void didReceiveData(Ref<WebCore::SharedBuffer>&&);
118     void didBecomeDownload();
119
120     void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&);
121     void transferSandboxExtensionToDownload(Download&);
122 #endif
123     NetworkDataTaskClient* client() const { return m_client; }
124     void clearClient() { m_client = nullptr; }
125     
126     DownloadID pendingDownloadID() const { return m_pendingDownloadID; }
127     PendingDownload* pendingDownload() const { return m_pendingDownload; }
128     void setPendingDownloadID(DownloadID downloadID)
129     {
130         ASSERT(!m_pendingDownloadID.downloadID());
131         ASSERT(downloadID.downloadID());
132         m_pendingDownloadID = downloadID;
133     }
134     void setPendingDownload(PendingDownload& pendingDownload)
135     {
136         ASSERT(!m_pendingDownload);
137         m_pendingDownload = &pendingDownload;
138     }
139     void setPendingDownloadLocation(const String& filename, const SandboxExtension::Handle&, bool allowOverwrite);
140     const String& pendingDownloadLocation() const { return m_pendingDownloadLocation; }
141     bool isDownload() const { return !!m_pendingDownloadID.downloadID(); }
142
143     const WebCore::ResourceRequest& firstRequest() const { return m_firstRequest; }
144     String suggestedFilename();
145     void setSuggestedFilename(const String&);
146     bool allowsSpecificHTTPSCertificateForHost(const WebCore::AuthenticationChallenge&);
147     
148 private:
149     NetworkDataTask(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::StoredCredentials, WebCore::ContentSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
150
151     bool tryPasswordBasedAuthentication(const WebCore::AuthenticationChallenge&, const ChallengeCompletionHandler&);
152
153 #if USE(SOUP)
154     void timeoutFired();
155     void startTimeout();
156     void stopTimeout();
157     void invalidateAndCancel();
158     void createRequest(const WebCore::ResourceRequest&);
159     void clearRequest();
160     static void sendRequestCallback(SoupRequest*, GAsyncResult*, NetworkDataTask*);
161     void didSendRequest(GRefPtr<GInputStream>&&);
162     void didReceiveResponse();
163     static void tlsErrorsChangedCallback(SoupMessage*, GParamSpec*, NetworkDataTask*);
164     void tlsErrorsChanged();
165     void applyAuthenticationToRequest(WebCore::ResourceRequest&);
166     static void authenticateCallback(SoupSession*, SoupMessage*, SoupAuth*, gboolean retrying, NetworkDataTask*);
167     void authenticate(WebCore::AuthenticationChallenge&&);
168     void continueAuthenticate(WebCore::AuthenticationChallenge&&);
169     static void skipInputStreamForRedirectionCallback(GInputStream*, GAsyncResult*, NetworkDataTask*);
170     void skipInputStreamForRedirection();
171     void didFinishSkipInputStreamForRedirection();
172     bool shouldStartHTTPRedirection();
173     void continueHTTPRedirection();
174     static void readCallback(GInputStream*, GAsyncResult*, NetworkDataTask*);
175     void read();
176     void didRead(gssize bytesRead);
177     void didFinishRead();
178     static void requestNextPartCallback(SoupMultipartInputStream*, GAsyncResult*, NetworkDataTask*);
179     void requestNextPart();
180     void didRequestNextPart(GRefPtr<GInputStream>&&);
181     void didFinishRequestNextPart();
182     static void gotHeadersCallback(SoupMessage*, NetworkDataTask*);
183     void didGetHeaders();
184     static void wroteBodyDataCallback(SoupMessage*, SoupBuffer*, NetworkDataTask*);
185     void didWriteBodyData(uint64_t bytesSent);
186     void download();
187     static void writeDownloadCallback(GOutputStream*, GAsyncResult*, NetworkDataTask*);
188     void writeDownload();
189     void didWriteDownload(gsize bytesWritten);
190     void didFailDownload(const WebCore::ResourceError&);
191     void didFinishDownload();
192     void cleanDownloadFiles();
193     void didFail(const WebCore::ResourceError&);
194 #if ENABLE(WEB_TIMING)
195     static void networkEventCallback(SoupMessage*, GSocketClientEvent, GIOStream*, NetworkDataTask*);
196     void networkEvent(GSocketClientEvent);
197 #if SOUP_CHECK_VERSION(2, 49, 91)
198     static void startingCallback(SoupMessage*, NetworkDataTask*);
199 #else
200     static void requestStartedCallback(SoupSession*, SoupMessage*, SoupSocket*, NetworkDataTask*);
201 #endif
202     void didStartRequest();
203     static void restartedCallback(SoupMessage*, NetworkDataTask*);
204     void didRestart();
205 #endif
206 #endif
207     
208     enum FailureType {
209         NoFailure,
210         BlockedFailure,
211         InvalidURLFailure
212     };
213     FailureType m_scheduledFailureType { NoFailure };
214     WebCore::Timer m_failureTimer;
215     void failureTimerFired();
216     void scheduleFailure(FailureType);
217     
218     RefPtr<NetworkSession> m_session;
219     NetworkDataTaskClient* m_client;
220     PendingDownload* m_pendingDownload { nullptr };
221     DownloadID m_pendingDownloadID;
222     String m_user;
223     String m_password;
224 #if USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
225     WebCore::Credential m_initialCredential;
226 #endif
227     WebCore::StoredCredentials m_storedCredentials;
228     String m_lastHTTPMethod;
229     String m_pendingDownloadLocation;
230     WebCore::ResourceRequest m_firstRequest;
231     bool m_shouldClearReferrerOnHTTPSToHTTPRedirect;
232     RefPtr<SandboxExtension> m_sandboxExtension;
233 #if PLATFORM(COCOA)
234     RetainPtr<NSURLSessionDataTask> m_task;
235 #endif
236 #if USE(SOUP)
237     State m_state { State::Suspended };
238     WebCore::ContentSniffingPolicy m_shouldContentSniff;
239     GRefPtr<SoupRequest> m_soupRequest;
240     GRefPtr<SoupMessage> m_soupMessage;
241     GRefPtr<GInputStream> m_inputStream;
242     GRefPtr<SoupMultipartInputStream> m_multipartInputStream;
243     GRefPtr<GCancellable> m_cancellable;
244     GRefPtr<GAsyncResult> m_pendingResult;
245     WebCore::ProtectionSpace m_protectionSpaceForPersistentStorage;
246     WebCore::Credential m_credentialForPersistentStorage;
247     WebCore::ResourceResponse m_response;
248     Vector<char> m_readBuffer;
249     unsigned m_redirectCount { 0 };
250     uint64_t m_bodyDataTotalBytesSent { 0 };
251     GRefPtr<GFile> m_downloadDestinationFile;
252     GRefPtr<GFile> m_downloadIntermediateFile;
253     GRefPtr<GOutputStream> m_downloadOutputStream;
254     bool m_allowOverwriteDownload { false };
255 #if ENABLE(WEB_TIMING)
256     double m_startTime { 0 };
257 #endif
258     RunLoop::Timer<NetworkDataTask> m_timeoutSource;
259 #endif
260     String m_suggestedFilename;
261 };
262
263 #if PLATFORM(COCOA)
264 WebCore::Credential serverTrustCredential(const WebCore::AuthenticationChallenge&);
265 #endif
266     
267 }
268
269 #endif // USE(NETWORK_SESSION)