a9b18b83905d0182d1d291fe499de8fbb2908109
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkDataTask.cpp
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 #include "config.h"
27 #include "NetworkDataTask.h"
28
29 #if USE(NETWORK_SESSION)
30
31 #include "NetworkDataTaskBlob.h"
32 #include "NetworkLoadParameters.h"
33 #include "NetworkSession.h"
34 #include <WebCore/ResourceError.h>
35 #include <WebCore/ResourceResponse.h>
36 #include <wtf/MainThread.h>
37 #include <wtf/RunLoop.h>
38
39 #if PLATFORM(COCOA)
40 #include "NetworkDataTaskCocoa.h"
41 #endif
42 #if USE(SOUP)
43 #include "NetworkDataTaskSoup.h"
44 #endif
45
46 using namespace WebCore;
47
48 namespace WebKit {
49
50 Ref<NetworkDataTask> NetworkDataTask::create(NetworkSession& session, NetworkDataTaskClient& client, const NetworkLoadParameters& parameters)
51 {
52     if (parameters.request.url().protocolIsBlob())
53         return NetworkDataTaskBlob::create(session, client, parameters.request, parameters.contentSniffingPolicy, parameters.blobFileReferences);
54
55 #if PLATFORM(COCOA)
56     return NetworkDataTaskCocoa::create(session, client, parameters.request, parameters.allowStoredCredentials, parameters.contentSniffingPolicy, parameters.shouldClearReferrerOnHTTPSToHTTPRedirect);
57 #endif
58 #if USE(SOUP)
59     return NetworkDataTaskSoup::create(session, client, parameters.request, parameters.allowStoredCredentials, parameters.contentSniffingPolicy, parameters.shouldClearReferrerOnHTTPSToHTTPRedirect);
60 #endif
61 }
62
63 NetworkDataTask::NetworkDataTask(NetworkSession& session, NetworkDataTaskClient& client, const ResourceRequest& requestWithCredentials, StoredCredentials storedCredentials, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
64     : m_failureTimer(*this, &NetworkDataTask::failureTimerFired)
65     , m_session(session)
66     , m_client(&client)
67     , m_partition(requestWithCredentials.cachePartition())
68     , m_storedCredentials(storedCredentials)
69     , m_lastHTTPMethod(requestWithCredentials.httpMethod())
70     , m_firstRequest(requestWithCredentials)
71     , m_shouldClearReferrerOnHTTPSToHTTPRedirect(shouldClearReferrerOnHTTPSToHTTPRedirect)
72 {
73     ASSERT(RunLoop::isMain());
74
75     if (!requestWithCredentials.url().isValid()) {
76         scheduleFailure(InvalidURLFailure);
77         return;
78     }
79
80     if (!portAllowed(requestWithCredentials.url())) {
81         scheduleFailure(BlockedFailure);
82         return;
83     }
84 }
85
86 NetworkDataTask::~NetworkDataTask()
87 {
88     ASSERT(RunLoop::isMain());
89     ASSERT(!m_client);
90 }
91
92 void NetworkDataTask::scheduleFailure(FailureType type)
93 {
94     ASSERT(type != NoFailure);
95     m_scheduledFailureType = type;
96     m_failureTimer.startOneShot(0_s);
97 }
98
99 void NetworkDataTask::didReceiveResponse(ResourceResponse&& response, ResponseCompletionHandler&& completionHandler)
100 {
101     ASSERT(m_client);
102     if (response.isHTTP09()) {
103         auto url = response.url();
104         std::optional<uint16_t> port = url.port();
105         if (port && !isDefaultPortForProtocol(port.value(), url.protocol())) {
106             cancel();
107             m_client->didCompleteWithError({ String(), 0, url, "Cancelled load from '" + url.stringCenterEllipsizedToLength() + "' because it is using HTTP/0.9." });
108             return;
109         }
110     }
111     m_client->didReceiveResponseNetworkSession(WTFMove(response), WTFMove(completionHandler));
112 }
113
114 bool NetworkDataTask::shouldCaptureExtraNetworkLoadMetrics() const
115 {
116     return m_client->shouldCaptureExtraNetworkLoadMetrics();
117 }
118
119 void NetworkDataTask::failureTimerFired()
120 {
121     RefPtr<NetworkDataTask> protectedThis(this);
122
123     switch (m_scheduledFailureType) {
124     case BlockedFailure:
125         m_scheduledFailureType = NoFailure;
126         if (m_client)
127             m_client->wasBlocked();
128         return;
129     case InvalidURLFailure:
130         m_scheduledFailureType = NoFailure;
131         if (m_client)
132             m_client->cannotShowURL();
133         return;
134     case NoFailure:
135         ASSERT_NOT_REACHED();
136         break;
137     }
138     ASSERT_NOT_REACHED();
139 }
140
141 } // namespace WebKit
142
143 #endif // USE(NETWORK_SESSION)