From ee435f84c3673625ad09382a6250d24edc3f28a2 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Tue, 9 Apr 2019 13:02:21 +0800 Subject: [PATCH] Refine the response for bad request. --- webcc/http_connection.cc | 7 +++---- webcc/http_response.cc | 13 ------------- webcc/http_response.h | 5 +---- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/webcc/http_connection.cc b/webcc/http_connection.cc index ef924ad..25bb81a 100644 --- a/webcc/http_connection.cc +++ b/webcc/http_connection.cc @@ -75,8 +75,7 @@ void HttpConnection::OnRead(boost::system::error_code ec, std::size_t length) { if (!request_parser_.Parse(buffer_.data(), length)) { // Bad request. LOG_ERRO("Failed to parse HTTP request."); - response_ = HttpResponse::Fault(http::Status::kBadRequest); - DoWrite(); + SendResponse(http::Status::kBadRequest); return; } @@ -117,8 +116,8 @@ void HttpConnection::OnWrite(boost::system::error_code ec, std::size_t length) { LOG_INFO("Response has been sent back, length: %u.", length); if (request_->IsConnectionKeepAlive()) { - LOG_INFO("The client asked for keep-alive connection."); - LOG_INFO("Continue to read next request..."); + LOG_INFO("The client asked for a keep-alive connection."); + LOG_INFO("Continue to read the next request..."); Start(); } else { Shutdown(); diff --git a/webcc/http_response.cc b/webcc/http_response.cc index fe366d6..493d1a9 100644 --- a/webcc/http_response.cc +++ b/webcc/http_response.cc @@ -66,17 +66,4 @@ bool HttpResponse::Prepare() { return true; } -HttpResponsePtr HttpResponse::Fault(http::Status status) { - assert(status != http::Status::kOK); - - auto response = std::make_shared(status); - - // TODO - response->SetHeader(http::headers::kConnection, "Close"); - - //response->Prepare(); - - return response; -} - } // namespace webcc diff --git a/webcc/http_response.h b/webcc/http_response.h index d7f346a..88754cd 100644 --- a/webcc/http_response.h +++ b/webcc/http_response.h @@ -25,10 +25,7 @@ public: // Set start line according to status code. bool Prepare() final; - - // Get a fault response when HTTP status is not OK. - static HttpResponsePtr Fault(http::Status status); - + private: int status_; };