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.m_status < 200 || 599 < responseInit.m_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.m_status); |
| 37 |
| 38 // "5. Set |r|'s response's status message to |init|'s statusText member." |
| 39 r->m_response->setStatusMessage(AtomicString(responseInit.m_statusText)); |
| 40 |
| 41 // "6. If |init|'s headers member is present, run these substeps:" |
| 42 if (responseInit.m_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.m_headers.get(), exceptionState); |
| 48 if (exceptionState.hadException()) |
| 49 return nullptr; |
| 50 } else if (!responseInit.m_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.m_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
sNull()) |
| 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 unsigned short Response::status() const |
| 119 { |
| 120 // "The status attribute's getter must return response's status." |
| 121 return m_response->status(); |
| 122 } |
| 123 |
| 124 String Response::statusText() const |
| 125 { |
| 126 // "The statusText attribute's getter must return response's status message.
" |
| 127 return m_response->statusMessage(); |
| 128 } |
| 129 |
| 130 PassRefPtr<Headers> Response::headers() const |
| 131 { |
| 132 // "The headers attribute's getter must return the associated Headers object
." |
33 return m_headers; | 133 return m_headers; |
34 } | 134 } |
35 | 135 |
36 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) | 136 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) |
37 { | 137 { |
38 response.setStatus(status()); | 138 m_response->populateWebServiceWorkerResponse(response); |
39 response.setStatusText(statusText()); | |
40 response.setHeaders(m_headers->headerMap()); | |
41 response.setBlobDataHandle(m_blobDataHandle); | |
42 } | 139 } |
43 | 140 |
44 Response::Response(PassRefPtr<BlobDataHandle> blobDataHandle, const ResponseInit
& responseInit) | 141 Response::Response() |
45 : m_status(responseInit.status) | 142 : m_response(FetchResponseData::create()) |
46 , m_statusText(responseInit.statusText) | 143 , m_headers(Headers::create(m_response->headerList())) |
47 , m_headers(responseInit.headers) | |
48 , m_blobDataHandle(blobDataHandle) | |
49 { | 144 { |
| 145 m_headers->setGuard(Headers::ResponseGuard); |
50 ScriptWrappable::init(this); | 146 ScriptWrappable::init(this); |
51 if (!m_headers) | 147 } |
52 m_headers = HeaderMap::create(); | 148 |
| 149 Response::Response(PassRefPtr<FetchResponseData> response) |
| 150 : m_response(response) |
| 151 , m_headers(Headers::create(m_response->headerList())) |
| 152 { |
| 153 m_headers->setGuard(Headers::ResponseGuard); |
| 154 ScriptWrappable::init(this); |
53 } | 155 } |
54 | 156 |
55 } // namespace WebCore | 157 } // namespace WebCore |
OLD | NEW |