better cookie setting support
This commit is contained in:
parent
21afe9ca13
commit
b8b830332a
17
inc/rum/http/cookie.h
Normal file
17
inc/rum/http/cookie.h
Normal 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
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <rum/http/cookie.h>
|
||||
#include <rum/http/method.h>
|
||||
#include <rum/http/request.h>
|
||||
#include <rum/http/response.h>
|
||||
|
@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <rum/http/cookie.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Rum::HTTP {
|
||||
class Response {
|
||||
@ -18,7 +20,7 @@ class Response {
|
||||
this->code = code;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> cookies;
|
||||
std::vector<Cookie> cookies;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::string body;
|
||||
};
|
||||
|
23
src/rum/http/cookie.cpp
Normal file
23
src/rum/http/cookie.cpp
Normal 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
|
@ -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) {
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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(
|
||||
|
@ -35,6 +35,12 @@ int main(int argc, char** argv) {
|
||||
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) {
|
||||
std::cout << "request accepted" << std::endl;
|
||||
resp.body = "<h1>Hello World</h1><pre>" + (std::string)req + "</pre>";
|
||||
|
Loading…
Reference in New Issue
Block a user