From 33410f8ccdaac6dc964c38f8d98ff9fb05539fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Ri=C3=9Fe?= Date: Wed, 6 Nov 2024 14:29:39 +0000 Subject: [PATCH] Explicitly set http(s) default ports in annex.url (#55) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise, git-annex tries to use its own default port (9417) and fails. Fixes #52. Reviewed-on: https://codeberg.org/forgejo-aneksajo/forgejo-aneksajo/pulls/55 Co-authored-by: Matthias Riße Co-committed-by: Matthias Riße --- routers/web/repo/githttp.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/routers/web/repo/githttp.go b/routers/web/repo/githttp.go index 1fc0f2d615..2ba5d3e372 100644 --- a/routers/web/repo/githttp.go +++ b/routers/web/repo/githttp.go @@ -10,6 +10,7 @@ import ( gocontext "context" "fmt" "net/http" + "net/url" "os" "path/filepath" "regexp" @@ -573,7 +574,23 @@ func GetConfig(ctx *context.Context) { return } if !setting.Annex.DisableP2PHTTP { - config = append(config, []byte("[annex]\n\turl = annex+"+setting.AppURL+"git-annex-p2phttp\n")...) + appURL, err := url.Parse(setting.AppURL) + if err != nil { + log.Error("Could not parse 'setting.AppURL': %v", err) + ctx.Resp.WriteHeader(http.StatusInternalServerError) + return + } + if appURL.Port() == "" { + // If there is no port set then set the http(s) default ports. + // Without this, git-annex would try its own default port (9417) and fail. + if appURL.Scheme == "http" { + appURL.Host += ":80" + } + if appURL.Scheme == "https" { + appURL.Host += ":443" + } + } + config = append(config, []byte("[annex]\n\turl = annex+"+appURL.String()+"git-annex-p2phttp\n")...) } ctx.Resp.Header().Set("Content-Type", "text/plain") ctx.Resp.Header().Set("Content-Length", fmt.Sprintf("%d", len(config)))