mirror of https://github.com/kiwix/libkiwix.git
Sparing use of "Set-Cookie: userlang=..." header
Server adds the "Set-Cookie: userlang=..." header to the response only if the "userlang" cookie is not already present with the same value.
This commit is contained in:
parent
0edee4d066
commit
fcb97c3c06
|
@ -212,26 +212,32 @@ std::string RequestContext::get_header(const std::string& name) const {
|
||||||
|
|
||||||
std::string RequestContext::get_user_language() const
|
std::string RequestContext::get_user_language() const
|
||||||
{
|
{
|
||||||
return userlang;
|
return userlang.lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RequestContext::determine_user_language() const
|
bool RequestContext::user_language_comes_from_cookie() const
|
||||||
|
{
|
||||||
|
return userlang.selectedBy == UserLanguage::SelectorKind::COOKIE;
|
||||||
|
}
|
||||||
|
|
||||||
|
RequestContext::UserLanguage RequestContext::determine_user_language() const
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return get_argument("userlang");
|
return {UserLanguage::SelectorKind::QUERY_PARAM, get_argument("userlang")};
|
||||||
} catch(const std::out_of_range&) {}
|
} catch(const std::out_of_range&) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return cookies.at("userlang");
|
return {UserLanguage::SelectorKind::COOKIE, cookies.at("userlang")};
|
||||||
} catch(const std::out_of_range&) {}
|
} 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);
|
||||||
return selectMostSuitableLanguage(userLangPrefs);
|
const auto lang = selectMostSuitableLanguage(userLangPrefs);
|
||||||
|
return {UserLanguage::SelectorKind::ACCEPT_LANGUAGE_HEADER, lang};
|
||||||
} catch(const std::out_of_range&) {}
|
} catch(const std::out_of_range&) {}
|
||||||
|
|
||||||
return "en";
|
return {UserLanguage::SelectorKind::DEFAULT, "en"};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RequestContext::get_requested_format() const
|
std::string RequestContext::get_requested_format() const
|
||||||
|
|
|
@ -118,6 +118,23 @@ 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
|
||||||
|
struct UserLanguage
|
||||||
|
{
|
||||||
|
enum SelectorKind
|
||||||
|
{
|
||||||
|
QUERY_PARAM,
|
||||||
|
COOKIE,
|
||||||
|
ACCEPT_LANGUAGE_HEADER,
|
||||||
|
DEFAULT
|
||||||
|
};
|
||||||
|
|
||||||
|
SelectorKind selectedBy;
|
||||||
|
std::string lang;
|
||||||
|
};
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
std::string full_url;
|
std::string full_url;
|
||||||
std::string url;
|
std::string url;
|
||||||
|
@ -132,10 +149,10 @@ class RequestContext {
|
||||||
std::map<std::string, std::vector<std::string>> arguments;
|
std::map<std::string, std::vector<std::string>> arguments;
|
||||||
std::map<std::string, std::string> cookies;
|
std::map<std::string, std::string> cookies;
|
||||||
std::string queryString;
|
std::string queryString;
|
||||||
std::string userlang;
|
UserLanguage userlang;
|
||||||
|
|
||||||
private: // functions
|
private: // functions
|
||||||
std::string determine_user_language() const;
|
UserLanguage determine_user_language() const;
|
||||||
|
|
||||||
static MHD_Result fill_header(void *, enum MHD_ValueKind, const char*, const char*);
|
static MHD_Result fill_header(void *, enum MHD_ValueKind, const char*, const char*);
|
||||||
static MHD_Result fill_cookie(void *, enum MHD_ValueKind, const char*, const char*);
|
static MHD_Result fill_cookie(void *, enum MHD_ValueKind, const char*, const char*);
|
||||||
|
|
|
@ -387,8 +387,10 @@ 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();
|
const std::string cookie = "userlang=" + request.get_user_language();
|
||||||
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());
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
@ -1047,7 +1047,7 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
||||||
/*Accept-Language:*/ "",
|
/*Accept-Language:*/ "",
|
||||||
/*Request Cookie:*/ "userlang=test",
|
/*Request Cookie:*/ "userlang=test",
|
||||||
/*Response Set-Cookie:*/ "userlang=test",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1055,7 +1055,7 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
||||||
/*Accept-Language:*/ "",
|
/*Accept-Language:*/ "",
|
||||||
/*Request Cookie:*/ "anothercookie=123; userlang=test",
|
/*Request Cookie:*/ "anothercookie=123; userlang=test",
|
||||||
/*Response Set-Cookie:*/ "userlang=test",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1063,7 +1063,7 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
||||||
/*Accept-Language:*/ "",
|
/*Accept-Language:*/ "",
|
||||||
/*Request Cookie:*/ "userlang=test; anothercookie=abc",
|
/*Request Cookie:*/ "userlang=test; anothercookie=abc",
|
||||||
/*Response Set-Cookie:*/ "userlang=test",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1071,7 +1071,7 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
||||||
/*Accept-Language:*/ "",
|
/*Accept-Language:*/ "",
|
||||||
/*Request Cookie:*/ "cookie1=abc; userlang=test; cookie2=xyz",
|
/*Request Cookie:*/ "cookie1=abc; userlang=test; cookie2=xyz",
|
||||||
/*Response Set-Cookie:*/ "userlang=test",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1079,7 +1079,7 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
/*url*/ "/ROOT/content/zimfile/invalid-article",
|
||||||
/*Accept-Language:*/ "",
|
/*Accept-Language:*/ "",
|
||||||
/*Request Cookie:*/ "cookie1=abc; userlang=en; userlang=test; cookie2=xyz",
|
/*Request Cookie:*/ "cookie1=abc; userlang=en; userlang=test; cookie2=xyz",
|
||||||
/*Response Set-Cookie:*/ "userlang=test",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1103,7 +1103,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:*/ "userlang=en",
|
/*Request Cookie:*/ "userlang=en",
|
||||||
/*Response Set-Cookie:*/ "userlang=en",
|
/*Response Set-Cookie:*/ NO_COOKIE,
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue