Changeset 203445 in webkit


Ignore:
Timestamp:
Jul 19, 2016 11:23:13 PM (8 years ago)
Author:
commit-queue@webkit.org
Message:

[Fetch API] Add a JS builtin to implement https://fetch.spec.whatwg.org/#concept-headers-fill
https://bugs.webkit.org/show_bug.cgi?id=159932

Patch by Youenn Fablet <youenn@apple.com> on 2016-07-19
Reviewed by Alex Christensen.

Covered by existing tests.

Refactoring Headers initializeWith to use the new built-in internal that implements
https://fetch.spec.whatwg.org/#concept-headers-fill.

Refactoring Response constructor to put more checks in the JS builtin fucntion called within constructor.
Making use of the new built-in internal that implements https://fetch.spec.whatwg.org/#concept-headers-fill.

  • CMakeLists.txt: Adding FetchHeadersInternals.js
  • DerivedSources.make: Ditto.
  • Modules/fetch/FetchHeaders.js:

(initializeFetchHeaders): Using fillFetchHeaders new built-in internal.

  • Modules/fetch/FetchInternals.js: Added.

(fillFetchHeaders):

  • Modules/fetch/FetchResponse.cpp: Refactoring to do more in the JS built-in. Splitting of initializeWith so

that the checks are done in the order defined by the spec.
(WebCore::FetchResponse::setStatus):
(WebCore::FetchResponse::initializeWith):
(WebCore::isNullBodyStatus): Deleted.

  • Modules/fetch/FetchResponse.h:
  • Modules/fetch/FetchResponse.idl:
  • Modules/fetch/FetchResponse.js:

(initializeFetchResponse): New built-in internal.

  • WebCore.xcodeproj/project.pbxproj:
  • bindings/js/WebCoreBuiltinNames.h:
Location:
trunk/Source/WebCore
Files:
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/CMakeLists.txt

    r203396 r203445  
    37123712set(WebCore_BUILTINS_SOURCES
    37133713    ${WEBCORE_DIR}/Modules/fetch/FetchHeaders.js
     3714    ${WEBCORE_DIR}/Modules/fetch/FetchInternals.js
    37143715    ${WEBCORE_DIR}/Modules/fetch/FetchResponse.js
    37153716    ${WEBCORE_DIR}/Modules/mediastream/MediaDevices.js
  • trunk/Source/WebCore/ChangeLog

    r203444 r203445  
     12016-07-19  Youenn Fablet  <youenn@apple.com>
     2
     3        [Fetch API] Add a JS builtin to implement https://fetch.spec.whatwg.org/#concept-headers-fill
     4        https://bugs.webkit.org/show_bug.cgi?id=159932
     5
     6        Reviewed by Alex Christensen.
     7
     8        Covered by existing tests.
     9
     10        Refactoring Headers initializeWith to use the new built-in internal that implements
     11        https://fetch.spec.whatwg.org/#concept-headers-fill.
     12
     13        Refactoring Response constructor to put more checks in the JS builtin fucntion called within constructor.
     14        Making use of the new built-in internal that implements https://fetch.spec.whatwg.org/#concept-headers-fill.
     15
     16        * CMakeLists.txt: Adding FetchHeadersInternals.js
     17        * DerivedSources.make: Ditto.
     18        * Modules/fetch/FetchHeaders.js:
     19        (initializeFetchHeaders): Using fillFetchHeaders new built-in internal.
     20        * Modules/fetch/FetchInternals.js: Added.
     21        (fillFetchHeaders):
     22        * Modules/fetch/FetchResponse.cpp: Refactoring to do more in the JS built-in. Splitting of initializeWith so
     23        that the checks are done in the order defined by the spec.
     24        (WebCore::FetchResponse::setStatus):
     25        (WebCore::FetchResponse::initializeWith):
     26        (WebCore::isNullBodyStatus): Deleted.
     27        * Modules/fetch/FetchResponse.h:
     28        * Modules/fetch/FetchResponse.idl:
     29        * Modules/fetch/FetchResponse.js:
     30        (initializeFetchResponse): New built-in internal.
     31        * WebCore.xcodeproj/project.pbxproj:
     32        * bindings/js/WebCoreBuiltinNames.h:
     33
    1342016-07-19  Chris Dumez  <cdumez@apple.com>
    235
  • trunk/Source/WebCore/DerivedSources.make

    r202975 r203445  
    12851285WebCore_BUILTINS_SOURCES = \
    12861286    $(WebCore)/Modules/fetch/FetchHeaders.js \
     1287    $(WebCore)/Modules/fetch/FetchInternals.js \
    12871288    $(WebCore)/Modules/fetch/FetchResponse.js \
    12881289    $(WebCore)/Modules/mediastream/MediaDevices.js \
  • trunk/Source/WebCore/Modules/fetch/FetchHeaders.js

    r203029 r203445  
    3636        throw new @TypeError("headersInit must be an object");
    3737
    38     if (headersInit instanceof @Headers) {
    39         this.@fillFromJS(headersInit);
    40         return this;
    41     }
    42 
    43     if (@isArray(headersInit)) {
    44         for (let i = 0; i < headersInit.length; i++) {
    45             let header = headersInit[i];
    46             if (header.length !== 2)
    47                 throw new @TypeError("headersInit sequence items should contain two values");
    48             this.@appendFromJS(header[0], header[1]);
    49         }
    50         return this;
    51     }
    52 
    53     let propertyNames = @Object.@getOwnPropertyNames(headersInit);
    54     for (let i = 0; i < propertyNames.length; ++i) {
    55         let name = propertyNames[i];
    56         this.@appendFromJS(name, headersInit[name]);
    57     }
     38    @fillFetchHeaders(this, headersInit);
    5839
    5940    return this;
  • trunk/Source/WebCore/Modules/fetch/FetchInternals.js

    r203444 r203445  
    11/*
    2  * Copyright (C) 2016 Canon Inc.
     2 * Copyright (C) 2016 Apple Inc.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    1111 *    documentation and/or other materials provided with the distribution.
    1212 *
    13  * THIS SOFTWARE IS PROVIDED BY CANON INC. ``AS IS'' AND ANY
     13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
    1414 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1515 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CANON INC. OR
     16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
    1717 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1818 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     
    2525
    2626// @conditional=ENABLE(FETCH_API)
     27// @internal
    2728
    28 function initializeFetchHeaders(headersInit)
     29function fillFetchHeaders(headers, headersInit)
    2930{
    3031    "use strict";
    3132
    32     if (headersInit === @undefined)
    33         return this;
    34 
    35     if (!@isObject(headersInit))
    36         throw new @TypeError("headersInit must be an object");
    37 
    3833    if (headersInit instanceof @Headers) {
    39         this.@fillFromJS(headersInit);
    40         return this;
     34        @Headers.prototype.@fillFromJS.@call(headers, headersInit);
     35        return;
    4136    }
    4237
     
    4641            if (header.length !== 2)
    4742                throw new @TypeError("headersInit sequence items should contain two values");
    48             this.@appendFromJS(header[0], header[1]);
     43            @Headers.prototype.@appendFromJS.@call(headers, header[0], header[1]);
    4944        }
    5045        return this;
     
    5449    for (let i = 0; i < propertyNames.length; ++i) {
    5550        let name = propertyNames[i];
    56         this.@appendFromJS(name, headersInit[name]);
     51        @Headers.prototype.@appendFromJS.@call(headers, name, headersInit[name]);
    5752    }
    58 
    59     return this;
    6053}
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.cpp

    r203221 r203445  
    3232#if ENABLE(FETCH_API)
    3333
    34 #include "Dictionary.h"
    3534#include "ExceptionCode.h"
    3635#include "FetchRequest.h"
     
    4645}
    4746
    48 static inline bool isNullBodyStatus(int status)
    49 {
    50     return status == 101 || status == 204 || status == 205 || status == 304;
    51 }
    52 
    5347Ref<FetchResponse> FetchResponse::error(ScriptExecutionContext& context)
    5448{
     
    7670}
    7771
    78 void FetchResponse::initializeWith(const Dictionary& init, ExceptionCode& ec)
    79 {
    80     int status;
    81     if (!init.get("status", status)) {
     72void FetchResponse::setStatus(int status, const String& statusText, ExceptionCode& ec)
     73{
     74    if (!isValidReasonPhrase(statusText)) {
    8275        ec = TypeError;
    8376        return;
    8477    }
    85     if (status < 200 || status > 599) {
    86         ec = RangeError;
    87         return;
    88     }
    89 
    90     String statusText;
    91     if (!init.get("statusText", statusText) || !isValidReasonPhrase(statusText)) {
    92         ec = TypeError;
    93         return;
    94     }
    9578    m_response.setHTTPStatusCode(status);
    9679    m_response.setHTTPStatusText(statusText);
    97 
    98     RefPtr<FetchHeaders> initialHeaders;
    99     if (init.get("headers", initialHeaders))
    100         m_headers->fill(initialHeaders.get());
    101 
    102     JSC::JSValue body;
    103     if (init.get("body", body)) {
    104         if (isNullBodyStatus(status)) {
    105             ec = TypeError;
    106             return;
    107         }
    108         m_body = FetchBody::extract(*init.execState(), body);
    109         if (m_headers->fastGet(HTTPHeaderName::ContentType).isEmpty() && !m_body.mimeType().isEmpty())
    110             m_headers->fastSet(HTTPHeaderName::ContentType, m_body.mimeType());
    111     }
     80}
     81
     82void FetchResponse::initializeWith(JSC::ExecState& execState, JSC::JSValue body)
     83{
     84    m_body = FetchBody::extract(execState, body);
     85    if (m_headers->fastGet(HTTPHeaderName::ContentType).isEmpty() && !m_body.mimeType().isEmpty())
     86        m_headers->fastSet(HTTPHeaderName::ContentType, m_body.mimeType());
    11287}
    11388
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.h

    r203221 r203445  
    3737namespace JSC {
    3838class ArrayBuffer;
     39class ExecState;
     40class JSValue;
    3941};
    4042
     
    5961    static void fetch(ScriptExecutionContext&, const String&, const Dictionary&, FetchPromise&&);
    6062
    61     void initializeWith(const Dictionary&, ExceptionCode&);
     63
     64    void setStatus(int, const String&, ExceptionCode&);
     65    void initializeWith(JSC::ExecState&, JSC::JSValue);
    6266
    6367    Type type() const { return m_response.type(); }
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.idl

    r202275 r203445  
    5555    [NewObject, CallWith=ScriptExecutionContext, RaisesException] FetchResponse clone();
    5656
    57     [PrivateIdentifier, RaisesException] void initializeWith(Dictionary parameters);
     57    [PrivateIdentifier, RaisesException] void setStatus(unsigned short status, DOMString statusText);
     58    [CallWith=ScriptState, PrivateIdentifier] void initializeWith(any body);
    5859};
    5960FetchResponse implements FetchBody;
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.js

    r203029 r203445  
    3030    "use strict";
    3131
    32     let parameters = { };
    33     if (init !== @undefined) {
    34         if (!@isObject(init))
    35             throw new @TypeError("Response init must be an object");
    36         parameters.status = init.status;
    37         parameters.statusText = init.statusText;
    38         if (init.headers)
    39             parameters.headers = (init.headers instanceof @Headers) ? init.headers : new @Headers(init.headers);
     32    if (init === @undefined)
     33        init = { };
     34    else if (!@isObject(init))
     35        throw new @TypeError("Response init must be an object");
     36
     37    let status = (init.status !== @undefined) ? @toNumber(init.status) : 200;
     38    if (status < 200  || status > 599)
     39        throw new @RangeError("Status must be between 200 and 599");
     40
     41    let statusText = (init.statusText !== @undefined) ? init.statusText : "OK";
     42
     43    this.@setStatus(status, statusText);
     44
     45    if (init.headers !== @undefined)
     46        @fillFetchHeaders(this.headers, init.headers);
     47
     48    if (body !== @undefined && body !== null) {
     49        if (status == 101 || status == 204 || status == 205 || status == 304)
     50            throw new @TypeError("Response cannot have a body with the given status");
     51        this.@initializeWith(body);
    4052    }
    4153
    42     if (parameters.status === @undefined)
    43         parameters.status = 200;
    44     if (parameters.statusText === @undefined)
    45         parameters.statusText = "OK";
    46 
    47     if (body !== @undefined && body !== null)
    48          parameters.body = body;
    49 
    50     this.@initializeWith(parameters);
    5154    return this;
    5255}
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r203423 r203445  
    15971597                4162A4581011464700DFF3ED /* JSDedicatedWorkerGlobalScope.h in Headers */ = {isa = PBXBuildFile; fileRef = 4162A4561011464700DFF3ED /* JSDedicatedWorkerGlobalScope.h */; };
    15981598                416E29A6102FA962007FC14E /* WorkerReportingProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 416E29A5102FA962007FC14E /* WorkerReportingProxy.h */; };
     1599                416E6FE81BBD12DF000A6023 /* FetchInternalsBuiltins.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B03D8061BB3110D00B764B9 /* FetchInternalsBuiltins.h */; settings = {ATTRIBUTES = (Private, ); }; };
    15991600                416E6FE81BBD12DF000A6033 /* StreamInternalsBuiltins.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B03D8061BB3110D00B764C9 /* StreamInternalsBuiltins.h */; settings = {ATTRIBUTES = (Private, ); }; };
    16001601                416E6FE81BBD12DF000A6043 /* ReadableStreamInternalsBuiltins.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B03D8061BB3110D00B764D9 /* ReadableStreamInternalsBuiltins.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    1181611817                9908B0FA1BCAD07D00ED0F65 /* ReadableStreamControllerBuiltins.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ReadableStreamControllerBuiltins.cpp; sourceTree = "<group>"; };
    1181711818                9908B0FB1BCAD07D00ED0F65 /* ReadableStreamControllerBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReadableStreamControllerBuiltins.h; sourceTree = "<group>"; };
     11819                9908B0FD1BCAD07D00ED0F45 /* FetchInternalsBuiltins.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FetchInternalsBuiltins.cpp; sourceTree = "<group>"; };
    1181811820                9908B0FD1BCAD07D00ED0F55 /* StreamInternalsBuiltins.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StreamInternalsBuiltins.cpp; sourceTree = "<group>"; };
    1181911821                9908B0FD1BCAD07D00ED0F65 /* ReadableStreamInternalsBuiltins.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ReadableStreamInternalsBuiltins.cpp; sourceTree = "<group>"; };
     
    1185811860                9AB1F37E18E2489A00534743 /* CSSToLengthConversionData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSToLengthConversionData.h; sourceTree = "<group>"; };
    1185911861                9AB1F37F18E2489A00534743 /* CSSToLengthConversionData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CSSToLengthConversionData.cpp; sourceTree = "<group>"; };
     11862                9B03D8061BB3110D00B764B9 /* FetchInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FetchInternalsBuiltins.h; sourceTree = "<group>"; };
    1186011863                9B03D8061BB3110D00B764C9 /* StreamInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StreamInternalsBuiltins.h; sourceTree = "<group>"; };
    1186111864                9B03D8061BB3110D00B764D8 /* ReadableStreamBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReadableStreamBuiltins.h; sourceTree = "<group>"; };
     
    1792017923                                9908B0FA1BCAD07D00ED0F65 /* ReadableStreamControllerBuiltins.cpp */,
    1792117924                                9908B0FB1BCAD07D00ED0F65 /* ReadableStreamControllerBuiltins.h */,
     17925                                9908B0FD1BCAD07D00ED0F45 /* FetchInternalsBuiltins.cpp */,
    1792217926                                9908B0FD1BCAD07D00ED0F65 /* ReadableStreamInternalsBuiltins.cpp */,
     17927                                9B03D8061BB3110D00B764B9 /* FetchInternalsBuiltins.h */,
    1792317928                                9B03D8061BB3110D00B764D9 /* ReadableStreamInternalsBuiltins.h */,
    1792417929                                9908B0FE1BCAD07D00ED0F65 /* ReadableStreamReaderBuiltins.cpp */,
     
    2785427859                                A84D827C11D333ED00972990 /* RawDataDocumentParser.h in Headers */,
    2785527860                                416E6FE91BBD12E5000A6043 /* ReadableStreamBuiltins.h in Headers */,
     27861                                416E6FE81BBD12DF000A6023 /* FetchInternalsBuiltins.h in Headers */,
    2785627862                                416E6FE81BBD12DF000A6043 /* ReadableStreamInternalsBuiltins.h in Headers */,
    2785727863                                FD31603C12B0267600C1A359 /* RealtimeAnalyser.h in Headers */,
  • trunk/Source/WebCore/bindings/js/WebCoreBuiltinNames.h

    r203162 r203445  
    6363    macro(readyPromiseCapability) \
    6464    macro(removeTrack) \
     65    macro(setStatus) \
    6566    macro(state) \
    6667    macro(started) \
Note: See TracChangeset for help on using the changeset viewer.