Skip to content

Commit 5b905ae

Browse files
authored
Merge pull request #350 from OffchainLabs/request_size
Add config option to change HTTPBodyLimit and WSReadLimit
2 parents 48de203 + a1fc200 commit 5b905ae

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

node/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ type Config struct {
221221
EnablePersonal bool `toml:"-"`
222222

223223
DBEngine string `toml:",omitempty"`
224+
225+
// HTTPBodyLimit is the maximum number of bytes allowed in the HTTP request body.
226+
HTTPBodyLimit int `toml:",omitempty"`
227+
228+
// WSReadLimit is the maximum number of bytes allowed in the websocket request body.
229+
WSReadLimit int64 `toml:",omitempty"`
224230
}
225231

226232
// IPCEndpoint resolves an IPC endpoint based on a configured value, taking into

node/node.go

+12
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ func (n *Node) startRPC() error {
423423
batchResponseSizeLimit: n.config.BatchResponseMaxSize,
424424
apiFilter: n.apiFilter,
425425
}
426+
if n.config.HTTPBodyLimit != 0 {
427+
rpcConfig.httpBodyLimit = n.config.HTTPBodyLimit
428+
}
429+
if n.config.WSReadLimit != 0 {
430+
rpcConfig.wsReadLimit = n.config.WSReadLimit
431+
}
426432

427433
initHttp := func(server *httpServer, port int) error {
428434
if err := server.setListenAddr(n.config.HTTPHost, port); err != nil {
@@ -470,6 +476,12 @@ func (n *Node) startRPC() error {
470476
batchResponseSizeLimit: engineAPIBatchResponseSizeLimit,
471477
httpBodyLimit: engineAPIBodyLimit,
472478
}
479+
if n.config.HTTPBodyLimit != 0 {
480+
sharedConfig.httpBodyLimit = n.config.HTTPBodyLimit
481+
}
482+
if n.config.WSReadLimit != 0 {
483+
sharedConfig.wsReadLimit = n.config.WSReadLimit
484+
}
473485
err := server.enableRPC(allAPIs, httpConfig{
474486
CorsAllowedOrigins: DefaultAuthCors,
475487
Vhosts: n.config.AuthVirtualHosts,

node/rpcstack.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type rpcEndpointConfig struct {
5858
batchResponseSizeLimit int
5959
apiFilter map[string]bool
6060
httpBodyLimit int
61+
wsReadLimit int64
6162
}
6263

6364
type rpcHandler struct {
@@ -362,7 +363,7 @@ func (h *httpServer) enableWS(apis []rpc.API, config wsConfig) error {
362363
}
363364
h.wsConfig = config
364365
h.wsHandler.Store(&rpcHandler{
365-
Handler: NewWSHandlerStack(srv.WebsocketHandler(config.Origins), config.jwtSecret),
366+
Handler: NewWSHandlerStack(srv.WebsocketHandler(config.Origins, config.wsReadLimit), config.jwtSecret),
366367
server: srv,
367368
})
368369
return nil

rpc/client_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func TestClientSubscriptionChannelClose(t *testing.T) {
584584

585585
var (
586586
srv = NewServer()
587-
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil))
587+
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil, 0))
588588
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
589589
)
590590
defer srv.Stop()
@@ -745,7 +745,7 @@ func TestClientReconnect(t *testing.T) {
745745
if err != nil {
746746
t.Fatal("can't listen:", err)
747747
}
748-
go http.Serve(l, srv.WebsocketHandler([]string{"*"}))
748+
go http.Serve(l, srv.WebsocketHandler([]string{"*"}, 0))
749749
return srv, l
750750
}
751751

@@ -811,7 +811,7 @@ func httpTestClient(srv *Server, transport string, fl *flakeyListener) (*Client,
811811
var hs *httptest.Server
812812
switch transport {
813813
case "ws":
814-
hs = httptest.NewUnstartedServer(srv.WebsocketHandler([]string{"*"}))
814+
hs = httptest.NewUnstartedServer(srv.WebsocketHandler([]string{"*"}, 0))
815815
case "http":
816816
hs = httptest.NewUnstartedServer(srv)
817817
default:

rpc/websocket.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ var wsBufferPool = new(sync.Pool)
4747
//
4848
// allowedOrigins should be a comma-separated list of allowed origin URLs.
4949
// To allow connections with any origin, pass "*".
50-
func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
50+
func (s *Server) WebsocketHandler(allowedOrigins []string, wsReadLimit int64) http.Handler {
51+
if wsReadLimit == 0 {
52+
wsReadLimit = wsDefaultReadLimit
53+
}
5154
var upgrader = websocket.Upgrader{
5255
ReadBufferSize: wsReadBuffer,
5356
WriteBufferSize: wsWriteBuffer,
@@ -60,7 +63,7 @@ func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
6063
log.Debug("WebSocket upgrade failed", "err", err)
6164
return
6265
}
63-
codec := newWebsocketCodec(conn, r.Host, r.Header, wsDefaultReadLimit)
66+
codec := newWebsocketCodec(conn, r.Host, r.Header, wsReadLimit)
6467
s.ServeCodec(codec, 0)
6568
})
6669
}

rpc/websocket_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestWebsocketOriginCheck(t *testing.T) {
5353

5454
var (
5555
srv = newTestServer()
56-
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"http://example.com"}))
56+
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"http://example.com"}, 0))
5757
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
5858
)
5959
defer srv.Stop()
@@ -83,7 +83,7 @@ func TestWebsocketLargeCall(t *testing.T) {
8383

8484
var (
8585
srv = newTestServer()
86-
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}))
86+
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}, 0))
8787
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
8888
)
8989
defer srv.Stop()
@@ -119,7 +119,7 @@ func TestWebsocketLargeRead(t *testing.T) {
119119

120120
var (
121121
srv = newTestServer()
122-
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}))
122+
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}, 0))
123123
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
124124
)
125125
defer srv.Stop()
@@ -176,7 +176,7 @@ func TestWebsocketLargeRead(t *testing.T) {
176176
func TestWebsocketPeerInfo(t *testing.T) {
177177
var (
178178
s = newTestServer()
179-
ts = httptest.NewServer(s.WebsocketHandler([]string{"origin.example.com"}))
179+
ts = httptest.NewServer(s.WebsocketHandler([]string{"origin.example.com"}, 0))
180180
tsurl = "ws:" + strings.TrimPrefix(ts.URL, "http:")
181181
)
182182
defer s.Stop()
@@ -260,7 +260,7 @@ func TestClientWebsocketPing(t *testing.T) {
260260
func TestClientWebsocketLargeMessage(t *testing.T) {
261261
var (
262262
srv = NewServer()
263-
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil))
263+
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil, 0))
264264
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
265265
)
266266
defer srv.Stop()

0 commit comments

Comments
 (0)