Server sets the userlang cookie on every response

This commit is contained in:
Veloman Yunkan 2022-11-28 12:08:51 +04:00 committed by Matthieu Gautier
parent c0fe6f4aee
commit 1d74b5e311
3 changed files with 28 additions and 9 deletions

View File

@ -25,6 +25,7 @@
#include <sstream>
#include <cstdio>
#include <atomic>
#include <cctype>
#include "tools/stringTools.h"
@ -63,6 +64,22 @@ fullURL2LocalURL(const std::string& full_url, const std::string& rootLocation)
}
}
std::string parseAcceptLanguageHeader(const std::string& s)
{
// TODO: implement properly
if ( s.empty() )
return "en";
for ( const char c : s ) {
if ( ! std::isalpha(c) ) {
return "en";
}
}
return s;
}
} // unnamed namespace
RequestContext::RequestContext(struct MHD_Connection* connection,
@ -204,7 +221,7 @@ std::string RequestContext::get_user_language() const
} catch(const std::out_of_range&) {}
try {
return get_header("Accept-Language");
return parseAcceptLanguageHeader(get_header("Accept-Language"));
} catch(const std::out_of_range&) {}
return "en";

View File

@ -387,6 +387,9 @@ MHD_Result Response::send(const RequestContext& request, MHD_Connection* connect
MHD_add_response_header(response, p.first.c_str(), p.second.c_str());
}
const std::string cookie = "userlang=" + request.get_user_language();
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)
m_returnCode = MHD_HTTP_PARTIAL_CONTENT;

View File

@ -998,42 +998,41 @@ TEST_F(ServerTest, UserLanguageControl)
};
const char* const NO_COOKIE = nullptr;
const char* const NO_SET_COOKIE = nullptr;
const TestData testData[] = {
{
/*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=en",
/* expected <h1> */ "Not Found"
},
{
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=en",
/* expected <h1> */ "Not Found"
},
{
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=test",
/*Accept-Language:*/ "",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=test",
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
/*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "*",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=en",
/* expected <h1> */ "Not Found"
},
{
/*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "test",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=test",
/* expected <h1> */ "[I18N TESTING] Content not found, but at least the server is alive"
},
{
@ -1041,7 +1040,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article?userlang=en",
/*Accept-Language:*/ "test",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=en",
/* expected <h1> */ "Not Found"
},
{
@ -1051,7 +1050,7 @@ TEST_F(ServerTest, UserLanguageControl)
/*url*/ "/ROOT/content/zimfile/invalid-article",
/*Accept-Language:*/ "test;q=0.9, en;q=0.2",
/*Request Cookie:*/ NO_COOKIE,
/*Response Set-Cookie:*/ NO_SET_COOKIE,
/*Response Set-Cookie:*/ "userlang=en",
/* expected <h1> */ "Not Found"
},
};