MCPcopy
hub / github.com/go-kit/kit / ServeHTTP

Method ServeHTTP

transport/http/jsonrpc/server.go:92–175  ·  view source on GitHub ↗

ServeHTTP implements http.Handler.

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

Source from the content-addressed store, hash-verified

90
91// ServeHTTP implements http.Handler.
92func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
93 if r.Method != http.MethodPost {
94 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
95 w.WriteHeader(http.StatusMethodNotAllowed)
96 _, _ = io.WriteString(w, "405 must POST\n")
97 return
98 }
99 ctx := r.Context()
100
101 if s.finalizer != nil {
102 iw := &interceptingWriter{w, http.StatusOK}
103 defer func() { s.finalizer(ctx, iw.code, r) }()
104 w = iw
105 }
106
107 for _, f := range s.before {
108 ctx = f(ctx, r)
109 }
110
111 // Decode the body into an object
112 var req Request
113 err := json.NewDecoder(r.Body).Decode(&req)
114 if err != nil {
115 rpcerr := parseError("JSON could not be decoded: " + err.Error())
116 s.logger.Log("err", rpcerr)
117 s.errorEncoder(ctx, rpcerr, w)
118 return
119 }
120
121 ctx = context.WithValue(ctx, requestIDKey, req.ID)
122 ctx = context.WithValue(ctx, ContextKeyRequestMethod, req.Method)
123
124 for _, f := range s.beforeCodec {
125 ctx = f(ctx, r, req)
126 }
127
128 // Get the endpoint and codecs from the map using the method
129 // defined in the JSON object
130 ecm, ok := s.ecm[req.Method]
131 if !ok {
132 err := methodNotFoundError(fmt.Sprintf("Method %s was not found.", req.Method))
133 s.logger.Log("err", err)
134 s.errorEncoder(ctx, err, w)
135 return
136 }
137
138 // Decode the JSON "params"
139 reqParams, err := ecm.Decode(ctx, req.Params)
140 if err != nil {
141 s.logger.Log("err", err)
142 s.errorEncoder(ctx, err, w)
143 return
144 }
145
146 // Call the Endpoint with the params
147 response, err := ecm.Endpoint(ctx, reqParams)
148 if err != nil {
149 s.logger.Log("err", err)

Callers

nothing calls this directly

Calls 7

parseErrorTypeAlias · 0.85
methodNotFoundErrorTypeAlias · 0.85
SetMethod · 0.65
EndpointMethod · 0.65
WriteHeaderMethod · 0.45
ErrorMethod · 0.45
LogMethod · 0.45

Tested by

no test coverage detected