do not send content when there is a htmx redirect

This commit is contained in:
2024-07-11 21:58:52 +02:00
parent 2490e2cc68
commit 68e7f44a76
4 changed files with 42 additions and 15 deletions

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2021-2023, Tomasz Sowa * Copyright (c) 2021-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -63,6 +63,18 @@ public:
static constexpr const wchar_t * access_control_expose_headers = L"Access-Control-Expose-Headers"; static constexpr const wchar_t * access_control_expose_headers = L"Access-Control-Expose-Headers";
static constexpr const wchar_t * access_control_max_age = L"Access-Control-Max-Age"; static constexpr const wchar_t * access_control_max_age = L"Access-Control-Max-Age";
static constexpr const wchar_t * hx_location = L"HX-Location";
static constexpr const wchar_t * hx_push_url = L"HX-Push-Url";
static constexpr const wchar_t * hx_redirect = L"HX-Redirect";
static constexpr const wchar_t * hx_refresh = L"HX-Refresh";
static constexpr const wchar_t * hx_replace_url = L"HX-Replace-Url";
static constexpr const wchar_t * hx_reswap = L"HX-Reswap";
static constexpr const wchar_t * hx_retarget = L"HX-Retarget";
static constexpr const wchar_t * hx_reselect = L"HX-Reselect";
static constexpr const wchar_t * hx_trigger = L"HX-Trigger";
static constexpr const wchar_t * hx_trigger_after_settle = L"HX-Trigger-After-Settle";
static constexpr const wchar_t * hx_trigger_after_swap = L"HX-Trigger-After-Swap";
/* /*
* headers' names lower case * headers' names lower case
*/ */

View File

@@ -854,9 +854,6 @@ void Request::ModifyStatusForRedirect()
} }
// may rename to something like PrepareAndSendAnswer()? // may rename to something like PrepareAndSendAnswer()?
void Request::PrepareAndSendAnswer() void Request::PrepareAndSendAnswer()
{ {
@@ -874,7 +871,7 @@ void Request::PrepareAndSendAnswer()
} }
} }
if( !redirect_to.empty() || !x_sendfile.empty() || method == Request::options ) if( method == Request::options || !redirect_to.empty() || !x_sendfile.empty() || has_htmx_redirect() )
{ {
Send8bitOutput(output_8bit); // send empty content Send8bitOutput(output_8bit); // send empty content
return; return;
@@ -2259,6 +2256,21 @@ void Request::PutMethodName(pt::Stream & stream)
} }
bool Request::has_htmx_redirect()
{
if( out_headers.has_key(Header::hx_redirect) ||
out_headers.has_key(Header::hx_location) ||
out_headers.has_key(Header::hx_refresh) )
return true;
pt::Space * trigger = out_headers.get_space(Header::hx_trigger);
if( trigger && trigger->has_key(L"winix:redirect") )
return true;
return false;
}
void Request::AddCookie( void Request::AddCookie(
const std::wstring & name, const std::wstring & name,

View File

@@ -542,6 +542,8 @@ public:
static void PutMethodName(Request::Method method, pt::Stream & stream); static void PutMethodName(Request::Method method, pt::Stream & stream);
void PutMethodName(pt::Stream & stream); void PutMethodName(pt::Stream & stream);
bool has_htmx_redirect();
private: private:
Config * config; Config * config;

View File

@@ -665,11 +665,11 @@ void FunctionBase::redirect_to(const wchar_t * url, bool append_domain)
{ {
if( append_domain ) if( append_domain )
{ {
cur->request->out_headers.add(L"HX-Redirect", prepare_doc_url(url)); cur->request->out_headers.add(Header::hx_redirect, prepare_doc_url(url));
} }
else else
{ {
cur->request->out_headers.add(L"HX-Redirect", url); cur->request->out_headers.add(Header::hx_redirect, url);
} }
} }
else else
@@ -702,7 +702,6 @@ void FunctionBase::redirect_to(const pt::WTextStream & url, bool append_domain)
} }
void FunctionBase::redirect_to(const wchar_t * url, const wchar_t * frame_url, const wchar_t * dom_target) void FunctionBase::redirect_to(const wchar_t * url, const wchar_t * frame_url, const wchar_t * dom_target)
{ {
if( cur->request->is_htmx_request ) if( cur->request->is_htmx_request )
@@ -715,7 +714,7 @@ void FunctionBase::redirect_to(const wchar_t * url, const wchar_t * frame_url, c
prepare_doc_url(url, full_url); prepare_doc_url(url, full_url);
prepare_doc_url(frame_url, full_frame_url); prepare_doc_url(frame_url, full_frame_url);
pt::Space & trigger = cur->request->out_headers.get_add_space(L"HX-Trigger"); pt::Space & trigger = cur->request->out_headers.get_add_space(Header::hx_trigger);
pt::Space & redirect = trigger.get_add_space(L"winix:redirect"); pt::Space & redirect = trigger.get_add_space(L"winix:redirect");
redirect.add(L"path", full_frame_url); redirect.add(L"path", full_frame_url);
redirect.add(L"target", dom_target); redirect.add(L"target", dom_target);
@@ -724,9 +723,11 @@ void FunctionBase::redirect_to(const wchar_t * url, const wchar_t * frame_url, c
if( can_push_url_to_browser_history() ) if( can_push_url_to_browser_history() )
{ {
cur->request->out_headers.add(L"HX-Push-Url", full_url); cur->request->out_headers.add(Header::hx_push_url, full_url);
log << log3 << "FunctionBase: pushing a new url to the browser history: " << full_url << logend; log << log3 << "FunctionBase: pushing a new url to the browser history: " << full_url << logend;
} }
cur->request->out_headers.add(Header::hx_reswap, L"none");
} }
else else
{ {
@@ -782,20 +783,20 @@ void FunctionBase::retarged(const wchar_t * frame, const wchar_t * dom_target, c
if( dom_target && (*dom_target) ) if( dom_target && (*dom_target) )
{ {
cur->request->out_headers.add(L"HX-Retarget", dom_target); cur->request->out_headers.add(Header::hx_retarget, dom_target);
log_msg << ", container: " << dom_target; log_msg << ", container: " << dom_target;
} }
if( push_url && can_push_url_to_browser_history() ) if( push_url && can_push_url_to_browser_history() )
{ {
std::wstring url = prepare_doc_url(push_url); std::wstring url = prepare_doc_url(push_url);
cur->request->out_headers.add(L"HX-Push-Url", url); cur->request->out_headers.add(Header::hx_push_url, url);
log << log3 << "FunctionBase: pushing a new url to the browser history: " << url << logend; log << log3 << "FunctionBase: pushing a new url to the browser history: " << url << logend;
} }
if( swap_algorithm && (*swap_algorithm) ) if( swap_algorithm && (*swap_algorithm) )
{ {
cur->request->out_headers.add(L"HX-Reswap", swap_algorithm); cur->request->out_headers.add(Header::hx_reswap, swap_algorithm);
} }
log << log3 << "FunctionBase: changing the targed" << log_msg << logend; log << log3 << "FunctionBase: changing the targed" << log_msg << logend;
@@ -819,7 +820,7 @@ void FunctionBase::retarged(const wchar_t * frame, pt::WTextStream & dom_target,
void FunctionBase::remove_content(pt::WTextStream & dom_target, bool close_dialogs) void FunctionBase::remove_content(pt::WTextStream & dom_target, bool close_dialogs)
{ {
pt::Space & trigger = cur->request->out_headers.get_add_space(L"HX-Trigger"); pt::Space & trigger = cur->request->out_headers.get_add_space(Header::hx_trigger);
pt::Space & remove_content = trigger.get_add_space(L"winix:removecontent"); pt::Space & remove_content = trigger.get_add_space(L"winix:removecontent");
pt::Space & rm = remove_content.get_add_space(L"rm"); pt::Space & rm = remove_content.get_add_space(L"rm");
rm.add(dom_target); rm.add(dom_target);
@@ -898,7 +899,7 @@ void FunctionBase::update_content(const wchar_t * frame, const wchar_t * dom_tar
void FunctionBase::close_modal_dialogs() void FunctionBase::close_modal_dialogs()
{ {
pt::Space & trigger = cur->request->out_headers.get_add_space(L"HX-Trigger"); pt::Space & trigger = cur->request->out_headers.get_add_space(Header::hx_trigger);
trigger.add(L"winix:closedialogs", true); trigger.add(L"winix:closedialogs", true);
} }