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 "modules/fetch/Response.h" | 6 #include "modules/fetch/Response.h" |
7 | 7 |
8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
10 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 case WebServiceWorkerResponseTypeDefault: | 55 case WebServiceWorkerResponseTypeDefault: |
56 break; | 56 break; |
57 case WebServiceWorkerResponseTypeError: | 57 case WebServiceWorkerResponseTypeError: |
58 ASSERT(response->type() == FetchResponseData::ErrorType); | 58 ASSERT(response->type() == FetchResponseData::ErrorType); |
59 break; | 59 break; |
60 } | 60 } |
61 | 61 |
62 return response; | 62 return response; |
63 } | 63 } |
64 | 64 |
| 65 // Checks whether |status| is a null body status. |
| 66 // Spec: https://fetch.spec.whatwg.org/#null-body-status |
| 67 bool isNullBodyStatus(unsigned short status) |
| 68 { |
| 69 if (status == 101 || status == 204 || status == 205 || status == 304) |
| 70 return true; |
| 71 |
| 72 return false; |
| 73 } |
| 74 |
65 // Check whether |statusText| is a ByteString and | 75 // Check whether |statusText| is a ByteString and |
66 // matches the Reason-Phrase token production. | 76 // matches the Reason-Phrase token production. |
67 // RFC 2616: https://tools.ietf.org/html/rfc2616 | 77 // RFC 2616: https://tools.ietf.org/html/rfc2616 |
68 // RFC 7230: https://tools.ietf.org/html/rfc7230 | 78 // RFC 7230: https://tools.ietf.org/html/rfc7230 |
69 // "reason-phrase = *( HTAB / SP / VCHAR / obs-text )" | 79 // "reason-phrase = *( HTAB / SP / VCHAR / obs-text )" |
70 bool isValidReasonPhrase(const String& statusText) | 80 bool isValidReasonPhrase(const String& statusText) |
71 { | 81 { |
72 for (unsigned i = 0; i < statusText.length(); ++i) { | 82 for (unsigned i = 0; i < statusText.length(); ++i) { |
73 UChar c = statusText[i]; | 83 UChar c = statusText[i]; |
74 if (!(c == 0x09 // HTAB | 84 if (!(c == 0x09 // HTAB |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 const long long length = blobData->length(); | 156 const long long length = blobData->length(); |
147 Blob* blob = Blob::create(BlobDataHandle::create(blobData.release(), len
gth)); | 157 Blob* blob = Blob::create(BlobDataHandle::create(blobData.release(), len
gth)); |
148 return create(context, blob, ResponseInit(responseInit, exceptionState),
exceptionState); | 158 return create(context, blob, ResponseInit(responseInit, exceptionState),
exceptionState); |
149 } | 159 } |
150 ASSERT_NOT_REACHED(); | 160 ASSERT_NOT_REACHED(); |
151 return nullptr; | 161 return nullptr; |
152 } | 162 } |
153 | 163 |
154 Response* Response::create(ExecutionContext* context, Blob* body, const Response
Init& responseInit, ExceptionState& exceptionState) | 164 Response* Response::create(ExecutionContext* context, Blob* body, const Response
Init& responseInit, ExceptionState& exceptionState) |
155 { | 165 { |
156 // "1. If |init|'s status member is not in the range 200 to 599, throw a | 166 unsigned short status = responseInit.status; |
| 167 |
| 168 // "1. If |init|'s status member is not in the range 200 to 599, inclusive,
throw a |
157 // RangeError." | 169 // RangeError." |
158 if (responseInit.status < 200 || 599 < responseInit.status) { | 170 if (status < 200 || 599 < status) { |
159 exceptionState.throwRangeError("Invalid status"); | 171 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi
gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe
ssages::InclusiveBound)); |
160 return 0; | 172 return 0; |
161 } | 173 } |
162 | 174 |
163 // "2. If |init|'s statusText member does not match the Reason-Phrase | 175 // "2. If |init|'s statusText member does not match the Reason-Phrase |
164 // token production, throw a TypeError." | 176 // token production, throw a TypeError." |
165 if (!isValidReasonPhrase(responseInit.statusText)) { | 177 if (!isValidReasonPhrase(responseInit.statusText)) { |
166 exceptionState.throwTypeError("Invalid statusText"); | 178 exceptionState.throwTypeError("Invalid statusText"); |
167 return 0; | 179 return 0; |
168 } | 180 } |
169 | 181 |
(...skipping 20 matching lines...) Expand all Loading... |
190 // "1. Empty |r|'s response's header list." | 202 // "1. Empty |r|'s response's header list." |
191 r->m_response->headerList()->clearList(); | 203 r->m_response->headerList()->clearList(); |
192 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | 204 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow |
193 // any exceptions." | 205 // any exceptions." |
194 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); | 206 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); |
195 if (exceptionState.hadException()) | 207 if (exceptionState.hadException()) |
196 return 0; | 208 return 0; |
197 } | 209 } |
198 // "7. If body is given, run these substeps:" | 210 // "7. If body is given, run these substeps:" |
199 if (body) { | 211 if (body) { |
200 // "1. Let |stream| and |Content-Type| be the result of extracting body.
" | 212 // "1. If |init|'s status member is a null body status, throw a TypeErro
r." |
201 // "2. Set |r|'s response's body to |stream|." | 213 // "2. Let |stream| and |Content-Type| be the result of extracting body.
" |
202 // "3. If |Content-Type| is non-null and |r|'s response's header list | 214 // "3. Set |r|'s response's body to |stream|." |
| 215 // "4. If |Content-Type| is non-null and |r|'s response's header list |
203 // contains no header named `Content-Type`, append `Content-Type`/ | 216 // contains no header named `Content-Type`, append `Content-Type`/ |
204 // |Content-Type| to |r|'s response's header list." | 217 // |Content-Type| to |r|'s response's header list." |
205 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract | 218 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract |
206 // Step 3, Blob: | 219 // Step 3, Blob: |
207 // "If object's type attribute is not the empty byte sequence, set | 220 // "If object's type attribute is not the empty byte sequence, set |
208 // Content-Type to its value." | 221 // Content-Type to its value." |
| 222 if (isNullBodyStatus(status)) { |
| 223 exceptionState.throwTypeError("Response with null body status cannot
have body"); |
| 224 return 0; |
| 225 } |
209 r->m_response->replaceBodyStreamBuffer(new BodyStreamBuffer(FetchBlobDat
aConsumerHandle::create(context, body->blobDataHandle()))); | 226 r->m_response->replaceBodyStreamBuffer(new BodyStreamBuffer(FetchBlobDat
aConsumerHandle::create(context, body->blobDataHandle()))); |
210 if (!body->type().isEmpty() && !r->m_response->headerList()->has("Conten
t-Type")) | 227 if (!body->type().isEmpty() && !r->m_response->headerList()->has("Conten
t-Type")) |
211 r->m_response->headerList()->append("Content-Type", body->type()); | 228 r->m_response->headerList()->append("Content-Type", body->type()); |
212 } | 229 } |
213 | 230 |
214 // "8. Set |r|'s MIME type to the result of extracting a MIME type | 231 // "8. Set |r|'s MIME type to the result of extracting a MIME type |
215 // from |r|'s response's header list." | 232 // from |r|'s response's header list." |
216 r->m_response->setMIMEType(r->m_response->headerList()->extractMIMEType()); | 233 r->m_response->setMIMEType(r->m_response->headerList()->extractMIMEType()); |
217 | 234 |
218 // "9. Return |r|." | 235 // "9. Return |r|." |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 394 } |
378 | 395 |
379 DEFINE_TRACE(Response) | 396 DEFINE_TRACE(Response) |
380 { | 397 { |
381 Body::trace(visitor); | 398 Body::trace(visitor); |
382 visitor->trace(m_response); | 399 visitor->trace(m_response); |
383 visitor->trace(m_headers); | 400 visitor->trace(m_headers); |
384 } | 401 } |
385 | 402 |
386 } // namespace blink | 403 } // namespace blink |
OLD | NEW |