MCPcopy Create free account
hub / github.com/emilk/ehttp / fetch_streaming_blocking

Function fetch_streaming_blocking

ehttp/src/streaming/native.rs:8–100  ·  view source on GitHub ↗
(
    request: Request,
    on_data: Box<dyn Fn(crate::Result<Part>) -> ControlFlow<()> + Send>,
)

Source from the content-addressed store, hash-verified

6use crate::types::PartialResponse;
7
8pub fn fetch_streaming_blocking(
9 request: Request,
10 on_data: Box<dyn Fn(crate::Result<Part>) -> ControlFlow<()> + Send>,
11) {
12 let resp = request.fetch_raw_native(false);
13
14 let mut resp = match resp {
15 Ok(t) => t,
16 Err(e) => {
17 let _ = on_data(Err(e.to_string()));
18 return;
19 }
20 };
21
22 let ok = resp.status().is_success();
23 use ureq::ResponseExt as _;
24 let url = resp.get_uri().to_string();
25 let status = resp.status().as_u16();
26 let status_text = resp
27 .status()
28 .canonical_reason()
29 .unwrap_or("ERROR")
30 .to_string();
31 let mut headers = crate::Headers::default();
32 for (k, v) in resp.headers().iter() {
33 headers.insert(
34 k,
35 match v.to_str() {
36 Ok(t) => t,
37 Err(e) => {
38 let _ = on_data(Err(e.to_string()));
39 break;
40 }
41 },
42 );
43 }
44 headers.sort(); // It reads nicer, and matches web backend.
45
46 let response = PartialResponse {
47 url,
48 ok,
49 status,
50 status_text,
51 headers,
52 };
53 if on_data(Ok(Part::Response(response))).is_break() {
54 return;
55 };
56
57 let mut reader = resp.body_mut().as_reader();
58 loop {
59 let mut buf = vec![0; 2048];
60 use std::io::Read;
61 match reader.read(&mut buf) {
62 Ok(n) if n > 0 => {
63 // clone data from buffer and clear it
64 let chunk = buf[..n].to_vec();
65 if on_data(Ok(Part::Chunk(chunk))).is_break() {

Callers 1

fetch_streamingFunction · 0.85

Calls 4

ResponseClass · 0.85
fetch_raw_nativeMethod · 0.80
insertMethod · 0.80
sortMethod · 0.80

Tested by

no test coverage detected