mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-10 15:47:36 +02:00
proxy: preserve path and query string for http->https redirect (#1456)
This commit is contained in:
parent
83415ee52f
commit
29b2fa4e60
2 changed files with 11 additions and 6 deletions
|
@ -4,10 +4,10 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
stdlog "log"
|
stdlog "log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -69,9 +69,13 @@ func NewServer(opt *ServerOptions, h http.Handler, wg *sync.WaitGroup) (*http.Se
|
||||||
// RedirectHandler takes an incoming request and redirects to its HTTPS counterpart
|
// RedirectHandler takes an incoming request and redirects to its HTTPS counterpart
|
||||||
func RedirectHandler() http.Handler {
|
func RedirectHandler() http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
newURL := new(url.URL)
|
||||||
|
*newURL = *r.URL
|
||||||
|
newURL.Scheme = "https"
|
||||||
|
newURL.Host = urlutil.StripPort(r.Host)
|
||||||
|
|
||||||
w.Header().Set("Connection", "close")
|
w.Header().Set("Connection", "close")
|
||||||
url := fmt.Sprintf("https://%s", urlutil.StripPort(r.Host))
|
http.Redirect(w, r, newURL.String(), http.StatusMovedPermanently)
|
||||||
http.Redirect(w, r, url, http.StatusMovedPermanently)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -130,16 +130,17 @@ func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
|
||||||
|
|
||||||
func TestRedirectHandler(t *testing.T) {
|
func TestRedirectHandler(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
url string
|
||||||
wantStatus int
|
wantStatus int
|
||||||
wantBody string
|
wantBody string
|
||||||
}{
|
}{
|
||||||
{"http://example", http.StatusMovedPermanently, "<a href=\"https://example\">Moved Permanently</a>.\n\n"},
|
{"http://example", http.StatusMovedPermanently, "<a href=\"https://example\">Moved Permanently</a>.\n\n"},
|
||||||
{"http://example:8080", http.StatusMovedPermanently, "<a href=\"https://example\">Moved Permanently</a>.\n\n"},
|
{"http://example:8080", http.StatusMovedPermanently, "<a href=\"https://example\">Moved Permanently</a>.\n\n"},
|
||||||
|
{"http://example:8080/some/path?x=y", http.StatusMovedPermanently, "<a href=\"https://example/some/path?x=y\">Moved Permanently</a>.\n\n"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.url, func(t *testing.T) {
|
||||||
req := httptest.NewRequest(http.MethodGet, "http://example/", nil)
|
req := httptest.NewRequest(http.MethodGet, tt.url, nil)
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
RedirectHandler().ServeHTTP(rr, req)
|
RedirectHandler().ServeHTTP(rr, req)
|
||||||
if diff := cmp.Diff(tt.wantStatus, rr.Code); diff != "" {
|
if diff := cmp.Diff(tt.wantStatus, rr.Code); diff != "" {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue