1/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "HTTPHeaderMap.h"
33
34#include "HTTPHeaderNames.h"
35#include <utility>
36#include <wtf/text/StringView.h>
37
38namespace WebCore {
39
40HTTPHeaderMap::HTTPHeaderMap()
41{
42}
43
44HTTPHeaderMap::~HTTPHeaderMap()
45{
46}
47
48std::unique_ptr<CrossThreadHTTPHeaderMapData> HTTPHeaderMap::copyData() const
49{
50    auto data = std::make_unique<CrossThreadHTTPHeaderMapData>();
51    data->reserveInitialCapacity(m_headers.size());
52
53    for (const auto& header : *this)
54        data->uncheckedAppend(std::make_pair(header.key.isolatedCopy(), header.value.isolatedCopy()));
55
56    return data;
57}
58
59void HTTPHeaderMap::adopt(std::unique_ptr<CrossThreadHTTPHeaderMapData> data)
60{
61    m_headers.clear();
62
63    for (auto& header : *data)
64        m_headers.add(WTF::move(header.first), WTF::move(header.second));
65}
66
67static String internHTTPHeaderNameString(const String& nameString)
68{
69    HTTPHeaderName headerName;
70    if (!findHTTPHeaderName(nameString, headerName))
71        return nameString;
72
73    return httpHeaderNameString(headerName).toStringWithoutCopying();
74}
75
76String HTTPHeaderMap::get(const String& name) const
77{
78    return m_headers.get(internHTTPHeaderNameString(name));
79}
80
81void HTTPHeaderMap::set(const String& name, const String& value)
82{
83    m_headers.set(internHTTPHeaderNameString(name), value);
84}
85
86void HTTPHeaderMap::add(const String& name, const String& value)
87{
88    auto result = m_headers.add(internHTTPHeaderNameString(name), value);
89    if (!result.isNewEntry)
90        result.iterator->value = result.iterator->value + ", " + value;
91}
92
93String HTTPHeaderMap::get(HTTPHeaderName name) const
94{
95    auto it = find(name);
96    if (it == end())
97        return String();
98
99    return it->value;
100}
101
102void HTTPHeaderMap::set(HTTPHeaderName name, const String& value)
103{
104    m_headers.set(httpHeaderNameString(name).toStringWithoutCopying(), value);
105}
106
107bool HTTPHeaderMap::contains(HTTPHeaderName name) const
108{
109    return m_headers.contains(httpHeaderNameString(name).toStringWithoutCopying());
110}
111
112HTTPHeaderMap::const_iterator HTTPHeaderMap::find(HTTPHeaderName name) const
113{
114    return m_headers.find(httpHeaderNameString(name).toStringWithoutCopying());
115}
116
117bool HTTPHeaderMap::remove(HTTPHeaderName name)
118{
119    return m_headers.remove(httpHeaderNameString(name).toStringWithoutCopying());
120}
121
122} // namespace WebCore
123