@@ -33,8 +33,8 @@ import (
3333)
3434
3535const (
36- maxRequestContentLength = 1024 * 1024 * 5
37- contentType = "application/json"
36+ defaultBodyLimit = 5 * 1024 * 1024
37+ contentType = "application/json"
3838)
3939
4040// https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13
@@ -253,8 +253,8 @@ type httpServerConn struct {
253253 r * http.Request
254254}
255255
256- func newHTTPServerConn (r * http.Request , w http.ResponseWriter ) ServerCodec {
257- body := io .LimitReader (r .Body , maxRequestContentLength )
256+ func ( s * Server ) newHTTPServerConn (r * http.Request , w http.ResponseWriter ) ServerCodec {
257+ body := io .LimitReader (r .Body , int64 ( s . httpBodyLimit ) )
258258 conn := & httpServerConn {Reader : body , Writer : w , r : r }
259259
260260 encoder := func (v any , isErrorResponse bool ) error {
@@ -312,7 +312,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
312312 w .WriteHeader (http .StatusOK )
313313 return
314314 }
315- if code , err := validateRequest (r ); err != nil {
315+ if code , err := s . validateRequest (r ); err != nil {
316316 http .Error (w , err .Error (), code )
317317 return
318318 }
@@ -330,19 +330,19 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
330330 // until EOF, writes the response to w, and orders the server to process a
331331 // single request.
332332 w .Header ().Set ("content-type" , contentType )
333- codec := newHTTPServerConn (r , w )
333+ codec := s . newHTTPServerConn (r , w )
334334 defer codec .close ()
335335 s .serveSingleRequest (ctx , codec )
336336}
337337
338338// validateRequest returns a non-zero response code and error message if the
339339// request is invalid.
340- func validateRequest (r * http.Request ) (int , error ) {
340+ func ( s * Server ) validateRequest (r * http.Request ) (int , error ) {
341341 if r .Method == http .MethodPut || r .Method == http .MethodDelete {
342342 return http .StatusMethodNotAllowed , errors .New ("method not allowed" )
343343 }
344- if r .ContentLength > maxRequestContentLength {
345- err := fmt .Errorf ("content length too large (%d>%d)" , r .ContentLength , maxRequestContentLength )
344+ if r .ContentLength > int64 ( s . httpBodyLimit ) {
345+ err := fmt .Errorf ("content length too large (%d>%d)" , r .ContentLength , s . httpBodyLimit )
346346 return http .StatusRequestEntityTooLarge , err
347347 }
348348 // Allow OPTIONS (regardless of content-type)
0 commit comments