From 7c45a420e444ee1509410238d3098901427850ea Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 8 Nov 2022 00:05:27 +0100 Subject: [PATCH] less indirect --- Justfile | 1 + server/gitea/cache.go | 23 +++++++++++------------ server/gitea/client.go | 21 +++++++++++---------- server/upstream/upstream.go | 24 +++++++++++------------- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Justfile b/Justfile index 03a7436..f7ea414 100644 --- a/Justfile +++ b/Justfile @@ -18,6 +18,7 @@ build-tag VERSION: lint: tool-golangci tool-gofumpt [ $(gofumpt -extra -l . | wc -l) != 0 ] && { echo 'code not formated'; exit 1; }; \ golangci-lint run --timeout 5m --build-tags integration + # TODO: run editorconfig-checker fmt: tool-gofumpt gofumpt -w --extra . diff --git a/server/gitea/cache.go b/server/gitea/cache.go index 8de2cd2..41e9c41 100644 --- a/server/gitea/cache.go +++ b/server/gitea/cache.go @@ -42,26 +42,25 @@ func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 } -func (f FileResponse) createHttpResponse() *http.Response { - resp := &http.Response{ - Header: make(http.Header), - } +func (f FileResponse) createHttpResponse() (http.Header, int) { + header := make(http.Header) + statusCode := http.StatusInternalServerError if f.Exists { - resp.StatusCode = http.StatusOK + statusCode = http.StatusOK } else { - resp.StatusCode = http.StatusNotFound + statusCode = http.StatusNotFound } if f.IsSymlink { - resp.Header.Set(giteaObjectTypeHeader, objTypeSymlink) + 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(PagesCacheIndicatorHeader, "true") + header.Set(ETagHeader, f.ETag) + header.Set(ContentTypeHeader, f.MimeType) + header.Set(ContentLengthHeader, fmt.Sprint(len(f.Body))) + header.Set(PagesCacheIndicatorHeader, "true") - return resp + return header, statusCode } type BranchTimestamp struct { diff --git a/server/gitea/client.go b/server/gitea/client.go index ff5a7e0..dff672a 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -87,7 +87,7 @@ func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, follo } func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) { - reader, _, err := client.ServeRawContent(targetOwner, targetRepo, ref, resource) + reader, _, _, err := client.ServeRawContent(targetOwner, targetRepo, ref, resource) if err != nil { return nil, err } @@ -95,13 +95,14 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str return io.ReadAll(reader) } -func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (io.ReadCloser, *http.Response, error) { +func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (io.ReadCloser, http.Header, int, error) { cacheKey := fmt.Sprintf("%s/%s/%s|%s|%s", rawContentCacheKeyPrefix, targetOwner, targetRepo, ref, resource) log := log.With().Str("cache_key", cacheKey).Logger() // handle if cache entry exist if cache, ok := client.responseCache.Get(cacheKey); ok { cache := cache.(FileResponse) + cachedHeader, cachedStatusCode := cache.createHttpResponse() // TODO: check against some timestamp missmatch?!? if cache.Exists { if cache.IsSymlink { @@ -110,10 +111,10 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str return client.ServeRawContent(targetOwner, targetRepo, ref, linkDest) } else { log.Debug().Msg("[cache] return bytes") - return io.NopCloser(bytes.NewReader(cache.Body)), cache.createHttpResponse(), nil + return io.NopCloser(bytes.NewReader(cache.Body)), cachedHeader, cachedStatusCode, nil } } else { - return nil, cache.createHttpResponse(), ErrorNotFound + return nil, cachedHeader, cachedStatusCode, ErrorNotFound } } @@ -132,7 +133,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str defer reader.Close() linkDestBytes, err := io.ReadAll(io.LimitReader(reader, 10000)) if err != nil { - return nil, nil, err + return nil, nil, http.StatusInternalServerError, err } linkDest := strings.TrimSpace(string(linkDestBytes)) @@ -157,7 +158,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str resp.Response.Header.Set(ContentTypeHeader, mimeType) if !shouldRespBeSavedToCache(resp.Response) { - return reader, resp.Response, err + return reader, resp.Response.Header, resp.StatusCode, err } // now we write to cache and respond at the sime time @@ -166,7 +167,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str ETag: resp.Header.Get(ETagHeader), MimeType: mimeType, } - return fileResp.CreateCacheReader(reader, client.responseCache, cacheKey), resp.Response, nil + return fileResp.CreateCacheReader(reader, client.responseCache, cacheKey), resp.Response.Header, resp.StatusCode, nil } case http.StatusNotFound: @@ -177,12 +178,12 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str log.Error().Err(err).Msg("could not save 404 in cache") } - return nil, resp.Response, ErrorNotFound + return nil, resp.Response.Header, http.StatusNotFound, ErrorNotFound default: - return nil, resp.Response, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) + return nil, resp.Response.Header, resp.StatusCode, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } } - return nil, nil, err + return nil, nil, http.StatusInternalServerError, err } func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (*BranchTimestamp, error) { diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 3c69bc2..38b7d1a 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -84,7 +84,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin log.Debug().Msg("Preparing") - reader, res, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) + reader, header, statusCode, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) if reader != nil { defer reader.Close() } @@ -92,7 +92,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin log.Debug().Msg("Aquisting") // Handle errors - if (err != nil && errors.Is(err, gitea.ErrorNotFound)) || (res == nil) { + if (err != nil && errors.Is(err, gitea.ErrorNotFound)) || (reader == nil) { if o.TryIndexPages { // copy the o struct & try if an index page exists optionsForIndexPages := *o @@ -128,8 +128,8 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin } return false } - if res != nil && (err != nil || res.StatusCode != http.StatusOK) { - log.Printf("Couldn't fetch contents (status code %d): %v\n", res.StatusCode, err) + if reader != nil && (err != nil || statusCode != http.StatusOK) { + log.Printf("Couldn't fetch contents (status code %d): %v\n", statusCode, err) html.ReturnErrorPage(ctx, fmt.Sprintf("%v", err), http.StatusInternalServerError) return true } @@ -152,15 +152,13 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin log.Debug().Msg("Handling error") // Set ETag & MIME - if res != nil { - 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(gitea.ContentTypeHeader, res.Header.Get(gitea.ContentTypeHeader)) - } else { - ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime) - } + ctx.Response().Header.Set(gitea.ETagHeader, header.Get(gitea.ETagHeader)) + ctx.Response().Header.Set(gitea.PagesCacheIndicatorHeader, header.Get(gitea.PagesCacheIndicatorHeader)) + ctx.Response().Header.Set(gitea.ContentLengthHeader, header.Get(gitea.ContentLengthHeader)) + if o.ServeRaw { + ctx.Response().Header.Set(gitea.ContentTypeHeader, header.Get(gitea.ContentTypeHeader)) + } else { + ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime) } if ctx.Response().StatusCode != http.StatusNotFound {