MCPcopy Create free account
hub / github.com/CPChain/chain / ServeHTTP

Method ServeHTTP

api/rpc/http.go:177–200  ·  view source on GitHub ↗

ServeHTTP serves JSON-RPC requests over HTTP.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

175
176// ServeHTTP serves JSON-RPC requests over HTTP.
177func (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.

Callers 1

ServeHTTPMethod · 0.45

Calls 7

ServeSingleRequestMethod · 0.95
validateRequestFunction · 0.85
NewJSONCodecFunction · 0.85
HeaderMethod · 0.80
ErrorMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected