From 10bdc8e77502b790af729bc49ebb0fc04ac76f60 Mon Sep 17 00:00:00 2001 From: flamingcow Date: Thu, 9 May 2019 23:04:04 -0700 Subject: [PATCH] Add WriteMany() --- request.cc | 24 +++++++++++++----------- request.h | 11 +++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/request.cc b/request.cc index 1c759a5..1f56970 100644 --- a/request.cc +++ b/request.cc @@ -65,13 +65,7 @@ void Request::WriteHeader(const std::string_view& name, const std::string_view& void Request::WriteBody(const std::string_view& body) { std::lock_guard l(output_mu_); - - if (!body_written_) { - CHECK(out_buf_.Write("\n")); - body_written_ = true; - } - // TODO: make this able to span multiple packets - CHECK(out_buf_.Write(body)); + WriteBodyLocked(body); } bool Request::Flush() { @@ -95,10 +89,7 @@ bool Request::Flush() { bool Request::End() { std::lock_guard l(output_mu_); - if (!body_written_) { - // Fully empty response not allowed - CHECK(out_buf_.Write("\n")); - } + WriteBodyLocked(""); std::vector vecs; @@ -129,4 +120,15 @@ Header Request::OutputHeader() { return Header(6, request_id_, out_buf_.ReadMaxLen()); } +void Request::WriteBodyLocked(const std::string_view& body) { + CHECK(!output_mu_.try_lock()); + + if (!body_written_) { + CHECK(out_buf_.Write("\n")); + body_written_ = true; + } + // TODO: make this able to span multiple packets + CHECK(out_buf_.Write(body)); +} + } // namespace firecgi diff --git a/request.h b/request.h index cfcff82..14cf1d1 100644 --- a/request.h +++ b/request.h @@ -30,9 +30,13 @@ class Request { [[nodiscard]] bool Flush(); bool End(); + template + void WriteMany(const std::string_view& first, Args... more); + private: Header OutputHeader(); iovec OutputVec(); + void WriteBodyLocked(const std::string_view& body); Connection *conn_; uint16_t request_id_ = 0; @@ -45,4 +49,11 @@ class Request { std::mutex output_mu_; }; +template +void Request::WriteMany(const std::string_view& first, Args... more) { + std::lock_guard l(output_mu_); + WriteBodyLocked(first); + WriteMany(std::forward(more)...); +} + } // namespace firecgi