nbreq 0.3.1

Runtime-independent cancellable HTTP client
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
//! Verified client TLS over the Engine-owned standalone TCP socket.

use std::cell::Cell;
use std::fmt;
use std::marker::PhantomData;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;

use crate::dns::normalize_dns_name;
use crate::registry::{TlsConnectCallback, TlsConnectState};
use crate::{Error, ErrorKind, ExecuteError, RequestId, RunMode};

use super::{
    TcpConnectRequest, TcpConnection, TcpConnectionHandle, TcpConnector, TcpFinishError,
    TcpFinishStatus, TcpRead, TcpReader, TcpSendError, TcpStreamError, TcpWriter,
};

/// TLS peer identity and establishment policy. Certificate verification is always enabled.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TlsOptions {
    server_name: String,
    handshake_timeout: Duration,
}

impl TlsOptions {
    /// Selects the name or IP address whose certificate must be verified.
    pub fn new(server_name: impl Into<String>) -> Result<Self, Error> {
        let server_name = server_name.into();
        let server_name = if let Ok(address) = server_name.parse::<IpAddr>() {
            address.to_string()
        } else {
            normalize_dns_name(&server_name)?.identity
        };
        if server_name.parse::<IpAddr>().is_err() && !valid_tls_dns_name(&server_name) {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "TLS server name is not a valid DNS identity or IP address",
            ));
        }
        Ok(Self {
            server_name,
            handshake_timeout: Duration::from_secs(10),
        })
    }

    /// Sets the finite deadline for DNS, TCP, and TLS establishment, or for an upgrade.
    #[must_use]
    pub fn handshake_timeout(mut self, timeout: Duration) -> Self {
        self.handshake_timeout = timeout;
        self
    }

    /// Returns the certificate identity used for verification.
    #[must_use]
    pub fn server_name(&self) -> &str {
        &self.server_name
    }

    /// Returns the establishment timeout selected for this connection.
    #[must_use]
    pub fn timeout(&self) -> Duration {
        self.handshake_timeout
    }

    pub(crate) fn validate(&self) -> Result<(), Error> {
        if self.handshake_timeout.is_zero() {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "TLS handshake timeout must be greater than zero",
            ));
        }
        if std::time::Instant::now()
            .checked_add(self.handshake_timeout)
            .is_none()
        {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "TLS handshake timeout cannot be represented by the Engine clock",
            ));
        }
        Ok(())
    }
}

fn valid_tls_dns_name(name: &str) -> bool {
    if name.is_empty() || name.len() > 253 {
        return false;
    }
    let mut labels = name.split('.').peekable();
    while let Some(label) = labels.next() {
        if label.is_empty()
            || label.len() > 63
            || !label
                .as_bytes()
                .first()
                .is_some_and(u8::is_ascii_alphanumeric)
            || !label
                .as_bytes()
                .last()
                .is_some_and(u8::is_ascii_alphanumeric)
            || !label
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
        {
            return false;
        }
        if labels.peek().is_none() && label.bytes().all(|byte| byte.is_ascii_digit()) {
            return false;
        }
    }
    true
}

/// Independent cancellation handle for a pending TLS connect or live TLS connection.
#[derive(Clone, Debug)]
pub struct TlsConnectHandle {
    connector: TcpConnector,
    id: RequestId,
}

impl TlsConnectHandle {
    pub(crate) fn new(connector: TcpConnector, id: RequestId) -> Self {
        Self { connector, id }
    }

    /// Returns the original TCP request identity, preserved across an upgrade.
    #[must_use]
    pub fn id(&self) -> RequestId {
        self.id
    }

    /// Cancels pending TLS establishment or aborts the live TLS connection.
    pub fn cancel(&self) -> Result<(), Error> {
        self.connector.cancel(self.id)
    }
}

/// Canonical outcome of either an immediate TLS connect or a consuming upgrade.
#[derive(Debug)]
#[non_exhaustive]
pub enum TlsConnectCompletion {
    /// A verified TLS connection is ready for application I/O.
    Completed(TlsConnection),
    /// DNS, TCP, or TLS establishment failed.
    Failed(Error),
    /// Cancellation won the terminal race.
    Cancelled,
}

/// Result of a timed wait for TLS establishment.
#[derive(Debug)]
#[non_exhaustive]
pub enum TlsConnectWaitOutcome {
    /// TLS establishment reached its canonical terminal outcome.
    Completed(TlsConnectCompletion),
    /// The wait elapsed; this handle can be waited on again or cancelled.
    TimedOut(PendingTlsConnect),
}

/// Accepted TLS establishment with a direct terminal-state waiter.
#[derive(Debug)]
pub struct PendingTlsConnect {
    handle: TlsConnectHandle,
    state: Arc<TlsConnectState>,
}

impl PendingTlsConnect {
    pub(crate) fn new(handle: TlsConnectHandle, state: Arc<TlsConnectState>) -> Self {
        Self { handle, state }
    }

    /// Returns a separate handle for cancellation and request identity.
    #[must_use]
    pub fn handle(&self) -> TlsConnectHandle {
        self.handle.clone()
    }

    /// Reports whether a terminal outcome has committed.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.state.is_terminal()
    }

    pub(crate) fn try_completion(&self) -> Option<TlsConnectCompletion> {
        self.state.try_completion()
    }

    pub(crate) fn issued_engine_id(&self) -> u64 {
        self.handle.id.engine
    }

    /// Waits for the terminal outcome. A manual Engine needs another driver to make progress.
    #[must_use]
    pub fn wait(self) -> TlsConnectCompletion {
        self.state.wait()
    }

    /// Waits up to `duration`, returning the pending handle when the wait expires.
    #[must_use]
    pub fn wait_for(self, duration: Duration) -> TlsConnectWaitOutcome {
        match self.state.wait_for(duration) {
            Some(completion) => TlsConnectWaitOutcome::Completed(completion),
            None => TlsConnectWaitOutcome::TimedOut(self),
        }
    }
}

/// Unique verified TLS stream. The underlying socket cannot be extracted or downgraded.
pub struct TlsConnection {
    inner: TcpConnection,
    _not_sync: PhantomData<Cell<()>>,
}

impl fmt::Debug for TlsConnection {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("TlsConnection")
            .finish_non_exhaustive()
    }
}

impl TlsConnection {
    #[cfg_attr(not(feature = "native"), allow(dead_code))]
    pub(crate) fn from_tcp(inner: TcpConnection) -> Self {
        Self {
            inner,
            _not_sync: PhantomData,
        }
    }

    /// Returns the connection's cancellation handle and original request identity.
    #[must_use]
    pub fn handle(&self) -> TcpConnectionHandle {
        self.inner.handle()
    }
    /// Returns the local socket address.
    pub fn local_addr(&self) -> Result<SocketAddr, Error> {
        self.inner.local_addr()
    }
    /// Returns the connected peer address.
    pub fn peer_addr(&self) -> Result<SocketAddr, Error> {
        self.inner.peer_addr()
    }
    /// Splits the verified stream into independently owned read and write halves.
    #[must_use]
    pub fn split(self) -> (TlsReader, TlsWriter) {
        let (reader, writer) = self.inner.split();
        (
            TlsReader {
                inner: reader,
                _not_sync: PhantomData,
            },
            TlsWriter {
                inner: writer,
                _not_sync: PhantomData,
            },
        )
    }
    /// Reads available authenticated plaintext without blocking.
    pub fn try_read(&mut self, destination: &mut [u8]) -> Result<TcpRead, TcpStreamError> {
        self.inner.try_read(destination)
    }
    /// Waits for authenticated plaintext or orderly TLS EOF.
    pub fn read(&mut self, destination: &mut [u8]) -> Result<Option<usize>, TcpStreamError> {
        self.inner.read(destination)
    }
    /// Admits plaintext to the bounded TLS send queue without blocking.
    pub fn try_send(&mut self, bytes: Vec<u8>) -> Result<(), TcpSendError> {
        self.inner.try_send(bytes)
    }
    /// Waits for enough send credit to admit the complete plaintext buffer.
    pub fn send(&mut self, bytes: Vec<u8>) -> Result<(), TcpSendError> {
        self.inner.send(bytes)
    }
    /// Requests an orderly TLS `close_notify` after accepted writes drain.
    pub fn try_finish(&mut self) -> Result<TcpFinishStatus, TcpFinishError> {
        self.inner.try_finish()
    }
    /// Waits until the orderly TLS write close completes.
    pub fn finish(&mut self) -> Result<(), TcpFinishError> {
        self.inner.finish()
    }
    /// Registers a callback for the orderly TLS write close.
    pub fn finish_with<F>(&mut self, callback: F) -> Result<(), Error>
    where
        F: FnOnce(Result<(), TcpFinishError>) + Send + 'static,
    {
        self.inner.finish_with(callback)
    }
}

