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 {
|
if f.IsSymlink {
|
||||||
resp.Header.Set(giteaObjectTypeHeader, objTypeSymlink)
|
resp.Header.Set(giteaObjectTypeHeader, objTypeSymlink)
|
||||||
}
|
}
|
||||||
resp.Header.Set(eTagHeader, f.ETag)
|
resp.Header.Set(ETagHeader, f.ETag)
|
||||||
resp.Header.Set(contentTypeHeader, f.MimeType)
|
resp.Header.Set(ContentTypeHeader, f.MimeType)
|
||||||
resp.Header.Set(contentLengthHeader, fmt.Sprint(len(f.Body)))
|
resp.Header.Set(ContentLengthHeader, fmt.Sprint(len(f.Body)))
|
||||||
resp.Header.Set(pagesCacheIndicator, "true")
|
resp.Header.Set(PagesCacheIndicatorHeader, "true")
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,16 +28,16 @@ const (
|
||||||
rawContentCacheKeyPrefix = "rawContent"
|
rawContentCacheKeyPrefix = "rawContent"
|
||||||
|
|
||||||
// pages server
|
// pages server
|
||||||
pagesCacheIndicator = "X-Pages-Cache"
|
PagesCacheIndicatorHeader = "X-Pages-Cache"
|
||||||
|
|
||||||
// gitea
|
// gitea
|
||||||
giteaObjectTypeHeader = "X-Gitea-Object-Type"
|
giteaObjectTypeHeader = "X-Gitea-Object-Type"
|
||||||
objTypeSymlink = "symlink"
|
objTypeSymlink = "symlink"
|
||||||
|
|
||||||
// std
|
// std
|
||||||
eTagHeader = "ETag"
|
ETagHeader = "ETag"
|
||||||
contentTypeHeader = "Content-Type"
|
ContentTypeHeader = "Content-Type"
|
||||||
contentLengthHeader = "Content-Length"
|
ContentLengthHeader = "Content-Length"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -140,7 +140,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
Exists: true,
|
Exists: true,
|
||||||
IsSymlink: true,
|
IsSymlink: true,
|
||||||
Body: []byte(linkDest),
|
Body: []byte(linkDest),
|
||||||
ETag: resp.Header.Get(eTagHeader),
|
ETag: resp.Header.Get(ETagHeader),
|
||||||
}, fileCacheTimeout); err != nil {
|
}, fileCacheTimeout); err != nil {
|
||||||
log.Error().Err(err).Msg("could not save symlink in cache")
|
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
|
// Set the MIME type
|
||||||
mimeType := client.getMimeTypeByExtension(resource)
|
mimeType := client.getMimeTypeByExtension(resource)
|
||||||
resp.Response.Header.Set(contentTypeHeader, mimeType)
|
resp.Response.Header.Set(ContentTypeHeader, mimeType)
|
||||||
|
|
||||||
if !shouldRespBeSavedToCache(resp.Response) {
|
if !shouldRespBeSavedToCache(resp.Response) {
|
||||||
return reader, resp.Response, err
|
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
|
// now we write to cache and respond at the sime time
|
||||||
fileResp := FileResponse{
|
fileResp := FileResponse{
|
||||||
Exists: true,
|
Exists: true,
|
||||||
ETag: resp.Header.Get(eTagHeader),
|
ETag: resp.Header.Get(ETagHeader),
|
||||||
MimeType: mimeType,
|
MimeType: mimeType,
|
||||||
}
|
}
|
||||||
return fileResp.CreateCacheReader(reader, client.responseCache, cacheKey), resp.Response, nil
|
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:
|
case http.StatusNotFound:
|
||||||
if err := client.responseCache.Set(cacheKey, FileResponse{
|
if err := client.responseCache.Set(cacheKey, FileResponse{
|
||||||
Exists: false,
|
Exists: false,
|
||||||
ETag: resp.Header.Get(eTagHeader),
|
ETag: resp.Header.Get(ETagHeader),
|
||||||
}, fileCacheTimeout); err != nil {
|
}, fileCacheTimeout); err != nil {
|
||||||
log.Error().Err(err).Msg("could not save 404 in cache")
|
log.Error().Err(err).Msg("could not save 404 in cache")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
headerContentType = "Content-Type"
|
|
||||||
headerETag = "ETag"
|
|
||||||
headerLastModified = "Last-Modified"
|
headerLastModified = "Last-Modified"
|
||||||
headerIfModifiedSince = "If-Modified-Since"
|
headerIfModifiedSince = "If-Modified-Since"
|
||||||
|
|
||||||
|
@ -155,11 +153,13 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
|
|
||||||
// Set ETag & MIME
|
// Set ETag & MIME
|
||||||
if res != nil {
|
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 {
|
if o.ServeRaw {
|
||||||
ctx.Response().Header.Set(headerContentType, res.Header.Get(headerContentType))
|
ctx.Response().Header.Set(gitea.ContentTypeHeader, res.Header.Get(gitea.ContentTypeHeader))
|
||||||
} else {
|
} else {
|
||||||
ctx.Response().Header.Set(headerContentType, rawMime)
|
ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue