From 03cb62aba73241b63e4439ae964486489aaa5820 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 6 Jun 2023 16:33:00 +0200 Subject: [PATCH] Add timeouts Adds read and write timeout configuration settings and allows to configure a maximum acceptable header size. --- cmd/weblug/config.go | 14 +++++++++++--- cmd/weblug/weblug.go | 11 ++++++++++- weblug.yml | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/weblug/config.go b/cmd/weblug/config.go index 190eab2..92fe0c0 100644 --- a/cmd/weblug/config.go +++ b/cmd/weblug/config.go @@ -13,13 +13,21 @@ type Config struct { } type ConfigSettings struct { - BindAddress string `yaml:"bind"` // Bind address for the webserver - UID int `yaml:"uid"` // Custom user ID or 0, if not being used - GID int `yaml:"gid"` // Custom group ID or 0, if not being used + BindAddress string `yaml:"bind"` // Bind address for the webserver + UID int `yaml:"uid"` // Custom user ID or 0, if not being used + GID int `yaml:"gid"` // Custom group ID or 0, if not being used + ReadTimeout int `yaml:"readtimeout"` // Timeout for reading the whole request + WriteTimeout int `yaml:"writetimeout"` // Timeout for writing the whole response + MaxHeaderBytes int `yaml:"maxheadersize"` // Maximum size of the receive body } func (cf *Config) SetDefaults() { cf.Settings.BindAddress = ":2088" + cf.Settings.UID = 0 + cf.Settings.GID = 0 + cf.Settings.ReadTimeout = 0 + cf.Settings.WriteTimeout = 0 + cf.Settings.MaxHeaderBytes = 0 } // Check performs sanity checks on the config diff --git a/cmd/weblug/weblug.go b/cmd/weblug/weblug.go index 3dba720..a9b90fe 100644 --- a/cmd/weblug/weblug.go +++ b/cmd/weblug/weblug.go @@ -10,6 +10,7 @@ import ( "os" "os/signal" "syscall" + "time" ) var cf Config @@ -122,7 +123,15 @@ func main() { awaitTerminationSignal() log.Printf("Launching webserver on %s", cf.Settings.BindAddress) - log.Fatal(http.ListenAndServe(cf.Settings.BindAddress, nil)) + server := &http.Server{ + Addr: cf.Settings.BindAddress, + ReadTimeout: time.Duration(cf.Settings.ReadTimeout) * time.Second, + WriteTimeout: time.Duration(cf.Settings.WriteTimeout) * time.Second, + MaxHeaderBytes: cf.Settings.MaxHeaderBytes, + } + err := server.ListenAndServe() + log.Fatal(err) + os.Exit(1) } // create a http handler function from the given hook diff --git a/weblug.yml b/weblug.yml index 7d788c5..e746de0 100644 --- a/weblug.yml +++ b/weblug.yml @@ -8,6 +8,9 @@ settings: # This is a known issue, see https://codeberg.org/grisu48/weblug/issues/9 uid: 0 # run under specified user id gid: 0 # run under specified group id + readtimeout: 10 # if set, maximum number of seconds to receive the full request + writetimeout: 10 # if set, maximum number of seconds to send the full response + maxheadersize: 4096 # maximum header size # hook definitions. A hook needs to define the HTTP endpoint ("route") and the command # See the following examples for more possible options.