/// Read half of a verified standalone TLS stream.
pub struct TlsReader {
    inner: TcpReader,
    _not_sync: PhantomData<Cell<()>>,
}
impl fmt::Debug for TlsReader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TlsReader").finish_non_exhaustive()
    }
}
impl TlsReader {
    /// Returns the connection's cancellation handle and request identity.
    #[must_use]
    pub fn handle(&self) -> TcpConnectionHandle {
        self.inner.handle()
    }
    /// Reads available authenticated plaintext without blocking.
    pub fn try_read(&mut self, destination: &mut [u8]) -> Result<TcpRead, TcpStreamError> {
        self.inner.try_read(destination)
    }
    /// Waits for authenticated plaintext or orderly TLS EOF.
    pub fn read(&mut self, destination: &mut [u8]) -> Result<Option<usize>, TcpStreamError> {
        self.inner.read(destination)
    }
}

/// Write half of a verified standalone TLS stream.
pub struct TlsWriter {
    inner: TcpWriter,
    _not_sync: PhantomData<Cell<()>>,
}
impl fmt::Debug for TlsWriter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TlsWriter").finish_non_exhaustive()
    }
}
impl TlsWriter {
    /// Returns the connection's cancellation handle and request identity.
    #[must_use]
    pub fn handle(&self) -> TcpConnectionHandle {
        self.inner.handle()
    }
    /// Admits plaintext to the bounded TLS send queue without blocking.
    pub fn try_send(&mut self, bytes: Vec<u8>) -> Result<(), TcpSendError> {
        self.inner.try_send(bytes)
    }
    /// Waits for enough send credit to admit the complete plaintext buffer.
    pub fn send(&mut self, bytes: Vec<u8>) -> Result<(), TcpSendError> {
        self.inner.send(bytes)
    }
    /// Requests an orderly TLS `close_notify` after accepted writes drain.
    pub fn try_finish(&mut self) -> Result<TcpFinishStatus, TcpFinishError> {
        self.inner.try_finish()
    }
    /// Waits until the orderly TLS write close completes.
    pub fn finish(&mut self) -> Result<(), TcpFinishError> {
        self.inner.finish()
    }
    /// Registers a callback for the orderly TLS write close.
    pub fn finish_with<F>(&mut self, callback: F) -> Result<(), Error>
    where
        F: FnOnce(Result<(), TcpFinishError>) + Send + 'static,
    {
        self.inner.finish_with(callback)
    }
}

impl TcpConnector {
    /// Starts a verified TLS connection and delivers one terminal callback.
    pub fn start_tls<F>(
        &self,
        request: TcpConnectRequest,
        options: TlsOptions,
        callback: F,
    ) -> Result<TlsConnectHandle, Error>
    where
        F: FnOnce(TlsConnectCompletion) + Send + 'static,
    {
        let callback: TlsConnectCallback = Box::new(callback);
        let accepted =
            self.shared
                .accept_tls_connect(self.clone(), request, options, Some(callback))?;
        Ok(TlsConnectHandle::new(self.clone(), accepted.id()))
    }

    /// Submits a verified TLS connection and returns a passive waiter.
    pub fn submit_tls(
        &self,
        request: TcpConnectRequest,
        options: TlsOptions,
    ) -> Result<PendingTlsConnect, Error> {
        let state = self
            .shared
            .accept_tls_connect(self.clone(), request, options, None)?;
        Ok(PendingTlsConnect::new(
            TlsConnectHandle::new(self.clone(), state.id()),
            state,
        ))
    }

