| 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/RequestInit.h" | 5 #include "modules/fetch/RequestInit.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/Dictionary.h" | 7 #include "bindings/core/v8/Dictionary.h" |
| 8 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 9 #include "bindings/core/v8/V8ArrayBufferView.h" | 9 #include "bindings/core/v8/V8ArrayBufferView.h" |
| 10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
| 11 #include "bindings/core/v8/V8Blob.h" | 11 #include "bindings/core/v8/V8Blob.h" |
| 12 #include "bindings/core/v8/V8FormData.h" | 12 #include "bindings/core/v8/V8FormData.h" |
| 13 #include "bindings/core/v8/V8URLSearchParams.h" | 13 #include "bindings/core/v8/V8URLSearchParams.h" |
| 14 #include "bindings/modules/v8/V8PasswordCredential.h" | 14 #include "bindings/modules/v8/V8PasswordCredential.h" |
| 15 #include "core/dom/URLSearchParams.h" | 15 #include "core/dom/URLSearchParams.h" |
| 16 #include "core/fileapi/Blob.h" | 16 #include "core/fileapi/Blob.h" |
| 17 #include "core/html/FormData.h" | 17 #include "core/html/FormData.h" |
| 18 #include "modules/credentialmanager/PasswordCredential.h" | |
| 19 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 18 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 20 #include "modules/fetch/FetchFormDataConsumerHandle.h" | 19 #include "modules/fetch/FetchFormDataConsumerHandle.h" |
| 21 #include "modules/fetch/Headers.h" | 20 #include "modules/fetch/Headers.h" |
| 22 #include "platform/blob/BlobData.h" | 21 #include "platform/blob/BlobData.h" |
| 23 #include "platform/network/EncodedFormData.h" | 22 #include "platform/network/EncodedFormData.h" |
| 24 #include "platform/weborigin/ReferrerPolicy.h" | 23 #include "platform/weborigin/ReferrerPolicy.h" |
| 25 | 24 |
| 26 namespace blink { | 25 namespace blink { |
| 27 | 26 |
| 28 RequestInit::RequestInit(ExecutionContext* context, const Dictionary& options, E
xceptionState& exceptionState) | 27 RequestInit::RequestInit(ExecutionContext* context, const Dictionary& options, E
xceptionState& exceptionState) |
| 29 : areAnyMembersSet(false) | 28 : areAnyMembersSet(false) |
| 30 , isCredentialRequest(false) | |
| 31 { | 29 { |
| 32 areAnyMembersSet |= DictionaryHelper::get(options, "method", method); | 30 areAnyMembersSet |= DictionaryHelper::get(options, "method", method); |
| 33 areAnyMembersSet |= DictionaryHelper::get(options, "headers", headers); | 31 areAnyMembersSet |= DictionaryHelper::get(options, "headers", headers); |
| 34 if (!headers) { | 32 if (!headers) { |
| 35 Vector<Vector<String>> headersVector; | 33 Vector<Vector<String>> headersVector; |
| 36 if (DictionaryHelper::get(options, "headers", headersVector, exceptionSt
ate)) { | 34 if (DictionaryHelper::get(options, "headers", headersVector, exceptionSt
ate)) { |
| 37 headers = Headers::create(headersVector, exceptionState); | 35 headers = Headers::create(headersVector, exceptionState); |
| 38 areAnyMembersSet = true; | 36 areAnyMembersSet = true; |
| 39 } else { | 37 } else { |
| 40 areAnyMembersSet |= DictionaryHelper::get(options, "headers", header
sDictionary); | 38 areAnyMembersSet |= DictionaryHelper::get(options, "headers", header
sDictionary); |
| 41 } | 39 } |
| 42 } | 40 } |
| 43 areAnyMembersSet |= DictionaryHelper::get(options, "mode", mode); | 41 areAnyMembersSet |= DictionaryHelper::get(options, "mode", mode); |
| 44 areAnyMembersSet |= DictionaryHelper::get(options, "credentials", credential
s); | |
| 45 areAnyMembersSet |= DictionaryHelper::get(options, "redirect", redirect); | 42 areAnyMembersSet |= DictionaryHelper::get(options, "redirect", redirect); |
| 46 AtomicString referrerString; | 43 AtomicString referrerString; |
| 47 bool isReferrerStringSet = DictionaryHelper::get(options, "referrer", referr
erString); | 44 bool isReferrerStringSet = DictionaryHelper::get(options, "referrer", referr
erString); |
| 48 areAnyMembersSet |= isReferrerStringSet; | 45 areAnyMembersSet |= isReferrerStringSet; |
| 49 areAnyMembersSet |= DictionaryHelper::get(options, "integrity", integrity); | 46 areAnyMembersSet |= DictionaryHelper::get(options, "integrity", integrity); |
| 50 | 47 |
| 51 v8::Local<v8::Value> v8Body; | 48 v8::Local<v8::Value> v8Body; |
| 52 bool isBodySet = DictionaryHelper::get(options, "body", v8Body); | 49 bool isBodySet = DictionaryHelper::get(options, "body", v8Body); |
| 53 areAnyMembersSet |= isBodySet; | 50 areAnyMembersSet |= isBodySet; |
| 54 | 51 |
| 52 v8::Local<v8::Value> v8Credential; |
| 53 bool isCredentialSet = DictionaryHelper::get(options, "credentials", v8Crede
ntial); |
| 54 areAnyMembersSet |= isCredentialSet; |
| 55 |
| 55 if (areAnyMembersSet) { | 56 if (areAnyMembersSet) { |
| 56 // A part of the Request constructor algorithm is performed here. See | 57 // A part of the Request constructor algorithm is performed here. See |
| 57 // the comments in the Request constructor code for the detail. | 58 // the comments in the Request constructor code for the detail. |
| 58 | 59 |
| 59 // We need to use "about:client" instead of |clientReferrerString|, | 60 // We need to use "about:client" instead of |clientReferrerString|, |
| 60 // because "about:client" => |clientReferrerString| conversion is done | 61 // because "about:client" => |clientReferrerString| conversion is done |
| 61 // in Request::createRequestWithRequestOrString. | 62 // in Request::createRequestWithRequestOrString. |
| 62 referrer = Referrer("about:client", ReferrerPolicyDefault); | 63 referrer = Referrer("about:client", ReferrerPolicyDefault); |
| 63 if (isReferrerStringSet) | 64 if (isReferrerStringSet) |
| 64 referrer.referrer = referrerString; | 65 referrer.referrer = referrerString; |
| 65 } | 66 } |
| 66 | 67 |
| 67 if (!isBodySet || v8Body->IsUndefined() || v8Body->IsNull()) | 68 v8::Isolate* isolate = toIsolate(context); |
| 69 if (isCredentialSet) { |
| 70 if (V8PasswordCredential::hasInstance(v8Credential, isolate)) { |
| 71 // TODO(mkwst): According to the spec, we'd serialize this once we t
ouch the network. We're |
| 72 // serializing it here, ahead of time, because lifetime issues aroun
d ResourceRequest make |
| 73 // it pretty difficult to pass a PasswordCredential around at the pl
atform level, and the |
| 74 // hop between the browser and renderer processes to deal with servi
ce workers is equally |
| 75 // painful. There should be no developer-visible difference in behav
ior with this option, |
| 76 // except that the `Content-Type` header will be set early. That see
ms reasonable. |
| 77 PasswordCredential* credential = V8PasswordCredential::toImpl(v8::Lo
cal<v8::Object>::Cast(v8Credential)); |
| 78 attachedCredential = credential->encodeFormData(contentType); |
| 79 credentials = "password"; |
| 80 } else if (v8Credential->IsString()) { |
| 81 credentials = toUSVString(isolate, v8Credential, exceptionState); |
| 82 } |
| 83 } |
| 84 |
| 85 if (isCredentialSet || !isBodySet || v8Body->IsUndefined() || v8Body->IsNull
()) |
| 68 return; | 86 return; |
| 69 v8::Isolate* isolate = toIsolate(context); | 87 |
| 70 if (v8Body->IsArrayBuffer()) { | 88 if (v8Body->IsArrayBuffer()) { |
| 71 body = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(v8::Loc
al<v8::Object>::Cast(v8Body))); | 89 body = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(v8::Loc
al<v8::Object>::Cast(v8Body))); |
| 72 } else if (v8Body->IsArrayBufferView()) { | 90 } else if (v8Body->IsArrayBufferView()) { |
| 73 body = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toImpl(v8:
:Local<v8::Object>::Cast(v8Body))); | 91 body = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toImpl(v8:
:Local<v8::Object>::Cast(v8Body))); |
| 74 } else if (V8Blob::hasInstance(v8Body, isolate)) { | 92 } else if (V8Blob::hasInstance(v8Body, isolate)) { |
| 75 RefPtr<BlobDataHandle> blobDataHandle = V8Blob::toImpl(v8::Local<v8::Obj
ect>::Cast(v8Body))->blobDataHandle(); | 93 RefPtr<BlobDataHandle> blobDataHandle = V8Blob::toImpl(v8::Local<v8::Obj
ect>::Cast(v8Body))->blobDataHandle(); |
| 76 contentType = blobDataHandle->type(); | 94 contentType = blobDataHandle->type(); |
| 77 body = FetchBlobDataConsumerHandle::create(context, blobDataHandle.relea
se()); | 95 body = FetchBlobDataConsumerHandle::create(context, blobDataHandle.relea
se()); |
| 78 } else if (V8FormData::hasInstance(v8Body, isolate)) { | 96 } else if (V8FormData::hasInstance(v8Body, isolate)) { |
| 79 RefPtr<EncodedFormData> formData = V8FormData::toImpl(v8::Local<v8::Obje
ct>::Cast(v8Body))->encodeMultiPartFormData(); | 97 RefPtr<EncodedFormData> formData = V8FormData::toImpl(v8::Local<v8::Obje
ct>::Cast(v8Body))->encodeMultiPartFormData(); |
| 80 // Here we handle formData->boundary() as a C-style string. See | 98 // Here we handle formData->boundary() as a C-style string. See |
| 81 // FormDataEncoder::generateUniqueBoundaryString. | 99 // FormDataEncoder::generateUniqueBoundaryString. |
| 82 contentType = AtomicString("multipart/form-data; boundary=") + formData-
>boundary().data(); | 100 contentType = AtomicString("multipart/form-data; boundary=") + formData-
>boundary().data(); |
| 83 body = FetchFormDataConsumerHandle::create(context, formData.release()); | 101 body = FetchFormDataConsumerHandle::create(context, formData.release()); |
| 84 } else if (V8URLSearchParams::hasInstance(v8Body, isolate)) { | 102 } else if (V8URLSearchParams::hasInstance(v8Body, isolate)) { |
| 85 RefPtr<EncodedFormData> formData = V8URLSearchParams::toImpl(v8::Local<v
8::Object>::Cast(v8Body))->encodeFormData(); | 103 RefPtr<EncodedFormData> formData = V8URLSearchParams::toImpl(v8::Local<v
8::Object>::Cast(v8Body))->encodeFormData(); |
| 86 contentType = AtomicString("application/x-www-form-urlencoded;charset=UT
F-8"); | 104 contentType = AtomicString("application/x-www-form-urlencoded;charset=UT
F-8"); |
| 87 body = FetchFormDataConsumerHandle::create(context, formData.release()); | 105 body = FetchFormDataConsumerHandle::create(context, formData.release()); |
| 88 } else if (V8PasswordCredential::hasInstance(v8Body, isolate)) { | |
| 89 // See https://w3c.github.io/webappsec-credential-management/#monkey-pat
ching-fetch-4 | |
| 90 // and https://w3c.github.io/webappsec-credential-management/#monkey-pat
ching-fetch-3 | |
| 91 isCredentialRequest = true; | |
| 92 PasswordCredential* credential = V8PasswordCredential::toImpl(v8::Local<
v8::Object>::Cast(v8Body)); | |
| 93 | |
| 94 RefPtr<EncodedFormData> encodedData = credential->encodeFormData(content
Type); | |
| 95 body = FetchFormDataConsumerHandle::create(context, encodedData.release(
)); | |
| 96 } else if (v8Body->IsString()) { | 106 } else if (v8Body->IsString()) { |
| 97 contentType = "text/plain;charset=UTF-8"; | 107 contentType = "text/plain;charset=UTF-8"; |
| 98 body = FetchFormDataConsumerHandle::create(toUSVString(isolate, v8Body,
exceptionState)); | 108 body = FetchFormDataConsumerHandle::create(toUSVString(isolate, v8Body,
exceptionState)); |
| 99 } | 109 } |
| 100 } | 110 } |
| 101 | 111 |
| 102 } // namespace blink | 112 } // namespace blink |
| OLD | NEW |