MCPcopy Create free account
hub / github.com/ElementsProject/lightning / handle

Method handle

plugins/lsps-plugin/src/core/router.rs:110–155  ·  view source on GitHub ↗
(&self, ctx: &RequestContext, request: &[u8])

Source from the content-addressed store, hash-verified

108
109impl JsonRpcRouter {
110 pub async fn handle(&self, ctx: &RequestContext, request: &[u8]) -> Option<Vec<u8>> {
111 #[derive(Deserialize)]
112 struct BorrowedRequest<'a> {
113 jsonrpc: &'a str,
114 method: &'a str,
115 #[serde(borrow)]
116 id: Option<&'a str>,
117 #[serde(borrow)]
118 params: Option<&'a RawValue>,
119 }
120
121 let req: BorrowedRequest<'_> = match serde_json::from_slice(request) {
122 Ok(req) => req,
123 Err(_) => {
124 return Some(error_response(
125 None,
126 RpcError::parse_error("failed to parse request"),
127 ));
128 }
129 };
130
131 if req.jsonrpc != "2.0" {
132 return Some(error_response(req.id, RpcError::invalid_request("")));
133 }
134
135 let handler = match self.handlers.get(req.method) {
136 Some(h) => h,
137 None => return Some(error_response(req.id, RpcError::method_not_found(""))),
138 };
139
140 // Notification -> no response
141 let id = match req.id {
142 Some(id) => id,
143 None => return None,
144 };
145
146 let param_bytes = match req.params {
147 Some(raw) => raw.get().as_bytes(),
148 None => b"{}",
149 };
150
151 Some(match handler(ctx, param_bytes).await {
152 Ok(r) => success_response(id, r),
153 Err(e) => error_response(Some(id), e),
154 })
155 }
156
157 pub fn methods(&self) -> Vec<&'static str> {
158 self.handlers.keys().copied().collect()

Calls 6

from_sliceFunction · 0.85
error_responseFunction · 0.85
parse_errorFunction · 0.85
success_responseFunction · 0.85
handlerFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected