fix: "Too big request header" (#244)

* 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>
This commit is contained in:
alphanoob1337 2023-11-20 09:27:40 +01:00 committed by GitHub
parent 2c8ba9c0f3
commit 8e7570eee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 8 deletions

View File

@ -30,7 +30,7 @@ jobs: # Docs: <https://git.io/JvxXE>
- 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}

View File

@ -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

View File

@ -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: <https://docs.docker.com/engine/reference/builder/#healthcheck>
HEALTHCHECK --interval=7s --timeout=2s CMD ["/bin/error-pages", "--log-json", "healthcheck"]

View File

@ -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

1
internal/env/env.go vendored
View File

@ -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.

View File

@ -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 {

View File

@ -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: <https://github.com/valyala/fasthttp>
fast: &fasthttp.Server{
WriteTimeout: defaultWriteTimeout,
ReadBufferSize: int(readBufferSize),
ReadTimeout: defaultReadTimeout,
IdleTimeout: defaultIdleTimeout,
NoDefaultServerHeader: true,