Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0df3b76

Browse files
committed
tsweb: fix Port80Handler redirect to https with FQDN unset
Fixes the current http://pkgs.tailscale.com/ redirect to https:/// as that server doesn't configure the Port80Handler.FQDN field. Change-Id: Iff56e6127a46c306ca97738d91b217bcab32a582 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent c6ac82e commit 0df3b76

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tsweb/tsweb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (h Port80Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
162162
}
163163
host := h.FQDN
164164
if host == "" {
165-
host = r.URL.Hostname()
165+
host = r.Host
166166
}
167167
target := "https://" + host + path
168168
http.Redirect(w, r, target, http.StatusFound)

tsweb/tsweb_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,45 @@ func TestAcceptsEncoding(t *testing.T) {
580580
}
581581
}
582582
}
583+
584+
func TestPort80Handler(t *testing.T) {
585+
tests := []struct {
586+
name string
587+
h *Port80Handler
588+
req string
589+
wantLoc string
590+
}{
591+
{
592+
name: "no_fqdn",
593+
h: &Port80Handler{},
594+
req: "GET / HTTP/1.1\r\nHost: foo.com\r\n\r\n",
595+
wantLoc: "https://foo.com/",
596+
},
597+
{
598+
name: "fqdn_and_path",
599+
h: &Port80Handler{FQDN: "bar.com"},
600+
req: "GET /path HTTP/1.1\r\nHost: foo.com\r\n\r\n",
601+
wantLoc: "https://bar.com/path",
602+
},
603+
{
604+
name: "path_and_query_string",
605+
h: &Port80Handler{FQDN: "baz.com"},
606+
req: "GET /path?a=b HTTP/1.1\r\nHost: foo.com\r\n\r\n",
607+
wantLoc: "https://baz.com/path?a=b",
608+
},
609+
}
610+
for _, tt := range tests {
611+
t.Run(tt.name, func(t *testing.T) {
612+
r, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(tt.req)))
613+
rec := httptest.NewRecorder()
614+
tt.h.ServeHTTP(rec, r)
615+
got := rec.Result()
616+
if got, want := got.StatusCode, 302; got != want {
617+
t.Errorf("got status code %v; want %v", got, want)
618+
}
619+
if got, want := got.Header.Get("Location"), "https://foo.com/"; got != tt.wantLoc {
620+
t.Errorf("Location = %q; want %q", got, want)
621+
}
622+
})
623+
}
624+
}

0 commit comments

Comments
 (0)