mirror of
https://github.com/kiwix/libkiwix.git
synced 2025-06-26 10:11:30 +00:00
Server sets the userlang cookie on every response
This commit is contained in:
committed by
Matthieu Gautier
parent
c0fe6f4aee
commit
1d74b5e311
@ -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";
|
||||
|
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user