jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google 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 are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 33 | #include "BlobResourceHandle.h" |
| 34 | |
| 35 | #include "AsyncFileStream.h" |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 36 | #include "BlobData.h" |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 37 | #include "FileStream.h" |
andersca@apple.com | a96cf71 | 2014-06-14 17:49:44 +0000 | [diff] [blame] | 38 | #include "HTTPHeaderNames.h" |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 39 | #include "HTTPParsers.h" |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 40 | #include "ParsedContentRange.h" |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 41 | #include "ResourceError.h" |
ap@apple.com | a4afd53 | 2012-03-01 18:06:06 +0000 | [diff] [blame] | 42 | #include "ResourceHandleClient.h" |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 43 | #include "ResourceRequest.h" |
| 44 | #include "ResourceResponse.h" |
ap@apple.com | e6543bf | 2013-04-10 00:04:55 +0000 | [diff] [blame] | 45 | #include "SharedBuffer.h" |
achristensen@apple.com | afdd08f | 2017-11-03 02:54:36 +0000 | [diff] [blame] | 46 | #include <wtf/CompletionHandler.h> |
ross.kirsling@sony.com | 5035543 | 2019-01-24 17:25:57 +0000 | [diff] [blame] | 47 | #include <wtf/FileSystem.h> |
paroga@webkit.org | c309e0e | 2011-07-31 02:23:31 +0000 | [diff] [blame] | 48 | #include <wtf/MainThread.h> |
akling@apple.com | f851598 | 2013-09-02 18:50:01 +0000 | [diff] [blame] | 49 | #include <wtf/Ref.h> |
ross.kirsling@sony.com | 5035543 | 2019-01-24 17:25:57 +0000 | [diff] [blame] | 50 | #include <wtf/URL.h> |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 51 | |
| 52 | namespace WebCore { |
| 53 | |
ap@apple.com | 6eda53b | 2012-03-01 22:50:07 +0000 | [diff] [blame] | 54 | static const unsigned bufferSize = 512 * 1024; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 55 | |
| 56 | static const int httpOK = 200; |
| 57 | static const int httpPartialContent = 206; |
| 58 | static const int httpNotAllowed = 403; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 59 | static const int httpRequestedRangeNotSatisfiable = 416; |
| 60 | static const int httpInternalError = 500; |
| 61 | static const char* httpOKText = "OK"; |
| 62 | static const char* httpPartialContentText = "Partial Content"; |
| 63 | static const char* httpNotAllowedText = "Not Allowed"; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 64 | static const char* httpRequestedRangeNotSatisfiableText = "Requested Range Not Satisfiable"; |
| 65 | static const char* httpInternalErrorText = "Internal Server Error"; |
| 66 | |
ap@apple.com | b2852ac | 2012-02-28 19:49:54 +0000 | [diff] [blame] | 67 | static const char* const webKitBlobResourceDomain = "WebKitBlobResource"; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // BlobResourceSynchronousLoader |
| 71 | |
| 72 | namespace { |
| 73 | |
| 74 | class BlobResourceSynchronousLoader : public ResourceHandleClient { |
| 75 | public: |
| 76 | BlobResourceSynchronousLoader(ResourceError&, ResourceResponse&, Vector<char>&); |
| 77 | |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 78 | void didReceiveResponseAsync(ResourceHandle*, ResourceResponse&&, CompletionHandler<void()>&&) final; |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 79 | void didFail(ResourceHandle*, const ResourceError&) final; |
achristensen@apple.com | afdd08f | 2017-11-03 02:54:36 +0000 | [diff] [blame] | 80 | void willSendRequestAsync(ResourceHandle*, ResourceRequest&&, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&&) final; |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 81 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK) |
achristensen@apple.com | 5953234 | 2018-03-25 06:19:02 +0000 | [diff] [blame] | 82 | void canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace&, CompletionHandler<void(bool)>&&) final; |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 83 | #endif |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | ResourceError& m_error; |
| 87 | ResourceResponse& m_response; |
| 88 | Vector<char>& m_data; |
| 89 | }; |
| 90 | |
| 91 | BlobResourceSynchronousLoader::BlobResourceSynchronousLoader(ResourceError& error, ResourceResponse& response, Vector<char>& data) |
| 92 | : m_error(error) |
| 93 | , m_response(response) |
| 94 | , m_data(data) |
| 95 | { |
| 96 | } |
| 97 | |
achristensen@apple.com | afdd08f | 2017-11-03 02:54:36 +0000 | [diff] [blame] | 98 | void BlobResourceSynchronousLoader::willSendRequestAsync(ResourceHandle*, ResourceRequest&& request, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&& completionHandler) |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 99 | { |
| 100 | ASSERT_NOT_REACHED(); |
achristensen@apple.com | afdd08f | 2017-11-03 02:54:36 +0000 | [diff] [blame] | 101 | completionHandler(WTFMove(request)); |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK) |
achristensen@apple.com | 5953234 | 2018-03-25 06:19:02 +0000 | [diff] [blame] | 105 | void BlobResourceSynchronousLoader::canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace&, CompletionHandler<void(bool)>&& completionHandler) |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 106 | { |
| 107 | ASSERT_NOT_REACHED(); |
achristensen@apple.com | 5953234 | 2018-03-25 06:19:02 +0000 | [diff] [blame] | 108 | completionHandler(false); |
achristensen@apple.com | ee6c1f4 | 2017-11-01 01:13:04 +0000 | [diff] [blame] | 109 | } |
| 110 | #endif |
| 111 | |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 112 | void BlobResourceSynchronousLoader::didReceiveResponseAsync(ResourceHandle* handle, ResourceResponse&& response, CompletionHandler<void()>&& completionHandler) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 113 | { |
| 114 | // We cannot handle the size that is more than maximum integer. |
ap@apple.com | b2852ac | 2012-02-28 19:49:54 +0000 | [diff] [blame] | 115 | if (response.expectedContentLength() > INT_MAX) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 116 | m_error = ResourceError(webKitBlobResourceDomain, static_cast<int>(BlobResourceHandle::Error::NotReadableError), response.url(), "File is too large"); |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 117 | completionHandler(); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | m_response = response; |
| 122 | |
| 123 | // Read all the data. |
| 124 | m_data.resize(static_cast<size_t>(response.expectedContentLength())); |
| 125 | static_cast<BlobResourceHandle*>(handle)->readSync(m_data.data(), static_cast<int>(m_data.size())); |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 126 | completionHandler(); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 127 | } |
| 128 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 129 | void BlobResourceSynchronousLoader::didFail(ResourceHandle*, const ResourceError& error) |
| 130 | { |
| 131 | m_error = error; |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | // BlobResourceHandle |
| 138 | |
achristensen@apple.com | 0faecda | 2016-06-07 18:52:06 +0000 | [diff] [blame] | 139 | Ref<BlobResourceHandle> BlobResourceHandle::createAsync(BlobData* blobData, const ResourceRequest& request, ResourceHandleClient* client) |
ap@apple.com | bb785b6 | 2013-02-21 08:27:10 +0000 | [diff] [blame] | 140 | { |
achristensen@apple.com | 0faecda | 2016-06-07 18:52:06 +0000 | [diff] [blame] | 141 | return adoptRef(*new BlobResourceHandle(blobData, request, client, true)); |
ap@apple.com | bb785b6 | 2013-02-21 08:27:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 144 | void BlobResourceHandle::loadResourceSynchronously(BlobData* blobData, const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 145 | { |
darin@apple.com | 048cc3a | 2016-01-22 17:17:04 +0000 | [diff] [blame] | 146 | if (!equalLettersIgnoringASCIICase(request.httpMethod(), "get")) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 147 | error = ResourceError(webKitBlobResourceDomain, static_cast<int>(Error::MethodNotAllowed), response.url(), "Request method must be GET"); |
ap@apple.com | bb785b6 | 2013-02-21 08:27:10 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 151 | BlobResourceSynchronousLoader loader(error, response, data); |
ap@apple.com | bb785b6 | 2013-02-21 08:27:10 +0000 | [diff] [blame] | 152 | RefPtr<BlobResourceHandle> handle = adoptRef(new BlobResourceHandle(blobData, request, &loader, false)); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 153 | handle->start(); |
| 154 | } |
| 155 | |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 156 | BlobResourceHandle::BlobResourceHandle(BlobData* blobData, const ResourceRequest& request, ResourceHandleClient* client, bool async) |
dbates@webkit.org | 8c69e06 | 2017-11-01 22:23:41 +0000 | [diff] [blame] | 157 | : ResourceHandle { nullptr, request, client, false /* defersLoading */, false /* shouldContentSniff */, true /* shouldContentEncodingSniff */ } |
| 158 | , m_blobData { blobData } |
| 159 | , m_async { async } |
ap@apple.com | 6bb2a24 | 2013-03-05 17:42:47 +0000 | [diff] [blame] | 160 | { |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 161 | if (m_async) |
ysuzuki@apple.com | 1d8e24d | 2019-08-19 06:59:40 +0000 | [diff] [blame^] | 162 | m_asyncStream = makeUnique<AsyncFileStream>(*this); |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 163 | else |
ysuzuki@apple.com | 1d8e24d | 2019-08-19 06:59:40 +0000 | [diff] [blame^] | 164 | m_stream = makeUnique<FileStream>(); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
dbates@webkit.org | f21f3ae | 2017-10-19 23:48:45 +0000 | [diff] [blame] | 167 | BlobResourceHandle::~BlobResourceHandle() = default; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 168 | |
| 169 | void BlobResourceHandle::cancel() |
| 170 | { |
darin@apple.com | c2c1a3e | 2014-11-09 01:01:22 +0000 | [diff] [blame] | 171 | m_asyncStream = nullptr; |
cdumez@apple.com | e548431 | 2016-11-10 05:28:53 +0000 | [diff] [blame] | 172 | m_fileOpened = false; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 173 | |
| 174 | m_aborted = true; |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 175 | |
| 176 | ResourceHandle::cancel(); |
| 177 | } |
| 178 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 179 | void BlobResourceHandle::start() |
| 180 | { |
andersca@apple.com | 79e754c | 2014-06-13 23:01:31 +0000 | [diff] [blame] | 181 | if (!m_async) { |
| 182 | doStart(); |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
andersca@apple.com | 79e754c | 2014-06-13 23:01:31 +0000 | [diff] [blame] | 186 | // Finish this async call quickly and return. |
ggaren@apple.com | 243eacc | 2016-07-15 21:51:07 +0000 | [diff] [blame] | 187 | callOnMainThread([protectedThis = makeRef(*this)]() mutable { |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 188 | protectedThis->doStart(); |
andersca@apple.com | 79e754c | 2014-06-13 23:01:31 +0000 | [diff] [blame] | 189 | }); |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void BlobResourceHandle::doStart() |
| 193 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 194 | ASSERT(isMainThread()); |
| 195 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 196 | // Do not continue if the request is aborted or an error occurs. |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 197 | if (erroredOrAborted()) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 198 | return; |
| 199 | |
youenn.fablet@crf.canon.fr | f7841df | 2016-03-25 14:19:31 +0000 | [diff] [blame] | 200 | if (!equalLettersIgnoringASCIICase(firstRequest().httpMethod(), "get")) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 201 | notifyFail(Error::MethodNotAllowed); |
youenn.fablet@crf.canon.fr | f7841df | 2016-03-25 14:19:31 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 205 | // If the blob data is not found, fail now. |
| 206 | if (!m_blobData) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 207 | notifyFail(Error::NotFoundError); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Parse the "Range" header we care about. |
andersca@apple.com | a96cf71 | 2014-06-14 17:49:44 +0000 | [diff] [blame] | 212 | String range = firstRequest().httpHeaderField(HTTPHeaderName::Range); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 213 | if (!range.isEmpty() && !parseRange(range, m_rangeOffset, m_rangeEnd, m_rangeSuffixLength)) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 214 | m_errorCode = Error::RangeError; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 215 | notifyResponse(); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | if (m_async) |
| 220 | getSizeForNext(); |
| 221 | else { |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 222 | Ref<BlobResourceHandle> protectedThis(*this); // getSizeForNext calls the client |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 223 | for (size_t i = 0; i < m_blobData->items().size() && !erroredOrAborted(); ++i) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 224 | getSizeForNext(); |
| 225 | notifyResponse(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void BlobResourceHandle::getSizeForNext() |
| 230 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 231 | ASSERT(isMainThread()); |
| 232 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 233 | // Do we finish validating and counting size for all items? |
| 234 | if (m_sizeItemCount >= m_blobData->items().size()) { |
| 235 | seek(); |
| 236 | |
| 237 | // Start reading if in asynchronous mode. |
| 238 | if (m_async) { |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 239 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 240 | notifyResponse(); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 241 | } |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | const BlobDataItem& item = m_blobData->items().at(m_sizeItemCount); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 246 | switch (item.type()) { |
| 247 | case BlobDataItem::Type::Data: |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 248 | didGetSize(item.length()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 249 | break; |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 250 | case BlobDataItem::Type::File: |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 251 | // Files know their sizes, but asking the stream to verify that the file wasn't modified. |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 252 | if (m_async) |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 253 | m_asyncStream->getSize(item.file()->path(), item.file()->expectedModificationTime()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 254 | else |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 255 | didGetSize(m_stream->getSize(item.file()->path(), item.file()->expectedModificationTime())); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 256 | break; |
| 257 | default: |
| 258 | ASSERT_NOT_REACHED(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void BlobResourceHandle::didGetSize(long long size) |
| 263 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 264 | ASSERT(isMainThread()); |
| 265 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 266 | // Do not continue if the request is aborted or an error occurs. |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 267 | if (erroredOrAborted()) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 268 | return; |
| 269 | |
| 270 | // If the size is -1, it means the file has been moved or changed. Fail now. |
| 271 | if (size == -1) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 272 | notifyFail(Error::NotFoundError); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | // The size passed back is the size of the whole file. If the underlying item is a sliced file, we need to use the slice length. |
| 277 | const BlobDataItem& item = m_blobData->items().at(m_sizeItemCount); |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 278 | size = item.length(); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 279 | |
| 280 | // Cache the size. |
| 281 | m_itemLengthList.append(size); |
| 282 | |
| 283 | // Count the size. |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 284 | m_totalSize += size; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 285 | m_totalRemainingSize += size; |
| 286 | m_sizeItemCount++; |
| 287 | |
| 288 | // Continue with the next item. |
| 289 | getSizeForNext(); |
| 290 | } |
| 291 | |
| 292 | void BlobResourceHandle::seek() |
| 293 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 294 | ASSERT(isMainThread()); |
| 295 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 296 | // Convert from the suffix length to the range. |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 297 | if (m_rangeSuffixLength != kPositionNotSpecified) { |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 298 | m_rangeOffset = m_totalRemainingSize - m_rangeSuffixLength; |
| 299 | m_rangeEnd = m_rangeOffset + m_rangeSuffixLength - 1; |
| 300 | } |
| 301 | |
| 302 | // Bail out if the range is not provided. |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 303 | if (m_rangeOffset == kPositionNotSpecified) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 304 | return; |
| 305 | |
| 306 | // Skip the initial items that are not in the range. |
| 307 | long long offset = m_rangeOffset; |
| 308 | for (m_readItemCount = 0; m_readItemCount < m_blobData->items().size() && offset >= m_itemLengthList[m_readItemCount]; ++m_readItemCount) |
| 309 | offset -= m_itemLengthList[m_readItemCount]; |
| 310 | |
| 311 | // Set the offset that need to jump to for the first item in the range. |
| 312 | m_currentItemReadSize = offset; |
| 313 | |
| 314 | // Adjust the total remaining size in order not to go beyond the range. |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 315 | if (m_rangeEnd != kPositionNotSpecified) { |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 316 | long long rangeSize = m_rangeEnd - m_rangeOffset + 1; |
| 317 | if (m_totalRemainingSize > rangeSize) |
| 318 | m_totalRemainingSize = rangeSize; |
| 319 | } else |
| 320 | m_totalRemainingSize -= m_rangeOffset; |
| 321 | } |
| 322 | |
| 323 | int BlobResourceHandle::readSync(char* buf, int length) |
| 324 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 325 | ASSERT(isMainThread()); |
| 326 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 327 | ASSERT(!m_async); |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 328 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 329 | |
| 330 | int offset = 0; |
| 331 | int remaining = length; |
| 332 | while (remaining) { |
| 333 | // Do not continue if the request is aborted or an error occurs. |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 334 | if (erroredOrAborted()) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 335 | break; |
| 336 | |
| 337 | // If there is no more remaining data to read, we are done. |
| 338 | if (!m_totalRemainingSize || m_readItemCount >= m_blobData->items().size()) |
| 339 | break; |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 340 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 341 | const BlobDataItem& item = m_blobData->items().at(m_readItemCount); |
| 342 | int bytesRead = 0; |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 343 | if (item.type() == BlobDataItem::Type::Data) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 344 | bytesRead = readDataSync(item, buf + offset, remaining); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 345 | else if (item.type() == BlobDataItem::Type::File) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 346 | bytesRead = readFileSync(item, buf + offset, remaining); |
| 347 | else |
| 348 | ASSERT_NOT_REACHED(); |
| 349 | |
| 350 | if (bytesRead > 0) { |
| 351 | offset += bytesRead; |
| 352 | remaining -= bytesRead; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | int result; |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 357 | if (erroredOrAborted()) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 358 | result = -1; |
| 359 | else |
| 360 | result = length - remaining; |
| 361 | |
ap@apple.com | e6543bf | 2013-04-10 00:04:55 +0000 | [diff] [blame] | 362 | if (result > 0) |
| 363 | notifyReceiveData(buf, result); |
| 364 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 365 | if (!result) |
| 366 | notifyFinish(); |
| 367 | |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | int BlobResourceHandle::readDataSync(const BlobDataItem& item, char* buf, int length) |
| 372 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 373 | ASSERT(isMainThread()); |
| 374 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 375 | ASSERT(!m_async); |
| 376 | |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 377 | long long remaining = item.length() - m_currentItemReadSize; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 378 | int bytesToRead = (length > remaining) ? static_cast<int>(remaining) : length; |
| 379 | if (bytesToRead > m_totalRemainingSize) |
| 380 | bytesToRead = static_cast<int>(m_totalRemainingSize); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 381 | memcpy(buf, item.data().data() + item.offset() + m_currentItemReadSize, bytesToRead); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 382 | m_totalRemainingSize -= bytesToRead; |
| 383 | |
| 384 | m_currentItemReadSize += bytesToRead; |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 385 | if (m_currentItemReadSize == item.length()) { |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 386 | m_readItemCount++; |
| 387 | m_currentItemReadSize = 0; |
| 388 | } |
| 389 | |
| 390 | return bytesToRead; |
| 391 | } |
| 392 | |
| 393 | int BlobResourceHandle::readFileSync(const BlobDataItem& item, char* buf, int length) |
| 394 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 395 | ASSERT(isMainThread()); |
| 396 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 397 | ASSERT(!m_async); |
| 398 | |
| 399 | if (!m_fileOpened) { |
| 400 | long long bytesToRead = m_itemLengthList[m_readItemCount] - m_currentItemReadSize; |
| 401 | if (bytesToRead > m_totalRemainingSize) |
| 402 | bytesToRead = m_totalRemainingSize; |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 403 | bool success = m_stream->openForRead(item.file()->path(), item.offset() + m_currentItemReadSize, bytesToRead); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 404 | m_currentItemReadSize = 0; |
| 405 | if (!success) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 406 | m_errorCode = Error::NotReadableError; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | m_fileOpened = true; |
| 411 | } |
| 412 | |
| 413 | int bytesRead = m_stream->read(buf, length); |
| 414 | if (bytesRead < 0) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 415 | m_errorCode = Error::NotReadableError; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | if (!bytesRead) { |
| 419 | m_stream->close(); |
| 420 | m_fileOpened = false; |
| 421 | m_readItemCount++; |
| 422 | } else |
| 423 | m_totalRemainingSize -= bytesRead; |
| 424 | |
| 425 | return bytesRead; |
| 426 | } |
| 427 | |
| 428 | void BlobResourceHandle::readAsync() |
| 429 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 430 | ASSERT(isMainThread()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 431 | |
| 432 | // Do not continue if the request is aborted or an error occurs. |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 433 | if (erroredOrAborted()) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 434 | return; |
| 435 | |
| 436 | // If there is no more remaining data to read, we are done. |
| 437 | if (!m_totalRemainingSize || m_readItemCount >= m_blobData->items().size()) { |
| 438 | notifyFinish(); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | const BlobDataItem& item = m_blobData->items().at(m_readItemCount); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 443 | if (item.type() == BlobDataItem::Type::Data) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 444 | readDataAsync(item); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 445 | else if (item.type() == BlobDataItem::Type::File) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 446 | readFileAsync(item); |
| 447 | else |
| 448 | ASSERT_NOT_REACHED(); |
| 449 | } |
| 450 | |
| 451 | void BlobResourceHandle::readDataAsync(const BlobDataItem& item) |
| 452 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 453 | ASSERT(isMainThread()); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 454 | ASSERT(item.data().data()); |
| 455 | |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 456 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 457 | |
ap@apple.com | 6901a79 | 2014-05-06 23:34:35 +0000 | [diff] [blame] | 458 | long long bytesToRead = item.length() - m_currentItemReadSize; |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 459 | if (bytesToRead > m_totalRemainingSize) |
| 460 | bytesToRead = m_totalRemainingSize; |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 461 | consumeData(reinterpret_cast<const char*>(item.data().data()->data()) + item.offset() + m_currentItemReadSize, static_cast<int>(bytesToRead)); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 462 | m_currentItemReadSize = 0; |
| 463 | } |
| 464 | |
| 465 | void BlobResourceHandle::readFileAsync(const BlobDataItem& item) |
| 466 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 467 | ASSERT(isMainThread()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 468 | |
| 469 | if (m_fileOpened) { |
| 470 | m_asyncStream->read(m_buffer.data(), m_buffer.size()); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | long long bytesToRead = m_itemLengthList[m_readItemCount] - m_currentItemReadSize; |
| 475 | if (bytesToRead > m_totalRemainingSize) |
| 476 | bytesToRead = static_cast<int>(m_totalRemainingSize); |
beidson@apple.com | 26c1d62 | 2016-03-31 01:21:04 +0000 | [diff] [blame] | 477 | m_asyncStream->openForRead(item.file()->path(), item.offset() + m_currentItemReadSize, bytesToRead); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 478 | m_fileOpened = true; |
| 479 | m_currentItemReadSize = 0; |
| 480 | } |
| 481 | |
| 482 | void BlobResourceHandle::didOpen(bool success) |
| 483 | { |
| 484 | ASSERT(m_async); |
| 485 | |
| 486 | if (!success) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 487 | failed(Error::NotReadableError); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | // Continue the reading. |
| 492 | readAsync(); |
| 493 | } |
| 494 | |
| 495 | void BlobResourceHandle::didRead(int bytesRead) |
| 496 | { |
ap@apple.com | b2852ac | 2012-02-28 19:49:54 +0000 | [diff] [blame] | 497 | if (bytesRead < 0) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 498 | failed(Error::NotReadableError); |
ap@apple.com | b2852ac | 2012-02-28 19:49:54 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 502 | consumeData(m_buffer.data(), bytesRead); |
| 503 | } |
| 504 | |
| 505 | void BlobResourceHandle::consumeData(const char* data, int bytesRead) |
| 506 | { |
| 507 | ASSERT(m_async); |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 508 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 509 | |
| 510 | m_totalRemainingSize -= bytesRead; |
| 511 | |
| 512 | // Notify the client. |
| 513 | if (bytesRead) |
| 514 | notifyReceiveData(data, bytesRead); |
| 515 | |
| 516 | if (m_fileOpened) { |
| 517 | // When the current item is a file item, the reading is completed only if bytesRead is 0. |
| 518 | if (!bytesRead) { |
| 519 | // Close the file. |
| 520 | m_fileOpened = false; |
| 521 | m_asyncStream->close(); |
| 522 | |
| 523 | // Move to the next item. |
| 524 | m_readItemCount++; |
| 525 | } |
| 526 | } else { |
| 527 | // Otherwise, we read the current text item as a whole and move to the next item. |
| 528 | m_readItemCount++; |
| 529 | } |
| 530 | |
| 531 | // Continue the reading. |
| 532 | readAsync(); |
| 533 | } |
| 534 | |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 535 | void BlobResourceHandle::failed(Error errorCode) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 536 | { |
| 537 | ASSERT(m_async); |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 538 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 539 | |
| 540 | // Notify the client. |
| 541 | notifyFail(errorCode); |
| 542 | |
| 543 | // Close the file if needed. |
| 544 | if (m_fileOpened) { |
| 545 | m_fileOpened = false; |
| 546 | m_asyncStream->close(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void BlobResourceHandle::notifyResponse() |
| 551 | { |
| 552 | if (!client()) |
| 553 | return; |
| 554 | |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 555 | if (m_errorCode != Error::NoError) { |
beidson@apple.com | 7bd3d3b | 2016-05-13 23:42:17 +0000 | [diff] [blame] | 556 | Ref<BlobResourceHandle> protectedThis(*this); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 557 | notifyResponseOnError(); |
| 558 | notifyFinish(); |
| 559 | } else |
| 560 | notifyResponseOnSuccess(); |
| 561 | } |
| 562 | |
| 563 | void BlobResourceHandle::notifyResponseOnSuccess() |
| 564 | { |
ap@apple.com | bbc1eb5 | 2014-05-06 22:11:07 +0000 | [diff] [blame] | 565 | ASSERT(isMainThread()); |
| 566 | |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 567 | bool isRangeRequest = m_rangeOffset != kPositionNotSpecified; |
antti@apple.com | 021dc01 | 2014-09-04 20:00:11 +0000 | [diff] [blame] | 568 | ResourceResponse response(firstRequest().url(), m_blobData->contentType(), m_totalRemainingSize, String()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 569 | response.setHTTPStatusCode(isRangeRequest ? httpPartialContent : httpOK); |
| 570 | response.setHTTPStatusText(isRangeRequest ? httpPartialContentText : httpOKText); |
youenn.fablet@crf.canon.fr | f7841df | 2016-03-25 14:19:31 +0000 | [diff] [blame] | 571 | |
| 572 | response.setHTTPHeaderField(HTTPHeaderName::ContentType, m_blobData->contentType()); |
| 573 | response.setHTTPHeaderField(HTTPHeaderName::ContentLength, String::number(m_totalRemainingSize)); |
| 574 | |
jer.noble@apple.com | 382a002 | 2016-01-28 19:17:17 +0000 | [diff] [blame] | 575 | if (isRangeRequest) |
| 576 | response.setHTTPHeaderField(HTTPHeaderName::ContentRange, ParsedContentRange(m_rangeOffset, m_rangeEnd, m_totalSize).headerValue()); |
ap@apple.com | 0c1d49d | 2014-05-02 22:41:24 +0000 | [diff] [blame] | 577 | // FIXME: If a resource identified with a blob: URL is a File object, user agents must use that file's name attribute, |
| 578 | // as if the response had a Content-Disposition header with the filename parameter set to the File's name attribute. |
| 579 | // Notably, this will affect a name suggested in "File Save As". |
ap@apple.com | e6543bf | 2013-04-10 00:04:55 +0000 | [diff] [blame] | 580 | |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 581 | client()->didReceiveResponseAsync(this, WTFMove(response), [this, protectedThis = makeRef(*this)] { |
| 582 | m_buffer.resize(bufferSize); |
| 583 | readAsync(); |
| 584 | }); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void BlobResourceHandle::notifyResponseOnError() |
| 588 | { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 589 | ASSERT(m_errorCode != Error::NoError); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 590 | |
antti@apple.com | 021dc01 | 2014-09-04 20:00:11 +0000 | [diff] [blame] | 591 | ResourceResponse response(firstRequest().url(), "text/plain", 0, String()); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 592 | switch (m_errorCode) { |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 593 | case Error::RangeError: |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 594 | response.setHTTPStatusCode(httpRequestedRangeNotSatisfiable); |
| 595 | response.setHTTPStatusText(httpRequestedRangeNotSatisfiableText); |
| 596 | break; |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 597 | case Error::SecurityError: |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 598 | response.setHTTPStatusCode(httpNotAllowed); |
| 599 | response.setHTTPStatusText(httpNotAllowedText); |
| 600 | break; |
| 601 | default: |
| 602 | response.setHTTPStatusCode(httpInternalError); |
| 603 | response.setHTTPStatusText(httpInternalErrorText); |
| 604 | break; |
| 605 | } |
ap@apple.com | e6543bf | 2013-04-10 00:04:55 +0000 | [diff] [blame] | 606 | |
achristensen@apple.com | cceb9b4 | 2018-01-23 23:22:19 +0000 | [diff] [blame] | 607 | client()->didReceiveResponseAsync(this, WTFMove(response), [this, protectedThis = makeRef(*this)] { |
| 608 | m_buffer.resize(bufferSize); |
| 609 | readAsync(); |
| 610 | }); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void BlobResourceHandle::notifyReceiveData(const char* data, int bytesRead) |
| 614 | { |
| 615 | if (client()) |
achristensen@apple.com | 8aff3d9 | 2017-04-24 17:31:05 +0000 | [diff] [blame] | 616 | client()->didReceiveBuffer(this, SharedBuffer::create(reinterpret_cast<const uint8_t*>(data), bytesRead), bytesRead); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 617 | } |
| 618 | |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 619 | void BlobResourceHandle::notifyFail(Error errorCode) |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 620 | { |
| 621 | if (client()) |
commit-queue@webkit.org | 97d42cd | 2016-08-30 18:22:15 +0000 | [diff] [blame] | 622 | client()->didFail(this, ResourceError(webKitBlobResourceDomain, static_cast<int>(errorCode), firstRequest().url(), String())); |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 623 | } |
| 624 | |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 625 | static void doNotifyFinish(BlobResourceHandle& handle) |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 626 | { |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 627 | if (handle.aborted()) |
| 628 | return; |
japhet@chromium.org | 2807875 | 2011-05-28 02:24:20 +0000 | [diff] [blame] | 629 | |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 630 | if (!handle.client()) |
| 631 | return; |
| 632 | |
joepeck@webkit.org | ff39b7b | 2017-02-25 05:48:51 +0000 | [diff] [blame] | 633 | handle.client()->didFinishLoading(&handle); |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 634 | } |
| 635 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 636 | void BlobResourceHandle::notifyFinish() |
| 637 | { |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 638 | if (!m_async) { |
| 639 | doNotifyFinish(*this); |
jianli@chromium.org | 859b548 | 2011-02-07 23:28:35 +0000 | [diff] [blame] | 640 | return; |
| 641 | } |
| 642 | |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 643 | // Schedule to notify the client from a standalone function because the client might dispose the handle immediately from the callback function |
| 644 | // while we still have BlobResourceHandle calls in the stack. |
ggaren@apple.com | 243eacc | 2016-07-15 21:51:07 +0000 | [diff] [blame] | 645 | callOnMainThread([protectedThis = makeRef(*this)]() mutable { |
cdumez@apple.com | da77cf7 | 2016-05-28 05:51:42 +0000 | [diff] [blame] | 646 | doNotifyFinish(protectedThis); |
andersca@apple.com | 31366e8 | 2015-08-21 20:50:59 +0000 | [diff] [blame] | 647 | }); |
| 648 | |
jianli@chromium.org | 4c6124f | 2010-08-23 20:09:56 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | } // namespace WebCore |