MCPcopy Create free account
hub / github.com/base/base / server

Function server

acceptance/src/check.rs:851–887  ·  view source on GitHub ↗
(replies: Vec<Reply>)

Source from the content-addressed store, hash-verified

849 }
850
851 async fn server(replies: Vec<Reply>) -> String {
852 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
853 let address = listener.local_addr().unwrap();
854 let fallback = replies
855 .last()
856 .cloned()
857 .unwrap_or_else(|| Reply { delay: Duration::ZERO, body: rpc(Value::Null) });
858 let replies = Arc::new(Mutex::new(VecDeque::from(replies)));
859 tokio::spawn(async move {
860 loop {
861 let Ok((mut socket, _)) = listener.accept().await else { break };
862 let replies = Arc::clone(&replies);
863 let fallback = fallback.clone();
864 tokio::spawn(async move {
865 let mut request = vec![0; 8192];
866 let _ = socket.read(&mut request).await;
867 let mut reply = replies.lock().await.pop_front().unwrap_or(fallback);
868 tokio::time::sleep(reply.delay).await;
869 if let Ok(mut body) = serde_json::from_str::<Value>(&reply.body)
870 && let Some(offset) = body["result"]["timestamp"].as_i64()
871 {
872 let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
873 body["result"]["timestamp"] =
874 json!(format!("0x{:x}", now.saturating_add_signed(offset)));
875 reply.body = body.to_string();
876 }
877 let response = format!(
878 "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
879 reply.body.len(),
880 reply.body
881 );
882 let _ = socket.write_all(response.as_bytes()).await;
883 });
884 }
885 });
886 format!("http://{address}")
887 }
888
889 fn rpc(result: Value) -> String {
890 json!({

Calls 6

local_addrMethod · 0.80
rpcFunction · 0.70
newFunction · 0.50
cloneMethod · 0.45
readMethod · 0.45
lockMethod · 0.45

Tested by

no test coverage detected