RIP userlang cookie

This commit drops the usage of the userlang cookie in the backend but
not in the frontend. UI language control should be broken at this point
and will be fixed in the next few commits.
This commit is contained in:
Veloman Yunkan 2023-09-09 18:01:14 +04:00
parent e7e8275a31
commit b5b98e7a61
4 changed files with 3 additions and 85 deletions

View File

@ -202,21 +202,12 @@ std::string RequestContext::get_user_language() const
return userlang.lang; return userlang.lang;
} }
bool RequestContext::user_language_comes_from_cookie() const
{
return userlang.selectedBy == UserLanguage::SelectorKind::COOKIE;
}
RequestContext::UserLanguage RequestContext::determine_user_language() const RequestContext::UserLanguage RequestContext::determine_user_language() const
{ {
try { try {
return {UserLanguage::SelectorKind::QUERY_PARAM, get_argument("userlang")}; return {UserLanguage::SelectorKind::QUERY_PARAM, get_argument("userlang")};
} catch(const std::out_of_range&) {} } catch(const std::out_of_range&) {}
try {
return {UserLanguage::SelectorKind::COOKIE, cookies.at("userlang")};
} catch(const std::out_of_range&) {}
try { try {
const std::string acceptLanguage = get_header("Accept-Language"); const std::string acceptLanguage = get_header("Accept-Language");
const auto userLangPrefs = parseUserLanguagePreferences(acceptLanguage); const auto userLangPrefs = parseUserLanguagePreferences(acceptLanguage);

View File

@ -119,15 +119,12 @@ class RequestContext {
std::string get_user_language() const; std::string get_user_language() const;
std::string get_requested_format() const; std::string get_requested_format() const;
bool user_language_comes_from_cookie() const;
private: // types private: // types
struct UserLanguage struct UserLanguage
{ {
enum SelectorKind enum SelectorKind
{ {
QUERY_PARAM, QUERY_PARAM,
COOKIE,
ACCEPT_LANGUAGE_HEADER, ACCEPT_LANGUAGE_HEADER,
DEFAULT DEFAULT
}; };

View File

@ -387,13 +387,6 @@ MHD_Result Response::send(const RequestContext& request, MHD_Connection* connect
MHD_add_response_header(response, p.first.c_str(), p.second.c_str()); MHD_add_response_header(response, p.first.c_str(), p.second.c_str());
} }
if ( ! request.user_language_comes_from_cookie() ) {
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());
}
if (m_returnCode == MHD_HTTP_OK && m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT) if (m_returnCode == MHD_HTTP_OK && m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT)
m_returnCode = MHD_HTTP_PARTIAL_CONTENT; m_returnCode = MHD_HTTP_PARTIAL_CONTENT;

View File

@ -1130,7 +1130,6 @@ TEST_F(ServerTest, UserLanguageControl)
const std::string url; const std::string url;
const std::string acceptLanguageHeader; const std::string acceptLanguageHeader;
const char* const requestCookie; // Cookie: header of the request const char* const requestCookie; // Cookie: header of the request
const char* const responseSetCookie; // Set-Cookie: header of the response
const std::string expectedH1; const std::string expectedH1;
operator TestContext() const operator TestContext() const
@ -1157,7 +1156,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1165,7 +1163,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=en", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1173,7 +1170,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=test", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=test",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=test;Path=/ROOT%23%3F;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"
}, },
{ {
@ -1181,7 +1177,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "*", /*Accept-Language:*/ "*",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=en;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1189,71 +1184,20 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "test", /*Accept-Language:*/ "test",
/*Request Cookie:*/ NO_COOKIE, /*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ "userlang=test;Path=/ROOT%23%3F;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"
}, },
{ {
"userlang cookie is respected", "userlang cookie is ignored",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "", /*Accept-Language:*/ "",
/*Request Cookie:*/ "userlang=test", /*Request Cookie:*/ "userlang=test",
/*Response Set-Cookie:*/ NO_COOKIE, /* expected <h1> */ "Not Found"
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
"userlang cookie is correctly parsed",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "",
/*Request Cookie:*/ "anothercookie=123; userlang=test",
/*Response Set-Cookie:*/ NO_COOKIE,
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
"userlang cookie is correctly parsed",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "",
/*Request Cookie:*/ "userlang=test; anothercookie=abc",
/*Response Set-Cookie:*/ NO_COOKIE,
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
"userlang cookie is correctly parsed",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "",
/*Request Cookie:*/ "cookie1=abc; userlang=test; cookie2=xyz",
/*Response Set-Cookie:*/ NO_COOKIE,
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
"Multiple userlang cookies are not a problem",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "",
/*Request Cookie:*/ "cookie1=abc; userlang=en; userlang=test; cookie2=xyz",
/*Response Set-Cookie:*/ NO_COOKIE,
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
}, },
{ {
"userlang query parameter takes precedence over Accept-Language", "userlang query parameter takes precedence over Accept-Language",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=en", /*url*/ "/ROOT%23%3F/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;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found"
},
{
"userlang query parameter takes precedence over its cookie counterpart",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "",
/*Request Cookie:*/ "userlang=test",
/*Response Set-Cookie:*/ "userlang=en;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found"
},
{
"userlang in cookies takes precedence over Accept-Language",
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article",
/*Accept-Language:*/ "test",
/*Request Cookie:*/ "userlang=en",
/*Response Set-Cookie:*/ NO_COOKIE,
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
{ {
@ -1263,7 +1207,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/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;Path=/ROOT%23%3F;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"
}, },
{ {
@ -1273,7 +1216,6 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT%23%3F/content/zimfile/invalid-article", /*url*/ "/ROOT%23%3F/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;Path=/ROOT%23%3F;Max-Age=31536000",
/* expected <h1> */ "Not Found" /* expected <h1> */ "Not Found"
}, },
}; };
@ -1289,12 +1231,7 @@ TEST_F(ServerTest, UserLanguageControl)
headers.insert({"Cookie", t.requestCookie}); headers.insert({"Cookie", t.requestCookie});
} }
const auto r = zfs1_->GET(t.url.c_str(), headers); const auto r = zfs1_->GET(t.url.c_str(), headers);
if ( t.responseSetCookie ) { EXPECT_FALSE(r->has_header("Set-Cookie"));
ASSERT_TRUE(r->has_header("Set-Cookie")) << t;
EXPECT_EQ(t.responseSetCookie, getHeaderValue(r->headers, "Set-Cookie")) << t;
} else {
EXPECT_FALSE(r->has_header("Set-Cookie"));
}
std::regex_search(r->body, h1Match, h1Regex); std::regex_search(r->body, h1Match, h1Regex);
const std::string h1(h1Match[1]); const std::string h1(h1Match[1]);
EXPECT_EQ(h1, t.expectedH1) << t; EXPECT_EQ(h1, t.expectedH1) << t;