Audacity 3.2.0
HeadersList.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file HeadersList.cpp
6 @brief Define HTTP headers list class.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11/*!********************************************************************
12
13 @class Header
14 @brief A string pair, representing HTTP header.
15
16 **********************************************************************/
17
18/*!********************************************************************
19
20 @class HeadersList
21 @brief A class, representing a list of HTTP headers.
22
23 **********************************************************************/
24
25#include "HeadersList.h"
26
27#include <algorithm>
28#include <cctype>
29
30namespace audacity
31{
32namespace network_manager
33{
34
35namespace common_headers
36{
37const std::string Accept = "Accept";
38const std::string AcceptEncoding = "Accept-Encoding";
39const std::string AcceptLanguage = "Accept-Language";
40const std::string Authorization = "Authorization";
41const std::string CacheControl = "Cache-Control";
42const std::string Connection = "Connection";
43const std::string ContentEncoding = "Content-Encoding";
44const std::string ContentLength = "Content-Length";
45const std::string ContentType = "Content-Type";
46const std::string ContentDisposition = "Content-Disposition";
47const std::string Cookie = "Cookie";
48const std::string Host = "Host";
49const std::string Origin = "Origin";
50const std::string Referer = "Referer";
51const std::string UserAgent = "User-Agent";
52const std::string IfNoneMatch = "If-None-Match";
53const std::string IfModifiedSince = "If-Modified-Since";
54} // namespace common_headers
55
56namespace common_content_types
57{
58const std::string ApplicationJson = "application/json";
59const std::string ApplicationXml = "application/xml";
60const std::string ApplicationXWwwFormUrlencoded = "application/x-www-form-urlencoded";
61const std::string ApplicationXOctetStream = "application/x-octet-stream";
62const std::string ApplicationXGzip = "application/x-gzip";
63const std::string MultipartFormData = "multipart/form-data";
64} // namespace common_content_types
65
66bool Header::hasSameName (const Header& header) const
67{
68 return hasSameName (header.Name);
69}
70
71bool Header::hasSameName (const std::string& name) const
72{
73 return std::equal (
74 name.begin (), name.end (),
75 Name.begin (), Name.end (),
76 [](const char leftChar, const char rightChar) {
77 return std::tolower (leftChar) == std::tolower (rightChar);
78 });
79}
80
81Header Header::Parse (const std::string& header)
82{
83 const size_t colonPosition = header.find (": ");
84
85 if (colonPosition == std::string::npos) // This can happen when we receive the first line of the response
86 return { header, std::string () };
87
88 return {
89 header.substr (0, colonPosition),
90 header.substr (colonPosition + 2)
91 };
92}
93
94void HeadersList::setHeader (const Header& header)
95{
96 setHeader (header.Name, header.Value);
97}
98
99void HeadersList::setHeader (const std::string& headerName, std::string headerValue)
100{
101 Header* item = getHeader (headerName);
102
103 if (item != nullptr)
104 item->Value = std::move (headerValue);
105 else
106 mHeaders.push_back ({ headerName, std::move (headerValue) });
107}
108
110{
111 addHeader (std::move (header.Name), std::move (header.Value));
112}
113
114void HeadersList::addHeader (std::string headerName, std::string headerValue)
115{
116 mHeaders.push_back ({ std::move (headerName), std::move (headerValue) });
117}
118
119bool HeadersList::hasHeader (const std::string& headerName) const noexcept
120{
121 return getHeader (headerName) != nullptr;
122}
123
124std::string HeadersList::getHeaderValue (const std::string& headerName) const
125{
126 const Header* header = getHeader (headerName);
127
128 if (header != nullptr)
129 return header->Value;
130
131 return {};
132}
133
134const Header* HeadersList::getHeader (size_t idx) const noexcept
135{
136 return const_cast<HeadersList*>(this)->getHeader (idx);
137}
138
139const Header* HeadersList::getHeader (const std::string& name) const noexcept
140{
141 return const_cast<HeadersList*>(this)->getHeader (name);
142}
143
144size_t HeadersList::getHeadersCount () const noexcept
145{
146 return mHeaders.size ();
147}
148
150{
151 return mHeaders.begin ();
152}
153
155{
156 return mHeaders.end ();
157}
158
160{
161 return mHeaders.begin ();
162}
163
165{
166 return mHeaders.end ();
167}
168
169Header* HeadersList::getHeader (size_t idx) noexcept
170{
171 if (idx < mHeaders.size ())
172 return &mHeaders[idx];
173
174 return nullptr;
175}
176
177Header* HeadersList::getHeader (const std::string& headerName) noexcept
178{
179 for (Header& header : mHeaders)
180 {
181 if (header.hasSameName (headerName))
182 return &header;
183 }
184
185 return nullptr;
186}
187
188}
189}
std::unique_ptr< DBConnection > Connection
Definition: DBConnection.h:132
Declare HTTP headers list class.
wxString name
Definition: TagsEditor.cpp:166
const Header * getHeader(size_t idx) const noexcept
void setHeader(const Header &header)
Definition: HeadersList.cpp:94
HeadersIterator begin() noexcept
size_t getHeadersCount() const noexcept
HeadersStorageType::iterator HeadersIterator
Definition: HeadersList.h:68
std::string getHeaderValue(const std::string &headerName) const
HeadersIterator end() noexcept
HeadersStorageType::const_iterator HeadersConstIterator
Definition: HeadersList.h:69
bool hasHeader(const std::string &headerName) const noexcept
constexpr size_t npos(-1)
static Header Parse(const std::string &header)
Definition: HeadersList.cpp:81
bool hasSameName(const Header &header) const
Definition: HeadersList.cpp:66