From 33e8df11c5499c81984f77c25ac84d83fa5abcaf Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 2 Feb 2022 18:20:32 +0100 Subject: [PATCH] parse Accept-Language header to Request::accept_languages table --- winixd/core/app.cpp | 34 ++++++++++++++++++++++++++++------ winixd/core/app.h | 3 ++- winixd/core/config.cpp | 5 ++++- winixd/core/config.h | 10 +++++++++- winixd/core/header.h | 2 ++ winixd/core/request.cpp | 1 + winixd/core/request.h | 3 +++ 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/winixd/core/app.cpp b/winixd/core/app.cpp index adef8b3..4af3fb1 100644 --- a/winixd/core/app.cpp +++ b/winixd/core/app.cpp @@ -932,23 +932,43 @@ void App::LogEnvironmentHTTPVariables() } -void App::ParseAcceptHeader() +void App::ParseAcceptHeader(const wchar_t * header_name, const std::wstring & env, std::vector & container, size_t max_len) { - accept_base_parser.parse(cur.request->env_http_accept, cur.request->accept_mime_types, 16); + accept_base_parser.parse(env, container, max_len); - if( !cur.request->accept_mime_types.empty() ) + if( !container.empty() ) { - log << log3 << "App: " << Winix::Header::accept << " header consists of: "; - HeaderValue::log_values(cur.request->accept_mime_types, log); + log << log3 << "App: " << header_name << " header consists of: "; + HeaderValue::log_values(container, log); log << logend; } else { - log << log3 << "App: there is no " << Winix::Header::accept << " header" << logend; + log << log3 << "App: there is no " << header_name << " header" << logend; } } +void App::ParseAcceptHeader() +{ + ParseAcceptHeader( + Winix::Header::accept, + cur.request->env_http_accept, + cur.request->accept_mime_types, + config.request_max_accept_fields); +} + + +void App::ParseAcceptLanguageHeader() +{ + ParseAcceptHeader( + Winix::Header::accept_language, + cur.request->env_http_accept_language, + cur.request->accept_languages, + config.request_max_accept_language_fields); +} + + /* * reading the request (without GET parameters in the URL) @@ -973,6 +993,7 @@ void App::ReadRequest() LogEnvironmentHTTPVariables(); ParseAcceptHeader(); + ParseAcceptLanguageHeader(); accept_encoding_parser.ParseAndLog(cur.request->env_http_accept_encoding, log); cookie_parser.Parse(cur.request->env_http_cookie, cur.request->cookie_tab); @@ -1014,6 +1035,7 @@ void App::ReadEnvVariables() SetEnv("HTTP_COOKIE", cur.request->env_http_cookie); SetEnv("HTTP_ACCEPT_ENCODING", cur.request->env_http_accept_encoding); SetEnv("HTTP_ACCEPT", cur.request->env_http_accept); + SetEnv("HTTP_ACCEPT_LANGUAGE", cur.request->env_http_accept_language); } diff --git a/winixd/core/app.h b/winixd/core/app.h index e67d638..f756844 100644 --- a/winixd/core/app.h +++ b/winixd/core/app.h @@ -54,7 +54,6 @@ #include "cookieparser.h" #include "postmultiparser.h" #include "acceptencodingparser.h" -#include "acceptparser.h" #include "winixrequest.h" #include "log/log.h" #include "filelog.h" @@ -240,7 +239,9 @@ private: void LogEnvironmentVariables(); void LogEnvironmentHTTPVariables(); + void ParseAcceptHeader(const wchar_t * header_name, const std::wstring & env, std::vector & container, size_t max_len); void ParseAcceptHeader(); + void ParseAcceptLanguageHeader(); void SetEnv(const char * name, std::wstring & env); void ReadEnvVariables(); diff --git a/winixd/core/config.cpp b/winixd/core/config.cpp index 77defce..3530278 100644 --- a/winixd/core/config.cpp +++ b/winixd/core/config.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2008-2021, Tomasz Sowa + * Copyright (c) 2008-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -295,6 +295,9 @@ void Config::AssignValues(bool stdout_is_closed) bin_stream_field = Text(L"bin_stream_field", L"bin_stream"); main_stream_field = Text(L"main_stream_field", L"main_stream"); ezc_frames_field = Text(L"ezc_frames_field", L"ezc_frames"); + request_max_accept_fields = Size(L"request_max_accept_fields", 8); + request_max_accept_language_fields = Size(L"request_max_accept_language_fields", 8); + account_need_email_verification = Bool(L"account_need_email_verification", true); reset_password_code_expiration_time = Long(L"reset_password_code_expiration_time", 86400); diff --git a/winixd/core/config.h b/winixd/core/config.h index 3680505..c92e023 100644 --- a/winixd/core/config.h +++ b/winixd/core/config.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2008-2021, Tomasz Sowa + * Copyright (c) 2008-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -709,6 +709,14 @@ public: // default: ezc_frames std::wstring ezc_frames_field; + // max Accept header mime types to be parsed in a request + // default: 8 + size_t request_max_accept_fields; + + // max Accept-Language languages to be parsed in a request + // default: 8 + size_t request_max_accept_language_fields; + // when true then when a user want to create a new account // he has to provide his email and a message will be sent back to him // with a link to activate the account diff --git a/winixd/core/header.h b/winixd/core/header.h index ae4da11..6dbf538 100644 --- a/winixd/core/header.h +++ b/winixd/core/header.h @@ -50,12 +50,14 @@ public: */ static constexpr const wchar_t * content_type = L"Content-Type"; static constexpr const wchar_t * accept = L"Accept"; + static constexpr const wchar_t * accept_language = L"Accept-Language"; /* * headers' names lower case */ static constexpr const wchar_t * content_type_lc = L"content-type"; static constexpr const wchar_t * accept_lc = L"accept"; + static constexpr const wchar_t * accept_language_lc = L"accept-language"; /* * headers' values diff --git a/winixd/core/request.cpp b/winixd/core/request.cpp index ae1e8be..3458a06 100644 --- a/winixd/core/request.cpp +++ b/winixd/core/request.cpp @@ -121,6 +121,7 @@ void Request::Clear() env_http_user_agent.clear(); env_http_accept_encoding.clear(); env_http_accept.clear(); + env_http_accept_language.clear(); env_fcgi_role.clear(); env_content_type.clear(); env_https.clear(); diff --git a/winixd/core/request.h b/winixd/core/request.h index c7c9b78..2dcffc0 100644 --- a/winixd/core/request.h +++ b/winixd/core/request.h @@ -191,6 +191,7 @@ public: std::wstring env_http_user_agent; std::wstring env_http_accept_encoding; std::wstring env_http_accept; + std::wstring env_http_accept_language; std::wstring env_fcgi_role; std::wstring env_content_type; std::wstring env_https; @@ -332,6 +333,8 @@ public: // at the beginning those with higher priority std::vector accept_mime_types; + // at the beginning those with higher priority + std::vector accept_languages; // request status // !! CHANGE ME it'll be better to use ordinary http result codes