mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
wip
This commit is contained in:
parent
7f2854d537
commit
05e075dd83
5 changed files with 25 additions and 23 deletions
|
@ -10,6 +10,7 @@ import (
|
||||||
type Context struct {
|
type Context struct {
|
||||||
RespWriter http.ResponseWriter
|
RespWriter http.ResponseWriter
|
||||||
Req *http.Request
|
Req *http.Request
|
||||||
|
StatusCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(w http.ResponseWriter, r *http.Request) *Context {
|
func New(w http.ResponseWriter, r *http.Request) *Context {
|
||||||
|
@ -27,10 +28,7 @@ func (c *Context) Context() stdContext.Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Response() *http.Response {
|
func (c *Context) Response() *http.Response {
|
||||||
if c.Req != nil {
|
if c.Req != nil && c.Req.Response != nil {
|
||||||
if c.Req.Response == nil {
|
|
||||||
c.Req.Response = &http.Response{Header: make(http.Header)}
|
|
||||||
}
|
|
||||||
return c.Req.Response
|
return c.Req.Response
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -243,6 +243,7 @@ func (client *Client) getMimeTypeByExtension(resource string) string {
|
||||||
if client.forbiddenMimeTypes[mimeTypeSplit[0]] || mimeType == "" {
|
if client.forbiddenMimeTypes[mimeTypeSplit[0]] || mimeType == "" {
|
||||||
mimeType = client.defaultMimeType
|
mimeType = client.defaultMimeType
|
||||||
}
|
}
|
||||||
|
log.Trace().Msgf("probe mime: %s", mimeType)
|
||||||
return mimeType
|
return mimeType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -119,8 +119,8 @@ func Handler(mainDomainSuffix, rawDomain string,
|
||||||
|
|
||||||
if canonicalLink != "" {
|
if canonicalLink != "" {
|
||||||
// Hide from search machines & add canonical link
|
// Hide from search machines & add canonical link
|
||||||
ctx.Response().Header.Set("X-Robots-Tag", "noarchive, noindex")
|
ctx.RespWriter.Header().Set("X-Robots-Tag", "noarchive, noindex")
|
||||||
ctx.Response().Header.Set("Link",
|
ctx.RespWriter.Header().Set("Link",
|
||||||
strings.NewReplacer("%b", targetBranch, "%p", targetPath).Replace(canonicalLink)+
|
strings.NewReplacer("%b", targetBranch, "%p", targetPath).Replace(canonicalLink)+
|
||||||
"; rel=\"canonical\"",
|
"; rel=\"canonical\"",
|
||||||
)
|
)
|
||||||
|
|
|
@ -44,6 +44,6 @@ func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
|
||||||
|
|
||||||
// Try to request the file from the Gitea API
|
// Try to request the file from the Gitea API
|
||||||
if !targetOptions.Upstream(ctx, giteaClient) {
|
if !targetOptions.Upstream(ctx, giteaClient) {
|
||||||
html.ReturnErrorPage(ctx, "", ctx.Response().StatusCode)
|
html.ReturnErrorPage(ctx, "", ctx.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,19 +75,22 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the browser has a cached version
|
// Check if the browser has a cached version
|
||||||
if ifModifiedSince, err := time.Parse(time.RFC1123, string(ctx.Response().Header.Get(headerIfModifiedSince))); err == nil {
|
if ctx.Response() != nil {
|
||||||
if !ifModifiedSince.Before(o.BranchTimestamp) {
|
if ifModifiedSince, err := time.Parse(time.RFC1123, string(ctx.Response().Header.Get(headerIfModifiedSince))); err == nil {
|
||||||
ctx.RespWriter.WriteHeader(http.StatusNotModified)
|
if !ifModifiedSince.Before(o.BranchTimestamp) {
|
||||||
return true
|
ctx.RespWriter.WriteHeader(http.StatusNotModified)
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Preparing")
|
log.Debug().Msg("Preparing")
|
||||||
|
|
||||||
reader, header, statusCode, 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 {
|
if reader != nil {
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
}
|
}
|
||||||
|
ctx.StatusCode = _statusCode
|
||||||
|
|
||||||
log.Debug().Msg("Aquisting")
|
log.Debug().Msg("Aquisting")
|
||||||
|
|
||||||
|
@ -113,7 +116,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Response().StatusCode = http.StatusNotFound
|
ctx.StatusCode = http.StatusNotFound
|
||||||
if o.TryIndexPages {
|
if o.TryIndexPages {
|
||||||
// copy the o struct & try if a not found page exists
|
// copy the o struct & try if a not found page exists
|
||||||
optionsForNotFoundPages := *o
|
optionsForNotFoundPages := *o
|
||||||
|
@ -128,8 +131,8 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if reader != nil && (err != nil || statusCode != http.StatusOK) {
|
if reader != nil && (err != nil || ctx.StatusCode != http.StatusOK) {
|
||||||
log.Printf("Couldn't fetch contents (status code %d): %v\n", statusCode, err)
|
log.Printf("Couldn't fetch contents (status code %d): %v\n", ctx.StatusCode, err)
|
||||||
html.ReturnErrorPage(ctx, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
html.ReturnErrorPage(ctx, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -152,20 +155,20 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
log.Debug().Msg("Handling error")
|
log.Debug().Msg("Handling error")
|
||||||
|
|
||||||
// Set ETag & MIME
|
// Set ETag & MIME
|
||||||
ctx.Response().Header.Set(gitea.ETagHeader, header.Get(gitea.ETagHeader))
|
ctx.RespWriter.Header().Set(gitea.ETagHeader, header.Get(gitea.ETagHeader))
|
||||||
ctx.Response().Header.Set(gitea.PagesCacheIndicatorHeader, header.Get(gitea.PagesCacheIndicatorHeader))
|
ctx.RespWriter.Header().Set(gitea.PagesCacheIndicatorHeader, header.Get(gitea.PagesCacheIndicatorHeader))
|
||||||
ctx.Response().Header.Set(gitea.ContentLengthHeader, header.Get(gitea.ContentLengthHeader))
|
ctx.RespWriter.Header().Set(gitea.ContentLengthHeader, header.Get(gitea.ContentLengthHeader))
|
||||||
if o.ServeRaw {
|
if o.ServeRaw {
|
||||||
ctx.Response().Header.Set(gitea.ContentTypeHeader, header.Get(gitea.ContentTypeHeader))
|
ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, header.Get(gitea.ContentTypeHeader))
|
||||||
} else {
|
} else {
|
||||||
ctx.Response().Header.Set(gitea.ContentTypeHeader, rawMime)
|
ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, rawMime)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Response().StatusCode != http.StatusNotFound {
|
if ctx.StatusCode != http.StatusNotFound {
|
||||||
// Everything's okay so far
|
// Everything's okay so far
|
||||||
ctx.Response().StatusCode = http.StatusOK
|
ctx.StatusCode = http.StatusOK
|
||||||
}
|
}
|
||||||
ctx.Response().Header.Set(headerLastModified, o.BranchTimestamp.In(time.UTC).Format(time.RFC1123))
|
ctx.RespWriter.Header().Set(headerLastModified, o.BranchTimestamp.In(time.UTC).Format(time.RFC1123))
|
||||||
|
|
||||||
log.Debug().Msg("Prepare response")
|
log.Debug().Msg("Prepare response")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue