forked from cerc-io/plugeth
This commit is contained in:
parent
e79821cabe
commit
3796751efc
27
rpc/http.go
27
rpc/http.go
@ -36,11 +36,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
contentType = "application/json"
|
|
||||||
maxRequestContentLength = 1024 * 512
|
maxRequestContentLength = 1024 * 512
|
||||||
)
|
)
|
||||||
|
|
||||||
var nullAddr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:0")
|
var (
|
||||||
|
// https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13
|
||||||
|
acceptedContentTypes = []string{"application/json", "application/json-rpc", "application/jsonrequest"}
|
||||||
|
contentType = acceptedContentTypes[0]
|
||||||
|
nullAddr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:0")
|
||||||
|
)
|
||||||
|
|
||||||
type httpConn struct {
|
type httpConn struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
@ -263,12 +267,21 @@ func validateRequest(r *http.Request) (int, error) {
|
|||||||
err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
|
err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
|
||||||
return http.StatusRequestEntityTooLarge, err
|
return http.StatusRequestEntityTooLarge, err
|
||||||
}
|
}
|
||||||
mt, _, err := mime.ParseMediaType(r.Header.Get("content-type"))
|
// Allow OPTIONS (regardless of content-type)
|
||||||
if r.Method != http.MethodOptions && (err != nil || mt != contentType) {
|
if r.Method == http.MethodOptions {
|
||||||
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
|
return 0, nil
|
||||||
return http.StatusUnsupportedMediaType, err
|
|
||||||
}
|
}
|
||||||
return 0, nil
|
// Check content-type
|
||||||
|
if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
|
||||||
|
for _, accepted := range acceptedContentTypes {
|
||||||
|
if accepted == mt {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Invalid content-type
|
||||||
|
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
|
||||||
|
return http.StatusUnsupportedMediaType, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {
|
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {
|
||||||
|
Loading…
Reference in New Issue
Block a user