less indirect

This commit is contained in:
6543 2022-11-08 00:05:27 +01:00
parent b2323ee8a5
commit 7c45a420e4
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
4 changed files with 34 additions and 35 deletions

View file

@ -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 .

View file

@ -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 {

View file

@ -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) {

View file

@ -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,16 +152,14 @@ 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))
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, res.Header.Get(gitea.ContentTypeHeader))
ctx.Response().Header.Set(gitea.ContentTypeHeader, header.Get(gitea.ContentTypeHeader))
} else {
ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime)
}
}
if ctx.Response().StatusCode != http.StatusNotFound {
// Everything's okay so far