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" |
11 | 13 |
12 namespace WebCore { | 14 namespace WebCore { |
13 | 15 |
14 PassRefPtr<Response> Response::create(Blob* body, const Dictionary& responseInit
, ExceptionState& exceptionState) | 16 PassRefPtr<Response> Response::create(Blob* body, const Dictionary& responseInit
) |
15 { | 17 { |
16 return create(body, ResponseInit(responseInit), exceptionState); | 18 return create(body, ResponseInit(responseInit)); |
17 } | 19 } |
18 | 20 |
19 PassRefPtr<Response> Response::create(Blob* body, const ResponseInit& responseIn
it, ExceptionState& exceptionState) | 21 PassRefPtr<Response> Response::create(Blob* body, const ResponseInit& responseIn
it) |
20 { | 22 { |
21 // "1. If |init|'s status member is not in the range 200 to 599, throw a | 23 RefPtr<BlobDataHandle> blobDataHandle = body ? body->blobDataHandle() : null
ptr; |
22 // RangeError." | |
23 if (responseInit.status < 200 || 599 < responseInit.status) { | |
24 exceptionState.throwRangeError("Invalid status"); | |
25 return nullptr; | |
26 } | |
27 | 24 |
28 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase | 25 // FIXME: Maybe append or override content-length and content-type headers u
sing the blob. The spec will clarify what to do: |
29 // token production, throw a TypeError." | 26 // https://github.com/slightlyoff/ServiceWorker/issues/192 |
30 | 27 return adoptRef(new Response(blobDataHandle.release(), responseInit)); |
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(); | |
84 } | 28 } |
85 | 29 |
86 PassRefPtr<Response> Response::create(PassRefPtr<FetchResponseData> response) | 30 PassRefPtr<HeaderMap> Response::headers() const |
87 { | 31 { |
88 return adoptRef(new Response(response)); | 32 // FIXME: Implement. Spec will eventually whitelist allowable headers. |
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
." | |
137 return m_headers; | 33 return m_headers; |
138 } | 34 } |
139 | 35 |
140 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) | 36 void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response) |
141 { | 37 { |
142 m_response->populateWebServiceWorkerResponse(response); | 38 response.setStatus(status()); |
| 39 response.setStatusText(statusText()); |
| 40 response.setHeaders(m_headers->headerMap()); |
| 41 response.setBlobDataHandle(m_blobDataHandle); |
143 } | 42 } |
144 | 43 |
145 Response::Response() | 44 Response::Response(PassRefPtr<BlobDataHandle> blobDataHandle, const ResponseInit
& responseInit) |
146 : m_response(FetchResponseData::create()) | 45 : m_status(responseInit.status) |
147 , m_headers(Headers::create(m_response->headerList())) | 46 , m_statusText(responseInit.statusText) |
| 47 , m_headers(responseInit.headers) |
| 48 , m_blobDataHandle(blobDataHandle) |
148 { | 49 { |
149 m_headers->setGuard(Headers::ResponseGuard); | |
150 ScriptWrappable::init(this); | 50 ScriptWrappable::init(this); |
151 } | 51 if (!m_headers) |
152 | 52 m_headers = HeaderMap::create(); |
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); | |
159 } | 53 } |
160 | 54 |
161 } // namespace WebCore | 55 } // namespace WebCore |
OLD | NEW |