Merge pull request #15496 from karalabe/rpc-get-healthcheck
rpc: allow dumb empty requests for AWS health checks
This commit is contained in:
commit
87f5b4123c
17
rpc/http.go
17
rpc/http.go
@ -146,13 +146,17 @@ func NewHTTPServer(cors []string, srv *Server) *http.Server {
|
|||||||
|
|
||||||
// ServeHTTP serves JSON-RPC requests over HTTP.
|
// ServeHTTP serves JSON-RPC requests over HTTP.
|
||||||
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Permit dumb empty requests for remote health-checks (AWS)
|
||||||
|
if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// For meaningful requests, validate it's size and content type
|
||||||
if r.ContentLength > maxHTTPRequestContentLength {
|
if r.ContentLength > maxHTTPRequestContentLength {
|
||||||
http.Error(w,
|
http.Error(w,
|
||||||
fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength),
|
fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength),
|
||||||
http.StatusRequestEntityTooLarge)
|
http.StatusRequestEntityTooLarge)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ct := r.Header.Get("content-type")
|
ct := r.Header.Get("content-type")
|
||||||
mt, _, err := mime.ParseMediaType(ct)
|
mt, _, err := mime.ParseMediaType(ct)
|
||||||
if err != nil || mt != "application/json" {
|
if err != nil || mt != "application/json" {
|
||||||
@ -161,14 +165,13 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.StatusUnsupportedMediaType)
|
http.StatusUnsupportedMediaType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// All checks passed, create a codec that reads direct from the request body
|
||||||
w.Header().Set("content-type", "application/json")
|
// untilEOF and writes the response to w and order the server to process a
|
||||||
|
// single request.
|
||||||
// create a codec that reads direct from the request body until
|
|
||||||
// EOF and writes the response to w and order the server to process
|
|
||||||
// a single request.
|
|
||||||
codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w})
|
codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w})
|
||||||
defer codec.Close()
|
defer codec.Close()
|
||||||
|
|
||||||
|
w.Header().Set("content-type", "application/json")
|
||||||
srv.ServeSingleRequest(codec, OptionMethodInvocation)
|
srv.ServeSingleRequest(codec, OptionMethodInvocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user