openkind-client 0.3.0

Async Rust client for the openkind / TypeSafe Jev SystemOne HTTP API.
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Lean HTTP/1.1 keep-alive transport for plain-`http` URLs.
//!
//! reqwest mediates every request through a per-connection dispatch task,
//! which adds a task handoff (and its scheduler wakeup) to each direction
//! of every round trip. On loopback daemons that overhead is a large share
//! of request latency. This module speaks HTTP/1.1 directly on pooled
//! `TcpStream`s from the caller's own task: the same wire bytes and
//! keep-alive semantics with one fewer cross-thread wakeup per direction.
//!
//! Scope and fallbacks, all preserving reqwest behavior where it differs:
//! - plain `http://` URLs only; `https://` always uses the reqwest client.
//! - disabled when the caller supplied their own `reqwest::Client` or when
//!   proxy environment variables are set (reqwest honors those; this
//!   transport must not bypass a configured proxy).
//! - a 3xx response with a `Location` header is reported back to the caller
//!   so the full logical request re-runs through reqwest and follows the
//!   redirect chain exactly as before.

use std::collections::VecDeque;
use std::io;
use std::time::Duration;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;

use crate::error::{Error, REQUEST_ID_HEADER};

/// Cap on the response head (status line + headers) this transport will
/// buffer before giving up, so a hostile server cannot grow memory without
/// bound.
const MAX_HEAD_BYTES: usize = 64 * 1024;

/// Largest single buffer growth step while scanning for the head end.
const READ_CHUNK: usize = 8 * 1024;

/// Upper bound on idle keep-alive connections retained per authority.
const MAX_IDLE_CONNECTIONS: usize = 8;

/// A parsed minimal response: everything the SDK layer needs after a call.
pub(crate) struct FastResponse {
    /// HTTP status code.
    pub(crate) status: u16,
    /// Header (name, value) pairs with names lowercased.
    headers: Vec<(String, String)>,
    /// Decoded body bytes.
    pub(crate) body: Vec<u8>,
    /// Whether response framing and protocol version permit connection reuse.
    reusable: bool,
}

impl FastResponse {
    /// First header value for a lowercase name, trimmed.
    pub(crate) fn header(&self, name: &str) -> Option<&str> {
        let value = self
            .headers
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value.trim())?;
        if name == REQUEST_ID_HEADER && value.bytes().any(|byte| !(b' '..=b'~').contains(&byte)) {
            return None;
        }
        Some(value)
    }
}

/// Internal transport failure, before HTTP-level error mapping.
pub(crate) enum FastError {
    /// Network or protocol failure; the connection must not be reused.
    Io(io::Error),
    /// A body exceeded the client's fixed memory-safety limit.
    TooLarge,
    /// Redirect response — re-run the logical request through reqwest.
    Redirect,
}

/// One reusable connection with its read-ahead buffer.
struct Connection {
    stream: TcpStream,
    /// Buffered bytes; `start` is the first unconsumed byte.
    buf: Vec<u8>,
    start: usize,
}

impl Connection {
    fn new(stream: TcpStream) -> Self {
        Self {
            stream,
            buf: Vec::with_capacity(READ_CHUNK),
            start: 0,
        }
    }

    fn buffered(&self) -> &[u8] {
        &self.buf[self.start..]
    }

    fn compact(&mut self) {
        if self.start > 0 {
            self.buf.drain(..self.start);
            self.start = 0;
        }
    }

    /// Read more bytes into the buffer; returns how many arrived. Zero
    /// means the peer closed the stream.
    async fn fill(&mut self) -> io::Result<usize> {
        self.compact();
        if self.buf.len() == self.buf.capacity() {
            self.buf.reserve(READ_CHUNK);
        }
        self.stream.read_buf(&mut self.buf).await
    }

    /// Ensure a full response head (`\r\n\r\n` terminated) is buffered;
    /// returns the head length excluding the terminator.
    async fn read_head(&mut self) -> io::Result<usize> {
        loop {
            if let Some(pos) = find_double_crlf(self.buffered()) {
                if pos + 4 > MAX_HEAD_BYTES {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "HTTP response head exceeded 64 KiB",
                    ));
                }
                return Ok(pos);
            }
            if self.buf.len() - self.start > MAX_HEAD_BYTES {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "HTTP response head exceeded 64 KiB",
                ));
            }
            if self.fill().await? == 0 {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "connection closed before response head was complete",
                ));
            }
        }
    }
}

fn find_double_crlf(buf: &[u8]) -> Option<usize> {
    buf.windows(4).position(|w| w == b"\r\n\r\n")
}

/// Pool of keep-alive connections to one authority.
///
/// Only the idle list is lock-protected, and never across an exchange:
/// concurrent callers each take a connection (opening a fresh one when the
/// idle list is empty), so parallel requests get parallel connections just
/// like the reqwest pool they replace.
pub(crate) struct H1Pool {
    authority: String,
    host: String,
    port: u16,
    connect_timeout: Option<Duration>,
    idle: tokio::sync::Mutex<VecDeque<Connection>>,
}

impl H1Pool {
    /// Build a pool for `host:port`. Connections are created lazily.
    pub(crate) fn new(host: &str, port: u16, connect_timeout: Option<Duration>) -> Self {
        // URLs bracket IPv6 literals, while socket address resolution needs
        // the literal without brackets. Keep brackets in the Host header.
        let host = host
            .strip_prefix('[')
            .and_then(|host| host.strip_suffix(']'))
            .unwrap_or(host);
        let authority_host = if host.contains(':') {
            format!("[{host}]")
        } else {
            host.to_owned()
        };
        let authority = if port == 80 {
            authority_host
        } else {
            format!("{authority_host}:{port}")
        };
        Self {
            authority,
            host: host.to_string(),
            port,
            connect_timeout,
            idle: tokio::sync::Mutex::new(VecDeque::new()),
        }
    }

    /// Authority string for the Host header (`host` or `host:port`).
    pub(crate) fn authority(&self) -> &str {
        &self.authority
    }

    /// Take a live idle connection, or connect a fresh one.
    ///
    /// Idle entries are probed non-blockingly first: a connection the
    /// server has since closed (or that carries unexpected bytes) is
    /// discarded, so a stale keep-alive surfaces as a fresh connect instead
    /// of a mid-request failure.
    async fn acquire(&self) -> io::Result<Connection> {
        loop {
            let candidate = { self.idle.lock().await.pop_back() };
            let Some(mut conn) = candidate else { break };
            let mut probe = [0u8; 16];
            match conn.stream.try_read(&mut probe) {
                Ok(0) => continue,  // closed by peer
                Ok(_n) => continue, // unexpected pre-response bytes: stale
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    conn.buf.clear();
                    conn.start = 0;
                    return Ok(conn);
                }
                Err(_) => continue,
            }
        }
        let connect = TcpStream::connect((self.host.as_str(), self.port));
        let stream = match self.connect_timeout {
            Some(timeout) => tokio::time::timeout(timeout, connect).await.map_err(|_| {
                io::Error::new(io::ErrorKind::TimedOut, "connect timeout elapsed")
            })??,
            None => connect.await?,
        };
        stream.set_nodelay(true)?;
        Ok(Connection::new(stream))
    }

    async fn release(&self, conn: Connection) {
        let mut idle = self.idle.lock().await;
        if idle.len() < MAX_IDLE_CONNECTIONS {
            idle.push_back(conn);
        }
    }
}

/// Whether any proxy environment variable is configured; the fast path must
/// not silently bypass a proxy reqwest would otherwise use.
pub(crate) fn proxy_env_present() -> bool {
    [
        "HTTP_PROXY",
        "http_proxy",
        "HTTPS_PROXY",
        "https_proxy",
        "ALL_PROXY",
        "all_proxy",
    ]
    .iter()
    .any(|name| std::env::var_os(name).is_some())
}

/// Serialize one request into wire bytes.
pub(crate) fn encode_request(
    method: &str,
    target: &str,
    authority: &str,
    headers: &reqwest::header::HeaderMap,
    body: Option<&[u8]>,
    retry_count: Option<u32>,
) -> Vec<u8> {
    // Request line, Host, framing headers, then the merged header set.
    let mut head = format!("{method} {target} HTTP/1.1\r\nhost: {authority}\r\n");
    if let Some(body) = body {
        head.push_str("content-type: application/json\r\n");
        head.push_str("content-length: ");
        head.push_str(&body.len().to_string());
        head.push_str("\r\n");
    }
    if let Some(retries) = retry_count {
        head.push_str("x-typesafe-retry-count: ");
        head.push_str(&retries.to_string());
        head.push_str("\r\n");
    }
    let mut bytes = head.into_bytes();
    for (name, value) in headers {
        // Framing is owned by this transport; a second Host or JSON type is ambiguous.
        if matches!(
            name.as_str(),
            "content-length" | "host" | "transfer-encoding"
        ) || (body.is_some() && name.as_str() == "content-type")
        {
            continue;
        }
        bytes.extend_from_slice(name.as_str().as_bytes());
        bytes.extend_from_slice(b": ");
        bytes.extend_from_slice(value.as_bytes());
        bytes.extend_from_slice(b"\r\n");
    }
    bytes.extend_from_slice(b"\r\n");
    if let Some(body) = body {
        bytes.extend_from_slice(body);
    }
    bytes
}

/// Execute one request/response exchange on a pooled connection.
///
/// The outer `Err` is a whole-attempt timeout (already mapped to
/// [`Error::Timeout`]); the inner result is the exchange outcome.
pub(crate) async fn exchange(
    pool: &H1Pool,
    request: &[u8],
    max_body: usize,
    timeout: Duration,
) -> Result<Result<FastResponse, FastError>, Error> {
    match tokio::time::timeout(timeout, exchange_inner(pool, request, max_body)).await {
        Ok(inner) => Ok(inner),
        Err(_) => Err(Error::Timeout { timeout }),
    }
}

async fn exchange_inner(
    pool: &H1Pool,
    request: &[u8],
    max_body: usize,
) -> Result<FastResponse, FastError> {
    // A connect timeout surfaces with kind `TimedOut` and is classified
    // into `Error::Timeout` by `classify_fast_error`.
    let mut conn = pool.acquire().await.map_err(FastError::Io)?;
    let result = exchange_on(&mut conn, request, max_body).await;
    if result.as_ref().is_ok_and(response_keep_alive) && conn.buffered().is_empty() {
        pool.release(conn).await;
    }
    result
}

async fn exchange_on(
    conn: &mut Connection,
    request: &[u8],
    max_body: usize,
) -> Result<FastResponse, FastError> {
    if let Err(e) = conn.stream.write_all(request).await {
        return Err(FastError::Io(e));
    }
    if let Err(e) = conn.stream.flush().await {
        return Err(FastError::Io(e));
    }

    // Informational heads precede the final response and never frame a body.
    let ResponseHead {
        status,
        headers,
        http11,
    } = loop {
        let head_len = conn.read_head().await.map_err(FastError::Io)?;
        let parsed = parse_head(&conn.buffered()[..head_len]).map_err(FastError::Io)?;
        conn.start += head_len + 4;
        if parsed.status == 101 {
            return Err(FastError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                "HTTP protocol switching is unsupported",
            )));
        }
        if !(100..200).contains(&parsed.status) {
            break parsed;
        }
    };

    if (300..400).contains(&status) && header_lookup(&headers, "location").is_some() {
        // The body is not consumed; the connection is dropped rather than
        // pooled, and the caller re-runs the request through reqwest.
        return Err(FastError::Redirect);
    }

    let mut transfers = headers
        .iter()
        .filter(|(name, _)| name == "transfer-encoding")
        .flat_map(|(_, value)| value.split(',').map(str::trim));
    let chunked = if let Some(encoding) = transfers.next() {
        if !encoding.eq_ignore_ascii_case("chunked") || transfers.next().is_some() {
            return Err(FastError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                "unsupported transfer encoding",
            )));
        }
        true
    } else {
        false
    };
    let mut content_length = None;
    for (_, value) in headers.iter().filter(|(name, _)| name == "content-length") {
        for raw in value.split(',').map(str::trim) {
            if raw.is_empty() || !raw.bytes().all(|byte| byte.is_ascii_digit()) {
                return Err(FastError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "malformed content length",
                )));
            }
            let length = raw.parse::<usize>().map_err(|_| {
                FastError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "malformed content length",
                ))
            })?;
            if content_length.is_some_and(|previous| previous != length) {
                return Err(FastError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "conflicting content lengths",
                )));
            }
            content_length = Some(length);
        }
    }
    let no_body = matches!(status, 204 | 304) || request.starts_with(b"HEAD ");
    let framed = no_body || chunked || content_length.is_some();
    let connection_has = |token: &str| {
        headers
            .iter()
            .filter(|(name, _)| name == "connection")
            .flat_map(|(_, value)| value.split(','))
            .any(|value| value.trim().eq_ignore_ascii_case(token))
    };
    let reusable = framed && !connection_has("close") && (http11 || connection_has("keep-alive"));

    let body = if no_body {
        Vec::new()
    } else if chunked {
        read_chunked_body(conn, max_body).await?
    } else if let Some(length) = content_length {
        if length > max_body {
            // The connection is abandoned, not drained.
            return Err(FastError::TooLarge);
        }
        read_exact_body(conn, length).await?
    } else {
        // No framing headers: the body runs to connection close, and the
        // connection cannot be reused.
        read_until_eof(conn, max_body).await?
    };

    Ok(FastResponse {
        status,
        headers,
        body,
        reusable,
    })
}

fn response_keep_alive(response: &FastResponse) -> bool {
    response.reusable
}

fn header_lookup<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> {
    headers
        .iter()
        .find(|(key, _)| key == name)
        .map(|(_, value)| value.as_str())
}

/// Parse a status line plus header block.
struct ResponseHead {
    status: u16,
    headers: Vec<(String, String)>,
    http11: bool,
}

fn parse_head(head: &[u8]) -> io::Result<ResponseHead> {
    let text = std::str::from_utf8(head)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "non-UTF-8 response head"))?;
    let mut lines = text.split("\r\n");
    let status_line = lines
        .next()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty response head"))?;
    let mut parts = status_line.splitn(3, ' ');
    let version = parts.next().unwrap_or_default();
    if !matches!(version, "HTTP/1.0" | "HTTP/1.1") {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("unsupported HTTP version in `{status_line}`"),
        ));
    }
    let raw_status = parts.next().unwrap_or_default();
    let status: u16 = raw_status.parse().map_err(|_| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("malformed status line `{status_line}`"),
        )
    })?;
    if raw_status.len() != 3 || !(100..600).contains(&status) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid HTTP status code",
        ));
    }
    let mut headers = Vec::new();
    for line in lines {
        if line.is_empty() {
            continue;
        }
        let (name, value) = line
            .split_once(':')
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "malformed HTTP header"))?;
        let name = reqwest::header::HeaderName::from_bytes(name.as_bytes())
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid HTTP header name"))?;
        headers.push((name.as_str().to_owned(), value.trim().to_string()));
    }
    Ok(ResponseHead {
        status,
        headers,
        http11: version == "HTTP/1.1",
    })
}

async fn read_exact_body(conn: &mut Connection, length: usize) -> Result<Vec<u8>, FastError> {
    let mut body = Vec::with_capacity(length.min(1 << 20));
    while body.len() < length {
        if conn.buffered().is_empty() {
            match conn.fill().await {
                Ok(0) => {
                    return Err(FastError::Io(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "connection closed inside response body",
                    )))
                }
                Ok(_) => continue,
                Err(e) => return Err(FastError::Io(e)),
            }
        }
        let take = (length - body.len()).min(conn.buffered().len());
        body.extend_from_slice(&conn.buffered()[..take]);
        conn.start += take;
    }
    Ok(body)
}

async fn read_until_eof(conn: &mut Connection, max_body: usize) -> Result<Vec<u8>, FastError> {
    let mut body = Vec::new();
    loop {
        let available = conn.buffered().len();
        if available > 0 {
            if available > max_body - body.len() {
                return Err(FastError::TooLarge);
            }
            body.extend_from_slice(conn.buffered());
            conn.start += available;
        }
        match conn.fill().await {
            Ok(0) => return Ok(body),
            Ok(_) => {}
            Err(e) => return Err(FastError::Io(e)),
        }
    }
}

/// Decode a `transfer-encoding: chunked` body.
async fn read_chunked_body(conn: &mut Connection, max_body: usize) -> Result<Vec<u8>, FastError> {
    let mut body = Vec::new();
    loop {
        // Chunk-size line, e.g. `1a;name=val`.
        let line = match read_line(conn).await? {
            Some(line) => line,
            None => {
                return Err(FastError::Io(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "connection closed before chunk size",
                )))
            }
        };
        let size_text = line.split(';').next().unwrap_or("").trim();
        let size = usize::from_str_radix(size_text, 16).map_err(|_| {
            FastError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("malformed chunk size `{size_text}`"),
            ))
        })?;
        if size == 0 {
            // Trailer section: consume lines until the empty line.
            let mut trailer_bytes: usize = 0;
            loop {
                match read_line(conn).await? {
                    Some(trailer) if trailer.is_empty() => return Ok(body),
                    Some(trailer) => {
                        trailer_bytes += trailer.len() + 2;
                        if trailer_bytes > MAX_HEAD_BYTES {
                            return Err(FastError::Io(io::Error::new(
                                io::ErrorKind::InvalidData,
                                "chunked trailers exceeded 64 KiB",
                            )));
                        }
                    }
                    None => {
                        return Err(FastError::Io(io::Error::new(
                            io::ErrorKind::UnexpectedEof,
                            "connection closed inside chunked trailers",
                        )))
                    }
                }
            }
        }
        if size > max_body - body.len() {
            return Err(FastError::TooLarge);
        }
        let chunk = read_exact_body(conn, size).await?;
        body.extend_from_slice(&chunk);
        // Trailing CRLF after each chunk.
        let crlf = read_exact_body(conn, 2).await?;
        if crlf != b"\r\n" {
            return Err(FastError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                "missing CRLF after chunk",
            )));
        }
    }
}

/// Read one CRLF-terminated line (returned without the terminator), or
/// `None` on a clean EOF at a line boundary.
async fn read_line(conn: &mut Connection) -> Result<Option<String>, FastError> {
    loop {
        if let Some(pos) = conn.buffered().iter().position(|&b| b == b'\n') {
            if pos == 0 || conn.buffered()[pos - 1] != b'\r' {
                return Err(FastError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "HTTP chunk line requires CRLF",
                )));
            }
            let line_end = pos - 1;
            if line_end > MAX_HEAD_BYTES {
                return Err(FastError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "HTTP chunk line exceeded 64 KiB",
                )));
            }
            let line = String::from_utf8_lossy(&conn.buffered()[..line_end]).into_owned();
            conn.start += pos + 1;
            return Ok(Some(line));
        }
        if conn.buffered().len() > MAX_HEAD_BYTES + 1 {
            return Err(FastError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                "HTTP chunk line exceeded 64 KiB",
            )));
        }
        match conn.fill().await {
            Ok(0) => return Ok(None),
            Ok(_) => {}
            Err(e) => return Err(FastError::Io(e)),
        }
    }
}

/// Map a fast-transport failure into the SDK error taxonomy.
pub(crate) fn classify_fast_error(error: FastError, timeout: Duration, max_body: usize) -> Error {
    match error {
        FastError::Redirect => {
            Error::Config("redirect handling requires the reqwest transport".into())
        }
        FastError::TooLarge => Error::ResponseTooLarge { limit: max_body },
        FastError::Io(e) if e.kind() == io::ErrorKind::TimedOut => Error::Timeout { timeout },
        FastError::Io(e) => Error::Connection(Box::new(e)),
    }
}

#[cfg(test)]
#[path = "http1_tests.rs"]
mod tests;