From 1d74b5e3115fc71855c9f925f5a52768ff24ae35 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 28 Nov 2022 12:08:51 +0400 Subject: [PATCH] Server sets the userlang cookie on every response --- src/server/request_context.cpp | 19 ++++++++++++++++++- src/server/response.cpp | 3 +++ test/server.cpp | 15 +++++++-------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 5e191afa7..db552a920 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #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"; diff --git a/src/server/response.cpp b/src/server/response.cpp index 6020f9f78..c0cd8bd5d 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -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; diff --git a/test/server.cpp b/test/server.cpp index be611ad68..5874f8be2 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -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

*/ "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

*/ "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

*/ "[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

*/ "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

*/ "[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

*/ "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

*/ "Not Found" }, };