From 49ceeebbf8b888d280ff3649864cd1a855887294 Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Tue, 17 Jun 2025 20:28:49 +0300 Subject: [PATCH] Add ability to redirect to enforced host --- miniserv.pl | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/miniserv.pl b/miniserv.pl index ce4a69edb..6356ce740 100755 --- a/miniserv.pl +++ b/miniserv.pl @@ -1520,7 +1520,8 @@ if (defined($header{'host'})) { else { $host = $header{'host'}; } - if ($config{'musthost'} && $host ne $config{'musthost'}) { + if ($config{'musthost'} && $host ne $config{'musthost'} && + !$config{'musthost_redirect'}) { # Disallowed hostname used &http_error(400, "Invalid HTTP hostname"); } @@ -1543,6 +1544,21 @@ if ($config{'redirect_prefix'}) { } $prot = $ssl ? "https" : "http"; +# Disallowed hostname used by redirecting to musthost +if ($config{'musthost'} && $host ne $config{'musthost'} && + $config{'musthost_redirect'}) { + &write_data("HTTP/1.0 302 Moved Temporarily\r\n"); + &write_data("Date: $datestr\r\n"); + &write_data("Server: @{[&server_info()]}\r\n"); + &write_data("Location: $prot://$config{'musthost'}:$redirport\r\n"); + &write_keep_alive(0); + &write_data("\r\n"); + &log_request($loghost, $authuser, $reqline, 302, 0) if $reqline; + shutdown(SOCK, 1); + close(SOCK); + return; + } + undef(%in); if ($page =~ /^([^\?]+)\?(.*)$/) { # There is some query string information