| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/fetch/Response.h" | 6 #include "modules/fetch/Response.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
| 9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/ReadableStreamOperations.h" | |
| 11 #include "bindings/core/v8/ScriptState.h" | |
| 12 #include "bindings/core/v8/V8ArrayBuffer.h" | |
| 13 #include "bindings/core/v8/V8ArrayBufferView.h" | |
| 14 #include "bindings/core/v8/V8Binding.h" | |
| 15 #include "bindings/core/v8/V8Blob.h" | |
| 16 #include "bindings/core/v8/V8FormData.h" | |
| 17 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
| 18 #include "core/dom/DOMArrayBufferView.h" | 11 #include "core/dom/DOMArrayBufferView.h" |
| 19 #include "core/fileapi/Blob.h" | 12 #include "core/fileapi/Blob.h" |
| 20 #include "core/html/FormData.h" | 13 #include "core/html/FormData.h" |
| 21 #include "modules/fetch/BodyStreamBuffer.h" | 14 #include "modules/fetch/BodyStreamBuffer.h" |
| 22 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 15 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 23 #include "modules/fetch/FetchFormDataConsumerHandle.h" | 16 #include "modules/fetch/FetchFormDataConsumerHandle.h" |
| 24 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" | |
| 25 #include "modules/fetch/ResponseInit.h" | 17 #include "modules/fetch/ResponseInit.h" |
| 26 #include "platform/RuntimeEnabledFeatures.h" | |
| 27 #include "platform/network/EncodedFormData.h" | 18 #include "platform/network/EncodedFormData.h" |
| 28 #include "platform/network/HTTPHeaderMap.h" | 19 #include "platform/network/HTTPHeaderMap.h" |
| 29 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" | 20 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
| 30 #include "wtf/RefPtr.h" | 21 #include "wtf/RefPtr.h" |
| 31 | 22 |
| 32 namespace blink { | 23 namespace blink { |
| 33 | 24 |
| 34 namespace { | 25 namespace { |
| 35 | 26 |
| 36 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec
utionContext, const WebServiceWorkerResponse& webResponse) | 27 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec
utionContext, const WebServiceWorkerResponse& webResponse) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 if (!(c == 0x09 // HTAB | 88 if (!(c == 0x09 // HTAB |
| 98 || (0x20 <= c && c <= 0x7E) // SP / VCHAR | 89 || (0x20 <= c && c <= 0x7E) // SP / VCHAR |
| 99 || (0x80 <= c && c <= 0xFF))) // obs-text | 90 || (0x80 <= c && c <= 0xFF))) // obs-text |
| 100 return false; | 91 return false; |
| 101 } | 92 } |
| 102 return true; | 93 return true; |
| 103 } | 94 } |
| 104 | 95 |
| 105 } | 96 } |
| 106 | 97 |
| 107 Response* Response::create(ScriptState* scriptState, ExceptionState& exceptionSt
ate) | 98 Response* Response::create(ExecutionContext* context, ExceptionState& exceptionS
tate) |
| 108 { | 99 { |
| 109 return create(scriptState->executionContext(), nullptr, String(), ResponseIn
it(), exceptionState); | 100 return create(context, nullptr, String(), ResponseInit(), exceptionState); |
| 110 } | 101 } |
| 111 | 102 |
| 112 Response* Response::create(ScriptState* scriptState, ScriptValue bodyValue, cons
t Dictionary& init, ExceptionState& exceptionState) | 103 Response* Response::create(ExecutionContext* context, const BodyInit& body, cons
t Dictionary& init, ExceptionState& exceptionState) |
| 113 { | 104 { |
| 114 v8::Local<v8::Value> body = bodyValue.v8Value(); | 105 ASSERT(!body.isNull()); |
| 115 v8::Isolate* isolate = scriptState->isolate(); | |
| 116 ExecutionContext* executionContext = scriptState->executionContext(); | |
| 117 | |
| 118 OwnPtr<FetchDataConsumerHandle> bodyHandle; | 106 OwnPtr<FetchDataConsumerHandle> bodyHandle; |
| 119 String contentType; | 107 String contentType; |
| 120 if (V8Blob::hasInstance(body, isolate)) { | 108 if (body.isBlob()) { |
| 121 Blob* blob = V8Blob::toImpl(body.As<v8::Object>()); | 109 bodyHandle = FetchBlobDataConsumerHandle::create(context, body.getAsBlob
()->blobDataHandle()); |
| 122 bodyHandle = FetchBlobDataConsumerHandle::create(executionContext, blob-
>blobDataHandle()); | 110 contentType = body.getAsBlob()->type(); |
| 123 contentType = blob->type(); | 111 } else if (body.isUSVString()) { |
| 124 } else if (V8ArrayBuffer::hasInstance(body, isolate)) { | 112 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsUSVString()); |
| 125 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(b
ody.As<v8::Object>())); | 113 contentType = "text/plain;charset=UTF-8"; |
| 126 } else if (V8ArrayBufferView::hasInstance(body, isolate)) { | 114 } else if (body.isArrayBuffer()) { |
| 127 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toIm
pl(body.As<v8::Object>())); | 115 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsArrayBuffer()
); |
| 128 } else if (V8FormData::hasInstance(body, isolate)) { | 116 } else if (body.isArrayBufferView()) { |
| 129 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object
>())->encodeMultiPartFormData(); | 117 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsArrayBufferVi
ew()); |
| 118 } else if (body.isFormData()) { |
| 119 RefPtr<EncodedFormData> formData = body.getAsFormData()->encodeMultiPart
FormData(); |
| 130 // Here we handle formData->boundary() as a C-style string. See | 120 // Here we handle formData->boundary() as a C-style string. See |
| 131 // FormDataEncoder::generateUniqueBoundaryString. | 121 // FormDataEncoder::generateUniqueBoundaryString. |
| 132 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin
g::ConstructFromLiteral) + formData->boundary().data(); | 122 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin
g::ConstructFromLiteral) + formData->boundary().data(); |
| 133 bodyHandle = FetchFormDataConsumerHandle::create(executionContext, formD
ata.release()); | 123 bodyHandle = FetchFormDataConsumerHandle::create(context, formData.relea
se()); |
| 134 } else if (RuntimeEnabledFeatures::responseConstructedWithReadableStreamEnab
led() && ReadableStreamOperations::isReadableStream(scriptState, body)) { | |
| 135 bodyHandle = ReadableStreamDataConsumerHandle::create(scriptState, body)
; | |
| 136 } else { | 124 } else { |
| 137 String string = toUSVString(isolate, body, exceptionState); | 125 ASSERT_NOT_REACHED(); |
| 138 if (exceptionState.hadException()) | 126 return nullptr; |
| 139 return nullptr; | |
| 140 bodyHandle = FetchFormDataConsumerHandle::create(string); | |
| 141 contentType = "text/plain;charset=UTF-8"; | |
| 142 } | 127 } |
| 143 // TODO(yhirano): Add the URLSearchParams case. | 128 return create(context, bodyHandle.release(), contentType, ResponseInit(init,
exceptionState), exceptionState); |
| 144 return create(executionContext, bodyHandle.release(), contentType, ResponseI
nit(init, exceptionState), exceptionState); | |
| 145 } | 129 } |
| 146 | 130 |
| 147 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum
erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep
tionState& exceptionState) | 131 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum
erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep
tionState& exceptionState) |
| 148 { | 132 { |
| 149 unsigned short status = init.status; | 133 unsigned short status = init.status; |
| 150 | 134 |
| 151 // "1. If |init|'s status member is not in the range 200 to 599, inclusive,
throw a | 135 // "1. If |init|'s status member is not in the range 200 to 599, inclusive,
throw a |
| 152 // RangeError." | 136 // RangeError." |
| 153 if (status < 200 || 599 < status) { | 137 if (status < 200 || 599 < status) { |
| 154 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi
gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe
ssages::InclusiveBound)); | 138 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi
gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe
ssages::InclusiveBound)); |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 } | 372 } |
| 389 | 373 |
| 390 DEFINE_TRACE(Response) | 374 DEFINE_TRACE(Response) |
| 391 { | 375 { |
| 392 Body::trace(visitor); | 376 Body::trace(visitor); |
| 393 visitor->trace(m_response); | 377 visitor->trace(m_response); |
| 394 visitor->trace(m_headers); | 378 visitor->trace(m_headers); |
| 395 } | 379 } |
| 396 | 380 |
| 397 } // namespace blink | 381 } // namespace blink |
| OLD | NEW |