1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "modules/fetch/Headers.h"
#include "bindings/core/v8/Dictionary.h"
#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/V8IteratorResultValue.h"
#include "core/dom/Iterator.h"
#include "core/fetch/FetchUtils.h"
#include "wtf/NotFound.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
#include "wtf/text/WTFString.h"
namespace blink {
namespace {
class HeadersIterator final : public Iterator {
public:
// Only KeyValue is currently used; the other types are to support
// Map-like iteration with entries(), keys() and values(), but this has
// not yet been added to any spec.
enum IterationType { KeyValue, Key, Value };
HeadersIterator(FetchHeaderList* headers, IterationType type) : m_headers(headers), m_type(type), m_current(0) { }
virtual ScriptValue next(ScriptState* scriptState, ExceptionState& exception) override
{
// FIXME: This simply advances an index and returns the next value if
// any, so if the iterated object is mutated values may be skipped.
if (m_current >= m_headers->size())
return v8IteratorResultDone(scriptState);
const FetchHeaderList::Header& header = m_headers->entry(m_current++);
switch (m_type) {
case KeyValue: {
Vector<String> pair;
pair.append(header.first);
pair.append(header.second);
return v8IteratorResult(scriptState, pair);
}
case Key:
return v8IteratorResult(scriptState, header.first);
case Value:
return v8IteratorResult(scriptState, header.second);
}
ASSERT_NOT_REACHED();
return ScriptValue();
}
virtual ScriptValue next(ScriptState* scriptState, ScriptValue, ExceptionState& exceptionState) override
{
return next(scriptState, exceptionState);
}
virtual void trace(Visitor* visitor)
{
Iterator::trace(visitor);
visitor->trace(m_headers);
}
private:
const Member<FetchHeaderList> m_headers;
const IterationType m_type;
size_t m_current;
};
} // namespace
Headers* Headers::create()
{
return new Headers;
}
Headers* Headers::create(ExceptionState&)
{
return create();
}
Headers* Headers::create(const Headers* init, ExceptionState& exceptionState)
{
// "The Headers(|init|) constructor, when invoked, must run these steps:"
// "1. Let |headers| be a new Headers object."
Headers* headers = create();
// "2. If |init| is given, fill headers with |init|. Rethrow any exception."
headers->fillWith(init, exceptionState);
// "3. Return |headers|."
return headers;
}
Headers* Headers::create(const Vector<Vector<String> >& init, ExceptionState& exceptionState)
{
// The same steps as above.
Headers* headers = create();
headers->fillWith(init, exceptionState);
return headers;
}
Headers* Headers::create(const Dictionary& init, ExceptionState& exceptionState)
{
// "The Headers(|init|) constructor, when invoked, must run these steps:"
// "1. Let |headers| be a new Headers object."
Headers* headers = create();
// "2. If |init| is given, fill headers with |init|. Rethrow any exception."
headers->fillWith(init, exceptionState);
// "3. Return |headers|."
return headers;
}
Headers* Headers::create(FetchHeaderList* headerList)
{
return new Headers(headerList);
}
Headers* Headers::createCopy() const
{
FetchHeaderList* headerList = m_headerList->createCopy();
Headers* headers = create(headerList);
headers->m_guard = m_guard;
return headers;
}
void Headers::append(const String& name, const String& value, ExceptionState& exceptionState)
{
// "To append a name/value (|name|/|value|) pair to a Headers object
// (|headers|), run these steps:"
// "1. If |name| is not a name or |value| is not a value, throw a
// TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return;
}
if (!FetchHeaderList::isValidHeaderValue(value)) {
exceptionState.throwTypeError("Invalid value");
return;
}
// "2. If guard is |request|, throw a TypeError."
if (m_guard == ImmutableGuard) {
exceptionState.throwTypeError("Headers are immutable");
return;
}
// "3. Otherwise, if guard is |request| and |name| is a forbidden header
// name, return."
if (m_guard == RequestGuard && FetchUtils::isForbiddenHeaderName(name))
return;
// "4. Otherwise, if guard is |request-no-CORS| and |name|/|value| is not a
// simple header, return."
if (m_guard == RequestNoCORSGuard && !FetchUtils::isSimpleHeader(AtomicString(name), AtomicString(value)))
return;
// "5. Otherwise, if guard is |response| and |name| is a forbidden response
// header name, return."
if (m_guard == ResponseGuard && FetchUtils::isForbiddenResponseHeaderName(name))
return;
// "6. Append |name|/|value| to header list."
m_headerList->append(name, value);
}
void Headers::remove(const String& name, ExceptionState& exceptionState)
{
// "The delete(|name|) method, when invoked, must run these steps:"
// "1. If name is not a name, throw a TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return;
}
// "2. If guard is |immutable|, throw a TypeError."
if (m_guard == ImmutableGuard) {
exceptionState.throwTypeError("Headers are immutable");
return;
}
// "3. Otherwise, if guard is |request| and |name| is a forbidden header
// name, return."
if (m_guard == RequestGuard && FetchUtils::isForbiddenHeaderName(name))
return;
// "4. Otherwise, if guard is |request-no-CORS| and |name|/`invalid` is not
// a simple header, return."
if (m_guard == RequestNoCORSGuard && !FetchUtils::isSimpleHeader(AtomicString(name), "invalid"))
return;
// "5. Otherwise, if guard is |response| and |name| is a forbidden response
// header name, return."
if (m_guard == ResponseGuard && FetchUtils::isForbiddenResponseHeaderName(name))
return;
// "6. Delete |name| from header list."
m_headerList->remove(name);
}
String Headers::get(const String& name, ExceptionState& exceptionState)
{
// "The get(|name|) method, when invoked, must run these steps:"
// "1. If |name| is not a name, throw a TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return String();
}
// "2. Return the value of the first header in header list whose name is
// |name|, and null otherwise."
String result;
m_headerList->get(name, result);
return result;
}
Vector<String> Headers::getAll(const String& name, ExceptionState& exceptionState)
{
// "The getAll(|name|) method, when invoked, must run these steps:"
// "1. If |name| is not a name, throw a TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return Vector<String>();
}
// "2. Return the values of all headers in header list whose name is |name|,
// in list order, and the empty sequence otherwise."
Vector<String> result;
m_headerList->getAll(name, result);
return result;
}
bool Headers::has(const String& name, ExceptionState& exceptionState)
{
// "The has(|name|) method, when invoked, must run these steps:"
// "1. If |name| is not a name, throw a TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return false;
}
// "2. Return true if there is a header in header list whose name is |name|,
// and false otherwise."
return m_headerList->has(name);
}
void Headers::set(const String& name, const String& value, ExceptionState& exceptionState)
{
// "The set(|name|, |value|) method, when invoked, must run these steps:"
// "1. If |name| is not a name or |value| is not a value, throw a
// TypeError."
if (!FetchHeaderList::isValidHeaderName(name)) {
exceptionState.throwTypeError("Invalid name");
return;
}
if (!FetchHeaderList::isValidHeaderValue(value)) {
exceptionState.throwTypeError("Invalid value");
return;
}
// "2. If guard is |immutable|, throw a TypeError."
if (m_guard == ImmutableGuard) {
exceptionState.throwTypeError("Headers are immutable");
return;
}
// "3. Otherwise, if guard is |request| and |name| is a forbidden header
// name, return."
if (m_guard == RequestGuard && FetchUtils::isForbiddenHeaderName(name))
return;
// "4. Otherwise, if guard is |request-no-CORS| and |name|/|value| is not a
// simple header, return."
if (m_guard == RequestNoCORSGuard && !FetchUtils::isSimpleHeader(AtomicString(name), AtomicString(value)))
return;
// "5. Otherwise, if guard is |response| and |name| is a forbidden response
// header name, return."
if (m_guard == ResponseGuard && FetchUtils::isForbiddenResponseHeaderName(name))
return;
// "6. Set |name|/|value| in header list."
m_headerList->set(name, value);
}
void Headers::fillWith(const Headers* object, ExceptionState& exceptionState)
{
ASSERT(m_headerList->size() == 0);
// "To fill a Headers object (|this|) with a given object (|object|), run
// these steps:"
// "1. If |object| is a Headers object, copy its header list as
// |headerListCopy| and then for each |header| in |headerListCopy|,
// retaining order, append header's |name|/|header|'s value to
// |headers|. Rethrow any exception."
for (size_t i = 0; i < object->m_headerList->list().size(); ++i) {
append(object->m_headerList->list()[i]->first, object->m_headerList->list()[i]->second, exceptionState);
if (exceptionState.hadException())
return;
}
}
void Headers::fillWith(const Vector<Vector<String> >& object, ExceptionState& exceptionState)
{
ASSERT(!m_headerList->size());
// "2. Otherwise, if |object| is a sequence, then for each |header| in
// |object|, run these substeps:
// 1. If |header| does not contain exactly two items, throw a
// TypeError.
// 2. Append |header|'s first item/|header|'s second item to
// |headers|. Rethrow any exception."
for (size_t i = 0; i < object.size(); ++i) {
if (object[i].size() != 2) {
exceptionState.throwTypeError("Invalid value");
return;
}
append(object[i][0], object[i][1], exceptionState);
if (exceptionState.hadException())
return;
}
}
void Headers::fillWith(const Dictionary& object, ExceptionState& exceptionState)
{
ASSERT(!m_headerList->size());
Vector<String> keys;
object.getPropertyNames(keys);
if (!keys.size())
return;
// "3. Otherwise, if |object| is an open-ended dictionary, then for each
// |header| in object, run these substeps:
// 1. Set |header|'s key to |header|'s key, converted to ByteString.
// Rethrow any exception.
// 2. Append |header|'s key/|header|'s value to |headers|. Rethrow any
// exception."
// FIXME: Support OpenEndedDictionary<ByteString>.
for (size_t i = 0; i < keys.size(); ++i) {
String value;
if (!DictionaryHelper::get(object, keys[i], value)) {
exceptionState.throwTypeError("Invalid value");
return;
}
append(keys[i], value, exceptionState);
if (exceptionState.hadException())
return;
}
}
Headers::Headers()
: m_headerList(FetchHeaderList::create())
, m_guard(NoneGuard)
{
}
Headers::Headers(FetchHeaderList* headerList)
: m_headerList(headerList)
, m_guard(NoneGuard)
{
}
Iterator* Headers::iterator(ScriptState*, ExceptionState&)
{
return new HeadersIterator(m_headerList, HeadersIterator::KeyValue);
}
void Headers::trace(Visitor* visitor)
{
visitor->trace(m_headerList);
}
} // namespace blink
|