Skip to content

Commit 3796751

Browse files
authored
rpc: add application/json-rpc as accepted content type, fixes #18293 (#18310)
1 parent e79821c commit 3796751

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

rpc/http.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ import (
3636
)
3737

3838
const (
39-
contentType = "application/json"
4039
maxRequestContentLength = 1024 * 512
4140
)
4241

43-
var nullAddr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:0")
42+
var (
43+
// https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13
44+
acceptedContentTypes = []string{"application/json", "application/json-rpc", "application/jsonrequest"}
45+
contentType = acceptedContentTypes[0]
46+
nullAddr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:0")
47+
)
4448

4549
type httpConn struct {
4650
client *http.Client
@@ -263,12 +267,21 @@ func validateRequest(r *http.Request) (int, error) {
263267
err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
264268
return http.StatusRequestEntityTooLarge, err
265269
}
266-
mt, _, err := mime.ParseMediaType(r.Header.Get("content-type"))
267-
if r.Method != http.MethodOptions && (err != nil || mt != contentType) {
268-
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
269-
return http.StatusUnsupportedMediaType, err
270+
// Allow OPTIONS (regardless of content-type)
271+
if r.Method == http.MethodOptions {
272+
return 0, nil
273+
}
274+
// Check content-type
275+
if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
276+
for _, accepted := range acceptedContentTypes {
277+
if accepted == mt {
278+
return 0, nil
279+
}
280+
}
270281
}
271-
return 0, nil
282+
// Invalid content-type
283+
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
284+
return http.StatusUnsupportedMediaType, err
272285
}
273286

274287
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {

0 commit comments

Comments
 (0)