Rework the prepare of response status line
parent
22b783ce97
commit
7af95778f5
@ -1,71 +1,52 @@
|
|||||||
#include "webcc/response.h"
|
#include "webcc/response.h"
|
||||||
|
|
||||||
#include "webcc/utility.h"
|
|
||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
namespace status_strings {
|
void Response::Prepare() {
|
||||||
|
PrepareStatusLine();
|
||||||
const std::string OK = "HTTP/1.1 200 OK";
|
|
||||||
const std::string CREATED = "HTTP/1.1 201 Created";
|
|
||||||
const std::string ACCEPTED = "HTTP/1.1 202 Accepted";
|
|
||||||
const std::string NO_CONTENT = "HTTP/1.1 204 No Content";
|
|
||||||
const std::string NOT_MODIFIED = "HTTP/1.1 304 Not Modified";
|
|
||||||
const std::string BAD_REQUEST = "HTTP/1.1 400 Bad Request";
|
|
||||||
const std::string NOT_FOUND = "HTTP/1.1 404 Not Found";
|
|
||||||
const std::string INTERNAL_SERVER_ERROR =
|
|
||||||
"HTTP/1.1 500 Internal Server Error";
|
|
||||||
const std::string NOT_IMPLEMENTED = "HTTP/1.1 501 Not Implemented";
|
|
||||||
const std::string SERVICE_UNAVAILABLE = "HTTP/1.1 503 Service Unavailable";
|
|
||||||
|
|
||||||
const std::string& ToString(int status) {
|
|
||||||
switch (status) {
|
|
||||||
case Status::kOK:
|
|
||||||
return OK;
|
|
||||||
|
|
||||||
case Status::kCreated:
|
|
||||||
return CREATED;
|
|
||||||
|
|
||||||
case Status::kAccepted:
|
|
||||||
return ACCEPTED;
|
|
||||||
|
|
||||||
case Status::kNoContent:
|
|
||||||
return NO_CONTENT;
|
|
||||||
|
|
||||||
case Status::kNotModified:
|
|
||||||
return NOT_MODIFIED;
|
|
||||||
|
|
||||||
case Status::kBadRequest:
|
|
||||||
return BAD_REQUEST;
|
|
||||||
|
|
||||||
case Status::kNotFound:
|
|
||||||
return NOT_FOUND;
|
|
||||||
|
|
||||||
case Status::kInternalServerError:
|
|
||||||
return INTERNAL_SERVER_ERROR;
|
|
||||||
|
|
||||||
case Status::kNotImplemented:
|
SetHeader(headers::kServer, UserAgent());
|
||||||
return NOT_IMPLEMENTED;
|
SetHeader(headers::kDate, GetTimestamp());
|
||||||
|
|
||||||
case Status::kServiceUnavailable:
|
Message::Prepare();
|
||||||
return SERVICE_UNAVAILABLE;
|
}
|
||||||
|
|
||||||
default:
|
static const std::pair<int, const char*> kTable[] = {
|
||||||
return NOT_IMPLEMENTED;
|
{ Status::kOK, "OK" },
|
||||||
|
{ Status::kCreated, "Created" },
|
||||||
|
{ Status::kAccepted, "Accepted" },
|
||||||
|
{ Status::kNoContent, "No Content" },
|
||||||
|
{ Status::kNotModified, "Not Modified" },
|
||||||
|
{ Status::kBadRequest, "Bad Request" },
|
||||||
|
{ Status::kNotFound, "Not Found" },
|
||||||
|
{ Status::kInternalServerError, "Internal Server Error" },
|
||||||
|
{ Status::kNotImplemented, "Not Implemented" },
|
||||||
|
{ Status::kServiceUnavailable, "Service Unavailable" },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char* GetReason(int status) {
|
||||||
|
for (auto& pair : kTable) {
|
||||||
|
if (pair.first == status) {
|
||||||
|
return pair.second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace status_strings
|
void Response::PrepareStatusLine() {
|
||||||
|
if (!start_line_.empty()) {
|
||||||
void Response::Prepare() {
|
return;
|
||||||
if (start_line_.empty()) {
|
|
||||||
start_line_ = status_strings::ToString(status_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetHeader(headers::kServer, UserAgent());
|
start_line_ = "HTTP/1.1 ";
|
||||||
SetHeader(headers::kDate, GetTimestamp());
|
start_line_ += std::to_string(status_);
|
||||||
|
start_line_ += " ";
|
||||||
|
|
||||||
Message::Prepare();
|
if (reason_.empty()) {
|
||||||
|
start_line_ += GetReason(status_);
|
||||||
|
} else {
|
||||||
|
start_line_ += reason_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
Loading…
Reference in New Issue