|
|
@ -23,6 +23,12 @@ type Config struct { |
|
|
|
MaxOpenConnections int |
|
|
|
} |
|
|
|
|
|
|
|
const ( |
|
|
|
// maxBodyBytes controls the maximum number of bytes the
|
|
|
|
// server will read parsing the request body.
|
|
|
|
maxBodyBytes = int64(1000000) // 1MB
|
|
|
|
) |
|
|
|
|
|
|
|
// StartHTTPServer starts an HTTP server on listenAddr with the given handler.
|
|
|
|
// It wraps handler with RecoverAndLogHandler.
|
|
|
|
func StartHTTPServer( |
|
|
@ -53,7 +59,7 @@ func StartHTTPServer( |
|
|
|
go func() { |
|
|
|
err := http.Serve( |
|
|
|
listener, |
|
|
|
RecoverAndLogHandler(handler, logger), |
|
|
|
RecoverAndLogHandler(maxBytesHandler{h: handler, n: maxBodyBytes}, logger), |
|
|
|
) |
|
|
|
logger.Error("RPC HTTP server stopped", "err", err) |
|
|
|
}() |
|
|
@ -99,7 +105,7 @@ func StartHTTPAndTLSServer( |
|
|
|
go func() { |
|
|
|
err := http.ServeTLS( |
|
|
|
listener, |
|
|
|
RecoverAndLogHandler(handler, logger), |
|
|
|
RecoverAndLogHandler(maxBytesHandler{h: handler, n: maxBodyBytes}, logger), |
|
|
|
certFile, |
|
|
|
keyFile, |
|
|
|
) |
|
|
@ -202,3 +208,13 @@ func (w *ResponseWriterWrapper) WriteHeader(status int) { |
|
|
|
func (w *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
|
|
|
return w.ResponseWriter.(http.Hijacker).Hijack() |
|
|
|
} |
|
|
|
|
|
|
|
type maxBytesHandler struct { |
|
|
|
h http.Handler |
|
|
|
n int64 |
|
|
|
} |
|
|
|
|
|
|
|
func (h maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, h.n) |
|
|
|
h.h.ServeHTTP(w, r) |
|
|
|
} |