This commit is contained in:
6543 2022-11-11 06:16:56 +01:00
parent f18169fdc2
commit a107ce0a05
No known key found for this signature in database
GPG Key ID: B8BE6D610E61C862
3 changed files with 29 additions and 17 deletions

View File

@ -50,7 +50,7 @@ func TestGetContent(t *testing.T) {
assert.EqualValues(t, http.StatusOK, resp.StatusCode)
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
assert.True(t, getSize(resp.Body) > 1000)
assert.Len(t, resp.Header.Get("ETag"), 42)
assert.Len(t, resp.Header.Get("ETag"), 44)
// access branch name contains '/'
resp, err = getTestHTTPSClient().Get("https://blumia.localhost.mock.directory:4430/pages-server-integration-tests/@docs~main/")
@ -60,7 +60,7 @@ func TestGetContent(t *testing.T) {
}
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
assert.True(t, getSize(resp.Body) > 100)
assert.Len(t, resp.Header.Get("ETag"), 42)
assert.Len(t, resp.Header.Get("ETag"), 44)
// TODO: test get of non cachable content (content size > fileCacheSizeLimit)
}

View File

@ -17,6 +17,7 @@ func New(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
RespWriter: w,
Req: r,
StatusCode: http.StatusOK,
}
}

View File

@ -88,16 +88,15 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
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 {
defer reader.Close()
}
ctx.StatusCode = _statusCode
log.Debug().Msg("Aquisting")
// Handle errors
if (err != nil && errors.Is(err, gitea.ErrorNotFound)) || (reader == nil) {
// Handle not found error
if err != nil && errors.Is(err, gitea.ErrorNotFound) {
if o.TryIndexPages {
// copy the o struct & try if an index page exists
optionsForIndexPages := *o
@ -133,9 +132,27 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
}
return false
}
if reader != nil && (err != nil || ctx.StatusCode != http.StatusOK) {
log.Printf("Couldn't fetch contents (status code %d): %v\n", ctx.StatusCode, err)
html.ReturnErrorPage(ctx, fmt.Sprintf("%v", err), http.StatusInternalServerError)
// handle unexpected client errors
if err != nil || reader == nil || statusCode != http.StatusOK {
log.Debug().Msg("Handling error")
var msg string
if err != nil {
msg = "gitea client returned unexpected error"
log.Error().Err(err).Msg(msg)
msg = fmt.Sprintf("%s: %v", msg, err)
}
if reader == nil {
msg = "gitea client returned no reader"
log.Error().Msg(msg)
}
if statusCode != http.StatusOK {
msg = fmt.Sprintf("Couldn't fetch contents (status code %d)", statusCode)
log.Error().Msg(msg)
}
html.ReturnErrorPage(ctx, msg, http.StatusInternalServerError)
return true
}
@ -154,8 +171,6 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
return true
}
log.Debug().Msg("Handling error")
// Set ETag & MIME
if eTag := header.Get(gitea.ETagHeader); eTag != "" {
ctx.RespWriter.Header().Set(gitea.ETagHeader, eTag)
@ -171,15 +186,12 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
} else {
ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, mime)
}
if ctx.StatusCode != http.StatusNotFound {
// Everything's okay so far
ctx.StatusCode = http.StatusOK
}
ctx.RespWriter.Header().Set(headerLastModified, o.BranchTimestamp.In(time.UTC).Format(time.RFC1123))
log.Debug().Msg("Prepare response")
ctx.RespWriter.WriteHeader(ctx.StatusCode)
// Write the response body to the original request
if reader != nil {
_, err := io.Copy(ctx.RespWriter, reader)
@ -191,7 +203,6 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
}
log.Debug().Msg("Sending response")
ctx.RespWriter.WriteHeader(ctx.StatusCode)
return true
}