From 8c999885d8bd74113ef4231641b75e71430aab16 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:40:24 -0700 Subject: [PATCH] Support non-chunked HTTP responses. --- net/http_client.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/net/http_client.cpp b/net/http_client.cpp index b362b9b202..a48585f045 100644 --- a/net/http_client.cpp +++ b/net/http_client.cpp @@ -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) {