add support for more cookie flags in Request::AddCookie() method

changed in config options:
- renamed: http_session_id_name to session_cookie_name
- add config options: session_cookie_path, session_cookie_domain, session_cookie_same_site,
  session_cookie_http_only, session_cookie_secure
This commit is contained in:
2022-09-08 05:40:44 +02:00
parent 222a1c8a1f
commit 05ecac8426
8 changed files with 174 additions and 55 deletions

View File

@@ -494,16 +494,18 @@ public:
std::wstring * PostVarp(const wchar_t * var);
std::wstring * PostVarp(const std::wstring & var);
void AddCookie(
const std::wstring & name,
const std::wstring * value_string = nullptr,
const pt::Stream * value_stream = nullptr,
pt::Date * expires = nullptr,
const std::wstring * path = nullptr,
const std::wstring * domain = nullptr,
CookieSameSite cookie_same_site = CookieSameSite::samesite_notset,
bool http_only = false,
bool secure = false);
// setting a cookie
// name - cookie name (either const wchar_t, or std::wstring or pt::WTextStream)
// value - cookie value (can be everything which can be put to pt::WTextStream stream)
// the return std::wstring reference is a reference to the cookie inserted value (in out_cookies structure)
template<typename NameType, typename ValueType>
void AddCookie(const NameType & name, const ValueType & value, pt::Date * expires = 0);
template<typename NameType, typename ValueType>
void AddCookie(const NameType & name, const ValueType & value, pt::Date & expires);
void AddDefaultSessionCookie(const std::wstring & value, pt::Date * expires = nullptr);
bool has_frame(const wchar_t * frame);
bool has_frame(const std::wstring & frame);
@@ -615,47 +617,6 @@ private:
template<typename NameType, typename ValueType>
void Request::AddCookie(const NameType & name, const ValueType & value, pt::Date * expires)
{
pt::WTextStream cookie;
cookie << value;
if( cookie.empty() )
cookie << L"\"\""; // cookie empty value
if( expires )
cookie << L"; expires=" << DateToStrCookie(*expires) << L" GMT";
cookie << L"; path=/; domain=" << config->base_url;
/*
!! IMPROVE ME add an option to the config
don't use '; secure' flag if you are using both sites (with SSL
and without SSL) -- with secure flag the cookie is sent only through
SSL and if you accidentally open a new window without SSL (http://)
then winix will create a new session for you and the previous session (https://)
will be lost (the session cookie will be overwritten in the client's browser)
*/
out_cookies.add_stream(name, cookie);
}
template<typename NameType, typename ValueType>
void Request::AddCookie(const NameType & name, const ValueType & value, pt::Date & expires)
{
AddCookie(name, value, &expires);
}
} // namespace Winix
#endif