diff --git a/request.cc b/request.cc index 97d9b72..1c759a5 100644 --- a/request.cc +++ b/request.cc @@ -54,6 +54,8 @@ const std::string_view& Request::GetBody() { } void Request::WriteHeader(const std::string_view& name, const std::string_view& value) { + std::lock_guard l(output_mu_); + CHECK(!body_written_); CHECK(out_buf_.Write(name)); CHECK(out_buf_.Write(": ")); @@ -62,6 +64,8 @@ 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; @@ -71,6 +75,8 @@ void Request::WriteBody(const std::string_view& body) { } bool Request::Flush() { + std::lock_guard l(output_mu_); + std::vector vecs; auto header = OutputHeader(); @@ -82,12 +88,17 @@ bool Request::Flush() { return false; } out_buf_.Commit(); + out_buf_.Consume(); return true; } bool Request::End() { - // Fully empty response not allowed - WriteBody(""); + std::lock_guard l(output_mu_); + + if (!body_written_) { + // Fully empty response not allowed + CHECK(out_buf_.Write("\n")); + } std::vector vecs; diff --git a/request.h b/request.h index a9fa19b..cfcff82 100644 --- a/request.h +++ b/request.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "firebuf/buffer.h" @@ -41,6 +42,7 @@ class Request { firebuf::Buffer out_buf_; bool body_written_; + std::mutex output_mu_; }; } // namespace firecgi