better cookie setting support

This commit is contained in:
BENEDEK László 2023-12-05 23:29:52 +01:00
parent 21afe9ca13
commit b8b830332a
7 changed files with 60 additions and 4 deletions

17
inc/rum/http/cookie.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <string>
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

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <rum/http/cookie.h>
#include <rum/http/method.h> #include <rum/http/method.h>
#include <rum/http/request.h> #include <rum/http/request.h>
#include <rum/http/response.h> #include <rum/http/response.h>

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <rum/http/cookie.h>
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
namespace Rum::HTTP { namespace Rum::HTTP {
class Response { class Response {
@ -18,7 +20,7 @@ class Response {
this->code = code; this->code = code;
} }
std::map<std::string, std::string> cookies; std::vector<Cookie> cookies;
std::map<std::string, std::string> headers; std::map<std::string, std::string> headers;
std::string body; std::string body;
}; };

23
src/rum/http/cookie.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <rum/http/cookie.h>
#include <sys/select.h>
#include <chrono>
#include <ctime>
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

View File

@ -11,14 +11,20 @@ Request::operator std::string() const {
for (auto header : headers) { for (auto header : headers) {
headers_string += "\t\t'" + header.first + "': '" + header.second + "'\n"; 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( return std::vformat(
"Request{{\n" "Request{{\n"
"\tmethod: {}\n" "\tmethod: {}\n"
"\t{}\n" "\t{}\n"
"\theaders: \n{}" "\theaders: \n{}"
"\tcookies: \n{}"
"\tbody: '{}'\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) { std::ostream& operator<<(std::ostream& stream, const Request req) {

View File

@ -4,6 +4,7 @@
#include <format> #include <format>
#include <map> #include <map>
#include <stdexcept> #include <stdexcept>
#include <string>
namespace Rum::HTTP { namespace Rum::HTTP {
std::string to_string(int code) { std::string to_string(int code) {
@ -81,8 +82,8 @@ Response::~Response() {
for (auto header : headers) { for (auto header : headers) {
headers_string += header.first + ": " + header.second + "\r\n"; headers_string += header.first + ": " + header.second + "\r\n";
} }
for (auto cookie : cookies) { for (Cookie& cookie : cookies) {
headers_string += "Set-cookie: " + cookie.first + "=" + cookie.second + "\r\n"; headers_string += "Set-cookie: " + (std::string)cookie + "\r\n";
} }
std::string message = std::vformat( std::string message = std::vformat(

View File

@ -35,6 +35,12 @@ int main(int argc, char** argv) {
resp.body = "<h1>asd</h1><pre>" + (std::string)req + "</pre>"; resp.body = "<h1>asd</h1><pre>" + (std::string)req + "</pre>";
}); });
server->add_path<Rum::HTTP::GET>("/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 = "<h1>Cookie</h1><pre>" + (std::string)req + "</pre>";
});
server->add_path<Rum::HTTP::GET>("/.*", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) { server->add_path<Rum::HTTP::GET>("/.*", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) {
std::cout << "request accepted" << std::endl; std::cout << "request accepted" << std::endl;
resp.body = "<h1>Hello World</h1><pre>" + (std::string)req + "</pre>"; resp.body = "<h1>Hello World</h1><pre>" + (std::string)req + "</pre>";