Handle commonly mixed-case headers better.

Sometimes you'll see Content-length, etc.
This commit is contained in:
Unknown W. Brackets 2014-11-25 00:30:13 -08:00
parent c85fb55502
commit 3de556b248
2 changed files with 20 additions and 4 deletions

View File

@ -12,6 +12,7 @@
#ifdef _MSC_VER
#pragma warning (disable:4996)
#define strncasecmp _strnicmp
#endif
#ifdef BLACKBERRY
@ -74,6 +75,20 @@ inline bool endsWith(const std::string &str, const std::string &what) {
return str.substr(str.size() - what.size()) == what;
}
// Only use on strings where you're only concerned about ASCII.
inline bool startsWithNoCase(const std::string &str, const std::string &what) {
if (str.size() < what.size())
return false;
return strncasecmp(str.c_str(), what.c_str(), what.size()) == 0;
}
inline bool endsWithNoCase(const std::string &str, const std::string &what) {
if (str.size() < what.size())
return false;
const size_t offset = str.size() - what.size();
return strncasecmp(str.c_str() + offset, what.c_str(), what.size()) == 0;
}
void DataToHexString(const uint8 *data, size_t size, std::string *output);
inline void StringToHexString(const std::string &data, std::string *output) {
DataToHexString((uint8_t *)(&data[0]), data.size(), output);

View File

@ -277,8 +277,7 @@ int Client::ReadResponseEntity(Buffer *readbuf, const std::vector<std::string> &
bool chunked = false;
int contentLength = 0;
for (std::string line : responseHeaders) {
// TODO: Case folding.
if (startsWith(line, "Content-Length:")) {
if (startsWithNoCase(line, "Content-Length:")) {
size_t size_pos = line.find_first_of(' ');
if (size_pos != line.npos) {
size_pos = line.find_first_not_of(' ', size_pos);
@ -287,11 +286,13 @@ int Client::ReadResponseEntity(Buffer *readbuf, const std::vector<std::string> &
contentLength = atoi(&line[size_pos]);
chunked = false;
}
} else if (startsWith(line, "Content-Encoding:")) {
} else if (startsWithNoCase(line, "Content-Encoding:")) {
// TODO: Case folding...
if (line.find("gzip") != std::string::npos) {
gzip = true;
}
} else if (startsWith(line, "Transfer-Encoding:")) {
} else if (startsWithNoCase(line, "Transfer-Encoding:")) {
// TODO: Case folding...
if (line.find("chunked") != std::string::npos) {
chunked = true;
}