Skip to content

Commit 4013e23

Browse files
committed
rpc: allow dumb empty requests for AWS health checks
1 parent 5aa3eac commit 4013e23

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

rpc/http.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ func NewHTTPServer(cors []string, srv *Server) *http.Server {
146146

147147
// ServeHTTP serves JSON-RPC requests over HTTP.
148148
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
149+
// Permit dumb empty requests for remote health-checks (AWS)
150+
if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" {
151+
return
152+
}
153+
// For meaningful requests, validate it's size and content type
149154
if r.ContentLength > maxHTTPRequestContentLength {
150155
http.Error(w,
151156
fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength),
152157
http.StatusRequestEntityTooLarge)
153158
return
154159
}
155-
156160
ct := r.Header.Get("content-type")
157161
mt, _, err := mime.ParseMediaType(ct)
158162
if err != nil || mt != "application/json" {
@@ -161,14 +165,13 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
161165
http.StatusUnsupportedMediaType)
162166
return
163167
}
164-
165-
w.Header().Set("content-type", "application/json")
166-
167-
// create a codec that reads direct from the request body until
168-
// EOF and writes the response to w and order the server to process
169-
// a single request.
168+
// All checks passed, create a codec that reads direct from the request body
169+
// untilEOF and writes the response to w and order the server to process a
170+
// single request.
170171
codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w})
171172
defer codec.Close()
173+
174+
w.Header().Set("content-type", "application/json")
172175
srv.ServeSingleRequest(codec, OptionMethodInvocation)
173176
}
174177

0 commit comments

Comments
 (0)