MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / hash

Function hash

crates/test-programs/src/bin/p2_api_proxy_streaming.rs:256–300  ·  view source on GitHub ↗
(url: &Url)

Source from the content-addressed store, hash-verified

254}
255
256async fn hash(url: &Url) -> Result<String> {
257 let request = OutgoingRequest::new(Fields::new());
258
259 request
260 .set_path_with_query(Some(url.path()))
261 .map_err(|()| anyhow!("failed to set path_with_query"))?;
262 request
263 .set_scheme(Some(&match url.scheme() {
264 "http" => Scheme::Http,
265 "https" => Scheme::Https,
266 scheme => Scheme::Other(scheme.into()),
267 }))
268 .map_err(|()| anyhow!("failed to set scheme"))?;
269 request
270 .set_authority(Some(&format!(
271 "{}{}",
272 url.host_str().unwrap_or(""),
273 if let Some(port) = url.port() {
274 format!(":{port}")
275 } else {
276 String::new()
277 }
278 )))
279 .map_err(|()| anyhow!("failed to set authority"))?;
280
281 let response = executor::outgoing_request_send(request).await?;
282
283 let status = response.status();
284
285 if !(200..300).contains(&status) {
286 bail!("unexpected status: {status}");
287 }
288
289 let mut body =
290 executor::incoming_body(response.consume().expect("response should be readable"));
291
292 use sha2::Digest;
293 let mut hasher = sha2::Sha256::new();
294 while let Some(chunk) = body.try_next().await? {
295 hasher.update(&chunk);
296 }
297
298 use base64::Engine;
299 Ok(base64::engine::general_purpose::STANDARD_NO_PAD.encode(hasher.finalize()))
300}
301
302// Technically this should not be here for a proxy, but given the current
303// framework for tests it's required since this file is built as a `bin`

Callers 3

handle_requestFunction · 0.70
hashMethod · 0.50
hashMethod · 0.50

Calls 15

outgoing_request_sendFunction · 0.85
incoming_bodyFunction · 0.85
OkFunction · 0.85
schemeMethod · 0.80
statusMethod · 0.80
newFunction · 0.50
set_path_with_queryMethod · 0.45
pathMethod · 0.45
set_schemeMethod · 0.45
set_authorityMethod · 0.45
containsMethod · 0.45
expectMethod · 0.45

Tested by

no test coverage detected