You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit cefe99f
Browse filesBrowse the repository at this point in the historyBrowse files
response: use a rolling response timeout when sending data to clients
Previously, clients could DoS the server by requesting large files
without ever reading them from the output socket because the timeout
was set based upon the size of the files being requested. This change
adds a rolling send timeout such that for every chunk of data that is
read by the client, the timeout resets. If the client is too slow,
then the connection is eventually terminated.
To test this change you can set up a file server with a low send timeout:
#lang racket/base
(require (prefix-in files: web-server/dispatchers/dispatch-files)
web-server/dispatchers/filesystem-map
web-server/web-server)
(define shutdown
(serve
#:dispatch (files:make #:url->path (make-url->path "."))
#:port 9000
#:response-send-timeout 5))
(with-handlers ([exn:break? (lambda _
(shutdown))])
(sync never-evt))
create a large file in the directory that the server is running in:
# creates a 100MB file called test.dat filled with zeros
# replace "bs=1M" with "bs=1m" on macOs
dd if=/dev/null of=test.dat count=100 bs=1M
and try to read that file using a client that's intentionally slower
than that send timeout:
#lang racket/base
(require racket/format
racket/tcp)
(define-values (in out)
(tcp-connect "127.0.0.1" 9000))
(parameterize ([current-output-port out])
(display "GET /test.dat HTTP/1.1\r\n")
(display "\r\n")
(flush-output))
(define st (current-inexact-milliseconds))
(define n 0)
(let loop ()
(printf "delta: ~ams and ~amb\n"
(- (current-inexact-milliseconds) st)
(~r (/ n 1024 1024)))
(define bs (read-bytes (* 1 1024 1024) in))
(unless (eof-object? bs)
(set! n (+ n (bytes-length bs)))
(sleep 10)
(loop)))
Following this sequence of steps on my machine, the client gets
disconnected within about 20 seconds:
$ racket client.rkt
delta: 0.075927734375ms and 0mb
delta: 10009.180908203125ms and 1mb
delta: 20015.35791015625ms and 1.5544mb
$
Before this change, the client would only get disconnected after 100
million seconds (1 second per every byte in the requested file).
0 commit comments