| 1 | /* |
| 2 | * Copyright (C) 2016 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. 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 | #include "NetworkDataTask.h" |
| 29 | #include <WebCore/NetworkLoadMetrics.h> |
| 30 | #include <WebCore/ProtectionSpace.h> |
| 31 | #include <WebCore/ResourceResponse.h> |
| 32 | #include <wtf/RunLoop.h> |
| 33 | #include <wtf/glib/GRefPtr.h> |
| 34 | |
| 35 | namespace WebKit { |
| 36 | |
| 37 | class NetworkDataTaskSoup final : public NetworkDataTask { |
| 38 | public: |
| 39 | static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::StoredCredentialsPolicy storedCredentialsPolicy, WebCore::ContentSniffingPolicy shouldContentSniff, WebCore::ContentEncodingSniffingPolicy shouldContentEncodingSniff, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation) |
| 40 | { |
| 41 | return adoptRef(*new NetworkDataTaskSoup(session, client, request, storedCredentialsPolicy, shouldContentSniff, shouldContentEncodingSniff, shouldClearReferrerOnHTTPSToHTTPRedirect, dataTaskIsForMainFrameNavigation)); |
| 42 | } |
| 43 | |
| 44 | ~NetworkDataTaskSoup(); |
| 45 | |
| 46 | private: |
| 47 | NetworkDataTaskSoup(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::StoredCredentialsPolicy, WebCore::ContentSniffingPolicy, WebCore::ContentEncodingSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation); |
| 48 | |
| 49 | void cancel() override; |
| 50 | void resume() override; |
| 51 | void invalidateAndCancel() override; |
| 52 | NetworkDataTask::State state() const override; |
| 53 | |
| 54 | void setPendingDownloadLocation(const String&, SandboxExtension::Handle&&, bool /*allowOverwrite*/) override; |
| 55 | String suggestedFilename() const override; |
| 56 | |
| 57 | void timeoutFired(); |
| 58 | void startTimeout(); |
| 59 | void stopTimeout(); |
| 60 | |
| 61 | void createRequest(WebCore::ResourceRequest&&); |
| 62 | void clearRequest(); |
| 63 | static void sendRequestCallback(SoupRequest*, GAsyncResult*, NetworkDataTaskSoup*); |
| 64 | void didSendRequest(GRefPtr<GInputStream>&&); |
| 65 | void dispatchDidReceiveResponse(); |
| 66 | void dispatchDidCompleteWithError(const WebCore::ResourceError&); |
| 67 | |
| 68 | static gboolean tlsConnectionAcceptCertificateCallback(GTlsConnection*, GTlsCertificate*, GTlsCertificateFlags, NetworkDataTaskSoup*); |
| 69 | bool tlsConnectionAcceptCertificate(GTlsCertificate*, GTlsCertificateFlags); |
| 70 | |
| 71 | void applyAuthenticationToRequest(WebCore::ResourceRequest&); |
| 72 | static void authenticateCallback(SoupSession*, SoupMessage*, SoupAuth*, gboolean retrying, NetworkDataTaskSoup*); |
| 73 | void authenticate(WebCore::AuthenticationChallenge&&); |
| 74 | void continueAuthenticate(WebCore::AuthenticationChallenge&&); |
| 75 | |
| 76 | static void skipInputStreamForRedirectionCallback(GInputStream*, GAsyncResult*, NetworkDataTaskSoup*); |
| 77 | void skipInputStreamForRedirection(); |
| 78 | void didFinishSkipInputStreamForRedirection(); |
| 79 | bool shouldStartHTTPRedirection(); |
| 80 | void continueHTTPRedirection(); |
| 81 | |
| 82 | static void readCallback(GInputStream*, GAsyncResult*, NetworkDataTaskSoup*); |
| 83 | void read(); |
| 84 | void didRead(gssize bytesRead); |
| 85 | void didFinishRead(); |
| 86 | |
| 87 | static void requestNextPartCallback(SoupMultipartInputStream*, GAsyncResult*, NetworkDataTaskSoup*); |
| 88 | void requestNextPart(); |
| 89 | void didRequestNextPart(GRefPtr<GInputStream>&&); |
| 90 | void didFinishRequestNextPart(); |
| 91 | |
| 92 | static void (SoupMessage*, NetworkDataTaskSoup*); |
| 93 | void (); |
| 94 | |
| 95 | static void wroteBodyDataCallback(SoupMessage*, SoupBuffer*, NetworkDataTaskSoup*); |
| 96 | void didWriteBodyData(uint64_t bytesSent); |
| 97 | |
| 98 | void download(); |
| 99 | static void writeDownloadCallback(GOutputStream*, GAsyncResult*, NetworkDataTaskSoup*); |
| 100 | void writeDownload(); |
| 101 | void didWriteDownload(gsize bytesWritten); |
| 102 | void didFailDownload(const WebCore::ResourceError&); |
| 103 | void didFinishDownload(); |
| 104 | void cleanDownloadFiles(); |
| 105 | |
| 106 | void didFail(const WebCore::ResourceError&); |
| 107 | |
| 108 | static void networkEventCallback(SoupMessage*, GSocketClientEvent, GIOStream*, NetworkDataTaskSoup*); |
| 109 | void networkEvent(GSocketClientEvent, GIOStream*); |
| 110 | #if SOUP_CHECK_VERSION(2, 49, 91) |
| 111 | static void startingCallback(SoupMessage*, NetworkDataTaskSoup*); |
| 112 | #else |
| 113 | static void requestStartedCallback(SoupSession*, SoupMessage*, SoupSocket*, NetworkDataTaskSoup*); |
| 114 | #endif |
| 115 | void didStartRequest(); |
| 116 | static void restartedCallback(SoupMessage*, NetworkDataTaskSoup*); |
| 117 | void didRestart(); |
| 118 | |
| 119 | State m_state { State::Suspended }; |
| 120 | WebCore::ContentSniffingPolicy m_shouldContentSniff; |
| 121 | GRefPtr<SoupRequest> m_soupRequest; |
| 122 | GRefPtr<SoupMessage> m_soupMessage; |
| 123 | GRefPtr<GInputStream> m_inputStream; |
| 124 | GRefPtr<SoupMultipartInputStream> m_multipartInputStream; |
| 125 | GRefPtr<GCancellable> m_cancellable; |
| 126 | GRefPtr<GAsyncResult> m_pendingResult; |
| 127 | WebCore::ProtectionSpace m_protectionSpaceForPersistentStorage; |
| 128 | WebCore::Credential m_credentialForPersistentStorage; |
| 129 | WebCore::ResourceRequest m_currentRequest; |
| 130 | WebCore::ResourceResponse m_response; |
| 131 | Vector<char> m_readBuffer; |
| 132 | unsigned m_redirectCount { 0 }; |
| 133 | uint64_t m_bodyDataTotalBytesSent { 0 }; |
| 134 | GRefPtr<GFile> m_downloadDestinationFile; |
| 135 | GRefPtr<GFile> m_downloadIntermediateFile; |
| 136 | GRefPtr<GOutputStream> m_downloadOutputStream; |
| 137 | bool m_allowOverwriteDownload { false }; |
| 138 | WebCore::NetworkLoadMetrics m_networkLoadMetrics; |
| 139 | MonotonicTime m_startTime; |
| 140 | RunLoop::Timer<NetworkDataTaskSoup> m_timeoutSource; |
| 141 | }; |
| 142 | |
| 143 | } // namespace WebKit |
| 144 | |