From 67f0fc48f9031d84c0e776898e3042d4318fdef3 Mon Sep 17 00:00:00 2001 From: flamingcow Date: Thu, 9 May 2019 19:19:26 -0700 Subject: [PATCH] Only Consume() after a full request is processed --- connection.cc | 6 +++--- connection.h | 2 +- firebuf | 2 +- parse.h | 4 ---- request.cc | 2 +- server.cc | 7 ++++--- server.h | 3 ++- 7 files changed, 12 insertions(+), 14 deletions(-) diff --git a/connection.cc b/connection.cc index a78eaba..ab20ba8 100644 --- a/connection.cc +++ b/connection.cc @@ -9,11 +9,11 @@ namespace firecgi { -Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function& callback, const std::unordered_set& headers) +Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function& callback, const std::unordered_set& headers, int max_request_len) : sock_(sock), callback_(callback), headers_(headers), - buf_(sock, max_record_len), + buf_(sock, max_request_len), request_(this) { char client_addr_str[INET6_ADDRSTRLEN]; PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str))); @@ -122,6 +122,7 @@ int Connection::Read() { // Magic signal for completed request (mirrors the HTTP/1.1 protocol) requests_++; callback_(&request_); + buf_.Consume(); // discard data and invalidate pointers } else { std::string_view in(buf_.Read(header->ContentLength()), header->ContentLength()); request_.AddIn(in); @@ -141,7 +142,6 @@ int Connection::Read() { buf_.Commit(); // we've acted on the bytes read so far } - buf_.Consume(); return -1; } diff --git a/connection.h b/connection.h index 1496b93..241d5fa 100644 --- a/connection.h +++ b/connection.h @@ -13,7 +13,7 @@ namespace firecgi { class Connection { public: - Connection(int sock, const sockaddr_in6& client_addr, const std::function& callback, const std::unordered_set& headers); + Connection(int sock, const sockaddr_in6& client_addr, const std::function& callback, const std::unordered_set& headers, int max_request_len); ~Connection(); [[nodiscard]] int Read(); diff --git a/firebuf b/firebuf index 49766e2..4d9486e 160000 --- a/firebuf +++ b/firebuf @@ -1 +1 @@ -Subproject commit 49766e29f41348ebd9a24cabd6656e8818d3e35b +Subproject commit 4d9486e281d5123d2ac68636e20acf70ef22776e diff --git a/parse.h b/parse.h index 6fb2246..6bf038f 100644 --- a/parse.h +++ b/parse.h @@ -44,8 +44,4 @@ struct ParamHeader { uint8_t value_length; }; -constexpr auto max_content_len = 65535; -constexpr auto max_padding_len = 255; -constexpr auto max_record_len = sizeof(Header) + max_content_len + max_padding_len; - } // namespace firecgi diff --git a/request.cc b/request.cc index 58f3b0a..fffa27a 100644 --- a/request.cc +++ b/request.cc @@ -18,7 +18,7 @@ template void AppendVec(const T& obj, std::vector* vec) { Request::Request(Connection* conn) : conn_(conn), - out_buf_(max_record_len) {} + out_buf_(64*1024) {} void Request::NewRequest(uint16_t request_id) { request_id_ = request_id; diff --git a/server.cc b/server.cc index d3fe199..123e7b7 100644 --- a/server.cc +++ b/server.cc @@ -11,11 +11,12 @@ namespace firecgi { -Server::Server(int port, const std::function& callback, int threads, const std::unordered_set& headers) +Server::Server(int port, const std::function& callback, int threads, const std::unordered_set& headers, int max_request_len) : port_(port), callback_(callback), threads_(threads), - headers_(headers) { + headers_(headers), + max_request_len_(max_request_len) { LOG(INFO) << "listening on [::1]:" << port_; signal(SIGPIPE, SIG_IGN); @@ -81,7 +82,7 @@ void Server::NewConn(int listen_sock, int epoll_fd) { PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0); { - auto *conn = new Connection(client_sock, client_addr, callback_, headers_); + auto *conn = new Connection(client_sock, client_addr, callback_, headers_, max_request_len_); struct epoll_event ev{ .events = EPOLLIN, .data = { diff --git a/server.h b/server.h index c4452ef..0f964a1 100644 --- a/server.h +++ b/server.h @@ -10,7 +10,7 @@ namespace firecgi { class Server { public: - Server(int port, const std::function& callback, int threads=1, const std::unordered_set& headers={}); + Server(int port, const std::function& callback, int threads=1, const std::unordered_set& headers={}, int max_request_len=(16*1024)); void Serve(); private: @@ -22,6 +22,7 @@ class Server { const std::function callback_; const int threads_; const std::unordered_set headers_; + const int max_request_len_; }; } // firecgi