Chromium Code Reviews| 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 "Response.h" | 6 #include "Response.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
| 9 #include "core/fileapi/Blob.h" | 9 #include "core/fileapi/Blob.h" |
| 10 #include "modules/serviceworkers/ResponseInit.h" | 10 #include "modules/serviceworkers/ResponseInit.h" |
| 11 #include "platform/NotImplemented.h" | |
| 12 #include "public/platform/WebServiceWorkerResponse.h" | |
| 13 | 11 |
| 14 namespace WebCore { | 12 namespace WebCore { |
| 15 | 13 |
| 16 PassRefPtr<Response> Response::create(Blob* body, const Dictionary& responseInit ) | 14 PassRefPtr<Response> Response::create(Blob* body, const Dictionary& responseInit , ExceptionState& exceptionState) |
| 17 { | 15 { |
| 18 return create(body, ResponseInit(responseInit)); | 16 return create(body, ResponseInit(responseInit), exceptionState); |
| 19 } | 17 } |
| 20 | 18 |
| 21 PassRefPtr<Response> Response::create(Blob* body, const ResponseInit& responseIn it) | 19 PassRefPtr<Response> Response::create(Blob* body, const ResponseInit& responseIn it, ExceptionState& exceptionState) |
| 22 { | 20 { |
| 23 RefPtr<BlobDataHandle> blobDataHandle = body ? body->blobDataHandle() : null ptr; | 21 // "1. If |init|'s status member is not in the range 200 to 599, throw a |
| 22 // RangeError." | |
| 23 if (responseInit.status < 200 || 599 < responseInit.status) { | |
| 24 exceptionState.throwRangeError("Invalid status"); | |
| 25 return nullptr; | |
| 26 } | |
| 24 | 27 |
| 25 // FIXME: Maybe append or override content-length and content-type headers u sing the blob. The spec will clarify what to do: | 28 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase |
| 26 // https://github.com/slightlyoff/ServiceWorker/issues/192 | 29 // token production, throw a TypeError." |
| 27 return adoptRef(new Response(blobDataHandle.release(), responseInit)); | 30 |
| 31 // "3. Let |r| be a new Response object, associated with a new response, | |
| 32 // Headers object, and FetchBodyStream object." | |
| 33 RefPtr<Response> r = adoptRef(new Response()); | |
| 34 | |
| 35 // "4. Set |r|'s response's status to |init|'s status member." | |
| 36 r->m_response->setStatus(responseInit.status); | |
| 37 | |
| 38 // "5. Set |r|'s response's status message to |init|'s statusText member." | |
| 39 r->m_response->setStatusMessage(AtomicString(responseInit.statusText)); | |
| 40 | |
| 41 // "6. If |init|'s headers member is present, run these substeps:" | |
| 42 if (responseInit.headers) { | |
| 43 // "1. Empty |r|'s response's header list." | |
| 44 r->m_response->headerList()->clearList(); | |
| 45 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | |
| 46 // any exceptions." | |
| 47 r->m_headers->fillWith(responseInit.headers.get(), exceptionState); | |
| 48 if (exceptionState.hadException()) | |
| 49 return nullptr; | |
| 50 } else if (!responseInit.headersDictionary.isUndefinedOrNull()) { | |
| 51 // "1. Empty |r|'s response's header list." | |
| 52 r->m_response->headerList()->clearList(); | |
| 53 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | |
| 54 // any exceptions." | |
| 55 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); | |
| 56 if (exceptionState.hadException()) | |
| 57 return nullptr; | |
| 58 } | |
| 59 // "7. If body is given, run these substeps:" | |
| 60 if (body) { | |
| 61 // "1. Let |stream| and |Content-Type| be the result of extracting body. " | |
| 62 // "2. Set |r|'s response's body to |stream|." | |
| 63 // "3. If |r|'s response's header list contains no header named | |
| 64 // `Content-Type`, append `Content-Type`/|Content-Type| to |r|'s | |
| 65 // response's header list." | |
| 66 r->m_response->setBlobDataHandle(body->blobDataHandle()); | |
| 67 if (!r->m_response->headerList()->has("Content-Type") && !body->type().i sEmpty()) | |
|
falken
2014/07/07 08:10:40
The isEmpty() check doesn't seem to be in the quot
horo
2014/07/07 10:19:44
Done.
Changed to !isNull()
falken
2014/07/08 03:56:24
I'm still not sure this follows the spec. The spec
horo
2014/07/08 05:04:43
Humm..
I can't say which is correct.
I filed an is
| |
| 68 r->m_response->headerList()->append("Content-Type", body->type()); | |
| 69 } | |
| 70 | |
| 71 // FIXME: "8. Set |r|'s FetchBodyStream object's MIME type to the result of | |
| 72 // extracting a MIME type from |r|'s response's header list." | |
| 73 | |
| 74 // FIXME: "9. Set |r|'s FetchBodyStream object's codings to the result of | |
| 75 // parsing `Content-Encoding` in |r|'s response's header list if that | |
| 76 // result is not failure." | |
| 77 | |
| 78 // "10. Return r." | |
| 79 return r.release(); | |
| 28 } | 80 } |
| 29 | 81 |
| 30 PassRefPtr<HeaderMap> Response::headers() const | 82 PassRefPtr<Response> Response::create(PassRefPtr<FetchResponseData> response) |
| 31 { | 83 { |
| 32 // FIXME: Implement. Spec will eventually whitelist allowable headers. | 84 return adoptRef(new Response(response)); |
| 85 } | |
| 86 | |
| 87 String Response::type() const | |
| 88 { | |
| 89 // "The type attribute's getter must return response's type." | |
| 90 switch (m_response->type()) { | |
| 91 case FetchResponseData::BasicType: | |
| 92 return "basic"; | |
| 93 case FetchResponseData::CORSType: | |
| 94 return "cors"; | |
| 95 case FetchResponseData::DefaultType: | |
| 96 return "default"; | |
| 97 case FetchResponseData::ErrorType: | |
| 98 return "error"; | |
| 99 case FetchResponseData::OpaqueType: | |
| 100 return "opaque"; | |
| 101 } | |
| 102 ASSERT_NOT_REACHED(); | |
| 103 return ""; | |
| 104 } | |
| 105 | |
| 106 String Response::url() const | |
| 107 { | |
| 108 // "The url attribute's getter must return the empty string if response's | |
| 109 // url is null and response's url, serialized with the exclude fragment | |
| 110 // flag set, otherwise." | |
| 111 if (!m_response->url().hasFragmentIdentifier()) | |
| 112 return m_response->url(); | |
| 113 KURL url(m_response->url()); | |
| 114 url.removeFragmentIdentifier(); | |
| 115 return url; | |
| 116 } | |
| 117 | |
| 118 | |
| 119 unsigned short Response::status() const | |
| 120 { | |
| 121 // "The status attribute's getter must return response's status." | |
| 122 return m_response->status(); | |
| 123 } | |
| 124 | |
| 125 | |
|
falken
2014/07/07 08:10:40
extra newline
horo
2014/07/07 10:19:44
Done.
| |
| 126 String Response::statusText() const | |
| 127 { | |
| 128 // "The statusText attribute's getter must return response's status message. " | |
| 129 return m_response->statusMessage(); | |
| 130 } | |
| 131 | |
| 132 | |
|
falken
2014/07/07 08:10:40
extra newl
horo
2014/07/07 10:19:44
Done.
| |
| 133 PassRefPtr<Headers> Response::headers() const | |
| 134 { | |
| 135 // "The headers attribute's getter must return the associated Headers object ." | |
| 33 return m_headers; | 136 return m_headers; |
| 34 } | 137 } |
| 35 | 138 |
| 36 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse& response) | 139 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse& response) |
| 37 { | 140 { |
| 38 response.setStatus(status()); | 141 m_response->populateWebServiceWorkerResponse(response); |
| 39 response.setStatusText(statusText()); | |
| 40 response.setHeaders(m_headers->headerMap()); | |
| 41 response.setBlobDataHandle(m_blobDataHandle); | |
| 42 } | 142 } |
| 43 | 143 |
| 44 Response::Response(PassRefPtr<BlobDataHandle> blobDataHandle, const ResponseInit & responseInit) | 144 Response::Response() |
| 45 : m_status(responseInit.status) | 145 : m_response(FetchResponseData::create()) |
| 46 , m_statusText(responseInit.statusText) | 146 , m_headers(Headers::create(m_response->headerList())) |
| 47 , m_headers(responseInit.headers) | |
| 48 , m_blobDataHandle(blobDataHandle) | |
| 49 { | 147 { |
| 148 m_headers->setGuard(Headers::ResponseGuard); | |
| 50 ScriptWrappable::init(this); | 149 ScriptWrappable::init(this); |
| 51 if (!m_headers) | 150 } |
| 52 m_headers = HeaderMap::create(); | 151 |
| 152 Response::Response(PassRefPtr<FetchResponseData> response) | |
| 153 : m_response(response) | |
| 154 , m_headers(Headers::create(m_response->headerList())) | |
| 155 { | |
| 156 m_headers->setGuard(Headers::ResponseGuard); | |
| 157 ScriptWrappable::init(this); | |
| 53 } | 158 } |
| 54 | 159 |
| 55 } // namespace WebCore | 160 } // namespace WebCore |
| OLD | NEW |