From daffc235b8d3afe2a4f8d6c1298aafc6d700afb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 18 Nov 2013 16:27:39 +0100 Subject: [PATCH] Minor stuff in http_client --- net/http_client.cpp | 33 ++++++++++++++++++++------------- net/http_client.h | 3 ++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/net/http_client.cpp b/net/http_client.cpp index 6671e5070c..202f8b128b 100644 --- a/net/http_client.cpp +++ b/net/http_client.cpp @@ -22,8 +22,6 @@ #include "net/resolve.h" #include "net/url.h" -// #include "strings/strutil.h" - namespace net { Connection::Connection() @@ -54,7 +52,7 @@ bool Connection::Resolve(const char *host, int port) { char port_str[10]; snprintf(port_str, sizeof(port_str), "%d", port); - + std::string err; if (!net::DNSResolve(host, port_str, &resolved_, err)) { ELOG("Failed to resolve host %s: %s", host, err.c_str()); @@ -144,7 +142,11 @@ void DeChunk(Buffer *inbuffer, Buffer *outbuffer) { } } -int Client::GET(const char *resource, Buffer *output) { +int Client::GET(const char *resource, Buffer *output, float *progress) { + if (progress) { + *progress = 0; + } + Buffer buffer; const char *tpl = "GET %s HTTP/1.1\r\n" @@ -213,29 +215,35 @@ int Client::GET(const char *resource, Buffer *output) { } } + if (!contentLength && progress) { + // Content length is unknown. + // Set progress to 1% so it looks like something is happening... + *progress = 0.01f; + } + // output now contains the rest of the reply. Dechunk it. if (chunked) { + // TODO: Turn this into a loop and update progress DeChunk(&readbuf, output); } else { + // TODO: Turn this into a loop and update progress output->Append(readbuf); } // If it's gzipped, we decompress it and put it back in the buffer. if (gzip) { - std::string compressed; + std::string compressed, decompressed; output->TakeAll(&compressed); - // What is this garbage? - //if (compressed[0] == 0x8e) -// compressed = compressed.substr(4); - std::string decompressed; bool result = decompress_string(compressed, &decompressed); if (!result) { ELOG("Error decompressing using zlib"); + *progress = 0.0f; return -1; } output->Append(decompressed); } + *progress = 1.0f; return code; } @@ -293,8 +301,7 @@ Download::~Download() { } -void Download::Start(std::shared_ptr self) -{ +void Download::Start(std::shared_ptr self) { std::thread th(std::bind(&Download::Do, this, self)); th.detach(); } @@ -344,9 +351,9 @@ void Download::Do(std::shared_ptr self) { } // TODO: Allow cancelling during a GET somehow... - int resultCode = client.GET(fileUrl.Resource().c_str(), &buffer_); + int resultCode = client.GET(fileUrl.Resource().c_str(), &buffer_, &progress_); if (resultCode == 200) { - ILOG("Completed downloading %s to %s", url_.c_str(), outfile_.c_str()); + ILOG("Completed downloading %s to %s", url_.c_str(), outfile_.empty() ? "memory" : outfile_.c_str()); if (!outfile_.empty() && !buffer_.FlushToFile(outfile_.c_str())) { ELOG("Failed writing download to %s", outfile_.c_str()); } diff --git a/net/http_client.h b/net/http_client.h index c9b18acfe6..dd19aac405 100644 --- a/net/http_client.h +++ b/net/http_client.h @@ -58,7 +58,7 @@ public: ~Client(); // Return value is the HTTP return code. 200 means OK. < 0 means some local error. - int GET(const char *resource, Buffer *output); + int GET(const char *resource, Buffer *output, float *progress = 0); // Return value is the HTTP return code. int POST(const char *resource, const std::string &data, const std::string &mime, Buffer *output); @@ -114,6 +114,7 @@ public: ~Downloader() { CancelAll(); } + std::shared_ptr StartDownload(const std::string &url, const std::string &outfile); // Drops finished downloads from the list.