Commit 51b20818 authored by raphael.kubo.da.costa's avatar raphael.kubo.da.costa Committed by Commit bot

fetch: Align RequestInit's |headers| with the spec.

RequestInit::header is supposed to hold a HeadersInit (ie. just a WebIDL
union) object, not a fully-fledged Headers one.

Make it so, and add a HeadersInit overload to Headers::FillWith() so all the
checks for which union type is being used are centralized in one place.
Additionally, this also makes it easier to finally define RequestInit with
an IDL file in the future.

R=tyoshino@chromium.org,yhirano@chromium.org

Review-Url: https://codereview.chromium.org/2840193002
Cr-Commit-Position: refs/heads/master@{#468309}
parent a17d9624
......@@ -52,23 +52,13 @@ Headers* Headers::Create(ExceptionState&) {
return new Headers;
}
Headers* Headers::Create(
const ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders& init,
ExceptionState& exception_state) {
Headers* Headers::Create(const HeadersInit& init,
ExceptionState& exception_state) {
// "The Headers(|init|) constructor, when invoked, must run these steps:"
// "1. Let |headers| be a new Headers object whose guard is "none".
Headers* headers = Create(exception_state);
// "2. If |init| is given, fill headers with |init|. Rethrow any exception."
if (init.isByteStringSequenceSequence()) {
headers->FillWith(init.getAsByteStringSequenceSequence(), exception_state);
} else if (init.isByteStringByteStringRecord()) {
headers->FillWith(init.getAsByteStringByteStringRecord(), exception_state);
} else if (init.isHeaders()) {
// This branch will not be necessary once http://crbug.com/690428 is fixed.
headers->FillWith(init.getAsHeaders(), exception_state);
} else {
NOTREACHED();
}
headers->FillWith(init, exception_state);
// "3. Return |headers|."
return headers;
}
......@@ -228,6 +218,21 @@ void Headers::FillWith(const Headers* object, ExceptionState& exception_state) {
}
}
void Headers::FillWith(const HeadersInit& init,
ExceptionState& exception_state) {
DCHECK_EQ(header_list_->size(), 0U);
if (init.isByteStringSequenceSequence()) {
FillWith(init.getAsByteStringSequenceSequence(), exception_state);
} else if (init.isByteStringByteStringRecord()) {
FillWith(init.getAsByteStringByteStringRecord(), exception_state);
} else if (init.isHeaders()) {
// This branch will not be necessary once http://crbug.com/690428 is fixed.
FillWith(init.getAsHeaders(), exception_state);
} else {
NOTREACHED();
}
}
void Headers::FillWith(const Vector<Vector<String>>& object,
ExceptionState& exception_state) {
DCHECK(!header_list_->size());
......
......@@ -17,6 +17,9 @@ namespace blink {
class ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders;
class ExceptionState;
using HeadersInit =
ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders;
// http://fetch.spec.whatwg.org/#headers-class
class MODULES_EXPORT Headers final : public GarbageCollected<Headers>,
public ScriptWrappable,
......@@ -33,9 +36,7 @@ class MODULES_EXPORT Headers final : public GarbageCollected<Headers>,
};
static Headers* Create(ExceptionState&);
static Headers* Create(
const ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders&,
ExceptionState&);
static Headers* Create(const HeadersInit&, ExceptionState&);
// Shares the FetchHeaderList. Called when creating a Request or Response.
static Headers* Create(FetchHeaderList*);
......@@ -54,8 +55,7 @@ class MODULES_EXPORT Headers final : public GarbageCollected<Headers>,
// These methods should only be called when size() would return 0.
void FillWith(const Headers*, ExceptionState&);
void FillWith(const Vector<Vector<String>>&, ExceptionState&);
void FillWith(const Vector<std::pair<String, String>>&, ExceptionState&);
void FillWith(const HeadersInit&, ExceptionState&);
FetchHeaderList* HeaderList() const { return header_list_; }
DECLARE_TRACE();
......@@ -65,6 +65,10 @@ class MODULES_EXPORT Headers final : public GarbageCollected<Headers>,
// Shares the FetchHeaderList. Called when creating a Request or Response.
explicit Headers(FetchHeaderList*);
// These methods should only be called when size() would return 0.
void FillWith(const Vector<Vector<String>>&, ExceptionState&);
void FillWith(const Vector<std::pair<String, String>>&, ExceptionState&);
Member<FetchHeaderList> header_list_;
Guard guard_;
......
......@@ -320,7 +320,7 @@ Request* Request::CreateRequestWithRequestOrString(
// We don't create a copy of r's Headers object when init's headers member
// is present.
Headers* headers = nullptr;
if (!init.headers) {
if (init.headers.isNull()) {
headers = r->getHeaders()->Clone();
}
// "Empty |r|'s request's header list."
......@@ -345,8 +345,8 @@ Request* Request::CreateRequestWithRequestOrString(
r->getHeaders()->SetGuard(Headers::kRequestNoCORSGuard);
}
// "Fill |r|'s Headers object with |headers|. Rethrow any exceptions."
if (init.headers) {
r->getHeaders()->FillWith(init.headers.Get(), exception_state);
if (!init.headers.isNull()) {
r->getHeaders()->FillWith(init.headers, exception_state);
} else {
DCHECK(headers);
r->getHeaders()->FillWith(headers, exception_state);
......
......@@ -11,7 +11,6 @@
#include "bindings/core/v8/V8Blob.h"
#include "bindings/core/v8/V8FormData.h"
#include "bindings/core/v8/V8URLSearchParams.h"
#include "bindings/modules/v8/ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders.h"
#include "bindings/modules/v8/V8PasswordCredential.h"
#include "core/dom/URLSearchParams.h"
#include "core/fileapi/Blob.h"
......@@ -107,14 +106,9 @@ RequestInit::RequestInit(ExecutionContext* context,
v8::Isolate* isolate = ToIsolate(context);
if (is_header_set) {
ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders
headers_init;
V8ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders::toImpl(
isolate, v8_headers, headers_init,
UnionTypeConversionMode::kNotNullable, exception_state);
if (exception_state.HadException())
return;
headers = Headers::Create(headers_init, exception_state);
isolate, v8_headers, headers, UnionTypeConversionMode::kNotNullable,
exception_state);
if (exception_state.HadException())
return;
}
......
......@@ -5,6 +5,8 @@
#ifndef RequestInit_h
#define RequestInit_h
#include "bindings/modules/v8/ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders.h"
#include "modules/fetch/Headers.h"
#include "platform/heap/Handle.h"
#include "platform/network/EncodedFormData.h"
#include "platform/weborigin/Referrer.h"
......@@ -17,7 +19,6 @@ class BytesConsumer;
class Dictionary;
class ExecutionContext;
class ExceptionState;
class Headers;
// FIXME: Use IDL dictionary instead of this class.
class RequestInit {
......@@ -27,7 +28,7 @@ class RequestInit {
explicit RequestInit(ExecutionContext*, const Dictionary&, ExceptionState&);
String method;
Member<Headers> headers;
HeadersInit headers;
String content_type;
Member<BytesConsumer> body;
Referrer referrer;
......
......@@ -15,7 +15,6 @@
#include "bindings/core/v8/V8FormData.h"
#include "bindings/core/v8/V8PrivateProperty.h"
#include "bindings/core/v8/V8URLSearchParams.h"
#include "bindings/modules/v8/ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders.h"
#include "core/dom/DOMArrayBuffer.h"
#include "core/dom/DOMArrayBufferView.h"
#include "core/dom/ExecutionContext.h"
......@@ -229,15 +228,7 @@ Response* Response::Create(ScriptState* script_state,
r->response_->HeaderList()->ClearList();
// "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow
// any exceptions."
if (init.headers().isByteStringSequenceSequence()) {
r->headers_->FillWith(init.headers().getAsByteStringSequenceSequence(),
exception_state);
} else if (init.headers().isByteStringByteStringRecord()) {
r->headers_->FillWith(init.headers().getAsByteStringByteStringRecord(),
exception_state);
} else if (init.headers().isHeaders()) {
r->headers_->FillWith(init.headers().getAsHeaders(), exception_state);
}
r->headers_->FillWith(init.headers(), exception_state);
if (exception_state.HadException())
return nullptr;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment