diff --git a/server/upstream/const.go b/server/upstream/const.go index ce76e21..1523437 100644 --- a/server/upstream/const.go +++ b/server/upstream/const.go @@ -21,3 +21,5 @@ var fileCacheSizeLimit = 1024 * 1024 var canonicalDomainCacheTimeout = 15 * time.Minute const canonicalDomainConfig = ".domains" + +const giteaApiRepos = "/api/v1/repos/" diff --git a/server/upstream/domains.go b/server/upstream/domains.go index cfe13e6..a80cc26 100644 --- a/server/upstream/domains.go +++ b/server/upstream/domains.go @@ -1,6 +1,9 @@ package upstream import ( + "fmt" + "net/url" + "path" "strings" "github.com/valyala/fasthttp" @@ -10,8 +13,30 @@ import ( // CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file). func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken string, canonicalDomainCache cache.SetGetKey) (string, bool) { - domains := []string{} - valid := false + return checkCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken, canonicalDomainCache) +} + +func giteaRawContent(targetOwner, targetRepo, ref, giteaRoot, giteaAPIToken, resource string) ([]byte, error) { + req := fasthttp.AcquireRequest() + + req.SetRequestURI(path.Join(giteaRoot, giteaApiRepos, targetOwner, "/", targetRepo, "/raw/", resource, "?ref="+url.QueryEscape(ref))) + req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) + res := fasthttp.AcquireResponse() + + if err := client.Do(req, res); err != nil { + return nil, err + } + if res.StatusCode() != fasthttp.StatusOK { + return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode()) + } + return res.Body(), nil +} + +func checkCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken string, canonicalDomainCache cache.SetGetKey) (string, bool) { + var ( + domains []string + valid bool + ) if cachedValue, ok := canonicalDomainCache.Get(targetOwner + "/" + targetRepo + "/" + targetBranch); ok { domains = cachedValue.([]string) for _, domain := range domains { @@ -21,13 +46,10 @@ func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, m } } } else { - req := fasthttp.AcquireRequest() - req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + canonicalDomainConfig + "?access_token=" + giteaAPIToken) - res := fasthttp.AcquireResponse() - err := client.Do(req, res) - if err == nil && res.StatusCode() == fasthttp.StatusOK { - for _, domain := range strings.Split(string(res.Body()), "\n") { + body, err := giteaRawContent(giteaRoot, targetRepo, targetBranch, giteaRoot, giteaAPIToken, canonicalDomainConfig) + if err == nil { + for _, domain := range strings.Split(string(body), "\n") { domain = strings.ToLower(domain) domain = strings.TrimSpace(domain) domain = strings.TrimPrefix(domain, "http://") diff --git a/server/upstream/helper.go b/server/upstream/helper.go index b5ee77a..c0e759e 100644 --- a/server/upstream/helper.go +++ b/server/upstream/helper.go @@ -29,7 +29,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaApiToken string, br // Get default branch var body = make([]byte, 0) // TODO: use header for API key? - status, body, err := fasthttp.GetTimeout(body, giteaRoot+"/api/v1/repos/"+owner+"/"+repo+"?access_token="+giteaApiToken, 5*time.Second) + status, body, err := fasthttp.GetTimeout(body, giteaRoot+giteaApiRepos+owner+"/"+repo+"?access_token="+giteaApiToken, 5*time.Second) if err != nil || status != 200 { _ = branchTimestampCache.Set(owner+"/"+repo+"/"+branch, nil, defaultBranchCacheTimeout) return nil @@ -38,7 +38,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaApiToken string, br } var body = make([]byte, 0) - status, body, err := fasthttp.GetTimeout(body, giteaRoot+"/api/v1/repos/"+owner+"/"+repo+"/branches/"+branch+"?access_token="+giteaApiToken, 5*time.Second) + status, body, err := fasthttp.GetTimeout(body, giteaRoot+giteaApiRepos+owner+"/"+repo+"/branches/"+branch+"?access_token="+giteaApiToken, 5*time.Second) if err != nil || status != 200 { return nil } diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 88d3471..133d6fe 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -89,7 +89,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st cachedResponse = cachedValue.(fileResponse) } else { req = fasthttp.AcquireRequest() - req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "?access_token=" + giteaAPIToken) + req.SetRequestURI(giteaRoot + giteaApiRepos + uri + "?access_token=" + giteaAPIToken) res = fasthttp.AcquireResponse() res.SetBodyStream(&strings.Reader{}, -1) err = client.Do(req, res)