HttpHeaders: header field names are case insensitive.

Whitespace before and after tokens should be ignored.
This commit is contained in:
Hamish Morrison
2013-02-07 19:32:14 +00:00
parent 4db1a8c61d
commit fef7dd705f
+39 -39
View File
@@ -7,7 +7,8 @@
*/ */
#include <cstring> #include <ctype.h>
#include <string.h>
#include <new> #include <new>
#include <String.h> #include <String.h>
@@ -37,10 +38,10 @@ BHttpHeader::BHttpHeader(const char* string)
BHttpHeader::BHttpHeader(const char* name, const char* value) BHttpHeader::BHttpHeader(const char* name, const char* value)
: :
fName(name),
fValue(value),
fRawHeaderValid(false) fRawHeaderValid(false)
{ {
SetName(name);
SetValue(value);
} }
@@ -58,6 +59,7 @@ BHttpHeader::SetName(const char* name)
{ {
fRawHeaderValid = false; fRawHeaderValid = false;
fName = name; fName = name;
fName.Trim().CapitalizeEachWord();
} }
@@ -66,27 +68,25 @@ BHttpHeader::SetValue(const char* value)
{ {
fRawHeaderValid = false; fRawHeaderValid = false;
fValue = value; fValue = value;
fValue.Trim();
} }
bool bool
BHttpHeader::SetHeader(const char* string) BHttpHeader::SetHeader(const char* string)
{ {
BString strLine(string);
fRawHeaderValid = false; fRawHeaderValid = false;
fName.Truncate(0); fName.Truncate(0);
fValue.Truncate(0); fValue.Truncate(0);
int32 separatorLocation = strLine.FindFirst(": "); const char* separator = strchr(string, ':');
if (separatorLocation == B_ERROR) if (separator == NULL)
return false; return false;
strLine.MoveInto(fName, 0, separatorLocation); fName.SetTo(string, separator - string);
strLine.Remove(0, 2); fName.Trim().CapitalizeEachWord();
fValue = strLine; SetValue(separator + 1);
return true; return true;
} }
@@ -110,11 +110,11 @@ BHttpHeader::Header() const
{ {
if (!fRawHeaderValid) { if (!fRawHeaderValid) {
fRawHeaderValid = true; fRawHeaderValid = true;
fRawHeader.Truncate(0); fRawHeader.Truncate(0);
fRawHeader << fName << ": " << fValue; fRawHeader << fName << ": " << fValue;
} }
return fRawHeader.String(); return fRawHeader.String();
} }
@@ -122,7 +122,7 @@ BHttpHeader::Header() const
bool bool
BHttpHeader::NameIs(const char* name) const BHttpHeader::NameIs(const char* name) const
{ {
return fName == BString(name); return fName == BString(name).Trim().CapitalizeEachWord();
} }
@@ -132,7 +132,7 @@ BHttpHeader::operator=(const BHttpHeader& other)
fName = other.fName; fName = other.fName;
fValue = other.fValue; fValue = other.fValue;
fRawHeaderValid = false; fRawHeaderValid = false;
return *this; return *this;
} }
@@ -164,18 +164,18 @@ BHttpHeaders::~BHttpHeaders()
// #pragma mark Header access // #pragma mark Header access
const char* const char*
BHttpHeaders::HeaderValue(const char* name) const BHttpHeaders::HeaderValue(const char* name) const
{ {
for (int32 i = 0; i < fHeaderList.CountItems(); i++) { for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
BHttpHeader* header BHttpHeader* header
= reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i)); = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i));
if (header->NameIs(name)) if (header->NameIs(name))
return header->Value(); return header->Value();
} }
return NULL; return NULL;
} }
@@ -183,8 +183,8 @@ BHttpHeaders::HeaderValue(const char* name) const
BHttpHeader& BHttpHeader&
BHttpHeaders::HeaderAt(int32 index) const BHttpHeaders::HeaderAt(int32 index) const
{ {
//! Note: index _must_ be in-bounds //! Note: index _must_ be in-bounds
BHttpHeader* header BHttpHeader* header
= reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index)); = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index));
return *header; return *header;
@@ -208,30 +208,30 @@ int32
BHttpHeaders::HasHeader(const char* name) const BHttpHeaders::HasHeader(const char* name) const
{ {
for (int32 i = 0; i < fHeaderList.CountItems(); i++) { for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
BHttpHeader* header BHttpHeader* header
= reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAt(i)); = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAt(i));
if (header->NameIs(name)) if (header->NameIs(name))
return i; return i;
} }
return B_ERROR; return B_ERROR;
} }
// #pragma mark Header add/replace // #pragma mark Header add/replace
bool bool
BHttpHeaders::AddHeader(const char* line) BHttpHeaders::AddHeader(const char* line)
{ {
BHttpHeader* heapHeader = new(std::nothrow) BHttpHeader(line); BHttpHeader* heapHeader = new(std::nothrow) BHttpHeader(line);
if (heapHeader != NULL) { if (heapHeader != NULL) {
fHeaderList.AddItem(heapHeader); fHeaderList.AddItem(heapHeader);
return true; return true;
} }
return false; return false;
} }
@@ -240,12 +240,12 @@ bool
BHttpHeaders::AddHeader(const char* name, const char* value) BHttpHeaders::AddHeader(const char* name, const char* value)
{ {
BHttpHeader* heapHeader = new(std::nothrow) BHttpHeader(name, value); BHttpHeader* heapHeader = new(std::nothrow) BHttpHeader(name, value);
if (heapHeader != NULL) { if (heapHeader != NULL) {
fHeaderList.AddItem(heapHeader); fHeaderList.AddItem(heapHeader);
return true; return true;
} }
return false; return false;
} }
@@ -255,7 +255,7 @@ BHttpHeaders::AddHeader(const char* name, int32 value)
{ {
BString strValue; BString strValue;
strValue << value; strValue << value;
return AddHeader(name, strValue); return AddHeader(name, strValue);
} }
@@ -279,7 +279,7 @@ BHttpHeaders::operator=(const BHttpHeaders& other)
{ {
for (int32 i = 0; i < other.CountHeaders(); i++) for (int32 i = 0; i < other.CountHeaders(); i++)
AddHeader(other.HeaderAt(i).Name(), other.HeaderAt(i).Value()); AddHeader(other.HeaderAt(i).Name(), other.HeaderAt(i).Value());
return *this; return *this;
} }
@@ -288,7 +288,7 @@ BHttpHeader&
BHttpHeaders::operator[](int32 index) const BHttpHeaders::operator[](int32 index) const
{ {
//! Note: Index _must_ be in-bounds //! Note: Index _must_ be in-bounds
BHttpHeader* header BHttpHeader* header
= reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index)); = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(index));
return *header; return *header;
@@ -304,12 +304,12 @@ BHttpHeaders::operator[](const char* name) const
void void
BHttpHeaders::_EraseData() BHttpHeaders::_EraseData()
{ {
// Free allocated data; // Free allocated data;
for (int32 i = 0; i < fHeaderList.CountItems(); i++) { for (int32 i = 0; i < fHeaderList.CountItems(); i++) {
BHttpHeader* header BHttpHeader* header
= reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i)); = reinterpret_cast<BHttpHeader*>(fHeaderList.ItemAtFast(i));
delete header; delete header;
} }
} }