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

Function parseRequest

api/rpc/json.go:172–216  ·  view source on GitHub ↗

parseRequest will parse a single request from the given RawMessage. It will return the parsed request, an indication if the request was a batch or an error when the request could not be parsed.

(incomingMsg json.RawMessage)

Source from the content-addressed store, hash-verified

170// the parsed request, an indication if the request was a batch or an error when
171// the request could not be parsed.
172func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) {
173 var in jsonRequest
174 if err := json.Unmarshal(incomingMsg, &in); err != nil {
175 return nil, false, &invalidMessageError{err.Error()}
176 }
177
178 if err := checkReqId(in.Id); err != nil {
179 return nil, false, &invalidMessageError{err.Error()}
180 }
181
182 // subscribe are special, they will always use `subscribeMethod` as first param in the payload
183 if strings.HasSuffix(in.Method, subscribeMethodSuffix) {
184 reqs := []rpcRequest{{id: &in.Id, isPubSub: true}}
185 if len(in.Payload) > 0 {
186 // first param must be subscription name
187 var subscribeMethod [1]string
188 if err := json.Unmarshal(in.Payload, &subscribeMethod); err != nil {
189 log.Debug(fmt.Sprintf("Unable to parse subscription method: %v\n", err))
190 return nil, false, &invalidRequestError{"Unable to parse subscription request"}
191 }
192
193 reqs[0].service, reqs[0].method = strings.TrimSuffix(in.Method, subscribeMethodSuffix), subscribeMethod[0]
194 reqs[0].params = in.Payload
195 return reqs, false, nil
196 }
197 return nil, false, &invalidRequestError{"Unable to parse subscription request"}
198 }
199
200 if strings.HasSuffix(in.Method, unsubscribeMethodSuffix) {
201 return []rpcRequest{{id: &in.Id, isPubSub: true,
202 method: in.Method, params: in.Payload}}, false, nil
203 }
204
205 elems := strings.Split(in.Method, serviceMethodSeparator)
206 if len(elems) != 2 {
207 return nil, false, &methodNotFoundError{in.Method, ""}
208 }
209
210 // regular RPC call
211 if len(in.Payload) == 0 {
212 return []rpcRequest{{service: elems[0], method: elems[1], id: &in.Id}}, false, nil
213 }
214
215 return []rpcRequest{{service: elems[0], method: elems[1], id: &in.Id, params: in.Payload}}, false, nil
216}
217
218// parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication
219// if the request was a batch or an error when the request could not be read.

Callers 1

ReadRequestHeadersMethod · 0.85

Calls 3

checkReqIdFunction · 0.85
DebugMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected