Add timeouts

Adds read and write timeout configuration settings and allows to
configure a maximum acceptable header size.
This commit is contained in:
Felix Niederwanger 2023-06-06 16:33:00 +02:00
parent cec58caffe
commit 03cb62aba7
Signed by: phoenix
GPG key ID: 31860289A704FB3C
3 changed files with 24 additions and 4 deletions

View file

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

View file

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

View file

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