From fd24b4a2bc7f6210bc18da09903accd2235697c2 Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 12 Jul 2022 15:32:48 +0200 Subject: [PATCH 1/4] Pass logger to fasthttp (#98) - Use a logger with `FASTHTTP` prefix as fasthttp's logger so it's easy to see what fasthttp is logging in console/journal. Co-authored-by: Gusted Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/98 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: Gusted Co-committed-by: Gusted --- server/setup.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/setup.go b/server/setup.go index 027b3e1..ea96d1e 100644 --- a/server/setup.go +++ b/server/setup.go @@ -5,12 +5,20 @@ import ( "net/http" "time" + "github.com/rs/zerolog/log" + "github.com/valyala/fasthttp" "codeberg.org/codeberg/pages/server/cache" "codeberg.org/codeberg/pages/server/utils" ) +type fasthttpLogger struct{} + +func (fasthttpLogger) Printf(format string, args ...interface{}) { + log.Printf("[FASTHTTP] "+format, args...) +} + func SetupServer(handler fasthttp.RequestHandler) *fasthttp.Server { // Enable compression by wrapping the handler with the compression function provided by FastHTTP compressedHandler := fasthttp.CompressHandlerBrotliLevel(handler, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed) @@ -21,6 +29,7 @@ func SetupServer(handler fasthttp.RequestHandler) *fasthttp.Server { NoDefaultServerHeader: true, NoDefaultDate: true, ReadTimeout: 30 * time.Second, // needs to be this high for ACME certificates with ZeroSSL & HTTP-01 challenge + Logger: fasthttpLogger{}, } } From baf4e7e326baea1a08e539d89f7c73c2980152ee Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 15 Jul 2022 17:18:25 +0200 Subject: [PATCH 2/4] Make the 404 page more readable and natural (#104) Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/104 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: Jeremy Co-committed-by: Jeremy --- html/404.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/html/404.html b/html/404.html index 08ab16d..b7ec96e 100644 --- a/html/404.html +++ b/html/404.html @@ -23,11 +23,11 @@

- This page was not found! + Page not found!

- Sorry, this page doesn't exist or is inaccessible for other reasons (%status).
- We hope this is not our fault ;) - Make sure to check the troubleshooting section in the Docs! + Sorry, but this page couldn't be found or is inaccessible (%status).
+ We hope this isn't a problem on our end ;) - Make sure to check the troubleshooting section in the Docs!
From 5411c96ef391e84cef358258272bf0e6fd406f59 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.codeberg.org> Date: Fri, 15 Jul 2022 21:06:05 +0200 Subject: [PATCH 3/4] Tell fasthttp to not set "Content-Length: 0" on non cached content (#107) fix #97 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/107 --- integration/get_test.go | 2 ++ server/gitea/client.go | 1 - server/upstream/upstream.go | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/integration/get_test.go b/integration/get_test.go index 1997010..8af40f0 100644 --- a/integration/get_test.go +++ b/integration/get_test.go @@ -59,6 +59,8 @@ 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) + + // TODO: test get of non cachable content (content size > fileCacheSizeLimit) } func TestCustomDomain(t *testing.T) { diff --git a/server/gitea/client.go b/server/gitea/client.go index 7b5d009..3b9ad6f 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -82,7 +82,6 @@ func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) { if err != nil { return nil, err } - // resp.SetBodyStream(&strings.Reader{}, -1) if err != nil { return nil, err diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 7c9a035..b9fc2bc 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -181,6 +181,9 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, var cacheBodyWriter bytes.Buffer if res != nil { if res.Header.ContentLength() > fileCacheSizeLimit { + // fasthttp else will set "Content-Length: 0" + ctx.Response.SetBodyStream(&strings.Reader{}, -1) + err = res.BodyWriteTo(ctx.Response.BodyWriter()) } else { // TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick? From bcaceda71177d701f91c01eb04213e126de71ea0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.codeberg.org> Date: Fri, 15 Jul 2022 21:21:26 +0200 Subject: [PATCH 4/4] dont cache if ContentLength greater fileCacheSizeLimit (#108) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/108 Reviewed-by: Otto --- Justfile | 4 ++++ server/upstream/upstream.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Justfile b/Justfile index a908e7a..c824efa 100644 --- a/Justfile +++ b/Justfile @@ -21,6 +21,10 @@ lint: tool-golangci tool-gofumpt fmt: tool-gofumpt gofumpt -w --extra . +clean: + go clean ./... + rm -rf build/ + tool-golangci: @hash golangci-lint> /dev/null 2>&1; if [ $? -ne 0 ]; then \ go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \ diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index b9fc2bc..5e888ac 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -199,7 +199,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, } log.Debug().Msg("response") - if res != nil && ctx.Err() == nil { + if res != nil && res.Header.ContentLength() > fileCacheSizeLimit && ctx.Err() == nil { cachedResponse.Exists = true cachedResponse.MimeType = mimeType cachedResponse.Body = cacheBodyWriter.Bytes()