43 lines
1.0 KiB
C++
43 lines
1.0 KiB
C++
|
#include "response.h"
|
||
|
|
||
|
constexpr std::array<std::string_view, static_cast<uint8_t>(Response::Status::__size)> statusCodes = {
|
||
|
"Status: 200 OK",
|
||
|
"Status: 404 Not Found",
|
||
|
"Status: 405 Method Not Allowed",
|
||
|
"Status: 500 Internal Error"
|
||
|
};
|
||
|
|
||
|
constexpr std::array<std::string_view, static_cast<uint8_t>(Response::ContentType::__size)> contentTypes = {
|
||
|
"Content-type: text/html"
|
||
|
};
|
||
|
|
||
|
Response::Response():
|
||
|
status(Status::ok),
|
||
|
type(ContentType::text),
|
||
|
body()
|
||
|
{}
|
||
|
|
||
|
Response::Response(Status status):
|
||
|
status(status),
|
||
|
type(ContentType::text),
|
||
|
body()
|
||
|
{}
|
||
|
|
||
|
void Response::replyTo(Request& request) const {
|
||
|
// OStream out = status == Status::ok ?
|
||
|
// request.getOutputStream() :
|
||
|
// request.getErrorStream();
|
||
|
OStream out = request.getOutputStream();
|
||
|
|
||
|
out << statusCodes[static_cast<uint8_t>(status)];
|
||
|
if (!body.empty())
|
||
|
out << '\n'
|
||
|
<< contentTypes[static_cast<uint8_t>(type)]
|
||
|
<< '\n' << '\n'
|
||
|
<< body;
|
||
|
}
|
||
|
|
||
|
void Response::setBody(const std::string& body) {
|
||
|
Response::body = body;
|
||
|
}
|