Skip to content

Commit

Permalink
Merge pull request #1185 from libcpr/fix/curl_8.12_cookie_expires
Browse files Browse the repository at this point in the history
Cookie expires date is now only 100 days in the future
  • Loading branch information
COM8 committed Feb 23, 2025
1 parent 22ce9b1 commit dd967cb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
16 changes: 8 additions & 8 deletions test/get_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ TEST(CookiesTests, BasicCookiesTest) {
cpr::Cookies res_cookies{response.cookies};
std::string expected_text{"Basic Cookies"};
cpr::Cookies expectedCookies{
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
EXPECT_EQ(expected_text, response.text);
EXPECT_EQ(url, response.url);
Expand All @@ -141,8 +141,8 @@ TEST(CookiesTests, EmptyCookieTest) {
cpr::Cookies res_cookies{response.cookies};
std::string expected_text{"Empty Cookies"};
cpr::Cookies expectedCookies{
{"SID", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
EXPECT_EQ(url, response.url);
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
Expand All @@ -163,8 +163,8 @@ TEST(CookiesTests, EmptyCookieTest) {
TEST(CookiesTests, ClientSetCookiesTest) {
Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
Cookies cookies{
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
Response response = cpr::Get(url, cookies);
std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
Expand All @@ -178,8 +178,8 @@ TEST(CookiesTests, ClientSetCookiesTest) {
TEST(CookiesTests, UnencodedCookiesTest) {
Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
Cookies cookies{
{"SID", "31d4d %$ 96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "31d4d %$ 96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
cookies.encode = false;
Response response = cpr::Get(url, cookies);
Expand Down
4 changes: 2 additions & 2 deletions test/head_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ TEST(HeadTests, CookieHeadTest) {
Url url{server->GetBaseUrl() + "/basic_cookies.html"};
Response response = cpr::Head(url);
cpr::Cookies expectedCookies{
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
cpr::Cookies res_cookies{response.cookies};
EXPECT_EQ(std::string{}, response.text);
Expand Down
26 changes: 23 additions & 3 deletions test/httpServer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "httpServer.hpp"
#include <array>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>
#include <system_error>
#include <thread>
Expand Down Expand Up @@ -142,8 +145,25 @@ void HttpServer::OnRequestLowSpeedBytes(mg_connection* conn, mg_http_message* /*
timer_arg);
}

std::string HttpServer::GetCookieExpiresIn100HoursString() {
static const std::chrono::system_clock::time_point expires = GetCookieExpiresIn100HoursTimePoint();
const std::time_t timeT = std::chrono::system_clock::to_time_t(expires);
const std::tm* utcTimeT = std::gmtime(&timeT); // NOLINT (concurrency-mt-unsafe) not relevant here

std::stringstream ss;
ss << std::put_time(utcTimeT, "%a, %d %b %Y %T GMT");

return ss.str();
}

std::chrono::system_clock::time_point HttpServer::GetCookieExpiresIn100HoursTimePoint() {
// Cookie timepoints have a maximum resolution of seconds so floor it to that.
static const std::chrono::system_clock::time_point expires = std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now() + std::chrono::hours(100));
return expires;
}

void HttpServer::OnRequestBasicCookies(mg_connection* conn, mg_http_message* /*msg*/) {
const std::string expires = "Wed, 30 Sep 2093 03:18:00 GMT";
const std::string expires = GetCookieExpiresIn100HoursString();

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

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

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

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

mg_str* request_cookies{nullptr};
std::string cookie_str;
Expand Down
13 changes: 11 additions & 2 deletions test/httpServer.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#ifndef CPR_TEST_HTTP_SERVER_H
#define CPR_TEST_HTTP_SERVER_H

#include <memory>
#include <string>

#include "abstractServer.hpp"
#include "cpr/cpr.h"
#include "mongoose.h"

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

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

/**
* Returns the current date and time + 100 hours from when this function was invoked for the first time.
**/
static std::chrono::system_clock::time_point GetCookieExpiresIn100HoursTimePoint();

/**
* 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.
* For example: Wed, 30 Sep 2093 03:18:00 GMT
**/
static std::string GetCookieExpiresIn100HoursString();

private:
static void OnRequestHello(mg_connection* conn, mg_http_message* msg);
static void OnRequestRoot(mg_connection* conn, mg_http_message* msg);
Expand Down
4 changes: 2 additions & 2 deletions test/session_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,8 @@ TEST(CookiesTests, BasicCookiesTest) {
Cookies res_cookies{response.cookies};
std::string expected_text{"Basic Cookies"};
cpr::Cookies expectedCookies{
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::time_point{} + std::chrono::seconds(3905119080)},
{"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
{"lang", "en-US", "127.0.0.1", false, "/", true, HttpServer::GetCookieExpiresIn100HoursTimePoint()},
};
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
EXPECT_EQ(200, response.status_code);
Expand Down

0 comments on commit dd967cb

Please sign in to comment.