eggfetch-core 0.2.2

Async-first Rust HTTP client engine (tokio + hyper) with connection pooling, phase-aware timeouts, TLS, proxies, and compression
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#![allow(
    missing_docs,
    dead_code,
    unused_mut,
    clippy::large_futures,
    clippy::missing_panics_doc,
    clippy::redundant_closure_for_method_calls,
    clippy::inefficient_to_string,
    clippy::manual_let_else,
    clippy::single_char_pattern,
    clippy::match_same_arms,
    clippy::needless_borrow,
    clippy::trim_split_whitespace,
    clippy::too_many_lines,
    clippy::unused_self,
    clippy::items_after_statements,
    clippy::expect_fun_call,
    clippy::len_zero,
    clippy::unnecessary_debug_formatting,
    clippy::format_push_string,
    clippy::new_without_default,
    clippy::map_unwrap_or
)]
//! Local TCP test server for connection management and streaming tests.

use std::io::{BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::Duration;

/// Configuration for a test server instance.
pub struct TestServerConfig {
    /// If true, send `Connection: close` in responses.
    pub close_connection: bool,
    /// Delay before responding (ms). 0 = immediate.
    pub response_delay_ms: u64,
    /// If true, read and discard the request body.
    pub consume_body: bool,
    /// Custom response body. If None, defaults to b"OK".
    pub response_body: Option<Vec<u8>>,
    /// If true, send response using chunked transfer encoding.
    /// Each chunk is sent with an optional inter-chunk delay.
    pub chunked: bool,
    /// Delay between chunked response chunks (ms). 0 = no delay.
    pub chunk_delay_ms: u64,
    /// After this many chunks have been sent, sleep `chunk_stall_ms`
    /// milliseconds before the next chunk (only meaningful when
    /// `chunked` is true).
    pub chunk_stall_after: Option<usize>,
    /// Stall duration in ms after `chunk_stall_after` chunks.
    pub chunk_stall_ms: u64,
    /// If set, send a redirect response with this status code and the
    /// value as the Location header. The body is empty for redirects.
    pub redirect: Option<(u16, String)>,
}

impl Default for TestServerConfig {
    fn default() -> Self {
        Self {
            close_connection: false,
            response_delay_ms: 0,
            consume_body: true,
            response_body: None,
            chunked: false,
            chunk_delay_ms: 0,
            chunk_stall_after: None,
            chunk_stall_ms: 0,
            redirect: None,
        }
    }
}

/// A running local TCP test server.
pub struct TestServer {
    port: u16,
    #[allow(dead_code)]
    connections_accepted: Arc<AtomicUsize>,
    #[allow(dead_code)]
    requests_served: Arc<AtomicUsize>,
    /// Currently open TCP connections (accepted minus closed).
    ///
    /// Incremented on accept and decremented when the per-connection handler
    /// exits. Used by idle-eviction tests to observe background pool expiry
    /// without issuing a second request: when Hyper's idle timer evicts a
    /// pooled connection it closes the socket, which the handler observes
    /// as EOF and exits.
    #[allow(dead_code)]
    open_connections: Arc<AtomicUsize>,
    shutdown: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
}

impl TestServer {
    /// Start a test server on a random available port.
    ///
    /// Uses a blocking accept loop. The blocking accept is intentional: a
    /// non-blocking accept with a busy-sleep thread interferes with
    /// hyper-util's connection pool in `#[tokio::test]` (current-thread
    /// runtime). To shut down, we open a dummy connection from this thread
    /// to ourselves, which unblocks `accept` and lets the loop observe the
    /// shutdown flag.
    ///
    /// # Panics
    ///
    /// Panics if the server cannot bind to a local port.
    pub fn start(config: &TestServerConfig) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind");
        let port = listener.local_addr().unwrap().port();
        let connections_accepted = Arc::new(AtomicUsize::new(0));
        let requests_served = Arc::new(AtomicUsize::new(0));
        let open_connections = Arc::new(AtomicUsize::new(0));
        let shutdown = Arc::new(AtomicBool::new(false));

        let ca = connections_accepted.clone();
        let rs = requests_served.clone();
        let oc = open_connections.clone();
        let sd = shutdown.clone();
        let close = config.close_connection;
        let delay = config.response_delay_ms;
        let consume = config.consume_body;
        let resp_body = config.response_body.clone();
        let chunked = config.chunked;
        let chunk_delay = config.chunk_delay_ms;
        let chunk_stall_after = config.chunk_stall_after;
        let chunk_stall_ms = config.chunk_stall_ms;
        let redirect = config.redirect.clone();

        let handle = thread::spawn(move || {
            while !sd.load(Ordering::Relaxed) {
                if let Ok((stream, _)) = listener.accept() {
                    ca.fetch_add(1, Ordering::SeqCst);
                    oc.fetch_add(1, Ordering::SeqCst);
                    let rs = rs.clone();
                    let oc = oc.clone();
                    let conn_config = ConnectionConfig {
                        close_connection: close,
                        response_delay_ms: delay,
                        consume_body: consume,
                        response_body: resp_body.clone(),
                        chunked,
                        chunk_delay_ms: chunk_delay,
                        redirect: redirect.clone(),
                    };
                    thread::spawn(move || {
                        handle_connection(
                            stream,
                            &conn_config,
                            chunk_stall_after,
                            chunk_stall_ms,
                            &rs,
                        );
                        oc.fetch_sub(1, Ordering::SeqCst);
                    });
                } else if sd.load(Ordering::Relaxed) {
                    break;
                } else {
                    thread::sleep(Duration::from_millis(10));
                }
            }
        });

        Self {
            port,
            connections_accepted,
            requests_served,
            open_connections,
            shutdown,
            handle: Some(handle),
        }
    }

    /// Returns the port the server is listening on.
    #[allow(dead_code)]
    #[must_use]
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Returns the base URL of the server (e.g. `http://127.0.0.1:12345/`).
    #[must_use]
    pub fn url(&self) -> String {
        format!("http://127.0.0.1:{}/", self.port)
    }

    /// Returns the number of TCP connections accepted so far.
    #[allow(dead_code)]
    #[must_use]
    pub fn connections_accepted(&self) -> usize {
        self.connections_accepted.load(Ordering::SeqCst)
    }

    /// Returns the total number of HTTP requests served.
    #[allow(dead_code)]
    #[must_use]
    pub fn requests_served(&self) -> usize {
        self.requests_served.load(Ordering::SeqCst)
    }

    /// Returns the number of currently open TCP connections.
    #[allow(dead_code)]
    #[must_use]
    pub fn open_connections(&self) -> usize {
        self.open_connections.load(Ordering::SeqCst)
    }

    /// Shut down the server and wait for the accept loop to exit.
    pub fn shutdown(&mut self) {
        self.shutdown.store(true, Ordering::SeqCst);
        let _ = TcpStream::connect(("127.0.0.1", self.port));
        if let Some(h) = self.handle.take() {
            let _ = h.join();
        }
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        self.shutdown();
    }
}

