Redirecting from HTTP to HTTPS on the same port, similar to nginx's error_page 497.
It can also redirect from port 80 to port 443.
This is my original work - the first solution to the issue without modifying the standard library.
If you like it, please give me a star⭐. Thanks! 😊
go get github.com/bddjr/hlfhr
// Use hlfhr.New
srv := hlfhr.New(&http.Server{
// Write something...
})
// Port 80 redirects to port 443.
// This option only takes effect when listening on port 443.
srv.Listen80RedirectTo443 = true
// Then just use it like [http.Server]
err := srv.ListenAndServeTLS("example.crt", "example.key")For example:
- Listening on port 8443,
http://127.0.0.1:8443will redirect tohttps://127.0.0.1:8443. - Listening on port 443,
http://127.0.0.1will redirect tohttps://127.0.0.1.
If you need to customize the redirect handler, see HlfhrHandler Example.
flowchart TD
Read("Hijacking net.Conn.Read")
IsLooksLikeHTTP("First byte looks like HTTP ?")
CancelHijacking(["✅ Cancel hijacking..."])
ReadRequest("🔍 Read request")
IsHandlerExist("HlfhrHandler exist ?")
Redirect{{"🟡 307 Redirect"}}
Handler{{"💡 Handler"}}
Close(["❌ Close."])
Read --> IsLooksLikeHTTP
IsLooksLikeHTTP -- "🔐false" --> CancelHijacking
IsLooksLikeHTTP -- "📄true" --> ReadRequest --> IsHandlerExist
IsHandlerExist -- "✖false" --> Redirect --> Close
IsHandlerExist -- "✅true" --> Handler --> Close
If you need
http.Hijackerorhttp.ResponseController.EnableFullDuplex, please use hahosp.
// Check Host Header
srv.HlfhrHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hostname, _port := r.Host, ""
if !strings.HasSuffix(hostname, "]") {
if i := strings.LastIndexByte(hostname, ':'); i != -1 {
_port = hostname[i:]
hostname = hostname[:i]
}
}
switch hostname {
case "localhost":
//
case "www.localhost", "127.0.0.1":
r.Host = "localhost" + _port
default:
w.WriteHeader(421)
return
}
hlfhr_utils.RedirectToHttps(w, r, 307)
})Linux:
git clone https://github.com/bddjr/hlfhr
cd hlfhr
cd testdata
sudo go test
sudo go run main.go
Windows:
git clone https://github.com/bddjr/hlfhr
cd hlfhr
cd testdata
go test
go run main.go
https://developer.mozilla.org/docs/Web/HTTP
https://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors
BSD-3-clause, Same as Golang.