reqx 0.1.35

Rust HTTP transport client for API SDK libraries with retry, timeout, idempotency, proxy, and pluggable TLS backends
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
use std::io::{Read, Write};
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

use bytes::Bytes;
use http::{HeaderMap, StatusCode};
use serde::de::DeserializeOwned;

use crate::blocking_client::limiters::{GlobalRequestPermit, HostRequestPermit};
use crate::content_encoding::{
    decode_content_encoded_body_limited, should_decode_content_encoded_body,
};
use crate::error::{Error, TimeoutPhase};
use crate::extensions::Clock;
use crate::util::is_timeout_io_error;

use super::{
    Response, StreamCompletion, StreamLifecycle, deadline_elapsed, deadline_limits_wait,
    deadline_within_slack,
};

fn map_read_error(
    source: std::io::Error,
    method: &http::Method,
    uri: &str,
    timeout_ms: u128,
) -> Error {
    if is_timeout_io_error(&source) {
        return Error::Timeout {
            phase: TimeoutPhase::ResponseBody,
            timeout_ms,
            method: method.clone(),
            uri: uri.to_owned(),
        };
    }

    Error::ReadBody {
        source: Box::new(source),
    }
}

fn deadline_exceeded_error(
    method: &http::Method,
    uri: &str,
    timeout_ms: u128,
    total_timeout_ms: Option<u128>,
) -> Error {
    Error::DeadlineExceeded {
        timeout_ms: total_timeout_ms.unwrap_or_else(|| timeout_ms.max(1)),
        method: method.clone(),
        uri: uri.to_owned(),
    }
}

/// Streaming blocking response body with request metadata.
///
/// Use this when you want to consume large responses incrementally without
/// buffering them into memory first.
///
/// See also `examples/blocking_streaming.rs`.
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "blocking-tls-rustls-ring",
        feature = "blocking-tls-rustls-aws-lc-rs",
        feature = "blocking-tls-native"
    )))
)]
pub struct BlockingResponseStream {
    status: StatusCode,
    headers: HeaderMap,
    body: ureq::Body,
    method: http::Method,
    uri_raw: String,
    uri_redacted: String,
    timeout_ms: u128,
    total_timeout_ms: Option<u128>,
    deadline_at: Option<Instant>,
    deadline_slack: Duration,
    clock: Arc<dyn Clock>,
    lifecycle: Option<StreamLifecycle>,
    _global_permit: Option<GlobalRequestPermit>,
    _host_permit: Option<HostRequestPermit>,
}

pub(crate) struct BlockingResponseStreamContext {
    pub(crate) method: http::Method,
    pub(crate) uri_raw: String,
    pub(crate) uri_redacted: String,
    pub(crate) timeout_ms: u128,
    pub(crate) total_timeout_ms: Option<u128>,
    pub(crate) deadline_at: Option<Instant>,
    pub(crate) deadline_slack: Duration,
    pub(crate) clock: Arc<dyn Clock>,
    pub(crate) lifecycle: Option<StreamLifecycle>,
    pub(crate) global_permit: Option<GlobalRequestPermit>,
    pub(crate) host_permit: Option<HostRequestPermit>,
}

impl BlockingResponseStream {
    pub(crate) fn new(
        status: StatusCode,
        headers: HeaderMap,
        body: ureq::Body,
        context: BlockingResponseStreamContext,
    ) -> Self {
        let BlockingResponseStreamContext {
            method,
            uri_raw,
            uri_redacted,
            timeout_ms,
            total_timeout_ms,
            deadline_at,
            deadline_slack,
            clock,
            lifecycle,
            global_permit,
            host_permit,
        } = context;
        Self {
            status,
            headers,
            body,
            method,
            uri_raw,
            uri_redacted,
            timeout_ms: timeout_ms.max(1),
            total_timeout_ms,
            deadline_at,
            deadline_slack,
            clock,
            lifecycle,
            _global_permit: global_permit,
            _host_permit: host_permit,
        }
    }

    pub(crate) fn attach_completion(&mut self, completion: StreamCompletion) {
        super::attach_completion(&mut self.lifecycle, completion);
    }

    /// Returns the HTTP status code.
    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// Returns the response headers.
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    /// Returns the originating request method.
    pub fn method(&self) -> &http::Method {
        &self.method
    }

    /// Returns the original request URI, including query string.
    pub fn uri(&self) -> &str {
        self.uri_raw()
    }

    /// Returns the original request URI, including query string.
    pub fn uri_raw(&self) -> &str {
        &self.uri_raw
    }

    /// Returns a redacted URI suitable for logs and errors.
    ///
    /// The redacted form omits the query string to reduce accidental
    /// leakage of sensitive parameters.
    pub fn uri_redacted(&self) -> &str {
        &self.uri_redacted
    }

