diff --git a/examples/form_server.cc b/examples/form_server.cc index 6f646df..c19c615 100644 --- a/examples/form_server.cc +++ b/examples/form_server.cc @@ -54,6 +54,8 @@ int main(int argc, char* argv[]) { try { webcc::Server server{ boost::asio::ip::tcp::v4(), port }; + server.set_buffer_size(webcc::kBufferSize * 10); + server.Route("/upload", std::make_shared(), { "POST" }); server.Run(); diff --git a/webcc/connection.cc b/webcc/connection.cc index 48cc41e..7538e90 100644 --- a/webcc/connection.cc +++ b/webcc/connection.cc @@ -12,9 +12,10 @@ using boost::asio::ip::tcp; namespace webcc { Connection::Connection(tcp::socket socket, ConnectionPool* pool, - Queue* queue, ViewMatcher&& view_matcher) + Queue* queue, ViewMatcher&& view_matcher, + std::size_t buffer_size) : socket_(std::move(socket)), pool_(pool), queue_(queue), - view_matcher_(std::move(view_matcher)), buffer_(kBufferSize) { + view_matcher_(std::move(view_matcher)), buffer_(buffer_size) { } void Connection::Start() { diff --git a/webcc/connection.h b/webcc/connection.h index 805527f..7259922 100644 --- a/webcc/connection.h +++ b/webcc/connection.h @@ -24,7 +24,8 @@ using ConnectionPtr = std::shared_ptr; class Connection : public std::enable_shared_from_this { public: Connection(boost::asio::ip::tcp::socket socket, ConnectionPool* pool, - Queue* queue, ViewMatcher&& view_matcher); + Queue* queue, ViewMatcher&& view_matcher, + std::size_t buffer_size); ~Connection() = default; diff --git a/webcc/request_parser.cc b/webcc/request_parser.cc index bd0be5f..a626ef3 100644 --- a/webcc/request_parser.cc +++ b/webcc/request_parser.cc @@ -217,7 +217,7 @@ bool RequestParser::ParsePartHeaders(bool* need_more_data) { } bool RequestParser::GetNextBoundaryLine(std::size_t* b_off, - std::size_t* b_count, + std::size_t* b_len, bool* ended) { std::size_t off = 0; @@ -227,15 +227,15 @@ bool RequestParser::GetNextBoundaryLine(std::size_t* b_off, break; } - std::size_t count = pos - off; - if (count == 0) { + std::size_t len = pos - off; + if (len == 0) { off = pos + 2; continue; // Empty line } - if (IsBoundary(pending_data_, off, count, ended)) { + if (IsBoundary(pending_data_, off, len, ended)) { *b_off = off; - *b_count = count; + *b_len = len; return true; } diff --git a/webcc/request_parser.h b/webcc/request_parser.h index 603366f..a753fb2 100644 --- a/webcc/request_parser.h +++ b/webcc/request_parser.h @@ -35,8 +35,7 @@ private: bool ParseMultipartContent(const char* data, std::size_t length); bool ParsePartHeaders(bool* need_more_data); - bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_count, - bool* ended); + bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_len, bool* ended); // Check if the str.substr(off, count) is a boundary. bool IsBoundary(const std::string& str, std::size_t off, diff --git a/webcc/server.cc b/webcc/server.cc index e9762c8..c8c759f 100644 --- a/webcc/server.cc +++ b/webcc/server.cc @@ -23,6 +23,7 @@ Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port, : protocol_(protocol), port_(port), doc_root_(doc_root), + buffer_size_(kBufferSize), file_chunk_size_(1024), running_(false), acceptor_(io_context_), @@ -173,7 +174,8 @@ void Server::AsyncAccept() { _2, _3); auto connection = std::make_shared( - std::move(socket), &pool_, &queue_, std::move(view_matcher)); + std::move(socket), &pool_, &queue_, std::move(view_matcher), + buffer_size_); pool_.Start(connection); } diff --git a/webcc/server.h b/webcc/server.h index e1d0df2..c4d6cf1 100644 --- a/webcc/server.h +++ b/webcc/server.h @@ -29,6 +29,12 @@ public: Server(const Server&) = delete; Server& operator=(const Server&) = delete; + void set_buffer_size(std::size_t buffer_size) { + if (buffer_size > 0) { + buffer_size_ = buffer_size; + } + } + void set_file_chunk_size(std::size_t file_chunk_size) { assert(file_chunk_size > 0); file_chunk_size_ = file_chunk_size; @@ -84,7 +90,7 @@ private: // The connection will keep alive if it's a persistent connection. When next // request comes, this connection will be put back to the queue again. virtual void Handle(ConnectionPtr connection); - + // Match the view by HTTP method and URL (path). // Return if a view or static file is matched or not. // If the view asks for data streaming, |stream| will be set to true. @@ -104,6 +110,9 @@ private: // The directory with the static files to be served. boost::filesystem::path doc_root_; + // The size of the buffer for reading request. + std::size_t buffer_size_; + // The size of the chunk loaded into memory each time when serving a // static file. std::size_t file_chunk_size_;