mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-05 14:07:01 +00:00
refactor, dedup & pass-forward
This commit is contained in:
parent
0339a52efc
commit
f31c0b1659
3 changed files with 17 additions and 17 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue