From ec66427904188875cdda2f72a64e3e6769d55f8b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:36:53 -0700 Subject: [PATCH 1/4] Cut down on CHECK() and FLOG() in Buffer. Also add a buffer copy (would rather not read into the wrong buffer to start, but it happens...) --- base/buffer.cpp | 28 +++++++++++++++++++++------- base/buffer.h | 5 +++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/base/buffer.cpp b/base/buffer.cpp index f618727c02..38900e0017 100644 --- a/base/buffer.cpp +++ b/base/buffer.cpp @@ -36,6 +36,12 @@ void Buffer::Append(const char *str) { memcpy(dest, str, len); } +void Buffer::Append(const Buffer &other) { + size_t len = other.size(); + char *dest = Append(len); + memcpy(dest, &other.data_[0], len); +} + void Buffer::AppendValue(int value) { char buf[16]; // This is slow. @@ -44,8 +50,11 @@ void Buffer::AppendValue(int value) { } void Buffer::Take(size_t length, std::string *dest) { - CHECK_LE(length, data_.size()); - dest->resize(length); + if (length > data_.size()) { + ELOG("Truncating length in Buffer::Take()"); + length = data_.size(); + } + dest->resize(length); if (length > 0) { memcpy(&(*dest)[0], &data_[0], length); data_.erase(data_.begin(), data_.begin() + length); @@ -64,7 +73,11 @@ int Buffer::TakeLineCRLF(std::string *dest) { } void Buffer::Skip(size_t length) { - data_.erase(data_.begin(), data_.begin() + length); + if (length > data_.size()) { + ELOG("Truncating length in Buffer::Skip()"); + length = data_.size(); + } + data_.erase(data_.begin(), data_.begin() + length); } int Buffer::SkipLineCRLF() { @@ -87,18 +100,19 @@ int Buffer::OffsetToAfterNextCRLF() { } void Buffer::Printf(const char *fmt, ...) { - char buffer[512]; + char buffer[2048]; va_list vl; va_start(vl, fmt); ssize_t retval = vsnprintf(buffer, sizeof(buffer), fmt, vl); if (retval >= (ssize_t)sizeof(buffer)) { // Output was truncated. TODO: Do something. - FLOG("Buffer::Printf truncated output"); + ELOG("Buffer::Printf truncated output"); + } + if (retval < 0) { + ELOG("Buffer::Printf failed"); } - CHECK_GE(retval, 0); va_end(vl); char *ptr = Append(retval); - CHECK(ptr); memcpy(ptr, buffer, retval); } diff --git a/base/buffer.h b/base/buffer.h index ff6fa9e1ab..0ebf7e3f2b 100644 --- a/base/buffer.h +++ b/base/buffer.h @@ -19,9 +19,10 @@ class Buffer { char *Append(ssize_t length); char *Append(size_t length) { return Append((ssize_t)length); } - // These work pretty much like you'd expect. + // These work pretty much like you'd expect. void Append(const char *str); // str null-terminated. The null is not copied. - void Append(const std::string &str); + void Append(const std::string &str); + void Append(const Buffer &other); // Various types. Useful for varz etc. Appends a string representation of the // value, rather than a binary representation. From 760c919408fdafe4873a8d2f273b926c32a7ec62 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:37:47 -0700 Subject: [PATCH 2/4] Add a NULL check, even though this is already called via a check. --- net/resolve.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/resolve.cpp b/net/resolve.cpp index 4b18a05a3b..304f3356cb 100644 --- a/net/resolve.cpp +++ b/net/resolve.cpp @@ -121,7 +121,8 @@ bool DNSResolve(const std::string &host, const std::string &service, addrinfo ** void DNSResolveFree(addrinfo *res) { - freeaddrinfo(res); + if (res != NULL) + freeaddrinfo(res); } int inet_pton(int af, const char* src, void* dst) From 6c638a5f841bee02146a26b299be8add1ed14fb8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:38:57 -0700 Subject: [PATCH 3/4] Avoid a CHECK() in Connect(), and invalidate sock(). --- net/http_client.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/net/http_client.cpp b/net/http_client.cpp index 9c01458663..b362b9b202 100644 --- a/net/http_client.cpp +++ b/net/http_client.cpp @@ -66,9 +66,12 @@ bool Connection::Resolve(const char *host, int port) { } bool Connection::Connect() { - CHECK_GE(port_, 0); + if (port_ <= 0) { + ELOG("Bad port"); + return false; + } sock_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if ((intptr_t)sock_ < 0) { + if ((intptr_t)sock_ == -1) { ELOG("Bad socket"); return false; } @@ -89,6 +92,10 @@ bool Connection::Connect() { sleep(1); #endif } + + // Let's not leak this socket. + closesocket(sock_); + sock_ = -1; return false; } From 8c999885d8bd74113ef4231641b75e71430aab16 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:40:24 -0700 Subject: [PATCH 4/4] 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) {