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
|
// 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/FetchHeaderList.h"
#include "core/fetch/FetchUtils.h"
#include "platform/network/HTTPParsers.h"
#include "wtf/PassOwnPtr.h"
namespace blink {
FetchHeaderList* FetchHeaderList::create()
{
return new FetchHeaderList();
}
FetchHeaderList* FetchHeaderList::createCopy()
{
FetchHeaderList* list = create();
for (size_t i = 0; i < m_headerList.size(); ++i)
list->append(m_headerList[i]->first, m_headerList[i]->second);
return list;
}
FetchHeaderList::FetchHeaderList()
{
}
FetchHeaderList::~FetchHeaderList()
{
}
void FetchHeaderList::append(const String& name, const String& value)
{
// "To append a name/value (|name|/|value|) pair to a header list (|list|),
// append a new header whose name is |name|, byte lowercased, and value is
// |value|, to |list|."
m_headerList.append(adoptPtr(new Header(name.lower(), value)));
}
void FetchHeaderList::set(const String& name, const String& value)
{
// "To set a name/value (|name|/|value|) pair in a header list (|list|), run
// these steps:
// 1. Byte lowercase |name|.
// 2. If there are any headers in |list| whose name is |name|, set the value
// of the first such header to |value| and remove the others.
// 3. Otherwise, append a new header whose name is |name| and value is
// |value|, to |list|."
const String lowercasedName = name.lower();
for (size_t i = 0; i < m_headerList.size(); ++i) {
if (m_headerList[i]->first == lowercasedName) {
m_headerList[i]->second = value;
for (size_t j = i + 1; j < m_headerList.size(); ) {
if (m_headerList[j]->first == lowercasedName)
m_headerList.remove(j);
else
++j;
}
return;
}
}
m_headerList.append(adoptPtr(new Header(lowercasedName, value)));
}
size_t FetchHeaderList::size() const
{
return m_headerList.size();
}
void FetchHeaderList::remove(const String& name)
{
// "To delete a name (|name|) from a header list (|list|), remove all headers
// whose name is |name|, byte lowercased, from |list|."
const String lowercasedName = name.lower();
for (size_t i = 0; i < m_headerList.size(); ) {
if (m_headerList[i]->first == lowercasedName)
m_headerList.remove(i);
else
++i;
}
}
bool FetchHeaderList::get(const String& name, String& result) const
{
const String lowercasedName = name.lower();
for (size_t i = 0; i < m_headerList.size(); ++i) {
if (m_headerList[i]->first == lowercasedName) {
result = m_headerList[i]->second;
return true;
}
}
return false;
}
void FetchHeaderList::getAll(const String& name, Vector<String>& result) const
{
const String lowercasedName = name.lower();
result.clear();
for (size_t i = 0; i < m_headerList.size(); ++i) {
if (m_headerList[i]->first == lowercasedName)
result.append(m_headerList[i]->second);
}
}
bool FetchHeaderList::has(const String& name) const
{
const String lowercasedName = name.lower();
for (size_t i = 0; i < m_headerList.size(); ++i) {
if (m_headerList[i]->first == lowercasedName)
return true;
}
return false;
}
void FetchHeaderList::clearList()
{
m_headerList.clear();
}
bool FetchHeaderList::containsNonSimpleHeader() const
{
for (size_t i = 0; i < m_headerList.size(); ++i) {
if (!FetchUtils::isSimpleHeader(AtomicString(m_headerList[i]->first), AtomicString(m_headerList[i]->second)))
return true;
}
return false;
}
bool FetchHeaderList::isValidHeaderName(const String& name)
{
// "A name is a case-insensitive byte sequence that matches the field-name
// token production."
return isValidHTTPToken(name);
}
bool FetchHeaderList::isValidHeaderValue(const String& value)
{
// "A value is a byte sequence that matches the field-value token production
// and contains no 0x0A or 0x0D bytes."
return isValidHTTPHeaderValue(value);
}
} // namespace blink
|