2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Christophe Huriaux, c.huriaux@gmail.com
15 #include <HttpHeaders.h>
18 // #pragma mark -- BHttpHeader
21 BHttpHeader::BHttpHeader()
31 BHttpHeader::BHttpHeader(const char* string
)
39 BHttpHeader::BHttpHeader(const char* name
, const char* value
)
41 fRawHeaderValid(false)
48 BHttpHeader::BHttpHeader(const BHttpHeader
& copy
)
52 fRawHeaderValid(false)
58 BHttpHeader::SetName(const char* name
)
60 fRawHeaderValid
= false;
62 fName
.Trim().CapitalizeEachWord();
67 BHttpHeader::SetValue(const char* value
)
69 fRawHeaderValid
= false;
76 BHttpHeader::SetHeader(const char* string
)
78 fRawHeaderValid
= false;
82 const char* separator
= strchr(string
, ':');
84 if (separator
== NULL
)
87 fName
.SetTo(string
, separator
- string
);
88 fName
.Trim().CapitalizeEachWord();
89 SetValue(separator
+ 1);
95 BHttpHeader::Name() const
97 return fName
.String();
102 BHttpHeader::Value() const
104 return fValue
.String();
109 BHttpHeader::Header() const
111 if (!fRawHeaderValid
) {
112 fRawHeaderValid
= true;
114 fRawHeader
.Truncate(0);
115 fRawHeader
<< fName
<< ": " << fValue
;
118 return fRawHeader
.String();
123 BHttpHeader::NameIs(const char* name
) const
125 return fName
== BString(name
).Trim().CapitalizeEachWord();
130 BHttpHeader::operator=(const BHttpHeader
& other
)
133 fValue
= other
.fValue
;
134 fRawHeaderValid
= false;
140 // #pragma mark -- BHttpHeaders
143 BHttpHeaders::BHttpHeaders()
150 BHttpHeaders::BHttpHeaders(const BHttpHeaders
& other
)
158 BHttpHeaders::~BHttpHeaders()
164 // #pragma mark Header access
168 BHttpHeaders::HeaderValue(const char* name
) const
170 for (int32 i
= 0; i
< fHeaderList
.CountItems(); i
++) {
172 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(i
));
174 if (header
->NameIs(name
))
175 return header
->Value();
183 BHttpHeaders::HeaderAt(int32 index
) const
185 //! Note: index _must_ be in-bounds
187 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(index
));
193 // #pragma mark Header count
197 BHttpHeaders::CountHeaders() const
199 return fHeaderList
.CountItems();
203 // #pragma Header tests
207 BHttpHeaders::HasHeader(const char* name
) const
209 for (int32 i
= 0; i
< fHeaderList
.CountItems(); i
++) {
211 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAt(i
));
213 if (header
->NameIs(name
))
221 // #pragma mark Header add/replace
225 BHttpHeaders::AddHeader(const char* line
)
227 return _AddOrDeleteHeader(new(std::nothrow
) BHttpHeader(line
));
232 BHttpHeaders::AddHeader(const char* name
, const char* value
)
234 return _AddOrDeleteHeader(new(std::nothrow
) BHttpHeader(name
, value
));
239 BHttpHeaders::AddHeader(const char* name
, int32 value
)
244 return AddHeader(name
, strValue
);
248 // #pragma mark Header deletion
252 BHttpHeaders::Clear()
255 fHeaderList
.MakeEmpty();
259 // #pragma mark Overloaded operators
263 BHttpHeaders::operator=(const BHttpHeaders
& other
)
267 for (int32 i
= 0; i
< other
.CountHeaders(); i
++)
268 AddHeader(other
.HeaderAt(i
).Name(), other
.HeaderAt(i
).Value());
275 BHttpHeaders::operator[](int32 index
) const
277 //! Note: Index _must_ be in-bounds
279 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(index
));
286 BHttpHeaders::operator[](const char* name
) const
288 return HeaderValue(name
);
293 BHttpHeaders::_EraseData()
295 // Free allocated data;
296 for (int32 i
= 0; i
< fHeaderList
.CountItems(); i
++) {
298 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(i
));
306 BHttpHeaders::_AddOrDeleteHeader(BHttpHeader
* header
)
308 if (header
!= NULL
) {
309 if (fHeaderList
.AddItem(header
))