From a645001f8b93c7ebd600649287314b65fb0165b2 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 31 Oct 2023 19:29:44 +0100 Subject: [PATCH] BaseURL: select port based on schema, plumb through custom httpsPort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to Christian Schlögl for the report. --- httpclient/instance.go | 2 +- updateflag/updateflag.go | 9 ++++-- updateflag/updateflag_test.go | 60 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 updateflag/updateflag_test.go diff --git a/httpclient/instance.go b/httpclient/instance.go index 4e1f3e7..7cf6669 100644 --- a/httpclient/instance.go +++ b/httpclient/instance.go @@ -37,7 +37,7 @@ func For(cfg *config.Struct) (_ *http.Client, foundMatchingCertificate bool, upd update.Hostname = cfg.Hostname } - updateBaseURL, err = updateflag.BaseURL(update.HTTPPort, schema, update.Hostname, update.HTTPPassword) + updateBaseURL, err = updateflag.BaseURL(update.HTTPPort, update.HTTPSPort, schema, update.Hostname, update.HTTPPassword) if err != nil { return nil, false, nil, err } diff --git a/updateflag/updateflag.go b/updateflag/updateflag.go index 4a4a56c..b3e22fe 100644 --- a/updateflag/updateflag.go +++ b/updateflag/updateflag.go @@ -50,17 +50,22 @@ func GetUpdateTarget(hostname string) (defaultPassword, updateHostname string) { return defaultPassword, u.Host } -func BaseURL(httpPort, schema, hostname, pw string) (*url.URL, error) { +func BaseURL(httpPort, httpsPort, schema, hostname, pw string) (*url.URL, error) { if update != "yes" && !strings.HasPrefix(update, ":") { // already fully qualified, nothing to add return url.Parse(update) } port := httpPort + defaultPort := "80" + if schema == "https" { + port = httpsPort + defaultPort = "443" + } if strings.HasPrefix(update, ":") { port = strings.TrimPrefix(update, ":") } update = schema + "://gokrazy:" + pw + "@" + hostname - if port != "80" { + if port != defaultPort { update += ":" + port } update += "/" diff --git a/updateflag/updateflag_test.go b/updateflag/updateflag_test.go new file mode 100644 index 0000000..0e7f086 --- /dev/null +++ b/updateflag/updateflag_test.go @@ -0,0 +1,60 @@ +package updateflag_test + +import ( + "testing" + + "github.com/gokrazy/internal/updateflag" +) + +func TestBaseURL(t *testing.T) { + for _, tt := range []struct { + desc string + HTTPPort string + HTTPSPort string + Schema string + Hostname string + Password string + wantURL string + }{ + { + desc: "default ports", + HTTPPort: "80", + HTTPSPort: "443", + Schema: "http", + Hostname: "bakery", + Password: "secret", + wantURL: "http://gokrazy:secret@bakery/", + }, + + { + desc: "custom ports (HTTP)", + HTTPPort: "81", + HTTPSPort: "444", + Schema: "http", + Hostname: "bakery", + Password: "secret", + wantURL: "http://gokrazy:secret@bakery:81/", + }, + + { + desc: "custom ports (HTTPS)", + HTTPPort: "81", + HTTPSPort: "444", + Schema: "https", + Hostname: "bakery", + Password: "secret", + wantURL: "https://gokrazy:secret@bakery:444/", + }, + } { + t.Run(tt.desc, func(t *testing.T) { + updateflag.SetUpdate("yes") + got, err := updateflag.BaseURL(tt.HTTPPort, tt.HTTPSPort, tt.Schema, tt.Hostname, tt.Password) + if err != nil { + t.Fatal(err) + } + if got.String() != tt.wantURL { + t.Errorf("BaseURL(): got %q, want %q", got, tt.wantURL) + } + }) + } +}