specters 4.1.8

Rust HTTP client with browser-like Chrome and Firefox fingerprints across TLS, HTTP/1.1, HTTP/2, HTTP/3, and WebSockets
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Tests for the H2 inline streaming shared-writer fast path.
//!
//! Verifies that:
//! - Eligible sequential body-less H2 streaming requests use the inline path
//!   (the driver receives the response HEADERS without the caller→driver
//!   command channel hop).
//! - Concurrent streaming requests fall back to the driver command path.
//! - Request bodies fall back to the driver command path.
//! - RFC 8441 tunnels coexist with subsequent inline streaming requests.

use bytes::Bytes;
use specter::transport::h2::hpack_impl::Encoder;
use specter::Client;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, Notify};
use tokio::time::timeout;

mod helpers;
use helpers::mock_h2_server::{MockH2Connection, MockH2Server};
use helpers::tls::generate_cert_bundle;

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);

fn build_acceptor() -> (boring::ssl::SslAcceptor, Vec<u8>) {
    let (mut builder, ca_cert) = generate_cert_bundle();
    builder.set_alpn_select_callback(|_, client_protos| {
        boring::ssl::select_next_proto(b"\x02h2", client_protos)
            .ok_or(boring::ssl::AlpnError::NOACK)
    });
    (builder.build(), ca_cert)
}

#[derive(Default)]
struct ServerObservations {
    headers_count: AtomicU32,
    data_count: AtomicU32,
    last_stream_id: AtomicU32,
}

#[tokio::test]
async fn inline_path_streams_two_sequential_requests_on_one_connection() {
    let (acceptor, ca_cert) = build_acceptor();
    let server = MockH2Server::new().await.unwrap();
    let url = server.url_tls();

    let conn_count = Arc::new(Mutex::new(0u32));
    let conn_count_clone = conn_count.clone();
    let observations = Arc::new(ServerObservations::default());
    let observations_clone = observations.clone();

    server.start_tls(acceptor, move |conn: MockH2Connection| {
        let conn_count = conn_count_clone.clone();
        let observations = observations_clone.clone();
        async move {
            {
                let mut lock = conn_count.lock().await;
                *lock += 1;
            }
            conn.read_preface().await.unwrap();
            let mut settings_sent = false;
            let mut encoder = Encoder::new();
            loop {
                let frame = match timeout(Duration::from_secs(3), conn.read_frame()).await {
                    Ok(Ok(f)) => f,
                    _ => break,
                };
                let (_len, frame_type, flags, stream_id, _payload) = frame;
                match frame_type {
                    0x04 if flags & 0x01 == 0 && !settings_sent => {
                        conn.send_settings(&[(0x01, 4096), (0x03, 100), (0x04, 65535)])
                            .await
                            .unwrap();
                        conn.send_settings_ack().await.unwrap();
                        settings_sent = true;
                    }
                    0x01 => {
                        observations.headers_count.fetch_add(1, Ordering::Relaxed);
                        observations
                            .last_stream_id
                            .store(stream_id, Ordering::Relaxed);
                        let response_headers =
                            encoder.encode(&[(b":status".as_slice(), b"200".as_slice())]);
                        conn.send_headers(stream_id, &response_headers, false, true)
                            .await
                            .unwrap();
                        conn.send_data(stream_id, b"inline-chunk", true)
                            .await
                            .unwrap();
                    }
                    0x00 => {
                        observations.data_count.fetch_add(1, Ordering::Relaxed);
                    }
                    _ => {}
                }
            }
        }
    });

    let client = Client::builder()
        .add_root_certificate(ca_cert)
        .prefer_http2(true)
        .build()
        .unwrap();

    for i in 0..2 {
        let req_url = format!("{}/inline-{}", url, i);
        let mut response = timeout(DEFAULT_TIMEOUT, client.get(&req_url).send_streaming())
            .await
            .expect("send_streaming did not complete in time")
            .expect("send_streaming returned error");

        assert_eq!(response.status().as_u16(), 200);
        let chunk = response
            .body_mut()
            .frame()
            .await
            .unwrap()
            .unwrap()
            .into_data()
            .unwrap();
        assert_eq!(chunk, Bytes::from("inline-chunk"));
        assert!(
            response.body_mut().frame().await.is_none(),
            "expected clean EOF"
        );
    }

    let count = *conn_count.lock().await;
    assert_eq!(count, 1, "Should have reused one H2 connection");
    assert_eq!(
        observations.headers_count.load(Ordering::Relaxed),
        2,
        "Server should have observed two HEADERS frames"
    );
    assert_eq!(
        observations.data_count.load(Ordering::Relaxed),
        0,
        "Body-less streaming should never send DATA from the client"
    );
    let last_stream = observations.last_stream_id.load(Ordering::Relaxed);
    assert!(
        last_stream % 2 == 1 && last_stream >= 3,
        "Second client stream must use a fresh client-allocated odd stream id, got {}",
        last_stream
    );
}

#[tokio::test]
async fn inline_path_falls_back_when_request_body_present() {
    let (acceptor, ca_cert) = build_acceptor();
    let server = MockH2Server::new().await.unwrap();
    let url = server.url_tls();

    let received_body_bytes = Arc::new(Mutex::new(Vec::<u8>::new()));
    let received_body_bytes_clone = received_body_bytes.clone();

    server.start_tls(acceptor, move |conn: MockH2Connection| {
        let received_body_bytes = received_body_bytes_clone.clone();
        async move {
            conn.read_preface().await.unwrap();
            let mut settings_sent = false;
            let mut encoder = Encoder::new();
            loop {
                let frame = match timeout(Duration::from_secs(3), conn.read_frame()).await {
                    Ok(Ok(f)) => f,
                    _ => break,
                };
                let (_len, frame_type, flags, stream_id, payload) = frame;
                match frame_type {
                    0x04 if flags & 0x01 == 0 && !settings_sent => {
                        conn.send_settings(&[(0x01, 4096), (0x03, 100), (0x04, 65535)])
                            .await
                            .unwrap();
                        conn.send_settings_ack().await.unwrap();
                        settings_sent = true;
                    }
                    0x01 => {
                        let response_headers =
                            encoder.encode(&[(b":status".as_slice(), b"200".as_slice())]);
                        conn.send_headers(stream_id, &response_headers, false, true)
                            .await
                            .unwrap();
                        conn.send_data(stream_id, b"echoed", true).await.unwrap();
                    }
                    0x00 => {
                        let mut received = received_body_bytes.lock().await;
                        received.extend_from_slice(&payload);
                    }
                    _ => {}
                }
            }
        }
    });

    let client = Client::builder()
        .add_root_certificate(ca_cert)
        .prefer_http2(true)
        .build()
        .unwrap();

    let body = b"upload-body".to_vec();
    let req_url = format!("{}/with-body", url);
    let mut response = timeout(
        DEFAULT_TIMEOUT,
        client.post(&req_url).body(body.clone()).send_streaming(),
    )
    .await
    .expect("send_streaming did not complete in time")
    .expect("send_streaming returned error");

    assert_eq!(response.status().as_u16(), 200);
    let chunk = response
        .body_mut()
        .frame()
        .await
        .unwrap()
        .unwrap()
        .into_data()
        .unwrap();
    assert_eq!(chunk, Bytes::from("echoed"));
    assert!(response.body_mut().frame().await.is_none());

    let received = received_body_bytes.lock().await.clone();
    assert_eq!(
        received, body,
        "Server must observe the upload body when fallback path is used"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_inline_attempts_serialize_with_fallback() {
    let (acceptor, ca_cert) = build_acceptor();
    let server = MockH2Server::new().await.unwrap();
    let url = server.url_tls();

    let observed_streams = Arc::new(Mutex::new(Vec::<u32>::new()));
    let observed_streams_clone = observed_streams.clone();

    server.start_tls(acceptor, move |conn: MockH2Connection| {
        let observed_streams = observed_streams_clone.clone();
        async move {
            conn.read_preface().await.unwrap();
            let mut settings_sent = false;
            let mut encoder = Encoder::new();

            loop {
                let frame = match timeout(Duration::from_secs(5), conn.read_frame()).await {
                    Ok(Ok(f)) => f,
                    _ => break,
                };
                let (_len, frame_type, flags, stream_id, _payload) = frame;
                match frame_type {
                    0x04 if flags & 0x01 == 0 && !settings_sent => {
                        conn.send_settings(&[(0x01, 4096), (0x03, 100), (0x04, 65535)])
                            .await
                            .unwrap();
                        conn.send_settings_ack().await.unwrap();
                        settings_sent = true;
                    }
                    0x01 => {
                        {
                            let mut s = observed_streams.lock().await;
                            s.push(stream_id);
                        }
                        let response_headers =
                            encoder.encode(&[(b":status".as_slice(), b"200".as_slice())]);
                        conn.send_headers(stream_id, &response_headers, false, true)
                            .await
                            .unwrap();
                        conn.send_data(stream_id, b"ok", true).await.unwrap();
                    }
                    _ => {}
                }
            }
        }
    });

    let client = Client::builder()
        .add_root_certificate(ca_cert)
        .prefer_http2(true)
        .build()
        .unwrap();

    let warmup_url = format!("{}/warmup", url);
    let mut resp = timeout(DEFAULT_TIMEOUT, client.get(&warmup_url).send_streaming())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(resp.status().as_u16(), 200);
    assert_eq!(
        resp.body_mut()
            .frame()
            .await
            .unwrap()
            .unwrap()
            .into_data()
            .unwrap(),
        Bytes::from("ok")
    );
    assert!(resp.body_mut().frame().await.is_none());

    let url1 = format!("{}/concurrent-1", url);
    let url2 = format!("{}/concurrent-2", url);
    let client1 = client.clone();
    let client2 = client.clone();

    let task1 = tokio::spawn(async move {
        let mut resp = timeout(DEFAULT_TIMEOUT, client1.get(&url1).send_streaming())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(resp.status().as_u16(), 200);
        let body = resp
            .body_mut()
            .frame()
            .await
            .unwrap()
            .unwrap()
            .into_data()
            .unwrap();
        assert_eq!(body, Bytes::from("ok"));
        assert!(resp.body_mut().frame().await.is_none());
    });

    let task2 = tokio::spawn(async move {
        let mut resp = timeout(DEFAULT_TIMEOUT, client2.get(&url2).send_streaming())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(resp.status().as_u16(), 200);
        let body = resp
            .body_mut()
            .frame()
            .await
            .unwrap()
            .unwrap()
            .into_data()
            .unwrap();
        assert_eq!(body, Bytes::from("ok"));
        assert!(resp.body_mut().frame().await.is_none());
    });

    timeout(Duration::from_secs(10), task1)
        .await
        .unwrap()
        .unwrap();
    timeout(Duration::from_secs(10), task2)
        .await
        .unwrap()
        .unwrap();

    let streams = observed_streams.lock().await.clone();
    assert!(
        streams.len() >= 3,
        "Expected at least 3 client streams (warmup + two concurrent), got {}: {:?}",
        streams.len(),
        streams
    );
    let unique: std::collections::HashSet<_> = streams.iter().copied().collect();
    assert!(
        unique.len() >= 3,
        "Concurrent streams must use distinct ids on the connection, got {:?}",
        streams
    );
}

#[tokio::test]
async fn inline_path_handles_dropped_receiver() {
    let (acceptor, ca_cert) = build_acceptor();
    let server = MockH2Server::new().await.unwrap();
    let url = server.url_tls();

    let rst_seen = Arc::new(Notify::new());
    let rst_seen_clone = rst_seen.clone();
    let client_dropped = Arc::new(Notify::new());
    let client_dropped_clone = client_dropped.clone();

    server.start_tls(acceptor, move |conn: MockH2Connection| {
        let rst_seen = rst_seen_clone.clone();
        let client_dropped = client_dropped_clone.clone();
        async move {
            conn.read_preface().await.unwrap();
            let mut settings_sent = false;
            let mut encoder = Encoder::new();

            let mut request_stream_id: Option<u32> = None;

            loop {
                let frame = match timeout(Duration::from_secs(3), conn.read_frame()).await {
                    Ok(Ok(f)) => f,
                    _ => break,
                };
                let (_len, frame_type, flags, stream_id, _payload) = frame;
                match frame_type {
                    0x04 if flags & 0x01 == 0 && !settings_sent => {
                        conn.send_settings(&[(0x01, 4096), (0x03, 100), (0x04, 65535)])
                            .await
                            .unwrap();
                        conn.send_settings_ack().await.unwrap();
                        settings_sent = true;
                    }
                    0x01 => {
                        request_stream_id = Some(stream_id);
                        let response_headers =
                            encoder.encode(&[(b":status".as_slice(), b"200".as_slice())]);
                        conn.send_headers(stream_id, &response_headers, false, true)
                            .await
                            .unwrap();
                        timeout(Duration::from_secs(1), client_dropped.notified())
                            .await
                            .expect("client should drop the streaming response");
                        let _ = conn.send_data(stream_id, b"after-drop", false).await;
                    }
                    0x03 if Some(stream_id) == request_stream_id => {
                        rst_seen.notify_one();
                    }
                    _ => {}
                }
            }
        }
    });

    let client = Client::builder()
        .add_root_certificate(ca_cert)
        .prefer_http2(true)
        .build()
        .unwrap();

    let req_url = format!("{}/dropped", url);
    let response = timeout(DEFAULT_TIMEOUT, client.get(&req_url).send_streaming())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(response.status().as_u16(), 200);

    drop(response);
    client_dropped.notify_one();

    timeout(Duration::from_secs(3), rst_seen.notified())
        .await
        .expect("Server should have observed RST_STREAM after receiver drop");

    let req_url2 = format!("{}/after-drop", url);
    let resp2 = timeout(DEFAULT_TIMEOUT, client.get(&req_url2).send_streaming())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(resp2.status().as_u16(), 200);
}

#[tokio::test]
async fn large_streaming_body_avoids_driver_backpressure_stalls() {
    use http::Method;
    use specter::fingerprint::http2::Http2Settings;
    use specter::transport::connector::BoringConnector;
    use specter::transport::h2::{
        H2BodyTimeouts, H2Connection, H2PooledConnection, PseudoHeaderOrder,
    };
    use specter::RequestBody;

    const BODY_LEN: usize = 6 * 1024 * 1024;
    const CHUNK_SIZE: usize = 16 * 1024;

    let (acceptor, ca_cert) = build_acceptor();
    let server = MockH2Server::new().await.unwrap();
    let server_port = server.port();

    server.start_tls(acceptor, move |conn: MockH2Connection| async move {
        conn.read_preface().await.unwrap();
        let mut settings_sent = false;
        let mut encoder = specter::transport::h2::hpack_impl::Encoder::new();
        let mut request_stream_id: Option<u32> = None;

        loop {
            let frame = match timeout(Duration::from_secs(10), conn.read_frame()).await {
                Ok(Ok(f)) => f,
                _ => break,
            };
            let (_len, frame_type, flags, stream_id, _payload) = frame;
            match frame_type {
                0x04 if flags & 0x01 == 0 && !settings_sent => {
                    conn.send_settings(&[(0x01, 4096), (0x03, 100), (0x04, 65535)])
                        .await
                        .unwrap();
                    conn.send_settings_ack().await.unwrap();
                    settings_sent = true;
                }
                0x01 => {
                    request_stream_id = Some(stream_id);
                    let response_headers =
                        encoder.encode(&[(b":status".as_slice(), b"200".as_slice())]);
                    conn.send_headers(stream_id, &response_headers, false, true)
                        .await
                        .unwrap();
                    let mut sent = 0usize;
                    while sent < BODY_LEN {
                        let take = (BODY_LEN - sent).min(CHUNK_SIZE);
                        let chunk = vec![0xABu8; take];
                        sent += take;
                        let end = sent >= BODY_LEN;
                        conn.send_data(stream_id, &chunk, end).await.unwrap();
                    }
                }
                0x03 if Some(stream_id) == request_stream_id => break,
                _ => {}
            }
        }
    });

    let fp = specter::fingerprint::tls::TlsFingerprint::chrome_142();
    let connector = BoringConnector::with_fingerprint(fp).with_root_certificates(vec![ca_cert]);
    let uri: http::Uri = format!("https://127.0.0.1:{server_port}/stream")
        .parse()
        .unwrap();

    let stream = timeout(DEFAULT_TIMEOUT, connector.connect(&uri))
        .await
        .unwrap()
        .unwrap();
    assert!(stream.is_h2());

    let h2_conn = timeout(
        DEFAULT_TIMEOUT,
        H2Connection::connect(stream, Http2Settings::default(), PseudoHeaderOrder::Chrome),
    )
    .await
    .unwrap()
    .unwrap();
    let pooled = H2PooledConnection::new(h2_conn);

    let mut response = timeout(
        DEFAULT_TIMEOUT,
        pooled.send_streaming_request(
            Method::GET,
            &uri,
            vec![],
            RequestBody::Empty,
            H2BodyTimeouts::default(),
        ),
    )
    .await
    .unwrap()
    .unwrap();
    assert_eq!(response.status().as_u16(), 200);

    let mut received = 0usize;
    loop {
        let Some(frame) = timeout(DEFAULT_TIMEOUT, response.body_mut().frame())
            .await
            .unwrap()
        else {
            break;
        };
        let frame = frame.unwrap();
        if let Ok(data) = frame.into_data() {
            received += data.len();
        }
    }

    assert_eq!(received, BODY_LEN);
    assert_eq!(
        pooled.backpressure_stall_count(),
        0,
        "6 MiB stream should not hit 1 ms backpressure stalls at default slot capacity"
    );
}