fn send_redirect(
    stream: &mut std::net::TcpStream,
    status: u16,
    location: &str,
    close_connection: bool,
) -> bool {
    let status_text = match status {
        301 => "Moved Permanently",
        302 => "Found",
        303 => "See Other",
        307 => "Temporary Redirect",
        308 => "Permanent Redirect",
        _ => "Redirect",
    };
    let conn = if close_connection {
        "close"
    } else {
        "keep-alive"
    };
    let response = format!(
        "HTTP/1.1 {status} {status_text}\r\nLocation: {location}\r\n\
         Connection: {conn}\r\nContent-Length: 0\r\n\r\n"
    );
    if stream.write_all(response.as_bytes()).is_err() {
        return false;
    }
    if stream.flush().is_err() {
        return false;
    }
    if close_connection {
        let _ = stream.shutdown(std::net::Shutdown::Both);
        false
    } else {
        true
    }
}

fn send_chunked(
    stream: &mut std::net::TcpStream,
    body: &[u8],
    conn: &str,
    chunk_delay_ms: u64,
    chunk_stall_after: Option<usize>,
    chunk_stall_ms: u64,
) -> bool {
    let header = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\
         Connection: {conn}\r\nTransfer-Encoding: chunked\r\n\r\n"
    );
    if stream.write_all(header.as_bytes()).is_err() {
        return false;
    }
    let chunk_size = 3;
    for (chunks_sent, chunk) in body.chunks(chunk_size).enumerate() {
        if chunk_delay_ms > 0 {
            thread::sleep(Duration::from_millis(chunk_delay_ms));
        }
        if let Some(stall_after) = chunk_stall_after {
            if chunks_sent == stall_after && chunk_stall_ms > 0 {
                thread::sleep(Duration::from_millis(chunk_stall_ms));
            }
        }
        if stream
            .write_all(format!("{:x}\r\n", chunk.len()).as_bytes())
            .is_err()
        {
            return false;
        }
        if stream.write_all(chunk).is_err() {
            return false;
        }
        if stream.write_all(b"\r\n").is_err() {
            return false;
        }
    }
    stream.write_all(b"0\r\n\r\n").is_ok() && stream.flush().is_ok()
}

fn send_normal(stream: &mut std::net::TcpStream, body: &[u8], conn: &str) -> bool {
    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\
         Connection: {conn}\r\nContent-Length: {}\r\n\r\n",
        body.len()
    );
    stream.write_all(response.as_bytes()).is_ok()
        && stream.write_all(body).is_ok()
        && stream.flush().is_ok()
}

fn handle_connection(
    stream: std::net::TcpStream,
    config: &ConnectionConfig,
    chunk_stall_after: Option<usize>,
    chunk_stall_ms: u64,
    requests_served: &Arc<AtomicUsize>,
) {
    stream.set_read_timeout(Some(Duration::from_secs(5))).ok();
    stream.set_write_timeout(Some(Duration::from_secs(5))).ok();

    let mut reader = BufReader::new(stream);

    loop {
        let mut rl = String::new();
        match reader.read_line(&mut rl) {
            Ok(0) | Err(_) => break,
            Ok(_) => {}
        }
        if rl.trim().is_empty() {
            break;
        }

        let mut content_length: usize = 0;
        loop {
            let mut line = String::new();
            if reader.read_line(&mut line).is_err() || line.trim().is_empty() {
                break;
            }
            let lower = line.to_lowercase();
            if lower.starts_with("content-length:") {
                if let Some(val) = line.split(':').nth(1) {
                    content_length = val.trim().parse().unwrap_or(0);
                }
            }
        }

        if config.consume_body && content_length > 0 {
            let mut body = vec![0u8; content_length];
            let mut total = 0usize;
            while total < content_length {
                match std::io::Read::read(&mut reader, &mut body[total..]) {
                    Ok(0) | Err(_) => break,
                    Ok(n) => total += n,
                }
            }
        }

        requests_served.fetch_add(1, Ordering::SeqCst);

        if config.response_delay_ms > 0 {
            thread::sleep(Duration::from_millis(config.response_delay_ms));
        }

        let stream = reader.get_mut();

        if let Some((status, ref location)) = config.redirect {
            if !send_redirect(stream, status, location, config.close_connection) {
                break;
            }
            continue;
        }

        let body = config.response_body.as_deref().unwrap_or(b"OK");
        let conn = if config.close_connection {
            "close"
        } else {
            "keep-alive"
        };

        let ok = if config.chunked {
            send_chunked(
                stream,
                body,
                conn,
                config.chunk_delay_ms,
                chunk_stall_after,
                chunk_stall_ms,
            )
        } else {
            send_normal(stream, body, conn)
        };

        if !ok || config.close_connection {
            let _ = stream.shutdown(std::net::Shutdown::Both);
            break;
        }
    }
}

/// Per-connection configuration derived from `TestServerConfig`.
struct ConnectionConfig {
    close_connection: bool,
    response_delay_ms: u64,
    consume_body: bool,
    response_body: Option<Vec<u8>>,
    chunked: bool,
    chunk_delay_ms: u64,
    redirect: Option<(u16, String)>,
}