diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cfe699..dd82f76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/src/RequestHandlers/BasicRequestHandler.cpp b/src/RequestHandlers/BasicRequestHandler.cpp index 9aa7349..c621bb4 100644 --- a/src/RequestHandlers/BasicRequestHandler.cpp +++ b/src/RequestHandlers/BasicRequestHandler.cpp @@ -1,6 +1,12 @@ #include "BasicRequestHandler.h" #include +#include + +#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 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"); diff --git a/src/content_type.h b/src/content_type.h new file mode 100644 index 0000000..4da6a77 --- /dev/null +++ b/src/content_type.h @@ -0,0 +1,27 @@ +#include + +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; +}; +}