Стандартизация req объекта

This commit is contained in:
Антон 2024-11-22 17:53:39 +03:00
parent aca5c81168
commit 1f00916d3a
3 changed files with 46 additions and 0 deletions

View File

@ -22,6 +22,7 @@ add_executable(application src/main.cpp
src/Session.cpp
src/Listener.h
src/Listener.cpp
src/content_type.h
src/RequestHandlers/BasicRequestHandler.cpp
src/RequestHandlers/BasicRequestHandler.h)
target_link_libraries(application PRIVATE Threads::Threads)

View File

@ -1,6 +1,12 @@
#include "BasicRequestHandler.h"
#include <iostream>
#include <string_view>
#include "../content_type.h"
using namespace std;
using namespace string_view_literals;
namespace uad
{
@ -48,6 +54,18 @@ http::message_generator HandleRequest(
return res;
};
if (req.target() == "/HelloWorld"sv)
{
http::response<http::string_body> res {};
res.body() = "{ \"Hello\": \"World\" }"sv;
res.content_length(res.body().size());
res.set(http::field::content_type, content_type::k_JSON);
res.prepare_payload();
res.keep_alive(req.keep_alive());
return res;
}
if (req.method() != http::verb::get &&
req.method() != http::verb::head)
return bad_request("Unknown HTTP-method");

27
src/content_type.h Normal file
View File

@ -0,0 +1,27 @@
#include <string_view>
namespace uad
{
using namespace std::string_view_literals;
struct content_type
{
content_type() = delete;
constexpr inline static std::string_view k_TextHtml = "text/html"sv;
constexpr inline static std::string_view k_TextCsv = "text/csv"sv;
constexpr inline static std::string_view k_TextPlain = "text/plain"sv;
constexpr inline static std::string_view k_JSON = "application/json"sv;
constexpr inline static std::string_view k_Audio = "audio/mpeg"sv;
constexpr inline static std::string_view k_AudioExample = "audio/example"sv;
constexpr inline static std::string_view k_FontWoff = "font/woff"sv;
constexpr inline static std::string_view k_FontTtf = "font/ttf"sv;
constexpr inline static std::string_view k_FontOtf = "font/otf"sv;
constexpr inline static std::string_view k_ImagePng = "image/png"sv;
constexpr inline static std::string_view k_ImageJpeg = "image/jpeg"sv;
constexpr inline static std::string_view k_ImageGif = "image/gif"sv;
constexpr inline static std::string_view k_Model = "model/vrml"sv;
constexpr inline static std::string_view k_VideoMp4 = "video/mp4"sv;
constexpr inline static std::string_view k_VideoWebm = "video/webm"sv;
constexpr inline static std::string_view k_Binary = "application/octet-stream"sv;
};
}