From 8e7570eee3e1d97319c053582cbcc5db5b8e13fe Mon Sep 17 00:00:00 2001 From: alphanoob1337 Date: Mon, 20 Nov 2023 09:27:40 +0100 Subject: [PATCH] fix: "Too big request header" (#244) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added environment variable and CLI parameter for adjusting the read buffer size * Changed readBufferSize to int and some bugfixing * Small fixes * ci: 👷 CI system updated * ci: 👷 CI system updated --------- Co-authored-by: Paramtamtam <7326800+tarampampam@users.noreply.github.com> --- .github/workflows/tests.yml | 2 +- CHANGELOG.md | 3 +++ Dockerfile | 3 ++- internal/cli/serve/command.go | 28 +++++++++++++++++++++++----- internal/env/env.go | 1 + internal/env/env_test.go | 2 ++ internal/http/server.go | 3 ++- 7 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6b55905..83f649f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,7 +30,7 @@ jobs: # Docs: - uses: actions/checkout@v4 - uses: gacts/setup-go-with-cache@v1 - with: {go-version: 1.19} + with: {go-version-file: go.mod} - uses: golangci/golangci-lint-action@v3 with: {skip-pkg-cache: true, skip-build-cache: true} diff --git a/CHANGELOG.md b/CHANGELOG.md index c60b642..193e4b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,11 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher ### Added - Error pages now translated into 🇵🇱 [#226] +- Possibility to set custom read buffer size (using `--read-buffer-size` flag or environment variable `READ_BUFFER_SIZE`) [#238], [#244] [#226]:https://github.com/tarampampam/error-pages/pull/226 +[#238]:https://github.com/tarampampam/error-pages/issues/238 +[#244]:https://github.com/tarampampam/error-pages/pull/244 ## v2.25.0 diff --git a/Dockerfile b/Dockerfile index 42d99d0..980c283 100644 --- a/Dockerfile +++ b/Dockerfile @@ -79,7 +79,8 @@ ENV LISTEN_PORT="8080" \ DEFAULT_ERROR_PAGE="404" \ DEFAULT_HTTP_CODE="404" \ SHOW_DETAILS="false" \ - DISABLE_L10N="false" + DISABLE_L10N="false" \ + READ_BUFFER_SIZE="2048" # Docs: HEALTHCHECK --interval=7s --timeout=2s CMD ["/bin/error-pages", "--log-json", "healthcheck"] diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index 0eb4791..32315c5 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -33,6 +33,7 @@ const ( proxyHTTPHeadersFlagName = "proxy-headers" disableL10nFlagName = "disable-l10n" catchAllFlagName = "catch-all" + readBufferSizeFlagName = "read-buffer-size" ) const ( @@ -107,7 +108,7 @@ func NewCommand(log *zap.Logger) *cli.Command { //nolint:funlen return fmt.Errorf("wrong default HTTP response code [%d]", o.Default.HTTPCode) } - return cmd.Run(c.Context, log, cfg, ip, port, o) + return cmd.Run(c.Context, log, cfg, ip, port, c.Uint(readBufferSizeFlagName), o) }, Flags: []cli.Flag{ shared.ConfigFileFlag, @@ -158,6 +159,11 @@ func NewCommand(log *zap.Logger) *cli.Command { //nolint:funlen Usage: "catch all pages", EnvVars: []string{env.CatchAll.String()}, }, + &cli.UintFlag{ + Name: readBufferSizeFlagName, + Usage: "read buffer size (0 = use default value)", + EnvVars: []string{env.ReadBufferSize.String()}, + }, }, } @@ -166,7 +172,13 @@ func NewCommand(log *zap.Logger) *cli.Command { //nolint:funlen // Run current command. func (cmd *command) Run( //nolint:funlen - parentCtx context.Context, log *zap.Logger, cfg *config.Config, ip string, port uint16, opt options.ErrorPage, + parentCtx context.Context, + log *zap.Logger, + cfg *config.Config, + ip string, + port uint16, + readBufferSize uint, + opt options.ErrorPage, ) error { var ( ctx, cancel = context.WithCancel(parentCtx) // serve context creation @@ -226,7 +238,7 @@ func (cmd *command) Run( //nolint:funlen } // create HTTP server - server := appHttp.NewServer(log) + server := appHttp.NewServer(log, readBufferSize) // register server routes, middlewares, etc. if err := server.Register(cfg, picker, opt); err != nil { @@ -239,7 +251,7 @@ func (cmd *command) Run( //nolint:funlen go func(errCh chan<- error) { defer close(errCh) - log.Info("Server starting", + var fields = []zap.Field{ zap.String("addr", ip), zap.Uint16("port", port), zap.String("default error page", opt.Default.PageCode), @@ -248,7 +260,13 @@ func (cmd *command) Run( //nolint:funlen zap.Bool("show request details", opt.ShowDetails), zap.Bool("localization disabled", opt.L10n.Disabled), zap.Bool("catch all enabled", opt.CatchAll), - ) + } + + if readBufferSize > 0 { + fields = append(fields, zap.Uint("read buffer size", readBufferSize)) + } + + log.Info("Server starting", fields...) if err := server.Start(ip, port); err != nil { errCh <- err diff --git a/internal/env/env.go b/internal/env/env.go index 3c64460..21348f7 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -19,6 +19,7 @@ const ( ProxyHTTPHeaders envVariable = "PROXY_HTTP_HEADERS" // proxy HTTP request headers list (request -> response) DisableL10n envVariable = "DISABLE_L10N" // disable pages localization CatchAll envVariable = "CATCH_ALL" // catch all pages + ReadBufferSize envVariable = "READ_BUFFER_SIZE" // https://github.com/tarampampam/error-pages/issues/238 ) // String returns environment variable name in the string representation. diff --git a/internal/env/env_test.go b/internal/env/env_test.go index 2e8d131..0873d7a 100644 --- a/internal/env/env_test.go +++ b/internal/env/env_test.go @@ -18,6 +18,7 @@ func TestConstants(t *testing.T) { assert.Equal(t, "PROXY_HTTP_HEADERS", string(ProxyHTTPHeaders)) assert.Equal(t, "DISABLE_L10N", string(DisableL10n)) assert.Equal(t, "CATCH_ALL", string(CatchAll)) + assert.Equal(t, "READ_BUFFER_SIZE", string(ReadBufferSize)) } func TestEnvVariable_Lookup(t *testing.T) { @@ -34,6 +35,7 @@ func TestEnvVariable_Lookup(t *testing.T) { {giveEnv: ProxyHTTPHeaders}, {giveEnv: DisableL10n}, {giveEnv: CatchAll}, + {giveEnv: ReadBufferSize}, } for _, tt := range cases { diff --git a/internal/http/server.go b/internal/http/server.go index 8d7f297..9f0c733 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -39,13 +39,14 @@ const ( defaultIdleTimeout = time.Second * 6 ) -func NewServer(log *zap.Logger) Server { +func NewServer(log *zap.Logger, readBufferSize uint) Server { rdr := tpl.NewTemplateRenderer() return Server{ // fasthttp docs: fast: &fasthttp.Server{ WriteTimeout: defaultWriteTimeout, + ReadBufferSize: int(readBufferSize), ReadTimeout: defaultReadTimeout, IdleTimeout: defaultIdleTimeout, NoDefaultServerHeader: true,