File: HeadersList.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (189 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download | duplicates (2)
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
/*!********************************************************************

 Audacity: A Digital Audio Editor

 @file HeadersList.cpp
 @brief Define HTTP headers list class.

 Dmitry Vedenko
 **********************************************************************/

/*!********************************************************************

 @class Header
 @brief A string pair, representing HTTP header.

 **********************************************************************/

/*!********************************************************************

 @class HeadersList
 @brief A class, representing a list of HTTP headers.

 **********************************************************************/

#include "HeadersList.h"

#include <algorithm>
#include <cctype>

namespace audacity
{
namespace network_manager
{

namespace common_headers
{
const std::string Accept = "Accept";
const std::string AcceptEncoding = "Accept-Encoding";
const std::string AcceptLanguage = "Accept-Language";
const std::string Authorization = "Authorization";
const std::string CacheControl = "Cache-Control";
const std::string Connection = "Connection";
const std::string ContentEncoding = "Content-Encoding";
const std::string ContentLength = "Content-Length";
const std::string ContentType = "Content-Type";
const std::string ContentDisposition = "Content-Disposition";
const std::string Cookie = "Cookie";
const std::string Host = "Host";
const std::string Origin = "Origin";
const std::string Referer = "Referer";
const std::string UserAgent = "User-Agent";
const std::string IfNoneMatch = "If-None-Match";
const std::string IfModifiedSince = "If-Modified-Since";
} // namespace common_headers

namespace common_content_types
{
const std::string ApplicationJson = "application/json";
const std::string ApplicationXml = "application/xml";
const std::string ApplicationXWwwFormUrlencoded = "application/x-www-form-urlencoded";
const std::string ApplicationXOctetStream = "application/x-octet-stream";
const std::string ApplicationXGzip = "application/x-gzip";
const std::string MultipartFormData = "multipart/form-data";
} // namespace common_content_types

bool Header::hasSameName (const Header& header) const
{
    return hasSameName (header.Name);
}

bool Header::hasSameName (const std::string& name) const
{
    return std::equal (
        name.begin (), name.end (),
        Name.begin (), Name.end (),
        [](const char leftChar, const char rightChar) {
            return std::tolower (leftChar) == std::tolower (rightChar);
    });
}

Header Header::Parse (const std::string& header)
{
    const size_t colonPosition = header.find (": ");

    if (colonPosition == std::string::npos) // This can happen when we receive the first line of the response
        return { header, std::string () };

    return {
        header.substr (0, colonPosition),
        header.substr (colonPosition + 2)
    };
}

void HeadersList::setHeader (const Header& header)
{
    setHeader (header.Name, header.Value);
}

void HeadersList::setHeader (const std::string& headerName, std::string headerValue)
{
    Header* item = getHeader (headerName);

    if (item != nullptr)
        item->Value = std::move (headerValue);
    else
        mHeaders.push_back ({ headerName, std::move (headerValue) });
}

void HeadersList::addHeader (Header header)
{
    addHeader (std::move (header.Name), std::move (header.Value));
}

void HeadersList::addHeader (std::string headerName, std::string headerValue)
{
    mHeaders.push_back ({ std::move (headerName), std::move (headerValue) });
}

bool HeadersList::hasHeader (const std::string& headerName) const noexcept
{
    return getHeader (headerName) != nullptr;
}

std::string HeadersList::getHeaderValue (const std::string& headerName) const
{
    const Header* header = getHeader (headerName);

    if (header != nullptr)
        return header->Value;

    return {};
}

const Header* HeadersList::getHeader (size_t idx) const noexcept
{
    return const_cast<HeadersList*>(this)->getHeader (idx);
}

const Header* HeadersList::getHeader (const std::string& name) const noexcept
{
    return const_cast<HeadersList*>(this)->getHeader (name);
}

size_t HeadersList::getHeadersCount () const noexcept
{
    return mHeaders.size ();
}

HeadersList::HeadersIterator HeadersList::begin () noexcept
{
    return mHeaders.begin ();
}

HeadersList::HeadersIterator HeadersList::end () noexcept
{
    return mHeaders.end ();
}

HeadersList::HeadersConstIterator HeadersList::begin () const noexcept
{
    return mHeaders.begin ();
}

HeadersList::HeadersConstIterator HeadersList::end () const noexcept
{
    return mHeaders.end ();
}

Header* HeadersList::getHeader (size_t idx) noexcept
{
    if (idx < mHeaders.size ())
        return &mHeaders[idx];

    return nullptr;
}

Header* HeadersList::getHeader (const std::string& headerName) noexcept
{
    for (Header& header : mHeaders)
    {
        if (header.hasSameName (headerName))
            return &header;
    }

    return nullptr;
}

}
}