From b8b830332af047d21fec260bb44ce46404e726d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Tue, 5 Dec 2023 23:29:52 +0100 Subject: [PATCH] better cookie setting support --- inc/rum/http/cookie.h | 17 +++++++++++++++++ inc/rum/http/http.h | 1 + inc/rum/http/response.h | 4 +++- src/rum/http/cookie.cpp | 23 +++++++++++++++++++++++ src/rum/http/request.cpp | 8 +++++++- src/rum/http/response.cpp | 5 +++-- src/server.cpp | 6 ++++++ 7 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 inc/rum/http/cookie.h create mode 100644 src/rum/http/cookie.cpp diff --git a/inc/rum/http/cookie.h b/inc/rum/http/cookie.h new file mode 100644 index 0000000..97e8964 --- /dev/null +++ b/inc/rum/http/cookie.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace Rum::HTTP { + +class Cookie { + private: + std::string cookie; + + public: + Cookie(const std::string& name, const std::string& value, const std::string& path, unsigned int lifetime); + + operator std::string() const; +}; + +} // namespace Rum::HTTP \ No newline at end of file diff --git a/inc/rum/http/http.h b/inc/rum/http/http.h index d10d4cc..e3b6b77 100644 --- a/inc/rum/http/http.h +++ b/inc/rum/http/http.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/inc/rum/http/response.h b/inc/rum/http/response.h index b44a916..6800a61 100644 --- a/inc/rum/http/response.h +++ b/inc/rum/http/response.h @@ -1,7 +1,9 @@ #pragma once +#include #include #include +#include namespace Rum::HTTP { class Response { @@ -18,7 +20,7 @@ class Response { this->code = code; } - std::map cookies; + std::vector cookies; std::map headers; std::string body; }; diff --git a/src/rum/http/cookie.cpp b/src/rum/http/cookie.cpp new file mode 100644 index 0000000..d1f09cb --- /dev/null +++ b/src/rum/http/cookie.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +namespace Rum::HTTP { + +Cookie::Cookie(const std::string& name, const std::string& value, const std::string& path, unsigned int lifetime) + : cookie(name + "=" + value + "; path=" + path + "; expires=") { + auto currentTime = std::chrono::system_clock::now(); + auto newTime = currentTime + std::chrono::seconds(lifetime); + std::time_t time = std::chrono::system_clock::to_time_t(newTime); + + std::tm* tmInfo = std::gmtime(&time); + char buffer[80]; + std::strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", tmInfo); + cookie += std::string(buffer); +} + +Cookie::operator std::string() const { + return cookie; +} +}; // namespace Rum::HTTP diff --git a/src/rum/http/request.cpp b/src/rum/http/request.cpp index 11d4382..cb19e6a 100644 --- a/src/rum/http/request.cpp +++ b/src/rum/http/request.cpp @@ -11,14 +11,20 @@ Request::operator std::string() const { for (auto header : headers) { headers_string += "\t\t'" + header.first + "': '" + header.second + "'\n"; } + + std::string cookie_string; + for (auto cookie : cookies) { + cookie_string += "\t\t'" + cookie.first + "': '" + cookie.second + "'\n"; + } return std::vformat( "Request{{\n" "\tmethod: {}\n" "\t{}\n" "\theaders: \n{}" + "\tcookies: \n{}" "\tbody: '{}'\n" "}}", - std::make_format_args(to_string(method), (std::string)uri, headers_string, body)); + std::make_format_args(to_string(method), (std::string)uri, headers_string, cookie_string, body)); } std::ostream& operator<<(std::ostream& stream, const Request req) { diff --git a/src/rum/http/response.cpp b/src/rum/http/response.cpp index 71a2264..7cc56b9 100644 --- a/src/rum/http/response.cpp +++ b/src/rum/http/response.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace Rum::HTTP { std::string to_string(int code) { @@ -81,8 +82,8 @@ Response::~Response() { for (auto header : headers) { headers_string += header.first + ": " + header.second + "\r\n"; } - for (auto cookie : cookies) { - headers_string += "Set-cookie: " + cookie.first + "=" + cookie.second + "\r\n"; + for (Cookie& cookie : cookies) { + headers_string += "Set-cookie: " + (std::string)cookie + "\r\n"; } std::string message = std::vformat( diff --git a/src/server.cpp b/src/server.cpp index 3dd336a..7c4eacd 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -35,6 +35,12 @@ int main(int argc, char** argv) { resp.body = "

asd

" + (std::string)req + "
"; }); + server->add_path("/cookie", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) { + std::cout << "request accepted" << std::endl; + resp.cookies.push_back(Rum::HTTP::Cookie("testCookie", "valueOfCookie", "/", 60*60)); + resp.body = "

Cookie

" + (std::string)req + "
"; + }); + server->add_path("/.*", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) { std::cout << "request accepted" << std::endl; resp.body = "

Hello World

" + (std::string)req + "
";