From f31c0b1659f2d5b3095e1e032c9042e5a42bf65b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 7 Nov 2022 23:21:35 +0100 Subject: [PATCH] refactor, dedup & pass-forward --- server/gitea/cache.go | 8 ++++---- server/gitea/client.go | 16 ++++++++-------- server/upstream/upstream.go | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/server/gitea/cache.go b/server/gitea/cache.go index fe7ead3..5808b07 100644 --- a/server/gitea/cache.go +++ b/server/gitea/cache.go @@ -54,10 +54,10 @@ func (f FileResponse) createHttpResponse() *http.Response { if f.IsSymlink { resp.Header.Set(giteaObjectTypeHeader, objTypeSymlink) } - resp.Header.Set(eTagHeader, f.ETag) - resp.Header.Set(contentTypeHeader, f.MimeType) - resp.Header.Set(contentLengthHeader, fmt.Sprint(len(f.Body))) - resp.Header.Set(pagesCacheIndicator, "true") + resp.Header.Set(ETagHeader, f.ETag) + resp.Header.Set(ContentTypeHeader, f.MimeType) + resp.Header.Set(ContentLengthHeader, fmt.Sprint(len(f.Body))) + resp.Header.Set(PagesCacheIndicatorHeader, "true") return resp } diff --git a/server/gitea/client.go b/server/gitea/client.go index bda5d14..8ce1598 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -28,16 +28,16 @@ const ( rawContentCacheKeyPrefix = "rawContent" // pages server - pagesCacheIndicator = "X-Pages-Cache" + PagesCacheIndicatorHeader = "X-Pages-Cache" // gitea giteaObjectTypeHeader = "X-Gitea-Object-Type" objTypeSymlink = "symlink" // std - eTagHeader = "ETag" - contentTypeHeader = "Content-Type" - contentLengthHeader = "Content-Length" + ETagHeader = "ETag" + ContentTypeHeader = "Content-Type" + ContentLengthHeader = "Content-Length" ) type Client struct { @@ -140,7 +140,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str Exists: true, IsSymlink: true, Body: []byte(linkDest), - ETag: resp.Header.Get(eTagHeader), + ETag: resp.Header.Get(ETagHeader), }, fileCacheTimeout); err != nil { log.Error().Err(err).Msg("could not save symlink in cache") } @@ -154,7 +154,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str { // Set the MIME type mimeType := client.getMimeTypeByExtension(resource) - resp.Response.Header.Set(contentTypeHeader, mimeType) + resp.Response.Header.Set(ContentTypeHeader, mimeType) if !shouldRespBeSavedToCache(resp.Response) { return reader, resp.Response, err @@ -163,7 +163,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str // now we write to cache and respond at the sime time fileResp := FileResponse{ Exists: true, - ETag: resp.Header.Get(eTagHeader), + ETag: resp.Header.Get(ETagHeader), MimeType: mimeType, } return fileResp.CreateCacheReader(reader, client.responseCache, cacheKey), resp.Response, nil @@ -172,7 +172,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str case http.StatusNotFound: if err := client.responseCache.Set(cacheKey, FileResponse{ Exists: false, - ETag: resp.Header.Get(eTagHeader), + ETag: resp.Header.Get(ETagHeader), }, fileCacheTimeout); err != nil { log.Error().Err(err).Msg("could not save 404 in cache") } diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index df54f4f..3c69bc2 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -16,8 +16,6 @@ import ( ) const ( - headerContentType = "Content-Type" - headerETag = "ETag" headerLastModified = "Last-Modified" headerIfModifiedSince = "If-Modified-Since" @@ -155,11 +153,13 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin // Set ETag & MIME if res != nil { - ctx.Response().Header.Set(headerETag, res.Header.Get(headerETag)) + ctx.Response().Header.Set(gitea.ETagHeader, res.Header.Get(gitea.ETagHeader)) + ctx.Response().Header.Set(gitea.PagesCacheIndicatorHeader, res.Header.Get(gitea.PagesCacheIndicatorHeader)) + ctx.Response().Header.Set(gitea.ContentLengthHeader, res.Header.Get(gitea.ContentLengthHeader)) if o.ServeRaw { - ctx.Response().Header.Set(headerContentType, res.Header.Get(headerContentType)) + ctx.Response().Header.Set(gitea.ContentTypeHeader, res.Header.Get(gitea.ContentTypeHeader)) } else { - ctx.Response().Header.Set(headerContentType, rawMime) + ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime) } }