parse url correctly?

This commit is contained in:
Ben Harris 2022-05-26 15:23:27 -04:00
parent 54946a45d7
commit 1955acce05
3 changed files with 20 additions and 12 deletions

View File

@ -19,10 +19,11 @@ const giteaAPIRepos = "/api/v1/repos/"
func giteaRawContent(targetOwner, targetRepo, ref, giteaRoot, giteaAPIToken, resource string) ([]byte, error) { func giteaRawContent(targetOwner, targetRepo, ref, giteaRoot, giteaAPIToken, resource string) ([]byte, error) {
req := fasthttp.AcquireRequest() req := fasthttp.AcquireRequest()
baseUrl, _ := url.Parse(giteaRoot)
apiPath, _ := baseUrl.Parse(path.Join(giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
apiPath := path.Join(giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
log.Debug().Msgf("giteaRawContent: trying path %s", apiPath) log.Debug().Msgf("giteaRawContent: trying path %s", apiPath)
req.SetRequestURI(apiPath) req.SetRequestURI(apiPath.String())
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
res := fasthttp.AcquireResponse() res := fasthttp.AcquireResponse()
@ -39,14 +40,16 @@ func giteaGetRepoBranchTimestamp(giteaRoot, repoOwner, repoName, branchName, git
client := getFastHTTPClient(5 * time.Second) client := getFastHTTPClient(5 * time.Second)
req := fasthttp.AcquireRequest() req := fasthttp.AcquireRequest()
apiPath := path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName) baseUrl, _ := url.Parse(giteaRoot)
apiPath, _ := baseUrl.Parse(path.Join(giteaAPIRepos, repoOwner, repoName, "branches", branchName))
log.Debug().Msgf("giteaGetRepoBranchTimestamp: trying path %s", apiPath) log.Debug().Msgf("giteaGetRepoBranchTimestamp: trying path %s", apiPath)
req.SetRequestURI(apiPath) req.SetRequestURI(apiPath.String())
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
res := fasthttp.AcquireResponse() res := fasthttp.AcquireResponse()
if err := client.Do(req, res); err != nil { if err := client.Do(req, res); err != nil {
log.Err(err) log.Err(err).Msg("giteaGetRepoBranchTimestamp: failed api lookup")
return time.Time{}, err return time.Time{}, err
} }
if res.StatusCode() != fasthttp.StatusOK { if res.StatusCode() != fasthttp.StatusOK {
@ -60,14 +63,16 @@ func giteaGetRepoDefaultBranch(giteaRoot, repoOwner, repoName, giteaAPIToken str
client := getFastHTTPClient(5 * time.Second) client := getFastHTTPClient(5 * time.Second)
req := fasthttp.AcquireRequest() req := fasthttp.AcquireRequest()
apiPath := path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName) baseUrl, _ := url.Parse(giteaRoot)
log.Debug().Msgf("trying path %s", apiPath) apiPath, _ := baseUrl.Parse(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName))
req.SetRequestURI(apiPath)
log.Debug().Msgf("giteaGetRepoDefaultBranch: trying path %s", apiPath)
req.SetRequestURI(apiPath.String())
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
res := fasthttp.AcquireResponse() res := fasthttp.AcquireResponse()
if err := client.Do(req, res); err != nil { if err := client.Do(req, res); err != nil {
log.Err(err) log.Err(err).Msg("giteaGetRepoDefaultBranch: failed api lookup")
return "", err return "", err
} }
if res.StatusCode() != fasthttp.StatusOK { if res.StatusCode() != fasthttp.StatusOK {

View File

@ -30,7 +30,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, br
// Get default branch // Get default branch
defaultBranch, err := giteaGetRepoDefaultBranch(giteaRoot, owner, repo, giteaAPIToken) defaultBranch, err := giteaGetRepoDefaultBranch(giteaRoot, owner, repo, giteaAPIToken)
if err != nil { if err != nil {
log.Err(err) log.Err(err).Msg("GetBranchTimestamp: something went wrong with giteaGetRepoDefaultBranch")
_ = branchTimestampCache.Set(owner+"/"+repo+"/", nil, defaultBranchCacheTimeout) _ = branchTimestampCache.Set(owner+"/"+repo+"/", nil, defaultBranchCacheTimeout)
return nil return nil
} }
@ -39,7 +39,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, br
timestamp, err := giteaGetRepoBranchTimestamp(giteaRoot, owner, repo, branch, giteaAPIToken) timestamp, err := giteaGetRepoBranchTimestamp(giteaRoot, owner, repo, branch, giteaAPIToken)
if err != nil { if err != nil {
log.Err(err) log.Err(err).Msg("GetBranchTimestamp: something went wrong with giteaGetRepoBranchTimestamp")
return nil return nil
} }
result.Timestamp = timestamp result.Timestamp = timestamp

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"mime" "mime"
"net/url"
"path" "path"
"strconv" "strconv"
"strings" "strings"
@ -93,7 +94,9 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
cachedResponse = cachedValue.(fileResponse) cachedResponse = cachedValue.(fileResponse)
} else { } else {
req = fasthttp.AcquireRequest() req = fasthttp.AcquireRequest()
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, uri)) baseUrl, _ := url.Parse(giteaRoot)
apiPath, _ := baseUrl.Parse(path.Join(giteaAPIRepos, uri))
req.SetRequestURI(apiPath.String())
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
res = fasthttp.AcquireResponse() res = fasthttp.AcquireResponse()
res.SetBodyStream(&strings.Reader{}, -1) res.SetBodyStream(&strings.Reader{}, -1)