mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
Merge 5002dd773563b7b04760bf794724e4e6e9f2835a into 1bd0ecf2d3e08da37efa270bb7be20c1eddb0d68
This commit is contained in:
commit
f31de8ad9e
10
gin.go
10
gin.go
@ -31,7 +31,7 @@ const (
|
||||
)
|
||||
|
||||
// defaultReadHeaderTimeout is the default ReadHeaderTimeout applied to the
|
||||
// http.Server created by Run, RunTLS and RunListener. It protects against
|
||||
// http.Server created by Run, RunTLS, RunUnix and RunListener. It protects against
|
||||
// slowloris-style resource exhaustion when the caller does not configure a
|
||||
// custom http.Server.
|
||||
const defaultReadHeaderTimeout = 10 * time.Second
|
||||
@ -174,7 +174,7 @@ type Engine struct {
|
||||
MaxMultipartMemory int64
|
||||
|
||||
// ReadHeaderTimeout is the maximum duration allowed for reading the request
|
||||
// headers by the http.Server created through Run, RunTLS and RunListener.
|
||||
// headers by the http.Server created through Run, RunTLS, RunUnix and RunListener.
|
||||
// If zero or negative, a default of 10 seconds is used. Set to a positive
|
||||
// value to customize, or use a custom http.Server directly to disable it.
|
||||
ReadHeaderTimeout time.Duration
|
||||
@ -606,6 +606,9 @@ func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {
|
||||
// RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
|
||||
// through the specified unix socket (i.e. a file).
|
||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||
// The created http.Server is configured with a default ReadHeaderTimeout of 10 seconds
|
||||
// (configurable via Engine.ReadHeaderTimeout) to mitigate slowloris attacks.
|
||||
// For full control over timeouts, create a custom http.Server and use ServeHTTP.
|
||||
func (engine *Engine) RunUnix(file string) (err error) {
|
||||
debugPrint("Listening and serving HTTP on unix:/%s", file)
|
||||
defer func() { debugPrintError(err) }()
|
||||
@ -623,7 +626,8 @@ func (engine *Engine) RunUnix(file string) (err error) {
|
||||
defer os.Remove(file)
|
||||
|
||||
server := &http.Server{ // #nosec G112
|
||||
Handler: engine.Handler(),
|
||||
Handler: engine.Handler(),
|
||||
ReadHeaderTimeout: engine.getReadHeaderTimeout(),
|
||||
}
|
||||
err = server.Serve(listener)
|
||||
return
|
||||
|
||||
@ -275,6 +275,36 @@ func TestUnixSocket(t *testing.T) {
|
||||
assert.Contains(t, response, "it worked", "resp body should match")
|
||||
}
|
||||
|
||||
func TestUnixSocketReadHeaderTimeout(t *testing.T) {
|
||||
router := New()
|
||||
router.ReadHeaderTimeout = 200 * time.Millisecond
|
||||
|
||||
unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test_timeout")
|
||||
|
||||
defer os.Remove(unixTestSocket)
|
||||
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.RunUnix(unixTestSocket))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
c, err := net.Dial("unix", unixTestSocket)
|
||||
require.NoError(t, err)
|
||||
defer c.Close()
|
||||
|
||||
// headers without the final CRLF keep the server waiting for them
|
||||
fmt.Fprint(c, "GET /example HTTP/1.1\r\nHost: gin.example\r\n")
|
||||
|
||||
start := time.Now()
|
||||
require.NoError(t, c.SetReadDeadline(time.Now().Add(5*time.Second)))
|
||||
buf := make([]byte, 512)
|
||||
_, _ = c.Read(buf)
|
||||
assert.Less(t, time.Since(start), 2*time.Second, "server should close the connection after ReadHeaderTimeout")
|
||||
}
|
||||
|
||||
func TestBadUnixSocket(t *testing.T) {
|
||||
router := New()
|
||||
require.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user