Support non-chunked HTTP responses.

This commit is contained in:
Unknown W. Brackets 2013-06-24 19:40:24 -07:00
parent 6c638a5f84
commit 8c999885d8

View File

@ -172,25 +172,52 @@ int Client::GET(const char *resource, Buffer *output) {
std::string line;
readbuf.TakeLineCRLF(&line);
int code = atoi(&line[line.find(" ") + 1]);
int code;
size_t code_pos = line.find(' ');
if (code_pos != line.npos) {
code_pos = line.find_first_not_of(' ', code_pos);
}
if (code_pos != line.npos) {
code = atoi(&line[code_pos]);
} else {
return -1;
}
bool gzip = false;
bool chunked = false;
int contentLength = 0;
while (true) {
int sz = readbuf.TakeLineCRLF(&line);
if (!sz)
break;
// TODO: Case folding.
if (startsWith(line, "Content-Length:")) {
contentLength = atoi(&line[16]);
size_t size_pos = line.find_first_of(' ');
if (size_pos != line.npos) {
size_pos = line.find_first_not_of(' ', size_pos);
}
if (size_pos != line.npos) {
contentLength = atoi(&line[size_pos]);
chunked = false;
}
} else if (startsWith(line, "Content-Encoding:")) {
if (line.find("gzip") != std::string::npos) {
gzip = true;
}
} else if (startsWith(line, "Transfer-Encoding:")) {
if (line.find("chunked") != std::string::npos) {
chunked = true;
}
}
}
// output now contains the rest of the reply. Dechunk it.
DeChunk(&readbuf, output);
if (chunked) {
DeChunk(&readbuf, output);
} else {
output->Append(readbuf);
}
// If it's gzipped, we decompress it and put it back in the buffer.
if (gzip) {