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 Archiving
252 BHttpHeaders::PopulateFromArchive(BMessage
* archive
)
259 if (archive
->GetInfo(B_STRING_TYPE
, index
, &nameFound
, NULL
) != B_OK
)
262 BString value
= archive
->FindString(nameFound
);
263 AddHeader(nameFound
, value
);
271 BHttpHeaders::Archive(BMessage
* message
) const
273 int32 count
= CountHeaders();
275 for (int32 i
= 0; i
< count
; i
++) {
276 BHttpHeader
& header
= HeaderAt(i
);
277 message
->AddString(header
.Name(), header
.Value());
282 // #pragma mark Header deletion
286 BHttpHeaders::Clear()
289 fHeaderList
.MakeEmpty();
293 // #pragma mark Overloaded operators
297 BHttpHeaders::operator=(const BHttpHeaders
& other
)
304 for (int32 i
= 0; i
< other
.CountHeaders(); i
++)
305 AddHeader(other
.HeaderAt(i
).Name(), other
.HeaderAt(i
).Value());
312 BHttpHeaders::operator[](int32 index
) const
314 //! Note: Index _must_ be in-bounds
316 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(index
));
323 BHttpHeaders::operator[](const char* name
) const
325 return HeaderValue(name
);
330 BHttpHeaders::_EraseData()
332 // Free allocated data;
333 for (int32 i
= 0; i
< fHeaderList
.CountItems(); i
++) {
335 = reinterpret_cast<BHttpHeader
*>(fHeaderList
.ItemAtFast(i
));
343 BHttpHeaders::_AddOrDeleteHeader(BHttpHeader
* header
)
345 if (header
!= NULL
) {
346 if (fHeaderList
.AddItem(header
))