Refine the parsing of start line; return 201 instead of 200 for POST request.

This commit is contained in:
Adam Gu
2018-07-13 13:08:36 +08:00
parent 9bf4bc3f4b
commit 9b3be4c18c
7 changed files with 37 additions and 49 deletions
+1 -3
View File
@@ -25,9 +25,7 @@ class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
DELETE_COPY_AND_ASSIGN(HttpConnection);
const HttpRequest& request() const {
return request_;
}
const HttpRequest& request() const { return request_; }
// Start to read and process the client request.
void Start();
+5 -5
View File
@@ -5,12 +5,12 @@ namespace webcc {
namespace status_strings {
const std::string OK = "HTTP/1.1 200 OK\r\n";
const std::string CREATED = "HTTP/1.0 201 Created\r\n";
const std::string ACCEPTED = "HTTP/1.0 202 Accepted\r\n";
const std::string NO_CONTENT = "HTTP/1.0 204 No Content\r\n";
const std::string NOT_MODIFIED = "HTTP/1.0 304 Not Modified\r\n";
const std::string CREATED = "HTTP/1.1 201 Created\r\n";
const std::string ACCEPTED = "HTTP/1.1 202 Accepted\r\n";
const std::string NO_CONTENT = "HTTP/1.1 204 No Content\r\n";
const std::string NOT_MODIFIED = "HTTP/1.1 304 Not Modified\r\n";
const std::string BAD_REQUEST = "HTTP/1.1 400 Bad Request\r\n";
const std::string NOT_FOUND = "HTTP/1.0 404 Not Found\r\n";
const std::string NOT_FOUND = "HTTP/1.1 404 Not Found\r\n";
const std::string INTERNAL_SERVER_ERROR =
"HTTP/1.1 500 Internal Server Error\r\n";
const std::string NOT_IMPLEMENTED = "HTTP/1.1 501 Not Implemented\r\n";
+8 -19
View File
@@ -1,5 +1,7 @@
#include "webcc/http_response_parser.h"
#include "boost/algorithm/string.hpp"
#include "webcc/logger.h"
#include "webcc/http_response.h"
@@ -10,33 +12,20 @@ HttpResponseParser::HttpResponseParser(HttpResponse* response)
}
bool HttpResponseParser::ParseStartLine(const std::string& line) {
std::size_t off = 0;
std::vector<std::string> splitted;
boost::split(splitted, line, boost::is_any_of(" "), boost::token_compress_on);
std::size_t pos = line.find(' ');
if (pos == std::string::npos) {
if (splitted.size() < 3) {
LOG_ERRO("Invalid HTTP response status line: %s", line.c_str());
return false;
}
// HTTP version
off = pos + 1; // Skip space.
pos = line.find(' ', off);
if (pos == std::string::npos) {
return false;
}
// Status code
std::string status_str = line.substr(off, pos - off);
std::string& status_str = splitted[1];
try {
response_->set_status(std::stoi(status_str));
} catch (const std::exception&) {
LOG_ERRO("Invalid HTTP status: %s", status_str.c_str());
return false;
}
if (response_->status() != HttpStatus::kOK) {
LOG_ERRO("Invalid HTTP status code: %s", status_str.c_str());
return false;
}
+15 -8
View File
@@ -14,7 +14,9 @@ bool RestRequestHandler::Bind(RestServicePtr service, const std::string& url,
}
void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) {
Url url(connection->request().url(), true);
const HttpRequest& request = connection->request();
Url url(request.url(), true);
if (!url.IsValid()) {
connection->SendResponse(HttpStatus::kBadRequest);
@@ -31,14 +33,14 @@ void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) {
}
UrlQuery query;
Url::SplitQuery(url.query(), &query);
if (request.method() == kHttpGet) {
// Suppose URL query is only available for HTTP GET.
Url::SplitQuery(url.query(), &query);
}
std::string content;
bool ok = service->Handle(connection->request().method(),
sub_matches,
query,
connection->request().content(),
&content);
bool ok = service->Handle(request.method(), sub_matches, query,
request.content(), &content);
if (!ok) {
connection->SendResponse(HttpStatus::kBadRequest);
return;
@@ -47,7 +49,12 @@ void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) {
if (!content.empty()) {
connection->SetResponseContent(std::move(content), kAppJsonUtf8);
}
connection->SendResponse(HttpStatus::kOK);
if (request.method() == kHttpPost) {
connection->SendResponse(HttpStatus::kCreated);
} else {
connection->SendResponse(HttpStatus::kOK);
}
}
} // namespace webcc