    /// Establishes TLS and waits for verification on a spawned Engine.
    pub fn execute_tls(
        &self,
        request: TcpConnectRequest,
        options: TlsOptions,
    ) -> Result<TlsConnection, ExecuteError> {
        if self.shared.run_mode == RunMode::Manual {
            return Err(ExecuteError::Submission(Error::new(
                ErrorKind::WrongMode,
                "blocking TLS connect requires a spawned Engine",
            )));
        }
        match self.submit_tls(request, options) {
            Ok(pending) => match pending.wait() {
                TlsConnectCompletion::Completed(connection) => Ok(connection),
                TlsConnectCompletion::Failed(error) => Err(ExecuteError::Failed(error)),
                TlsConnectCompletion::Cancelled => Err(ExecuteError::Cancelled),
            },
            Err(error) => Err(ExecuteError::Submission(error)),
        }
    }
}

impl TcpConnection {
    /// Consumes an idle unsplit TCP connection and starts verified TLS upgrade.
    pub fn start_tls<F>(
        mut self,
        options: TlsOptions,
        callback: F,
    ) -> Result<TlsConnectHandle, Error>
    where
        F: FnOnce(TlsConnectCompletion) + Send + 'static,
    {
        let callback: TlsConnectCallback = Box::new(callback);
        let state = self.handle.connector.shared.accept_tls_upgrade(
            self.handle.connector.clone(),
            Arc::clone(&self.io),
            options,
            Some(callback),
        )?;
        self.live = false;
        Ok(TlsConnectHandle::new(
            self.handle.connector.clone(),
            state.id(),
        ))
    }

    /// Consumes an idle unsplit TCP connection and returns an upgrade waiter.
    pub fn submit_tls(mut self, options: TlsOptions) -> Result<PendingTlsConnect, Error> {
        let state = self.handle.connector.shared.accept_tls_upgrade(
            self.handle.connector.clone(),
            Arc::clone(&self.io),
            options,
            None,
        )?;
        self.live = false;
        Ok(PendingTlsConnect::new(
            TlsConnectHandle::new(self.handle.connector.clone(), state.id()),
            state,
        ))
    }

    /// Consumes an idle unsplit TCP connection and waits for verified upgrade.
    pub fn into_tls(self, options: TlsOptions) -> Result<TlsConnection, ExecuteError> {
        if self.handle.connector.shared.run_mode == RunMode::Manual {
            return Err(ExecuteError::Submission(Error::new(
                ErrorKind::WrongMode,
                "blocking TLS upgrade requires a spawned Engine",
            )));
        }
        match self.submit_tls(options) {
            Ok(pending) => match pending.wait() {
                TlsConnectCompletion::Completed(connection) => Ok(connection),
                TlsConnectCompletion::Failed(error) => Err(ExecuteError::Failed(error)),
                TlsConnectCompletion::Cancelled => Err(ExecuteError::Cancelled),
            },
            Err(error) => Err(ExecuteError::Submission(error)),
        }
    }
}

#[cfg(test)]
mod review_regressions {
    use super::*;

    #[test]
    fn tls_identity_admission_rejects_invalid_numeric_tlds_and_accepts_ip() {
        for invalid in ["mail.123", "127.0.0.999", "bad..example"] {
            assert_eq!(
                TlsOptions::new(invalid)
                    .expect_err("invalid TLS identity")
                    .kind(),
                ErrorKind::InvalidRequest,
                "{invalid} must fail before admission",
            );
        }
        assert_eq!(
            TlsOptions::new("127.0.0.1")
                .expect("IPv4 identity")
                .server_name(),
            "127.0.0.1"
        );
        assert_eq!(
            TlsOptions::new("::1").expect("IPv6 identity").server_name(),
            "::1"
        );
        assert_eq!(
            TlsOptions::new("mail.example")
                .expect("DNS identity")
                .server_name(),
            "mail.example"
        );
    }

    #[test]
    fn tls_establishment_deadline_must_be_finite_and_representable() {
        let options = TlsOptions::new("mail.example").expect("DNS identity");
        assert_eq!(
            options
                .clone()
                .handshake_timeout(Duration::ZERO)
                .validate()
                .expect_err("zero timeout rejected")
                .kind(),
            ErrorKind::InvalidRequest,
        );
        assert_eq!(
            options
                .handshake_timeout(Duration::MAX)
                .validate()
                .expect_err("unrepresentable timeout rejected")
                .kind(),
            ErrorKind::InvalidRequest,
        );
    }
}