Skip to content

Commit be949aa

Browse files
committed
use custom cookie class
1 parent 29b29fa commit be949aa

9 files changed

Lines changed: 108 additions & 6 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1616
option(USE_INSTALL_RESOURCE_PATH "Set resource path to install location" OFF)
1717

1818
add_executable(server
19+
./src/core/cookie.cpp
1920
./src/core/request.cpp
2021
./src/core/response.cpp
2122
./src/core/router.cpp

‎example/main.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ void helloWorldHandler(Request &req, Response &res) {
3838
resp["submodules"][2]["location"] = "lib/json";
3939
resp["submodules"][2]["name"] = "JSON";
4040

41+
Cookie *cookie = new Cookie();
42+
cookie->name = "hello";
43+
cookie->value = "world";
44+
res.setCookie(cookie);
45+
4146
res.json(resp).status(200);
4247
return;
4348
}

‎include/expresso/core/cookie.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include <expresso/utils/sterlize.h>
6+
7+
namespace expresso {
8+
9+
namespace core {
10+
11+
class Cookie {
12+
public:
13+
bool secure;
14+
bool httpOnly;
15+
16+
std::string name;
17+
std::string value;
18+
std::string domain;
19+
std::string path;
20+
std::string expires;
21+
std::string maxAge;
22+
std::string sameSite;
23+
24+
Cookie();
25+
~Cookie();
26+
27+
std::string serialize();
28+
};
29+
30+
} // namespace core
31+
32+
} // namespace expresso

‎include/expresso/core/request.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Request {
2929
std::string tempPath;
3030

3131
json::object body;
32-
std::map<std::string, std::string> cookies;
32+
std::vector<Cookie *> cookies;
3333
std::map<std::string, std::string> headers;
3434
std::map<std::string, std::string> params;
3535
std::map<std::string, std::string> queries;

‎include/expresso/core/response.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <json/object.h>
99
#include <logger/log.h>
1010

11+
#include <expresso/core/cookie.h>
1112
#include <expresso/core/status_code.h>
1213
#include <expresso/sys.h>
1314
#include <expresso/utils/common.h>
@@ -26,6 +27,7 @@ class Response {
2627

2728
std::string message;
2829

30+
std::vector<Cookie *> cookies;
2931
std::map<std::string, std::string> headers;
3032

3133
void sendToClient();
@@ -36,6 +38,7 @@ class Response {
3638
~Response();
3739

3840
void set(std::string headerName, std::string headerValue);
41+
void setCookie(Cookie *cookie);
3942
std::string get(std::string headerName);
4043

4144
Response status(int code);

‎src/core/cookie.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <expresso/core/cookie.h>
2+
3+
expresso::core::Cookie::Cookie() {
4+
this->secure = false;
5+
this->httpOnly = false;
6+
this->name = "";
7+
this->value = "";
8+
this->domain = "";
9+
this->path = "";
10+
this->expires = "";
11+
this->maxAge = "";
12+
this->sameSite = "";
13+
14+
return;
15+
}
16+
17+
expresso::core::Cookie::~Cookie() { return; }
18+
19+
std::string expresso::core::Cookie::serialize() {
20+
std::string cookieString = this->name + "=" + utils::urlEncode(this->value);
21+
if (!this->domain.empty()) {
22+
cookieString += "; Domain=" + this->domain;
23+
}
24+
if (!this->path.empty()) {
25+
cookieString += "; Path=" + this->path;
26+
}
27+
if (!this->expires.empty()) {
28+
cookieString += "; Expires=" + this->expires;
29+
}
30+
if (!this->maxAge.empty()) {
31+
cookieString += "; Max-Age=" + this->maxAge;
32+
}
33+
if (!this->sameSite.empty()) {
34+
cookieString += "; SameSite=" + this->sameSite;
35+
}
36+
if (this->secure) {
37+
cookieString += "; Secure";
38+
}
39+
if (this->httpOnly) {
40+
cookieString += "; HttpOnly";
41+
}
42+
43+
return cookieString;
44+
}

‎src/core/request.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ void expresso::core::Request::print() {
2121
logger::info(this->body.dumps(2, 2));
2222

2323
logger::info(" cookies: ");
24-
for (const std::pair<std::string, std::string> &cookie : this->cookies) {
25-
logger::info(" " + cookie.first + ": " + cookie.second);
24+
for (expresso::core::Cookie *cookie : this->cookies) {
25+
logger::info(" " + cookie->serialize());
2626
}
2727

2828
logger::info(" headers: ");

‎src/core/response.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ void expresso::core::Response::set(std::string headerName,
2323
return;
2424
}
2525

26+
void expresso::core::Response::setCookie(Cookie *cookie) {
27+
this->cookies.push_back(cookie);
28+
29+
return;
30+
}
31+
2632
std::string expresso::core::Response::get(std::string headerName) {
2733
if (this->headers.find(headerName) != this->headers.end()) {
2834
return this->headers[headerName];
@@ -86,6 +92,9 @@ void expresso::core::Response::sendFile(std::string path) {
8692
for (std::pair<const std::string, std::string> it : this->headers) {
8793
header += it.first + ": " + it.second + "\r\n";
8894
}
95+
for (Cookie *cookie : this->cookies) {
96+
header += "Set-Cookie: " + cookie->serialize() + "\r\n";
97+
}
8998
header += "\r\n";
9099
sys::send(this->socket, header.c_str(), header.length(), 0);
91100

@@ -155,6 +164,9 @@ void expresso::core::Response::sendToClient() {
155164
for (std::pair<const std::string, std::string> it : this->headers) {
156165
header += it.first + ": " + it.second + "\r\n";
157166
}
167+
for (Cookie *cookie : this->cookies) {
168+
header += "Set-Cookie: " + cookie->serialize() + "\r\n";
169+
}
158170
header += "\r\n";
159171

160172
sys::send(this->socket, header.c_str(), header.length(), 0);

‎src/middleware/cookie_parser.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ bool expresso::middleware::CookieParser::use(expresso::core::Request &req,
1212
}
1313

1414
std::vector<std::string> cookies = utils::split(cookieString, ";");
15-
for (std::string &cookie : cookies) {
16-
std::vector<std::string> cookiePair = utils::split(cookie, "=");
17-
req.cookies[utils::trim(cookiePair[0])] = utils::trim(cookiePair[1]);
15+
std::vector<std::string> cookiePair;
16+
for (std::string &cookieStr : cookies) {
17+
expresso::core::Cookie *cookie = new expresso::core::Cookie();
18+
cookiePair = utils::split(cookieStr, "=");
19+
cookie->name = utils::trim(cookiePair[0]);
20+
cookie->value = utils::trim(cookiePair[1]);
21+
cookie->value = utils::urlDecode(cookie->value);
22+
req.cookies.push_back(cookie);
1823
}
1924

2025
return true;

0 commit comments

Comments
 (0)