| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/fetch/Response.h" | 5 #include "modules/fetch/Response.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/Dictionary.h" | 7 #include "bindings/core/v8/Dictionary.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/V8ArrayBuffer.h" | 10 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 11 #include "bindings/core/v8/V8ArrayBufferView.h" | 11 #include "bindings/core/v8/V8ArrayBufferView.h" |
| 12 #include "bindings/core/v8/V8Binding.h" | 12 #include "bindings/core/v8/V8Binding.h" |
| 13 #include "bindings/core/v8/V8Blob.h" | 13 #include "bindings/core/v8/V8Blob.h" |
| 14 #include "bindings/core/v8/V8FormData.h" | 14 #include "bindings/core/v8/V8FormData.h" |
| 15 #include "bindings/core/v8/V8HiddenValue.h" | 15 #include "bindings/core/v8/V8HiddenValue.h" |
| 16 #include "bindings/core/v8/V8URLSearchParams.h" | 16 #include "bindings/core/v8/V8URLSearchParams.h" |
| 17 #include "core/dom/DOMArrayBuffer.h" | 17 #include "core/dom/DOMArrayBuffer.h" |
| 18 #include "core/dom/DOMArrayBufferView.h" | 18 #include "core/dom/DOMArrayBufferView.h" |
| 19 #include "core/dom/URLSearchParams.h" | 19 #include "core/dom/URLSearchParams.h" |
| 20 #include "core/fileapi/Blob.h" | 20 #include "core/fileapi/Blob.h" |
| 21 #include "core/html/FormData.h" | 21 #include "core/html/FormData.h" |
| 22 #include "core/streams/ReadableStreamOperations.h" | 22 #include "core/streams/ReadableStreamOperations.h" |
| 23 #include "modules/fetch/BlobBytesConsumer.h" |
| 23 #include "modules/fetch/BodyStreamBuffer.h" | 24 #include "modules/fetch/BodyStreamBuffer.h" |
| 24 #include "modules/fetch/DataConsumerHandleUtil.h" | 25 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 25 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 26 #include "modules/fetch/FormDataBytesConsumer.h" |
| 26 #include "modules/fetch/FetchFormDataConsumerHandle.h" | |
| 27 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" | 27 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" |
| 28 #include "modules/fetch/ResponseInit.h" | 28 #include "modules/fetch/ResponseInit.h" |
| 29 #include "platform/network/EncodedFormData.h" | 29 #include "platform/network/EncodedFormData.h" |
| 30 #include "platform/network/HTTPHeaderMap.h" | 30 #include "platform/network/HTTPHeaderMap.h" |
| 31 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" | 31 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
| 32 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
| 33 #include <memory> | 33 #include <memory> |
| 34 | 34 |
| 35 namespace blink { | 35 namespace blink { |
| 36 | 36 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 response->setURL(webResponse.url()); | 47 response->setURL(webResponse.url()); |
| 48 response->setStatus(webResponse.status()); | 48 response->setStatus(webResponse.status()); |
| 49 response->setStatusMessage(webResponse.statusText()); | 49 response->setStatusMessage(webResponse.statusText()); |
| 50 response->setResponseTime(webResponse.responseTime()); | 50 response->setResponseTime(webResponse.responseTime()); |
| 51 response->setCacheStorageCacheName(webResponse.cacheStorageCacheName()); | 51 response->setCacheStorageCacheName(webResponse.cacheStorageCacheName()); |
| 52 | 52 |
| 53 for (HTTPHeaderMap::const_iterator i = webResponse.headers().begin(), end =
webResponse.headers().end(); i != end; ++i) { | 53 for (HTTPHeaderMap::const_iterator i = webResponse.headers().begin(), end =
webResponse.headers().end(); i != end; ++i) { |
| 54 response->headerList()->append(i->key, i->value); | 54 response->headerList()->append(i->key, i->value); |
| 55 } | 55 } |
| 56 | 56 |
| 57 response->replaceBodyStreamBuffer(new BodyStreamBuffer(scriptState, FetchBlo
bDataConsumerHandle::create(scriptState->getExecutionContext(), webResponse.blob
DataHandle()))); | 57 response->replaceBodyStreamBuffer(new BodyStreamBuffer(scriptState, new Blob
BytesConsumer(scriptState->getExecutionContext(), webResponse.blobDataHandle()))
); |
| 58 | 58 |
| 59 // Filter the response according to |webResponse|'s ResponseType. | 59 // Filter the response according to |webResponse|'s ResponseType. |
| 60 switch (webResponse.responseType()) { | 60 switch (webResponse.responseType()) { |
| 61 case WebServiceWorkerResponseTypeBasic: | 61 case WebServiceWorkerResponseTypeBasic: |
| 62 response = response->createBasicFilteredResponse(); | 62 response = response->createBasicFilteredResponse(); |
| 63 break; | 63 break; |
| 64 case WebServiceWorkerResponseTypeCORS: { | 64 case WebServiceWorkerResponseTypeCORS: { |
| 65 HTTPHeaderSet headerNames; | 65 HTTPHeaderSet headerNames; |
| 66 for (const auto& header : webResponse.corsExposedHeaderNames()) | 66 for (const auto& header : webResponse.corsExposedHeaderNames()) |
| 67 headerNames.add(String(header)); | 67 headerNames.add(String(header)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 v8::Isolate* isolate = scriptState->isolate(); | 125 v8::Isolate* isolate = scriptState->isolate(); |
| 126 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 126 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 127 | 127 |
| 128 BodyStreamBuffer* bodyBuffer = nullptr; | 128 BodyStreamBuffer* bodyBuffer = nullptr; |
| 129 String contentType; | 129 String contentType; |
| 130 if (bodyValue.isUndefined() || bodyValue.isNull()) { | 130 if (bodyValue.isUndefined() || bodyValue.isNull()) { |
| 131 // Note: The IDL processor cannot handle this situation. See | 131 // Note: The IDL processor cannot handle this situation. See |
| 132 // https://crbug.com/335871. | 132 // https://crbug.com/335871. |
| 133 } else if (V8Blob::hasInstance(body, isolate)) { | 133 } else if (V8Blob::hasInstance(body, isolate)) { |
| 134 Blob* blob = V8Blob::toImpl(body.As<v8::Object>()); | 134 Blob* blob = V8Blob::toImpl(body.As<v8::Object>()); |
| 135 bodyBuffer = new BodyStreamBuffer(scriptState, FetchBlobDataConsumerHand
le::create(executionContext, blob->blobDataHandle())); | 135 bodyBuffer = new BodyStreamBuffer(scriptState, new BlobBytesConsumer(exe
cutionContext, blob->blobDataHandle())); |
| 136 contentType = blob->type(); | 136 contentType = blob->type(); |
| 137 } else if (body->IsArrayBuffer()) { | 137 } else if (body->IsArrayBuffer()) { |
| 138 bodyBuffer = new BodyStreamBuffer(scriptState, FetchFormDataConsumerHand
le::create(V8ArrayBuffer::toImpl(body.As<v8::Object>()))); | 138 bodyBuffer = new BodyStreamBuffer(scriptState, new FormDataBytesConsumer
(V8ArrayBuffer::toImpl(body.As<v8::Object>()))); |
| 139 } else if (body->IsArrayBufferView()) { | 139 } else if (body->IsArrayBufferView()) { |
| 140 bodyBuffer = new BodyStreamBuffer(scriptState, FetchFormDataConsumerHand
le::create(V8ArrayBufferView::toImpl(body.As<v8::Object>()))); | 140 bodyBuffer = new BodyStreamBuffer(scriptState, new FormDataBytesConsumer
(V8ArrayBufferView::toImpl(body.As<v8::Object>()))); |
| 141 } else if (V8FormData::hasInstance(body, isolate)) { | 141 } else if (V8FormData::hasInstance(body, isolate)) { |
| 142 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object
>())->encodeMultiPartFormData(); | 142 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object
>())->encodeMultiPartFormData(); |
| 143 // Here we handle formData->boundary() as a C-style string. See | 143 // Here we handle formData->boundary() as a C-style string. See |
| 144 // FormDataEncoder::generateUniqueBoundaryString. | 144 // FormDataEncoder::generateUniqueBoundaryString. |
| 145 contentType = AtomicString("multipart/form-data; boundary=") + formData-
>boundary().data(); | 145 contentType = AtomicString("multipart/form-data; boundary=") + formData-
>boundary().data(); |
| 146 bodyBuffer = new BodyStreamBuffer(scriptState, FetchFormDataConsumerHand
le::create(executionContext, formData.release())); | 146 bodyBuffer = new BodyStreamBuffer(scriptState, new FormDataBytesConsumer
(executionContext, formData.release())); |
| 147 } else if (V8URLSearchParams::hasInstance(body, isolate)) { | 147 } else if (V8URLSearchParams::hasInstance(body, isolate)) { |
| 148 RefPtr<EncodedFormData> formData = V8URLSearchParams::toImpl(body.As<v8:
:Object>())->toEncodedFormData(); | 148 RefPtr<EncodedFormData> formData = V8URLSearchParams::toImpl(body.As<v8:
:Object>())->toEncodedFormData(); |
| 149 bodyBuffer = new BodyStreamBuffer(scriptState, FetchFormDataConsumerHand
le::create(executionContext, formData.release())); | 149 bodyBuffer = new BodyStreamBuffer(scriptState, new FormDataBytesConsumer
(executionContext, formData.release())); |
| 150 contentType = "application/x-www-form-urlencoded;charset=UTF-8"; | 150 contentType = "application/x-www-form-urlencoded;charset=UTF-8"; |
| 151 } else if (ReadableStreamOperations::isReadableStream(scriptState, bodyValue
)) { | 151 } else if (ReadableStreamOperations::isReadableStream(scriptState, bodyValue
)) { |
| 152 bodyBuffer = new BodyStreamBuffer(scriptState, bodyValue); | 152 bodyBuffer = new BodyStreamBuffer(scriptState, bodyValue); |
| 153 } else { | 153 } else { |
| 154 String string = toUSVString(isolate, body, exceptionState); | 154 String string = toUSVString(isolate, body, exceptionState); |
| 155 if (exceptionState.hadException()) | 155 if (exceptionState.hadException()) |
| 156 return nullptr; | 156 return nullptr; |
| 157 bodyBuffer = new BodyStreamBuffer(scriptState, FetchFormDataConsumerHand
le::create(string)); | 157 bodyBuffer = new BodyStreamBuffer(scriptState, new FormDataBytesConsumer
(string)); |
| 158 contentType = "text/plain;charset=UTF-8"; | 158 contentType = "text/plain;charset=UTF-8"; |
| 159 } | 159 } |
| 160 Response* response = create(scriptState, bodyBuffer, contentType, ResponseIn
it(init, exceptionState), exceptionState); | 160 Response* response = create(scriptState, bodyBuffer, contentType, ResponseIn
it(init, exceptionState), exceptionState); |
| 161 if (!exceptionState.hadException() && !reader.isEmpty()) { | 161 if (!exceptionState.hadException() && !reader.isEmpty()) { |
| 162 // Add a hidden reference so that the weak persistent in the | 162 // Add a hidden reference so that the weak persistent in the |
| 163 // ReadableStreamDataConsumerHandle will be valid as long as the | 163 // ReadableStreamDataConsumerHandle will be valid as long as the |
| 164 // Response is valid. | 164 // Response is valid. |
| 165 v8::Local<v8::Value> wrapper = toV8(response, scriptState); | 165 v8::Local<v8::Value> wrapper = toV8(response, scriptState); |
| 166 if (wrapper.IsEmpty()) { | 166 if (wrapper.IsEmpty()) { |
| 167 exceptionState.throwTypeError("Cannot create a Response wrapper"); | 167 exceptionState.throwTypeError("Cannot create a Response wrapper"); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 } | 438 } |
| 439 | 439 |
| 440 DEFINE_TRACE(Response) | 440 DEFINE_TRACE(Response) |
| 441 { | 441 { |
| 442 Body::trace(visitor); | 442 Body::trace(visitor); |
| 443 visitor->trace(m_response); | 443 visitor->trace(m_response); |
| 444 visitor->trace(m_headers); | 444 visitor->trace(m_headers); |
| 445 } | 445 } |
| 446 | 446 |
| 447 } // namespace blink | 447 } // namespace blink |
| OLD | NEW |