From 05382c76a53ba68966d4f6d57771ea1138e4db28 Mon Sep 17 00:00:00 2001 From: flamingcow Date: Thu, 9 May 2019 19:30:36 -0700 Subject: [PATCH] Only allow one stdin record, avoid the copy --- connection.cc | 6 ++++-- request.cc | 10 +++++++--- request.h | 5 +++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/connection.cc b/connection.cc index ab20ba8..764b91a 100644 --- a/connection.cc +++ b/connection.cc @@ -124,8 +124,10 @@ int Connection::Read() { callback_(&request_); buf_.Consume(); // discard data and invalidate pointers } else { - std::string_view in(buf_.Read(header->ContentLength()), header->ContentLength()); - request_.AddIn(in); + if (!request_.GetBody().empty()) { + LOG(ERROR) << "received multiple stdin records. have you set \"fastcgi_request_buffering on\"?"; + } + request_.SetBody({buf_.Read(header->ContentLength()), header->ContentLength()}); } } break; diff --git a/request.cc b/request.cc index fffa27a..8b40b83 100644 --- a/request.cc +++ b/request.cc @@ -23,7 +23,7 @@ Request::Request(Connection* conn) void Request::NewRequest(uint16_t request_id) { request_id_ = request_id; params_.clear(); - in_.clear(); + body_ = {}; out_buf_.Reset(); body_written_ = false; } @@ -36,8 +36,8 @@ void Request::AddParam(const std::string_view& key, const std::string_view& valu params_.try_emplace(std::string(key), std::string(value)); } -void Request::AddIn(const std::string_view& in) { - in_.append(in); +void Request::SetBody(const std::string_view& body) { + body_ = body; } const std::string& Request::GetParam(const std::string& key) { @@ -49,6 +49,10 @@ const std::string& Request::GetParam(const std::string& key) { return iter->second; } +const std::string_view& Request::GetBody() { + return body_; +} + void Request::WriteHeader(const std::string_view& name, const std::string_view& value) { CHECK(!body_written_); CHECK(out_buf_.Write(name)); diff --git a/request.h b/request.h index a47e834..e078c19 100644 --- a/request.h +++ b/request.h @@ -19,9 +19,10 @@ class Request { uint16_t RequestId(); void AddParam(const std::string_view& key, const std::string_view& value); - void AddIn(const std::string_view& in); + void SetBody(const std::string_view& in); const std::string& GetParam(const std::string& key); + const std::string_view& GetBody(); void WriteHeader(const std::string_view& name, const std::string_view& value); void WriteBody(const std::string_view& body); @@ -36,7 +37,7 @@ class Request { uint16_t request_id_ = 0; std::unordered_map params_; - std::string in_; + std::string_view body_; firebuf::Buffer out_buf_; bool body_written_;