60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#include "response.h"
|
|
|
|
#include "root.h"
|
|
#include "notfound.h"
|
|
#include "badge.h"
|
|
|
|
const char* Response::status200Header("Status: 200 OK");
|
|
const char* Response::status404Header("Status: 404 Not Found");
|
|
const char* Response::status405Header("Status: 405 Method Not Allowed");
|
|
const char* Response::status500Header("Status: 500 Internal Error");
|
|
|
|
const char* Response::pathDelimiter("/");
|
|
const char* Response::newLine("\n");
|
|
const char* Response::headerTerminator("\n\n");
|
|
|
|
const char* Response::contentTypeHTML("Content-type: text/html");
|
|
const char* Response::contentTypeSVG("Content-type: image/svg+xml;charset=utf-8");
|
|
|
|
constexpr const char* staticHop("static");
|
|
|
|
Response::Response(Type type):
|
|
type(type)
|
|
{}
|
|
|
|
Response::~Response() {}
|
|
|
|
Response* Response::create(const std::string_view& path) {
|
|
std::vector<std::string_view> split = splitPath(path);
|
|
|
|
if (path.size() == 0) {
|
|
return new Root();
|
|
}
|
|
|
|
const std::string_view& firstHop = split[0];
|
|
if (firstHop == staticHop) {
|
|
return new Badge(Badge::Kind::allStatic);
|
|
}
|
|
|
|
return new NotFound();
|
|
}
|
|
|
|
std::vector<std::string_view> Response::splitPath(std::string_view path) {
|
|
std::vector<std::string_view> output;
|
|
std::string_view::size_type first = 0;
|
|
|
|
while (first < path.size()) {
|
|
const std::string_view::size_type second = path.find_first_of(pathDelimiter, first);
|
|
|
|
if (first != second)
|
|
output.emplace_back(path.substr(first, second - first));
|
|
|
|
if (second == std::string_view::npos)
|
|
break;
|
|
|
|
first = second + 1;
|
|
}
|
|
|
|
return output;
|
|
}
|