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 "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" |
| 11 #include "core/dom/DOMArrayBufferView.h" | 11 #include "core/dom/DOMArrayBufferView.h" |
| 12 #include "core/fileapi/Blob.h" | 12 #include "core/fileapi/Blob.h" |
| 13 #include "core/html/DOMFormData.h" | 13 #include "core/html/DOMFormData.h" |
| 14 #include "modules/fetch/BodyStreamBuffer.h" | 14 #include "modules/fetch/BodyStreamBuffer.h" |
| 15 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 15 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 16 #include "modules/fetch/ResponseInit.h" | 16 #include "modules/fetch/ResponseInit.h" |
| 17 #include "platform/network/FormData.h" | 17 #include "platform/network/FormData.h" |
| 18 #include "platform/network/HTTPHeaderMap.h" | 18 #include "platform/network/HTTPHeaderMap.h" |
| 19 #include "public/platform/WebServiceWorkerResponse.h" | 19 #include "public/platform/WebServiceWorkerResponse.h" |
| 20 #include "wtf/RefPtr.h" | 20 #include "wtf/RefPtr.h" |
| 21 | 21 |
| 22 // Check whether |status| is a null body status | |
|
hiroshige
2015/07/30 11:20:40
nit: s/Check/Checks/, and append a period at the l
| |
| 23 // Spec: https://fetch.spec.whatwg.org/#statuses | |
|
hiroshige
2015/07/30 11:20:40
nit: https://fetch.spec.whatwg.org/#null-body-stat
shiva.jm
2015/07/30 11:47:16
Done.
| |
| 24 bool isNullBodyStatus(unsigned short status) | |
| 25 { | |
| 26 if (status == 101 || status == 204 || status == 205 || status == 304) | |
| 27 return true; | |
| 28 | |
| 29 return false; | |
| 30 } | |
| 31 | |
| 22 namespace blink { | 32 namespace blink { |
| 23 | 33 |
| 24 namespace { | 34 namespace { |
| 25 | 35 |
|
hiroshige
2015/07/30 11:20:40
Please move isNullBodyStatus() here (or somewhere
shiva.jm
2015/07/30 11:47:16
Done.
| |
| 26 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec utionContext, const WebServiceWorkerResponse& webResponse) | 36 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec utionContext, const WebServiceWorkerResponse& webResponse) |
| 27 { | 37 { |
| 28 FetchResponseData* response = 0; | 38 FetchResponseData* response = 0; |
| 29 if (webResponse.status() > 0) | 39 if (webResponse.status() > 0) |
| 30 response = FetchResponseData::create(); | 40 response = FetchResponseData::create(); |
| 31 else | 41 else |
| 32 response = FetchResponseData::createNetworkErrorResponse(); | 42 response = FetchResponseData::createNetworkErrorResponse(); |
| 33 | 43 |
| 34 response->setURL(webResponse.url()); | 44 response->setURL(webResponse.url()); |
| 35 response->setStatus(webResponse.status()); | 45 response->setStatus(webResponse.status()); |
| (...skipping 110 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("Invalid status"); |
|
hiroshige
2015/07/30 11:20:40
How about applying this?
https://codereview.chromi
shiva.jm
2015/07/30 11:47:16
Done.
| |
| 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 21 matching lines...) Expand all Loading... | |
| 191 // "1. Empty |r|'s response's header list." | 203 // "1. Empty |r|'s response's header list." |
| 192 r->m_response->headerList()->clearList(); | 204 r->m_response->headerList()->clearList(); |
| 193 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | 205 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow |
| 194 // any exceptions." | 206 // any exceptions." |
| 195 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); | 207 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); |
| 196 if (exceptionState.hadException()) | 208 if (exceptionState.hadException()) |
| 197 return 0; | 209 return 0; |
| 198 } | 210 } |
| 199 // "7. If body is given, run these substeps:" | 211 // "7. If body is given, run these substeps:" |
| 200 if (body) { | 212 if (body) { |
| 201 // "1. Let |stream| and |Content-Type| be the result of extracting body. " | 213 // "1. If |init|'s status member is a null body status, throw a TypeErro r." |
| 202 // "2. Set |r|'s response's body to |stream|." | 214 // "2. Let |stream| and |Content-Type| be the result of extracting body. " |
| 203 // "3. If |Content-Type| is non-null and |r|'s response's header list | 215 // "3. Set |r|'s response's body to |stream|." |
| 216 // "4. If |Content-Type| is non-null and |r|'s response's header list | |
| 204 // contains no header named `Content-Type`, append `Content-Type`/ | 217 // contains no header named `Content-Type`, append `Content-Type`/ |
| 205 // |Content-Type| to |r|'s response's header list." | 218 // |Content-Type| to |r|'s response's header list." |
| 206 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract | 219 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract |
| 207 // Step 3, Blob: | 220 // Step 3, Blob: |
| 208 // "If object's type attribute is not the empty byte sequence, set | 221 // "If object's type attribute is not the empty byte sequence, set |
| 209 // Content-Type to its value." | 222 // Content-Type to its value." |
| 223 if (isNullBodyStatus(status)) { | |
| 224 exceptionState.throwTypeError("Response with null body status cannot have body"); | |
| 225 return 0; | |
| 226 } | |
| 210 r->m_response->replaceBodyStreamBuffer(BodyStreamBuffer::create(FetchBlo bDataConsumerHandle::create(context, body->blobDataHandle()))); | 227 r->m_response->replaceBodyStreamBuffer(BodyStreamBuffer::create(FetchBlo bDataConsumerHandle::create(context, body->blobDataHandle()))); |
| 211 r->refreshBody(); | 228 r->refreshBody(); |
| 212 if (!body->type().isEmpty() && !r->m_response->headerList()->has("Conten t-Type")) | 229 if (!body->type().isEmpty() && !r->m_response->headerList()->has("Conten t-Type")) |
| 213 r->m_response->headerList()->append("Content-Type", body->type()); | 230 r->m_response->headerList()->append("Content-Type", body->type()); |
| 214 } | 231 } |
| 215 | 232 |
| 216 // "8. Set |r|'s MIME type to the result of extracting a MIME type | 233 // "8. Set |r|'s MIME type to the result of extracting a MIME type |
| 217 // from |r|'s response's header list." | 234 // from |r|'s response's header list." |
| 218 r->m_response->setMIMEType(r->m_response->headerList()->extractMIMEType()); | 235 r->m_response->setMIMEType(r->m_response->headerList()->extractMIMEType()); |
| 219 | 236 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 } | 451 } |
| 435 | 452 |
| 436 DEFINE_TRACE(Response) | 453 DEFINE_TRACE(Response) |
| 437 { | 454 { |
| 438 Body::trace(visitor); | 455 Body::trace(visitor); |
| 439 visitor->trace(m_response); | 456 visitor->trace(m_response); |
| 440 visitor->trace(m_headers); | 457 visitor->trace(m_headers); |
| 441 } | 458 } |
| 442 | 459 |
| 443 } // namespace blink | 460 } // namespace blink |
| OLD | NEW |