Server sets userlang cookie as global and permanent

Without specifying the "Path" attribute of the cookie in the "Set-Cookie" header
we end up with multiple instances of the cookie for different URLs. We
want a single "global" cookie for kiwix-serve. Besides we want it to be
"permanent" rather than a session cookie, hence the large (1-year-long)
TTL value for the "Max-Age" attribute.
This commit is contained in:
Veloman Yunkan 2023-01-10 20:26:54 +04:00 committed by Matthieu Gautier
parent fcb97c3c06
commit e35e7585e0
4 changed files with 21 additions and 12 deletions

View File

@ -68,12 +68,13 @@ fullURL2LocalURL(const std::string& full_url, const std::string& rootLocation)
} // unnamed namespace } // unnamed namespace
RequestContext::RequestContext(struct MHD_Connection* connection, RequestContext::RequestContext(struct MHD_Connection* connection,
std::string rootLocation, std::string _rootLocation,
const std::string& _url, const std::string& _url,
const std::string& _method, const std::string& _method,
const std::string& version) : const std::string& version) :
rootLocation(_rootLocation),
full_url(_url), full_url(_url),
url(fullURL2LocalURL(_url, rootLocation)), url(fullURL2LocalURL(_url, _rootLocation)),
method(str2RequestMethod(_method)), method(str2RequestMethod(_method)),
version(version), version(version),
requestIndex(s_requestIndex++), requestIndex(s_requestIndex++),
@ -193,6 +194,10 @@ std::string RequestContext::get_full_url() const {
return full_url; return full_url;
} }
std::string RequestContext::get_root_path() const {
return rootLocation.empty() ? "/" : rootLocation;
}
bool RequestContext::is_valid_url() const { bool RequestContext::is_valid_url() const {
return !url.empty(); return !url.empty();
} }

View File

@ -91,6 +91,7 @@ class RequestContext {
std::string get_url() const; std::string get_url() const;
std::string get_url_part(int part) const; std::string get_url_part(int part) const;
std::string get_full_url() const; std::string get_full_url() const;
std::string get_root_path() const;
std::string get_query() const { return queryString; } std::string get_query() const { return queryString; }
@ -136,6 +137,7 @@ class RequestContext {
}; };
private: // data private: // data
std::string rootLocation;
std::string full_url; std::string full_url;
std::string url; std::string url;
RequestMethod method; RequestMethod method;

View File

@ -388,7 +388,9 @@ MHD_Result Response::send(const RequestContext& request, MHD_Connection* connect
} }
if ( ! request.user_language_comes_from_cookie() ) { if ( ! request.user_language_comes_from_cookie() ) {
const std::string cookie = "userlang=" + request.get_user_language(); const std::string cookie = "userlang=" + request.get_user_language()
+ ";Path=" + request.get_root_path()
+ ";Max-Age=31536000";
MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie.c_str()); MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie.c_str());
} }

View File

@ -1007,7 +1007,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article", /*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1015,7 +1015,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en", /*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1023,7 +1023,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=test", /*url*/ "/ROOT/content/zimfile/invalid-article?userlang=test",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=test", /*Response Set-Cookie:*/ "userlang=test;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive" /* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
}, },
{ {
@ -1031,7 +1031,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article", /*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "*", /*Accept-Language:*/ "*",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1039,7 +1039,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article", /*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "test", /*Accept-Language:*/ "test",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=test", /*Response Set-Cookie:*/ "userlang=test;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive" /* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
}, },
{ {
@ -1087,7 +1087,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en", /*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "test", /*Accept-Language:*/ "test",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1095,7 +1095,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en", /*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ "userlang=test", /*Request Cookie:*/ "userlang=test",
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1113,7 +1113,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article", /*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "test;q=0.9, en;q=0.2", /*Accept-Language:*/ "test;q=0.9, en;q=0.2",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=test", /*Response Set-Cookie:*/ "userlang=test;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive" /* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
}, },
{ {
@ -1123,7 +1123,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article", /*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "test;q=0.2, en;q=0.9", /*Accept-Language:*/ "test;q=0.2, en;q=0.9",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en", /*Response Set-Cookie:*/ "userlang=en;Path=/ROOT;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
}; };