Skip to content

Commit

Permalink
BaseURL: select port based on schema, plumb through custom httpsPort
Browse files Browse the repository at this point in the history
Thanks to Christian Schlögl for the report.
  • Loading branch information
stapelberg committed Oct 31, 2023
1 parent 235d00c commit a645001
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion httpclient/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions updateflag/updateflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "/"
Expand Down
60 changes: 60 additions & 0 deletions updateflag/updateflag_test.go
Original file line number Diff line number Diff line change
@@ -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(<TODO>): got %q, want %q", got, tt.wantURL)
}
})
}
}

0 comments on commit a645001

Please sign in to comment.