BPicture: Fix archive constructor.
[haiku.git] / src / kits / network / libnetapi / HttpHeaders.cpp
blob352f2c1c64e92e358f7bc26b13a9c3a2c74badf6
1 /*
2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 */
10 #include <ctype.h>
11 #include <string.h>
12 #include <new>
14 #include <String.h>
15 #include <HttpHeaders.h>
18 // #pragma mark -- BHttpHeader
21 BHttpHeader::BHttpHeader()
23 fName(),
24 fValue(),
25 fRawHeader(),
26 fRawHeaderValid(true)
31 BHttpHeader::BHttpHeader(const char* string)
33 fRawHeaderValid(true)
35 SetHeader(string);
39 BHttpHeader::BHttpHeader(const char* name, const char* value)
41 fRawHeaderValid(false)
43 SetName(name);
44 SetValue(value);
48 BHttpHeader::BHttpHeader(const BHttpHeader& copy)
50 fName(copy.fName),
51 fValue(copy.fValue),
52 fRawHeaderValid(false)
57 void
58 BHttpHeader::SetName(const char* name)
60 fRawHeaderValid = false;
61 fName = name;
62 fName.Trim().CapitalizeEachWord();
66 void
67 BHttpHeader::SetValue(const char* value)
69 fRawHeaderValid = false;
70 fValue = value;
71 fValue.Trim();
75 bool
76 BHttpHeader::SetHeader(const char* string)
78 fRawHeaderValid = false;
79 fName.Truncate(0);
80 fValue.Truncate(0);
82 const char* separator = strchr(string, ':');
84 if (separator == NULL)
85 return false;
87 fName.SetTo(string, separator - string);
88 fName.Trim().CapitalizeEachWord();
89 SetValue(separator + 1);
90 return true;
94 const char*
95 BHttpHeader::Name() const
97 return fName.String();
101 const char*
102 BHttpHeader::Value() const
104 return fValue.String();
108 const char*
109 BHttpHeader::Header() const
111 if (!fRawHeaderValid) {
112 fRawHeaderValid = true;
114 fRawHeader.Truncate(0);
115 fRawHeader << fName << ": " << fValue;
118 return fRawHeader.String();
122 bool
123 BHttpHeader::NameIs(const char* name) const
125 return fName == BString(name).Trim().CapitalizeEachWord();
129 BHttpHeader&
130 BHttpHeader::operator=(const BHttpHeader& other)
132 fName = other.fName;
133 fValue = other.fValue;
134 fRawHeaderValid = false;
136 return *this;
140 // #pragma mark -- BHttpHeaders
143 BHttpHeaders::BHttpHeaders()
145 fHeaderList()
150 BHttpHeaders::BHttpHeaders(const BHttpHeaders& other)
152 fHeaderList()
154 *this = other;
158 BHttpHeaders::~BHttpHeaders()
160 _EraseData();
164 // #pragma mark Header access
167 const char*
168 BHttpHeaders::HeaderValue(const char* name) const
170 for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
171 BHttpHeader* header
172 = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i));
174 if (header->NameIs(name))
175 return header->Value();
178 return NULL;
182 BHttpHeader&
183 BHttpHeaders::HeaderAt(int32 index) const
185 //! Note: index _must_ be in-bounds
186 BHttpHeader* header
187 = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index));
189 return *header;
193 // #pragma mark Header count
196 int32
197 BHttpHeaders::CountHeaders() const
199 return fHeaderList.CountItems();
203 // #pragma Header tests
206 int32
207 BHttpHeaders::HasHeader(const char* name) const
209 for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
210 BHttpHeader* header
211 = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAt(i));
213 if (header->NameIs(name))
214 return i;
217 return -1;
221 // #pragma mark Header add/replace
224 bool
225 BHttpHeaders::AddHeader(const char* line)
227 return _AddOrDeleteHeader(new(std::nothrow) BHttpHeader(line));
231 bool
232 BHttpHeaders::AddHeader(const char* name, const char* value)
234 return _AddOrDeleteHeader(new(std::nothrow) BHttpHeader(name, value));
238 bool
239 BHttpHeaders::AddHeader(const char* name, int32 value)
241 BString strValue;
242 strValue << value;
244 return AddHeader(name, strValue);
248 // #pragma mark Header deletion
251 void
252 BHttpHeaders::Clear()
254 _EraseData();
255 fHeaderList.MakeEmpty();
259 // #pragma mark Overloaded operators
262 BHttpHeaders&
263 BHttpHeaders::operator=(const BHttpHeaders& other)
265 Clear();
267 for (int32 i = 0; i < other.CountHeaders(); i++)
268 AddHeader(other.HeaderAt(i).Name(), other.HeaderAt(i).Value());
270 return *this;
274 BHttpHeader&
275 BHttpHeaders::operator[](int32 index) const
277 //! Note: Index _must_ be in-bounds
278 BHttpHeader* header
279 = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index));
281 return *header;
285 const char*
286 BHttpHeaders::operator[](const char* name) const
288 return HeaderValue(name);
292 void
293 BHttpHeaders::_EraseData()
295 // Free allocated data;
296 for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
297 BHttpHeader* header
298 = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i));
300 delete header;
305 bool
306 BHttpHeaders::_AddOrDeleteHeader(BHttpHeader* header)
308 if (header != NULL) {
309 if (fHeaderList.AddItem(header))
310 return true;
311 delete header;
313 return false;