Skip to content

Commit fdf5e97

Browse files
committed
Add error messages
1 parent fc7ee98 commit fdf5e97

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎coroio/http/httpd.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ namespace NNet
55

66
namespace {
77

8+
const std::unordered_map<int, std::string_view> kHttpReasons = {
9+
{200, "OK"}, {201, "Created"}, {204, "No Content"},
10+
{301, "Moved Permanently"}, {302, "Found"}, {304, "Not Modified"},
11+
{400, "Bad Request"}, {401, "Unauthorized"}, {403, "Forbidden"},
12+
{404, "Not Found"}, {405, "Method Not Allowed"}, {408, "Request Timeout"},
13+
{409, "Conflict"}, {413, "Payload Too Large"}, {422, "Unprocessable Entity"},
14+
{429, "Too Many Requests"},
15+
{500, "Internal Server Error"}, {501, "Not Implemented"},
16+
{502, "Bad Gateway"}, {503, "Service Unavailable"},
17+
};
18+
819
std::string UrlDecode(const std::string& str) {
920
std::string result;
1021
result.reserve(str.size());
@@ -309,7 +320,9 @@ TFuture<void> TResponse::SendHeaders() {
309320
}
310321
HeadersSent_ = true;
311322

312-
std::string headerStr = "HTTP/1.1 " + std::to_string(StatusCode_) + " OK\r\n";
323+
auto it = kHttpReasons.find(StatusCode_);
324+
std::string_view reason = it != kHttpReasons.end() ? it->second : "Unknown";
325+
std::string headerStr = "HTTP/1.1 " + std::to_string(StatusCode_) + " " + std::string(reason) + "\r\n";
313326
for (const auto& header : Headers_) {
314327
headerStr += header.first + ": " + header.second + "\r\n";
315328
}

‎coroio/http/httpd.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <sstream>
1111
#include <cstdio>
1212
#include <cstdlib>
13+
#include <string_view>
14+
#include <unordered_map>
1315

1416
namespace NNet
1517
{

0 commit comments

Comments
 (0)