fixed redirectHTTP middleware

This commit is contained in:
yusing 2024-11-30 08:58:30 +08:00
parent f8bdc7044c
commit 51f9afb471

View file

@ -2,6 +2,7 @@ package middleware
import (
"net/http"
"strings"
"github.com/yusing/go-proxy/internal/common"
)
@ -10,7 +11,12 @@ var RedirectHTTP = &Middleware{
before: func(next http.HandlerFunc, w ResponseWriter, r *Request) {
if r.TLS == nil {
r.URL.Scheme = "https"
r.URL.Host = r.URL.Hostname() + ":" + common.ProxyHTTPSPort
host := r.Host
if i := strings.Index(host, ":"); i != -1 {
host = host[:i] // strip port number if present
}
r.URL.Host = host + ":" + common.ProxyHTTPSPort
logger.Info().Str("url", r.URL.String()).Msg("redirect to https")
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
return
}