Skip to content

Commit dd967cb

Browse files
committed
Merge pull request #1185 from libcpr/fix/curl_8.12_cookie_expires
Cookie expires date is now only 100 days in the future
1 parent 22ce9b1 commit dd967cb

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

test/get_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ TEST(CookiesTests, BasicCookiesTest) {
116116
cpr::Cookies res_cookies{response.cookies};
117117
std::string expected_text{"Basic Cookies"};
118118
cpr::Cookies expectedCookies{
119-
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
120-
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
119+
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
120+
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
121121
};
122122
EXPECT_EQ(expected_text, response.text);
123123
EXPECT_EQ(url, response.url);
@@ -141,8 +141,8 @@ TEST(CookiesTests, EmptyCookieTest) {
141141
cpr::Cookies res_cookies{response.cookies};
142142
std::string expected_text{"Empty Cookies"};
143143
cpr::Cookies expectedCookies{
144-
{"SID", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
145-
{"lang", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
144+
{"SID", "", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
145+
{"lang", "", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
146146
};
147147
EXPECT_EQ(url, response.url);
148148
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
@@ -163,8 +163,8 @@ TEST(CookiesTests, EmptyCookieTest) {
163163
TEST(CookiesTests, ClientSetCookiesTest) {
164164
Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
165165
Cookies cookies{
166-
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
167-
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
166+
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
167+
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
168168
};
169169
Response response = cpr::Get(url, cookies);
170170
std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
@@ -178,8 +178,8 @@ TEST(CookiesTests, ClientSetCookiesTest) {
178178
TEST(CookiesTests, UnencodedCookiesTest) {
179179
Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
180180
Cookies cookies{
181-
{"SID", "31d4d %$ 96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
182-
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
181+
{"SID", "31d4d %$ 96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
182+
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
183183
};
184184
cookies.encode = false;
185185
Response response = cpr::Get(url, cookies);

test/head_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ TEST(HeadTests, CookieHeadTest) {
5555
Url url{server->GetBaseUrl() + "/basic_cookies.html"};
5656
Response response = cpr::Head(url);
5757
cpr::Cookies expectedCookies{
58-
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
59-
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
58+
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
59+
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
6060
};
6161
cpr::Cookies res_cookies{response.cookies};
6262
EXPECT_EQ(std::string{}, response.text);

test/httpServer.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "httpServer.hpp"
22
#include <array>
33
#include <chrono>
4+
#include <ctime>
5+
#include <iomanip>
6+
#include <sstream>
47
#include <string>
58
#include <system_error>
69
#include <thread>
@@ -142,8 +145,25 @@ void HttpServer::OnRequestLowSpeedBytes(mg_connection* conn, mg_http_message* /*
142145
timer_arg);
143146
}
144147

148+
std::string HttpServer::GetCookieExpiresIn100HoursString() {
149+
static const std::chrono::system_clock::time_point expires = GetCookieExpiresIn100HoursTimePoint();
150+
const std::time_t timeT = std::chrono::system_clock::to_time_t(expires);
151+
const std::tm* utcTimeT = std::gmtime(&timeT); // NOLINT (concurrency-mt-unsafe) not relevant here
152+
153+
std::stringstream ss;
154+
ss << std::put_time(utcTimeT, "%a, %d %b %Y %T GMT");
155+
156+
return ss.str();
157+
}
158+
159+
std::chrono::system_clock::time_point HttpServer::GetCookieExpiresIn100HoursTimePoint() {
160+
// Cookie timepoints have a maximum resolution of seconds so floor it to that.
161+
static const std::chrono::system_clock::time_point expires = std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now() + std::chrono::hours(100));
162+
return expires;
163+
}
164+
145165
void HttpServer::OnRequestBasicCookies(mg_connection* conn, mg_http_message* /*msg*/) {
146-
const std::string expires = "Wed, 30 Sep 2093 03:18:00 GMT";
166+
const std::string expires = GetCookieExpiresIn100HoursString();
147167

148168
std::string cookie1{"SID=31d4d96e407aad42; Expires=" + expires + "; Secure"};
149169
std::string cookie2{"lang=en-US; Expires=" + expires + "; Secure"};
@@ -160,7 +180,7 @@ void HttpServer::OnRequestBasicCookies(mg_connection* conn, mg_http_message* /*m
160180
}
161181

162182
void HttpServer::OnRequestEmptyCookies(mg_connection* conn, mg_http_message* /*msg*/) {
163-
const std::string expires = "Wed, 30 Sep 2093 03:18:00 GMT";
183+
const std::string expires = GetCookieExpiresIn100HoursString();
164184

165185
std::string cookie1{"SID=; Expires=" + expires + "; Secure"};
166186
std::string cookie2{"lang=; Expires=" + expires + "; Secure"};
@@ -189,7 +209,7 @@ void HttpServer::OnRequestCookiesReflect(mg_connection* conn, mg_http_message* m
189209
}
190210

191211
void HttpServer::OnRequestRedirectionWithChangingCookies(mg_connection* conn, mg_http_message* msg) {
192-
const std::string expires = "Wed, 30 Sep 2093 03:18:00 GMT";
212+
const std::string expires = GetCookieExpiresIn100HoursString();
193213

194214
mg_str* request_cookies{nullptr};
195215
std::string cookie_str;

test/httpServer.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#ifndef CPR_TEST_HTTP_SERVER_H
22
#define CPR_TEST_HTTP_SERVER_H
33

4-
#include <memory>
54
#include <string>
65

76
#include "abstractServer.hpp"
8-
#include "cpr/cpr.h"
97
#include "mongoose.h"
108

119
namespace cpr {
@@ -18,6 +16,17 @@ class HttpServer : public AbstractServer {
1816

1917
void OnRequest(mg_connection* conn, mg_http_message* msg) override;
2018

19+
/**
20+
* Returns the current date and time + 100 hours from when this function was invoked for the first time.
21+
**/
22+
static std::chrono::system_clock::time_point GetCookieExpiresIn100HoursTimePoint();
23+
24+
/**
25+
* Returns the current date and time + 100 hours from when this function (or better GetCookieExpiresIn100HoursTimePoint()) was invoked for the first time as cookies expires string.
26+
* For example: Wed, 30 Sep 2093 03:18:00 GMT
27+
**/
28+
static std::string GetCookieExpiresIn100HoursString();
29+
2130
private:
2231
static void OnRequestHello(mg_connection* conn, mg_http_message* msg);
2332
static void OnRequestRoot(mg_connection* conn, mg_http_message* msg);

test/session_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ TEST(CookiesTests, BasicCookiesTest) {
778778
Cookies res_cookies{response.cookies};
779779
std::string expected_text{"Basic Cookies"};
780780
cpr::Cookies expectedCookies{
781-
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
782-
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
781+
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
782+
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
783783
};
784784
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
785785
EXPECT_EQ(200, response.status_code);

0 commit comments

Comments
 (0)