From 1f4b6cac30dac3feb88165716f4e1824934fa8ac Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Mon, 5 Aug 2019 10:45:18 +0800 Subject: [PATCH] Allow to set file chunk size to the server. --- webcc/body.h | 2 +- webcc/gzip.cc | 2 +- webcc/server.cc | 4 ++-- webcc/server.h | 8 ++++++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/webcc/body.h b/webcc/body.h index 76a5db9..fb7d220 100644 --- a/webcc/body.h +++ b/webcc/body.h @@ -131,7 +131,7 @@ private: // the memory. class FileBody : public Body { public: - explicit FileBody(const Path& path, std::size_t chunk_size = 1024); + FileBody(const Path& path, std::size_t chunk_size); std::size_t GetSize() const override { return size_; diff --git a/webcc/gzip.cc b/webcc/gzip.cc index 28489e2..36f16ac 100644 --- a/webcc/gzip.cc +++ b/webcc/gzip.cc @@ -80,7 +80,7 @@ bool Decompress(const std::string& input, std::string* output) { stream.zalloc = Z_NULL; stream.zfree = Z_NULL; - // About the windowBits paramter: + // About the windowBits parameter: // (https://stackoverflow.com/a/1838702) // (http://www.zlib.net/manual.html) // windowBits can also be greater than 15 for optional gzip decoding. Add 32 diff --git a/webcc/server.cc b/webcc/server.cc index 17934ca..bbf7704 100644 --- a/webcc/server.cc +++ b/webcc/server.cc @@ -21,7 +21,7 @@ using tcp = boost::asio::ip::tcp; namespace webcc { Server::Server(std::uint16_t port, const Path& doc_root) - : port_(port), doc_root_(doc_root), running_(false), + : port_(port), doc_root_(doc_root), file_chunk_size_(1024), running_(false), acceptor_(io_context_), signals_(io_context_) { AddSignals(); } @@ -352,7 +352,7 @@ bool Server::ServeStatic(ConnectionPtr connection) { Path p = doc_root_ / path; try { - auto body = std::make_shared(p); + auto body = std::make_shared(p, file_chunk_size_); auto response = std::make_shared(Status::kOK); diff --git a/webcc/server.h b/webcc/server.h index 9df0735..f2d1151 100644 --- a/webcc/server.h +++ b/webcc/server.h @@ -26,6 +26,10 @@ public: Server(const Server&) = delete; Server& operator=(const Server&) = delete; + void set_file_chunk_size(std::size_t file_chunk_size) { + file_chunk_size_ = file_chunk_size; + } + // Route a URL to a view. // The URL should start with "/". E.g., "/instances". bool Route(const std::string& url, ViewPtr view, @@ -109,6 +113,10 @@ private: // The directory with the static files to be served. Path doc_root_; + // The size of the chunk loaded into memory each time when serving a + // static file. + std::size_t file_chunk_size_; + // Is the server running? bool running_;