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")) { |
| 68 if (body->type().isNull()) |
| 69 r->m_response->headerList()->append("Content-Type", ""); |
| 70 else |
| 71 r->m_response->headerList()->append("Content-Type", body->type()
); |
| 72 } |
| 73 } |
| 74 |
| 75 // FIXME: "8. Set |r|'s FetchBodyStream object's MIME type to the result of |
| 76 // extracting a MIME type from |r|'s response's header list." |
| 77 |
| 78 // FIXME: "9. Set |r|'s FetchBodyStream object's codings to the result of |
| 79 // parsing `Content-Encoding` in |r|'s response's header list if that |
| 80 // result is not failure." |
| 81 |
| 82 // "10. Return r." |
| 83 return r.release(); |
28 } | 84 } |
29 | 85 |
30 PassRefPtr<HeaderMap> Response::headers() const | 86 PassRefPtr<Response> Response::create(PassRefPtr<FetchResponseData> response) |
31 { | 87 { |
32 // FIXME: Implement. Spec will eventually whitelist allowable headers. | 88 return adoptRef(new Response(response)); |
| 89 } |
| 90 |
| 91 String Response::type() const |
| 92 { |
| 93 // "The type attribute's getter must return response's type." |
| 94 switch (m_response->type()) { |
| 95 case FetchResponseData::BasicType: |
| 96 return "basic"; |
| 97 case FetchResponseData::CORSType: |
| 98 return "cors"; |
| 99 case FetchResponseData::DefaultType: |
| 100 return "default"; |
| 101 case FetchResponseData::ErrorType: |
| 102 return "error"; |
| 103 case FetchResponseData::OpaqueType: |
| 104 return "opaque"; |
| 105 } |
| 106 ASSERT_NOT_REACHED(); |
| 107 return ""; |
| 108 } |
| 109 |
| 110 String Response::url() const |
| 111 { |
| 112 // "The url attribute's getter must return the empty string if response's |
| 113 // url is null and response's url, serialized with the exclude fragment |
| 114 // flag set, otherwise." |
| 115 if (!m_response->url().hasFragmentIdentifier()) |
| 116 return m_response->url(); |
| 117 KURL url(m_response->url()); |
| 118 url.removeFragmentIdentifier(); |
| 119 return url; |
| 120 } |
| 121 |
| 122 unsigned short Response::status() const |
| 123 { |
| 124 // "The status attribute's getter must return response's status." |
| 125 return m_response->status(); |
| 126 } |
| 127 |
| 128 String Response::statusText() const |
| 129 { |
| 130 // "The statusText attribute's getter must return response's status message.
" |
| 131 return m_response->statusMessage(); |
| 132 } |
| 133 |
| 134 PassRefPtr<Headers> Response::headers() const |
| 135 { |
| 136 // "The headers attribute's getter must return the associated Headers object
." |
33 return m_headers; | 137 return m_headers; |
34 } | 138 } |
35 | 139 |
36 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) | 140 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) |
37 { | 141 { |
38 response.setStatus(status()); | 142 m_response->populateWebServiceWorkerResponse(response); |
39 response.setStatusText(statusText()); | |
40 response.setHeaders(m_headers->headerMap()); | |
41 response.setBlobDataHandle(m_blobDataHandle); | |
42 } | 143 } |
43 | 144 |
44 Response::Response(PassRefPtr<BlobDataHandle> blobDataHandle, const ResponseInit
& responseInit) | 145 Response::Response() |
45 : m_status(responseInit.status) | 146 : m_response(FetchResponseData::create()) |
46 , m_statusText(responseInit.statusText) | 147 , m_headers(Headers::create(m_response->headerList())) |
47 , m_headers(responseInit.headers) | |
48 , m_blobDataHandle(blobDataHandle) | |
49 { | 148 { |
| 149 m_headers->setGuard(Headers::ResponseGuard); |
50 ScriptWrappable::init(this); | 150 ScriptWrappable::init(this); |
51 if (!m_headers) | 151 } |
52 m_headers = HeaderMap::create(); | 152 |
| 153 Response::Response(PassRefPtr<FetchResponseData> response) |
| 154 : m_response(response) |
| 155 , m_headers(Headers::create(m_response->headerList())) |
| 156 { |
| 157 m_headers->setGuard(Headers::ResponseGuard); |
| 158 ScriptWrappable::init(this); |
53 } | 159 } |
54 | 160 |
55 } // namespace WebCore | 161 } // namespace WebCore |
OLD | NEW |