    fn response_body_too_large_error(&self, limit_bytes: usize, actual_bytes: usize) -> Error {
        Error::ResponseBodyTooLarge {
            limit_bytes,
            actual_bytes,
            method: self.method.clone(),
            uri: self.uri_redacted.clone(),
        }
    }

    fn write_error(&self, source: std::io::Error) -> Error {
        super::write_body_error(&self.method, &self.uri_redacted, source)
    }

    fn current_read_is_deadline_limited(&self) -> bool {
        let Some(deadline_at) = self.deadline_at else {
            return false;
        };
        deadline_limits_wait(
            Duration::from_millis(self.timeout_ms.max(1) as u64),
            deadline_at,
            self.clock.now_monotonic(),
        )
    }

    fn map_read_error(&self, source: std::io::Error, deadline_limited: bool) -> Error {
        let mapped = map_read_error(source, &self.method, &self.uri_redacted, self.timeout_ms);
        let now = self.clock.now_monotonic();
        if matches!(mapped, Error::Timeout { .. })
            && self.deadline_at.is_some_and(|deadline_at| {
                deadline_elapsed(deadline_at, now)
                    || (deadline_limited
                        && deadline_within_slack(deadline_at, now, self.deadline_slack))
            })
        {
            return deadline_exceeded_error(
                &self.method,
                &self.uri_redacted,
                self.timeout_ms,
                self.total_timeout_ms,
            );
        }
        mapped
    }

    fn ensure_within_deadline(&self) -> crate::Result<()> {
        if let Some(deadline_at) = self.deadline_at
            && deadline_elapsed(deadline_at, self.clock.now_monotonic())
        {
            return Err(deadline_exceeded_error(
                &self.method,
                &self.uri_redacted,
                self.timeout_ms,
                self.total_timeout_ms,
            ));
        }
        Ok(())
    }

    fn read_chunk_internal(
        &mut self,
        buffer: &mut [u8],
        complete_success_on_eof: bool,
    ) -> crate::Result<usize> {
        if buffer.is_empty() {
            return Ok(0);
        }
        if let Err(error) = self.ensure_within_deadline() {
            self.complete_error(&error);
            return Err(error);
        }
        let deadline_limited = self.current_read_is_deadline_limited();
        let read = self
            .body
            .as_reader()
            .read(buffer)
            .map_err(|source| self.map_read_error(source, deadline_limited));
        match read {
            Ok(read) => {
                if let Err(error) = self.ensure_within_deadline() {
                    self.complete_error(&error);
                    return Err(error);
                }
                if read == 0 && complete_success_on_eof {
                    self.complete_success();
                }
                Ok(read)
            }
            Err(error) => {
                self.complete_error(&error);
                Err(error)
            }
        }
    }

    /// Reads the next chunk of body bytes into `buffer`.
    ///
    /// Returns `0` at end of stream.
    pub fn read_chunk(&mut self, buffer: &mut [u8]) -> crate::Result<usize> {
        self.read_chunk_internal(buffer, true)
    }

    fn write_chunk<W>(&mut self, writer: &mut W, chunk: &[u8]) -> crate::Result<()>
    where
        W: Write + ?Sized,
    {
        if let Err(source) = writer.write_all(chunk) {
            let error = self.write_error(source);
            self.complete_error(&error);
            return Err(error);
        }
        Ok(())
    }

    fn flush_writer<W>(&mut self, writer: &mut W) -> crate::Result<()>
    where
        W: Write + ?Sized,
    {
        if let Err(source) = writer.flush() {
            let error = self.write_error(source);
            self.complete_error(&error);
            return Err(error);
        }
        Ok(())
    }

    /// Copies the streamed body into `writer`.
    ///
    /// See also `examples/blocking_streaming.rs`.
    pub fn copy_to_writer<W>(mut self, writer: &mut W) -> crate::Result<u64>
    where
        W: Write + ?Sized,
    {
        let mut chunk = [0_u8; 8192];
        let mut copied = 0_u64;
        loop {
            let read = self.read_chunk_internal(&mut chunk, false)?;
            if read == 0 {
                break;
            }
            self.write_chunk(writer, &chunk[..read])?;
            copied = copied.saturating_add(read as u64);
        }
        self.flush_writer(writer)?;
        self.complete_success();
        Ok(copied)
    }

