mirror of https://github.com/kiwix/libkiwix.git
Server sets the userlang cookie on every response
This commit is contained in:
parent
c0fe6f4aee
commit
1d74b5e311
|
@ -25,6 +25,7 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
#include "tools/stringTools.h"
|
#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
|
} // unnamed namespace
|
||||||
|
|
||||||
RequestContext::RequestContext(struct MHD_Connection* connection,
|
RequestContext::RequestContext(struct MHD_Connection* connection,
|
||||||
|
@ -204,7 +221,7 @@ std::string RequestContext::get_user_language() const
|
||||||
} catch(const std::out_of_range&) {}
|
} catch(const std::out_of_range&) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return get_header("Accept-Language");
|
return parseAcceptLanguageHeader(get_header("Accept-Language"));
|
||||||
} catch(const std::out_of_range&) {}
|
} catch(const std::out_of_range&) {}
|
||||||
|
|
||||||
return "en";
|
return "en";
|
||||||
|
|
|
@ -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());
|
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)
|
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;
|
||||||
|
|
||||||
|
|
|
@ -998,42 +998,41 @@ TEST_F(ServerTest, UserLanguageControl)
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* const NO_COOKIE = nullptr;
|
const char* const NO_COOKIE = nullptr;
|
||||||
const char* const NO_SET_COOKIE = nullptr;
|
|
||||||
|
|
||||||
const TestData testData[] = {
|
const TestData testData[] = {
|
||||||
{
|
{
|
||||||
/*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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=en",
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
/*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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=en",
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
/*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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=test",
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
/*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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=en",
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
/*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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=test",
|
||||||
/* 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"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1041,7 +1040,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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=en",
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1051,7 +1050,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:*/ NO_SET_COOKIE,
|
/*Response Set-Cookie:*/ "userlang=en",
|
||||||
/* expected <h1> */ "Not Found"
|
/* expected <h1> */ "Not Found"
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue