refactor, dedup & pass-forward

This commit is contained in:
6543 2022-11-07 23:21:35 +01:00
parent 0339a52efc
commit f31c0b1659
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
3 changed files with 17 additions and 17 deletions

View file

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

View file

@ -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")
}

View file

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