    /// Copies the streamed body into `writer`, enforcing `max_bytes`.
    pub fn copy_to_writer_limited<W>(
        mut self,
        writer: &mut W,
        max_bytes: usize,
    ) -> crate::Result<u64>
    where
        W: Write + ?Sized,
    {
        let max_bytes = max_bytes.max(1);
        let mut chunk = [0_u8; 8192];
        let mut copied = 0_u64;
        loop {
            let read = self.read_chunk_internal(&mut chunk, false)?;
            if read == 0 {
                break;
            }
            copied = copied.saturating_add(read as u64);
            if copied > max_bytes as u64 {
                let error = self.response_body_too_large_error(max_bytes, copied as usize);
                self.complete_error(&error);
                return Err(error);
            }
            self.write_chunk(writer, &chunk[..read])?;
        }
        self.flush_writer(writer)?;
        self.complete_success();
        Ok(copied)
    }

    /// Buffers the stream into memory, enforcing `max_bytes`.
    pub fn into_bytes_limited(mut self, max_bytes: usize) -> crate::Result<Bytes> {
        let max_bytes = max_bytes.max(1);
        let mut chunk = [0_u8; 8192];
        let mut collected = Vec::new();
        let mut total_len = 0_usize;

        loop {
            let read = self.read_chunk_internal(&mut chunk, false)?;
            if read == 0 {
                break;
            }
            total_len = total_len.saturating_add(read);
            if total_len > max_bytes {
                let error = self.response_body_too_large_error(max_bytes, total_len);
                self.complete_error(&error);
                return Err(error);
            }
            collected.extend_from_slice(&chunk[..read]);
        }
        self.complete_success();
        Ok(Bytes::from(collected))
    }

    /// Buffers and decodes the stream into a [`Response`], enforcing `max_bytes`.
    ///
    /// See also `examples/blocking_streaming.rs`.
    pub fn into_response_limited(mut self, max_bytes: usize) -> crate::Result<Response> {
        let max_bytes = max_bytes.max(1);
        let mut chunk = [0_u8; 8192];
        let mut collected = Vec::new();
        let mut total_len = 0_usize;

        loop {
            let read = self.read_chunk_internal(&mut chunk, false)?;
            if read == 0 {
                break;
            }
            total_len = total_len.saturating_add(read);
            if total_len > max_bytes {
                let error = self.response_body_too_large_error(max_bytes, total_len);
                self.complete_error(&error);
                return Err(error);
            }
            collected.extend_from_slice(&chunk[..read]);
        }
        let body = Bytes::from(collected);
        let status = self.status;
        let method = self.method.clone();
        let uri_redacted = self.uri_redacted.clone();
        let mut headers = std::mem::take(&mut self.headers);
        let should_decode = should_decode_content_encoded_body(&method, status, body.len());
        let body = if should_decode {
            decode_content_encoded_body_limited(body, &headers, max_bytes).map_err(|error| {
                let error = super::map_decode_body_error(error, &method, &uri_redacted, max_bytes);
                self.complete_error(&error);
                error
            })?
        } else {
            body
        };
        if should_decode && headers.contains_key(super::CONTENT_ENCODING) {
            headers.remove(super::CONTENT_ENCODING);
            headers.remove(super::CONTENT_LENGTH);
        }
        self.complete_success();
        Ok(Response::new(status, headers, body))
    }

    /// Buffers the stream as UTF-8 text, enforcing `max_bytes`.
    pub fn into_text_limited(self, max_bytes: usize) -> crate::Result<String> {
        let response = self.into_response_limited(max_bytes)?;
        response.text().map(ToOwned::to_owned)
    }

    /// Buffers the stream as lossy UTF-8 text, enforcing `max_bytes`.
    pub fn into_text_lossy_limited(self, max_bytes: usize) -> crate::Result<String> {
        let response = self.into_response_limited(max_bytes)?;
        Ok(response.text_lossy())
    }

    /// Buffers and deserializes the stream from JSON, enforcing `max_bytes`.
    pub fn into_json_limited<T>(self, max_bytes: usize) -> crate::Result<T>
    where
        T: DeserializeOwned,
    {
        let response = self.into_response_limited(max_bytes)?;
        response.json()
    }

    fn complete_success(&mut self) {
        super::complete_success(&mut self.lifecycle);
    }

    fn complete_error(&mut self, error: &Error) {
        super::complete_error(&mut self.lifecycle, error);
    }
}

impl std::fmt::Debug for BlockingResponseStream {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("BlockingResponseStream")
            .field("status", &self.status)
            .field("headers", &self.headers)
            .field("method", &self.method)
            .field("uri_raw", &self.uri_raw)
            .field("uri_redacted", &self.uri_redacted)
            .field("timeout_ms", &self.timeout_ms)
            .field("total_timeout_ms", &self.total_timeout_ms)
            .field("deadline_at", &self.deadline_at)
            .field("deadline_slack", &self.deadline_slack)
            .field("has_lifecycle", &self.lifecycle.is_some())
            .finish()
    }
}

impl Read for BlockingResponseStream {
    fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
        self.read_chunk(buffer)
            .map_err(super::into_stream_read_io_error)
    }
}