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 "modules/fetch/Headers.h" | 5 #include "modules/fetch/Headers.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/Dictionary.h" | 7 #include "bindings/core/v8/Dictionary.h" |
|
yhirano
2017/04/11 03:32:41
Do we still need this?
Raphael Kubo da Costa (rakuco)
2017/04/11 08:02:21
Nope; removed in patch v6.
| |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/V8IteratorResultValue.h" | 9 #include "bindings/core/v8/V8IteratorResultValue.h" |
| 10 #include "bindings/modules/v8/ByteStringSequenceSequenceOrByteStringByteStringRe cordOrHeaders.h" | |
| 10 #include "core/dom/Iterator.h" | 11 #include "core/dom/Iterator.h" |
| 11 #include "platform/loader/fetch/FetchUtils.h" | 12 #include "platform/loader/fetch/FetchUtils.h" |
| 12 #include "wtf/NotFound.h" | 13 #include "wtf/NotFound.h" |
| 13 #include "wtf/PassRefPtr.h" | 14 #include "wtf/PassRefPtr.h" |
| 14 #include "wtf/RefPtr.h" | 15 #include "wtf/RefPtr.h" |
| 15 #include "wtf/text/WTFString.h" | 16 #include "wtf/text/WTFString.h" |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 47 PairIterable<String, String>::IterationSource::Trace(visitor); | 48 PairIterable<String, String>::IterationSource::Trace(visitor); |
| 48 } | 49 } |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 const Member<FetchHeaderList> headers_; | 52 const Member<FetchHeaderList> headers_; |
| 52 size_t current_; | 53 size_t current_; |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 } // namespace | 56 } // namespace |
| 56 | 57 |
| 57 Headers* Headers::Create() { | 58 Headers* Headers::Create(ExceptionState&) { |
| 58 return new Headers; | 59 return new Headers; |
| 59 } | 60 } |
| 60 | 61 |
| 61 Headers* Headers::Create(ExceptionState&) { | 62 Headers* Headers::Create( |
| 62 return Create(); | 63 const ByteStringSequenceSequenceOrByteStringByteStringRecordOrHeaders& init, |
| 63 } | 64 ExceptionState& exception_state) { |
| 64 | |
| 65 Headers* Headers::Create(const Headers* init, ExceptionState& exception_state) { | |
| 66 // "The Headers(|init|) constructor, when invoked, must run these steps:" | 65 // "The Headers(|init|) constructor, when invoked, must run these steps:" |
| 67 // "1. Let |headers| be a new Headers object." | 66 // "1. Let |headers| be a new Headers object whose guard is "none". |
| 68 Headers* headers = Create(); | 67 Headers* headers = Create(exception_state); |
| 69 // "2. If |init| is given, fill headers with |init|. Rethrow any exception." | 68 // "2. If |init| is given, fill headers with |init|. Rethrow any exception." |
| 70 headers->FillWith(init, exception_state); | 69 if (init.isByteStringSequenceSequence()) { |
| 70 headers->FillWith(init.getAsByteStringSequenceSequence(), exception_state); | |
| 71 } else if (init.isByteStringByteStringRecord()) { | |
| 72 headers->FillWith(init.getAsByteStringByteStringRecord(), exception_state); | |
| 73 } else if (init.isHeaders()) { | |
| 74 // This branch will not be necessary once http://crbug.com/690428 is fixed. | |
| 75 headers->FillWith(init.getAsHeaders(), exception_state); | |
| 76 } else { | |
| 77 NOTREACHED(); | |
| 78 } | |
| 71 // "3. Return |headers|." | 79 // "3. Return |headers|." |
| 72 return headers; | 80 return headers; |
| 73 } | 81 } |
| 74 | |
| 75 Headers* Headers::Create(const Vector<Vector<String>>& init, | |
| 76 ExceptionState& exception_state) { | |
| 77 // The same steps as above. | |
| 78 Headers* headers = Create(); | |
| 79 headers->FillWith(init, exception_state); | |
| 80 return headers; | |
| 81 } | |
| 82 | |
| 83 Headers* Headers::Create(const Dictionary& init, | |
| 84 ExceptionState& exception_state) { | |
| 85 // "The Headers(|init|) constructor, when invoked, must run these steps:" | |
| 86 // "1. Let |headers| be a new Headers object." | |
| 87 Headers* headers = Create(); | |
| 88 // "2. If |init| is given, fill headers with |init|. Rethrow any exception." | |
| 89 headers->FillWith(init, exception_state); | |
| 90 // "3. Return |headers|." | |
| 91 return headers; | |
| 92 } | |
| 93 | 82 |
| 94 Headers* Headers::Create(FetchHeaderList* header_list) { | 83 Headers* Headers::Create(FetchHeaderList* header_list) { |
| 95 return new Headers(header_list); | 84 return new Headers(header_list); |
| 96 } | 85 } |
| 97 | 86 |
| 98 Headers* Headers::Clone() const { | 87 Headers* Headers::Clone() const { |
| 99 FetchHeaderList* header_list = header_list_->Clone(); | 88 FetchHeaderList* header_list = header_list_->Clone(); |
| 100 Headers* headers = Create(header_list); | 89 Headers* headers = Create(header_list); |
| 101 headers->guard_ = guard_; | 90 headers->guard_ = guard_; |
| 102 return headers; | 91 return headers; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 // header name, return." | 233 // header name, return." |
| 245 if (guard_ == kResponseGuard && | 234 if (guard_ == kResponseGuard && |
| 246 FetchUtils::IsForbiddenResponseHeaderName(name)) | 235 FetchUtils::IsForbiddenResponseHeaderName(name)) |
| 247 return; | 236 return; |
| 248 // "6. Set |name|/|value| in header list." | 237 // "6. Set |name|/|value| in header list." |
| 249 header_list_->Set(name, value); | 238 header_list_->Set(name, value); |
| 250 } | 239 } |
| 251 | 240 |
| 252 void Headers::FillWith(const Headers* object, ExceptionState& exception_state) { | 241 void Headers::FillWith(const Headers* object, ExceptionState& exception_state) { |
| 253 ASSERT(header_list_->size() == 0); | 242 ASSERT(header_list_->size() == 0); |
| 254 // "To fill a Headers object (|this|) with a given object (|object|), run | 243 // There used to be specific steps describing filling a Headers object with |
| 255 // these steps:" | 244 // another Headers object, but it has since been removed because it should be |
| 256 // "1. If |object| is a Headers object, copy its header list as | 245 // handled like a sequence (http://crbug.com/690428). |
| 257 // |headerListCopy| and then for each |header| in |headerListCopy|, | |
| 258 // retaining order, append header's |name|/|header|'s value to | |
| 259 // |headers|. Rethrow any exception." | |
| 260 for (size_t i = 0; i < object->header_list_->List().size(); ++i) { | 246 for (size_t i = 0; i < object->header_list_->List().size(); ++i) { |
| 261 append(object->header_list_->List()[i]->first, | 247 append(object->header_list_->List()[i]->first, |
| 262 object->header_list_->List()[i]->second, exception_state); | 248 object->header_list_->List()[i]->second, exception_state); |
| 263 if (exception_state.HadException()) | 249 if (exception_state.HadException()) |
| 264 return; | 250 return; |
| 265 } | 251 } |
| 266 } | 252 } |
| 267 | 253 |
| 268 void Headers::FillWith(const Vector<Vector<String>>& object, | 254 void Headers::FillWith(const Vector<Vector<String>>& object, |
| 269 ExceptionState& exception_state) { | 255 ExceptionState& exception_state) { |
| 270 ASSERT(!header_list_->size()); | 256 ASSERT(!header_list_->size()); |
| 271 // "2. Otherwise, if |object| is a sequence, then for each |header| in | 257 // "1. If |object| is a sequence, then for each |header| in |object|, run |
| 272 // |object|, run these substeps: | 258 // these substeps: |
| 273 // 1. If |header| does not contain exactly two items, throw a | 259 // 1. If |header| does not contain exactly two items, then throw a |
| 274 // TypeError. | 260 // TypeError. |
| 275 // 2. Append |header|'s first item/|header|'s second item to | 261 // 2. Append |header|’s first item/|header|’s second item to |headers|. |
| 276 // |headers|. Rethrow any exception." | 262 // Rethrow any exception." |
| 277 for (size_t i = 0; i < object.size(); ++i) { | 263 for (size_t i = 0; i < object.size(); ++i) { |
| 278 if (object[i].size() != 2) { | 264 if (object[i].size() != 2) { |
| 279 exception_state.ThrowTypeError("Invalid value"); | 265 exception_state.ThrowTypeError("Invalid value"); |
| 280 return; | 266 return; |
| 281 } | 267 } |
| 282 append(object[i][0], object[i][1], exception_state); | 268 append(object[i][0], object[i][1], exception_state); |
| 283 if (exception_state.HadException()) | 269 if (exception_state.HadException()) |
| 284 return; | 270 return; |
| 285 } | 271 } |
| 286 } | 272 } |
| 287 | 273 |
| 288 void Headers::FillWith(const Dictionary& object, | 274 void Headers::FillWith(const Vector<std::pair<String, String>>& object, |
| 289 ExceptionState& exception_state) { | 275 ExceptionState& exception_state) { |
| 290 ASSERT(!header_list_->size()); | 276 ASSERT(!header_list_->size()); |
| 291 const Vector<String>& keys = object.GetPropertyNames(exception_state); | |
| 292 if (exception_state.HadException() || !keys.size()) | |
| 293 return; | |
| 294 | 277 |
| 295 // "3. Otherwise, if |object| is an open-ended dictionary, then for each | 278 for (const auto& item : object) { |
| 296 // |header| in object, run these substeps: | 279 append(item.first, item.second, exception_state); |
| 297 // 1. Set |header|'s key to |header|'s key, converted to ByteString. | |
| 298 // Rethrow any exception. | |
| 299 // 2. Append |header|'s key/|header|'s value to |headers|. Rethrow any | |
| 300 // exception." | |
| 301 // FIXME: Support OpenEndedDictionary<ByteString>. | |
| 302 for (size_t i = 0; i < keys.size(); ++i) { | |
| 303 String value; | |
| 304 if (!DictionaryHelper::Get(object, keys[i], value)) { | |
| 305 exception_state.ThrowTypeError("Invalid value"); | |
| 306 return; | |
| 307 } | |
| 308 append(keys[i], value, exception_state); | |
| 309 if (exception_state.HadException()) | 280 if (exception_state.HadException()) |
| 310 return; | 281 return; |
| 311 } | 282 } |
| 312 } | 283 } |
| 313 | 284 |
| 314 Headers::Headers() | 285 Headers::Headers() |
| 315 : header_list_(FetchHeaderList::Create()), guard_(kNoneGuard) {} | 286 : header_list_(FetchHeaderList::Create()), guard_(kNoneGuard) {} |
| 316 | 287 |
| 317 Headers::Headers(FetchHeaderList* header_list) | 288 Headers::Headers(FetchHeaderList* header_list) |
| 318 : header_list_(header_list), guard_(kNoneGuard) {} | 289 : header_list_(header_list), guard_(kNoneGuard) {} |
| 319 | 290 |
| 320 DEFINE_TRACE(Headers) { | 291 DEFINE_TRACE(Headers) { |
| 321 visitor->Trace(header_list_); | 292 visitor->Trace(header_list_); |
| 322 } | 293 } |
| 323 | 294 |
| 324 PairIterable<String, String>::IterationSource* Headers::StartIteration( | 295 PairIterable<String, String>::IterationSource* Headers::StartIteration( |
| 325 ScriptState*, | 296 ScriptState*, |
| 326 ExceptionState&) { | 297 ExceptionState&) { |
| 327 return new HeadersIterationSource(header_list_); | 298 return new HeadersIterationSource(header_list_); |
| 328 } | 299 } |
| 329 | 300 |
| 330 } // namespace blink | 301 } // namespace blink |
| OLD | NEW |