From 1e1c67be93f59af3fad68cdc8faad72aac8a742b Mon Sep 17 00:00:00 2001 From: crapStone Date: Wed, 15 Nov 2023 15:25:14 +0000 Subject: [PATCH] let gitea client send user-agent with version (#258) closes #255 Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/258 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: crapStone Co-committed-by: crapStone --- server/gitea/client.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/server/gitea/client.go b/server/gitea/client.go index 7a2bf63..f3bda54 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -17,12 +17,13 @@ import ( "github.com/rs/zerolog/log" "codeberg.org/codeberg/pages/server/cache" + "codeberg.org/codeberg/pages/server/version" ) var ErrorNotFound = errors.New("not found") const ( - // cache key prefixe + // cache key prefixes branchTimestampCacheKeyPrefix = "branchTime" defaultBranchCacheKeyPrefix = "defaultBranch" rawContentCacheKeyPrefix = "rawContent" @@ -76,7 +77,13 @@ func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, follo defaultMimeType = "application/octet-stream" } - sdk, err := gitea.NewClient(giteaRoot, gitea.SetHTTPClient(&stdClient), gitea.SetToken(giteaAPIToken)) + sdk, err := gitea.NewClient( + giteaRoot, + gitea.SetHTTPClient(&stdClient), + gitea.SetToken(giteaAPIToken), + gitea.SetUserAgent("pages-server/"+version.Version), + ) + return &Client{ sdkClient: sdk, responseCache: respCache, @@ -172,7 +179,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str return reader, resp.Response.Header, resp.StatusCode, err } - // now we write to cache and respond at the sime time + // now we write to cache and respond at the same time fileResp := FileResponse{ Exists: true, ETag: resp.Header.Get(ETagHeader), @@ -278,11 +285,11 @@ func shouldRespBeSavedToCache(resp *http.Response) bool { return false } - contentLeng, err := strconv.ParseInt(contentLengthRaw, 10, 64) + contentLength, err := strconv.ParseInt(contentLengthRaw, 10, 64) if err != nil { log.Error().Err(err).Msg("could not parse content length") } // if content to big or could not be determined we not cache it - return contentLeng > 0 && contentLeng < fileCacheSizeLimit + return contentLength > 0 && contentLength < fileCacheSizeLimit }