Initial setup
This commit is contained in:
parent
3a0fa57a06
commit
68e795f0e6
17 changed files with 466 additions and 8 deletions
9
response/CMakeLists.txt
Normal file
9
response/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
set(HEADERS
|
||||
response.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
response.cpp
|
||||
)
|
||||
|
||||
target_sources(pica PRIVATE ${SOURCES})
|
42
response/response.cpp
Normal file
42
response/response.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#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;
|
||||
}
|
34
response/response.h
Normal file
34
response/response.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
|
||||
#include "request/request.h"
|
||||
#include "stream/ostream.h"
|
||||
|
||||
class Response {
|
||||
public:
|
||||
enum class Status {
|
||||
ok,
|
||||
notFound,
|
||||
methodNotAllowed,
|
||||
internalError,
|
||||
__size
|
||||
};
|
||||
|
||||
enum class ContentType {
|
||||
text,
|
||||
__size
|
||||
};
|
||||
Response();
|
||||
Response(Status status);
|
||||
|
||||
void replyTo(Request& request) const;
|
||||
void setBody(const std::string& body);
|
||||
|
||||
private:
|
||||
Status status;
|
||||
ContentType type;
|
||||
std::string body;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue