ServeHTTP serves JSON-RPC requests over HTTP.
(w http.ResponseWriter, r *http.Request)
| 175 | |
| 176 | // ServeHTTP serves JSON-RPC requests over HTTP. |
| 177 | func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 178 | // Permit dumb empty requests for remote health-checks (AWS) |
| 179 | if r.Method == http.MethodGet && r.ContentLength == 0 && r.URL.RawQuery == "" { |
| 180 | return |
| 181 | } |
| 182 | if code, err := validateRequest(r); err != nil { |
| 183 | http.Error(w, err.Error(), code) |
| 184 | return |
| 185 | } |
| 186 | // All checks passed, create a codec that reads direct from the request body |
| 187 | // untilEOF and writes the response to w and order the server to process a |
| 188 | // single request. |
| 189 | ctx := r.Context() |
| 190 | ctx = context.WithValue(ctx, "remote", r.RemoteAddr) |
| 191 | ctx = context.WithValue(ctx, "scheme", r.Proto) |
| 192 | ctx = context.WithValue(ctx, "local", r.Host) |
| 193 | |
| 194 | body := io.LimitReader(r.Body, maxRequestContentLength) |
| 195 | codec := NewJSONCodec(&httpReadWriteNopCloser{body, w}) |
| 196 | defer codec.Close() |
| 197 | |
| 198 | w.Header().Set("content-type", contentType) |
| 199 | srv.ServeSingleRequest(ctx, codec, OptionMethodInvocation) |
| 200 | } |
| 201 | |
| 202 | // validateRequest returns a non-zero response code and error message if the |
| 203 | // request is invalid. |
no test coverage detected