Skip to main content

eggfetch_core/
response.rs

1//! Incoming response type.
2//!
3//! A `Response` holds the HTTP status, version, headers, final URL, and a
4//! body handle. The body can be consumed in two ways:
5//!
6//! - **Buffered**: `bytes()` or `text()` collects the entire body.
7//! - **Streaming**: `bytes_stream()` yields decoded body chunks incrementally;
8//!   `raw_bytes_stream()` selects encoded chunks for compressed responses.
9//!
10//! # Body ownership
11//!
12//! The body can be consumed exactly once. After calling `bytes()`,
13//! `text()`, `bytes_stream()`, or `raw_bytes_stream()`, further reads return
14//! an error.
15
16use std::pin::Pin;
17use std::task::{Context, Poll};
18
19use bytes::{Bytes, BytesMut};
20use futures_core::Stream;
21use http::{HeaderMap, StatusCode, Version};
22use url::Url;
23
24use crate::body::{BoxBytesStream, ResponseBody, SharedTrailers};
25use crate::error::Result;
26use crate::network_stream::NetworkStream;
27
28/// A metadata-only snapshot of a redirect response.
29///
30/// History entries intentionally omit the response body. This makes it
31/// structurally impossible for redirect history to retain active body
32/// streams or pool permits.
33#[derive(Clone)]
34pub struct HistoryEntry {
35    status: StatusCode,
36    version: Version,
37    headers: HeaderMap,
38    url: Url,
39    reason_phrase: String,
40}
41
42impl std::fmt::Debug for HistoryEntry {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("HistoryEntry")
45            .field("status", &self.status)
46            .field("version", &self.version)
47            .field("headers", &redacted_headers(&self.headers))
48            .field("url", &redacted_url(&self.url))
49            .field("reason_phrase", &self.reason_phrase)
50            .finish()
51    }
52}
53
54impl HistoryEntry {
55    /// Create a history entry from a response that has already been
56    /// drained (body consumed).
57    ///
58    /// Only available with the `redirects` feature; the lean profile
59    /// constructs no redirect history.
60    #[cfg(feature = "redirects")]
61    pub(crate) fn from_response(response: &Response) -> Self {
62        let reason = response
63            .wire_reason_phrase
64            .clone()
65            .or_else(|| response.status.canonical_reason().map(str::to_owned))
66            .unwrap_or_default();
67        Self {
68            status: response.status,
69            version: response.version,
70            headers: response.headers.clone(),
71            url: response.url.clone(),
72            reason_phrase: reason,
73        }
74    }
75
76    /// Returns the HTTP status code.
77    #[must_use]
78    pub fn status(&self) -> StatusCode {
79        self.status
80    }
81
82    /// Returns the HTTP version.
83    #[must_use]
84    pub fn version(&self) -> Version {
85        self.version
86    }
87
88    /// Returns the response headers.
89    #[must_use]
90    pub fn headers(&self) -> &HeaderMap {
91        &self.headers
92    }
93
94    /// Returns the URL.
95    #[must_use]
96    pub fn url(&self) -> &Url {
97        &self.url
98    }
99
100    /// Returns the reason phrase.
101    #[must_use]
102    pub fn reason_phrase(&self) -> &str {
103        &self.reason_phrase
104    }
105}
106
107/// An HTTP response.
108///
109/// # Trailers
110///
111/// HTTP trailers (HTTP/1.1 chunked trailers, HTTP/2 trailing HEADERS, and
112/// HTTP/3 trailing headers where the h3 crate exposes them) are captured
113/// without buffering the body. The body stream yields data frames exactly
114/// as before; when the trailing-headers frame arrives it is stored and the
115/// stream ends normally. Use [`Self::trailers`] after fully consuming the
116/// body (`bytes()`, `bytes_stream()` to EOF) to retrieve them.
117///
118/// Lifecycle: `trailers()` returns `None` until the body has advanced far
119/// enough for trailers to arrive, when the response carried no trailers,
120/// or when a body error occurred before trailers (errors remain body
121/// errors, never fabricated trailer state). Partial consumption or early
122/// drop never reports trailers and releases the pool permit without
123/// waiting for them. Read timeouts apply while waiting for trailers at
124/// the body boundary; the absolute native `total` deadline spans the full
125/// lifecycle through body EOF/trailers, never resets per trailer wait, and
126/// reports `Total` when polled after expiry. Dropping before EOF remains
127/// ordinary cancellation.
128pub struct Response {
129    status: StatusCode,
130    version: Version,
131    headers: HeaderMap,
132    url: Url,
133    /// Original wire value retained for compatibility adapters that expose
134    /// response metadata after automatic decompression.
135    wire_content_encoding: Option<String>,
136    /// Original wire value retained for compatibility adapters that expose
137    /// response metadata after automatic decompression.
138    wire_content_length: Option<String>,
139    /// Original wire reason phrase from the HTTP status line.
140    ///
141    /// HTTP/1.x responses include a reason phrase after the status code.
142    /// This field preserves the original wire bytes so compatibility
143    /// adapters can expose the exact phrase rather than deriving one from
144    /// the status code alone. HTTP/2 has no wire reason phrase; this
145    /// field will be `None` for H2 responses.
146    wire_reason_phrase: Option<String>,
147    pub(crate) body: ResponseBody,
148    history: Vec<HistoryEntry>,
149    /// Shared trailer store populated as the body stream advances.
150    ///
151    /// Cloned into the body stream at construction so `trailers()` stays
152    /// available after `bytes()` collection. `None` until trailers arrive.
153    trailers: SharedTrailers,
154    /// Optional network stream handle for connection metadata and
155    /// upgraded-connection IO. Set for responses where the underlying
156    /// transport metadata is available:
157    ///
158    /// - **101 Switching Protocols**: set to `NetworkStream::Upgraded`
159    ///   with full IO access.
160    /// - **Successful CONNECT (200)**: set to `NetworkStream::Upgraded`
161    ///   when the tunnel is established (handled in the proxy path).
162    /// - **Ordinary HTTP/1.1 and HTTP/2 responses**: always `None`.
163    ///   Hyper's connection pool retains ownership of the socket and
164    ///   does not expose per-response socket metadata. Raw IO would
165    ///   corrupt pool state. This is a documented bounded difference
166    ///   from httpcore, which owns its connections directly.
167    network_stream: Option<NetworkStream>,
168}
169
170impl std::fmt::Debug for Response {
171    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172        f.debug_struct("Response")
173            .field("status", &self.status)
174            .field("version", &self.version)
175            .field("headers", &redacted_headers(&self.headers))
176            .field("url", &redacted_url(&self.url))
177            .field("wire_content_encoding", &self.wire_content_encoding)
178            .field("wire_content_length", &self.wire_content_length)
179            .field("wire_reason_phrase", &self.wire_reason_phrase)
180            .field("body", &self.body)
181            .field("history", &self.history)
182            .field(
183                "trailers",
184                &self.trailers.get().map(|h| redacted_headers(&h)),
185            )
186            .field(
187                "network_stream",
188                &self.network_stream.as_ref().map(|_| "..."),
189            )
190            .finish()
191    }
192}
193
194fn redacted_headers(headers: &HeaderMap) -> HeaderMap {
195    crate::redact::redact_headers(headers)
196}
197
198fn redacted_url(url: &Url) -> String {
199    crate::redact::redact_url(url)
200}
201
202impl Response {
203    /// Create a new response (crate-internal).
204    pub(crate) fn new(
205        status: StatusCode,
206        version: Version,
207        headers: HeaderMap,
208        url: Url,
209        body: ResponseBody,
210    ) -> Self {
211        // Non-UTF8 wire values are dropped here (defensible: headers are
212        // opaque bytes); emit a trace event so corrupt-wire debugging has a
213        // signal instead of a silent absence. Laziness is load-bearing: the
214        // trace event must fire only when the value is dropped, so eager
215        // evaluation is not equivalent here.
216        #[allow(clippy::unnecessary_lazy_evaluations)]
217        let wire_content_encoding = headers
218            .get("content-encoding")
219            .and_then(|value| {
220                value.to_str().ok().or_else(|| {
221                    #[cfg(feature = "tracing")]
222                    tracing::debug!("eggfetch: dropped non-UTF8 content-encoding");
223                    None
224                })
225            })
226            .map(ToOwned::to_owned);
227        // Laziness is load-bearing here as above: eager evaluation would
228        // emit the drop trace event even for valid values.
229        #[allow(clippy::unnecessary_lazy_evaluations)]
230        let wire_content_length = headers
231            .get("content-length")
232            .and_then(|value| {
233                value.to_str().ok().or_else(|| {
234                    #[cfg(feature = "tracing")]
235                    tracing::debug!("eggfetch: dropped non-UTF8 content-length");
236                    None
237                })
238            })
239            .map(ToOwned::to_owned);
240        Self {
241            status,
242            version,
243            headers,
244            url,
245            wire_content_encoding,
246            wire_content_length,
247            wire_reason_phrase: None,
248            body,
249            history: Vec::new(),
250            trailers: SharedTrailers::new(),
251            network_stream: None,
252        }
253    }
254
255    /// Replace the body of this response. Crate-internal: used to attach
256    /// a leased body after pool acquisition.
257    pub(crate) fn set_body(&mut self, body: ResponseBody) {
258        self.body = body;
259    }
260
261    /// Attach the shared trailer store linked to the body stream.
262    ///
263    /// Transports create one [`SharedTrailers`], hand a clone to the
264    /// streaming adapter, and install the same instance here so
265    /// [`Self::trailers`] observes trailers populated during streaming.
266    pub(crate) fn set_trailers(&mut self, trailers: SharedTrailers) {
267        self.trailers = trailers;
268    }
269
270    /// Returns received HTTP trailers, if any.
271    ///
272    /// Returns `None` until the body has been fully consumed and trailers
273    /// have arrived, when the response carried no trailers, or when a body
274    /// error occurred before trailers. Duplicate/multi-value trailing
275    /// headers are preserved in the returned [`HeaderMap`].
276    ///
277    /// This never buffers the body: drive the body to EOF via `bytes()` or
278    /// `bytes_stream()` first, then call this method.
279    #[must_use]
280    pub fn trailers(&self) -> Option<HeaderMap> {
281        self.trailers.get()
282    }
283
284    /// Consume the response and return its body.
285    #[allow(dead_code)]
286    pub(crate) fn into_body(self) -> ResponseBody {
287        self.body
288    }
289
290    /// Returns the HTTP status code.
291    #[must_use]
292    pub fn status(&self) -> StatusCode {
293        self.status
294    }
295
296    /// Returns the HTTP version.
297    #[must_use]
298    pub fn version(&self) -> Version {
299        self.version
300    }
301
302    /// Returns the response headers.
303    #[must_use]
304    pub fn headers(&self) -> &HeaderMap {
305        &self.headers
306    }
307
308    /// Returns a mutable reference to the response headers.
309    pub fn headers_mut(&mut self) -> &mut HeaderMap {
310        &mut self.headers
311    }
312
313    /// Returns the original wire `Content-Encoding` value, if present.
314    ///
315    /// Automatic decompression may remove this value from [`Self::headers`];
316    /// compatibility adapters can use this read-only snapshot to preserve
317    /// wire metadata without changing the core decoded-header policy.
318    #[must_use]
319    pub fn wire_content_encoding(&self) -> Option<&str> {
320        self.wire_content_encoding.as_deref()
321    }
322
323    /// Returns the original wire `Content-Length` value, if present.
324    ///
325    /// The value describes encoded wire bytes and is never derived from the
326    /// decompressed response body.
327    #[must_use]
328    pub fn wire_content_length(&self) -> Option<&str> {
329        self.wire_content_length.as_deref()
330    }
331
332    /// Returns the declared wire `Content-Length` as an unsigned integer.
333    ///
334    /// This parses only the original header value. It never reports the
335    /// collected or decompressed body length and returns `None` when the
336    /// header is absent or invalid.
337    #[must_use]
338    pub fn content_length(&self) -> Option<u64> {
339        self.wire_content_length.as_deref()?.parse().ok()
340    }
341
342    /// Returns the original wire reason phrase, if present.
343    ///
344    /// HTTP/1.x responses include a reason phrase after the status code.
345    /// This preserves the original wire bytes rather than deriving from
346    /// the status code alone. HTTP/2 has no wire reason phrase.
347    #[must_use]
348    pub fn wire_reason_phrase(&self) -> Option<&str> {
349        self.wire_reason_phrase.as_deref()
350    }
351
352    /// Set the wire reason phrase for this response.
353    ///
354    /// Used by transports that parse the HTTP/1.x status line directly
355    /// and can extract the original reason phrase.
356    #[allow(dead_code)]
357    pub(crate) fn set_wire_reason_phrase(&mut self, reason: Option<String>) {
358        self.wire_reason_phrase = reason;
359    }
360
361    /// Returns the final URL of the response (after any redirects).
362    #[must_use]
363    pub fn url(&self) -> &Url {
364        &self.url
365    }
366
367    /// Returns the redirect history (prior responses in order).
368    #[must_use]
369    pub fn history(&self) -> &[HistoryEntry] {
370        &self.history
371    }
372
373    /// Returns a mutable reference to the redirect history.
374    pub fn history_mut(&mut self) -> &mut Vec<HistoryEntry> {
375        &mut self.history
376    }
377
378    /// Set the redirect history.
379    ///
380    /// Only available with the `redirects` feature; the lean profile leaves
381    /// history empty.
382    #[cfg(feature = "redirects")]
383    pub(crate) fn set_history(&mut self, history: Vec<HistoryEntry>) {
384        self.history = history;
385    }
386
387    /// Returns `true` if the status code indicates success (2xx).
388    #[must_use]
389    pub fn is_success(&self) -> bool {
390        self.status.is_success()
391    }
392
393    /// Returns a reference to the response body.
394    #[must_use]
395    pub fn body(&self) -> &ResponseBody {
396        &self.body
397    }
398
399    /// Returns a mutable reference to the response body.
400    pub fn body_mut(&mut self) -> &mut ResponseBody {
401        &mut self.body
402    }
403
404    /// Consume the response and return the body bytes.
405    ///
406    /// For buffered bodies, this is O(1). For streaming bodies, this
407    /// collects all chunks.
408    ///
409    /// # Errors
410    ///
411    /// Returns an error if the body has already been consumed or if a
412    /// stream chunk fails.
413    pub async fn bytes(&mut self) -> Result<Bytes> {
414        self.body.bytes().await
415    }
416
417    /// Consume the response and return the body as a UTF-8 string.
418    ///
419    /// # Errors
420    ///
421    /// Returns an error if the body is not valid UTF-8 or has already
422    /// been consumed.
423    pub async fn text(&mut self) -> Result<String> {
424        self.body.text().await
425    }
426
427    /// Consume and deserialize the response body as JSON.
428    ///
429    /// This follows [`Self::bytes`] single-consumption and decoded-body-limit
430    /// semantics. Media type is not checked; calling this method is explicit
431    /// caller intent. The body is parsed directly from bytes without a lossy
432    /// UTF-8 conversion.
433    ///
434    /// # Errors
435    ///
436    /// Returns a body error when the response cannot be consumed, or
437    /// [`crate::Error::JsonDeserialize`] when the bytes are not valid JSON.
438    #[cfg(feature = "json")]
439    pub async fn json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
440        let bytes = self.bytes().await?;
441        serde_json::from_slice(&bytes).map_err(|error| {
442            crate::Error::JsonDeserialize(format!(
443                "invalid JSON at line {}, column {}",
444                error.line(),
445                error.column()
446            ))
447        })
448    }
449
450    /// Consume the response body and return a stream of byte chunks.
451    ///
452    /// For buffered bodies, yields the entire body as a single chunk.
453    /// For streaming bodies, yields chunks incrementally.
454    ///
455    /// # Errors
456    ///
457    /// Returns an error if the body has already been consumed.
458    pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
459        self.body.bytes_stream()
460    }
461
462    /// Consume the response body and return encoded byte chunks.
463    ///
464    /// Compressed streaming responses select the original encoded transport
465    /// body. Raw and decoded body selection are mutually exclusive and
466    /// single-consumption operations.
467    ///
468    /// # Errors
469    ///
470    /// Returns an error if the response body has already been consumed.
471    pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
472        self.body.raw_bytes_stream()
473    }
474
475    /// Consume the response body and return a stream of text lines.
476    ///
477    /// Each line is decoded as UTF-8 without the trailing newline.
478    /// Incomplete lines at chunk boundaries are buffered until the
479    /// next chunk completes them.
480    ///
481    /// # Errors
482    ///
483    /// Returns an error if the body has already been consumed or if a
484    /// chunk contains invalid UTF-8.
485    pub fn text_lines(&mut self) -> Result<impl Stream<Item = Result<String>>> {
486        let byte_stream = self.bytes_stream()?;
487        Ok(LineStream::new(byte_stream))
488    }
489
490    /// Returns a reference to the network stream handle, if available.
491    ///
492    /// For upgraded connections (101/CONNECT), this provides full
493    /// IO access. For ordinary pooled connections, this provides
494    /// read-only metadata.
495    #[must_use]
496    pub fn network_stream(&self) -> Option<&NetworkStream> {
497        self.network_stream.as_ref()
498    }
499
500    /// Returns a mutable reference to the network stream handle.
501    pub fn network_stream_mut(&mut self) -> Option<&mut NetworkStream> {
502        self.network_stream.as_mut()
503    }
504
505    /// Take the network stream out of the response, leaving `None` in
506    /// its place. This is the canonical way to obtain ownership of an
507    /// upgraded stream while still keeping the rest of the response.
508    #[must_use]
509    pub fn take_network_stream(&mut self) -> Option<NetworkStream> {
510        self.network_stream.take()
511    }
512
513    /// Set the network stream handle for this response.
514    pub(crate) fn set_network_stream(&mut self, stream: NetworkStream) {
515        self.network_stream = Some(stream);
516    }
517
518    /// Consume the response and return the network stream, if any.
519    ///
520    /// This is the primary way to obtain ownership of an upgraded
521    /// stream (101 Switching Protocols or successful CONNECT).
522    #[must_use]
523    pub fn into_network_stream(self) -> Option<NetworkStream> {
524        self.network_stream
525    }
526}
527
528/// Stream adapter that splits a byte stream into text lines.
529struct LineStream {
530    stream: BoxBytesStream,
531    buffer: BytesMut,
532}
533
534const MAX_LINE_LENGTH: usize = 1024 * 1024;
535
536impl LineStream {
537    fn new(stream: BoxBytesStream) -> Self {
538        Self {
539            stream,
540            buffer: BytesMut::new(),
541        }
542    }
543}
544
545impl Stream for LineStream {
546    type Item = Result<String>;
547
548    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
549        loop {
550            // Fast path: emit a line already sitting in the buffer. The
551            // length cap applies here too — a single chunk may carry an
552            // over-long line piggybacked behind a valid one.
553            if let Some(pos) = self.buffer.iter().position(|&b| b == b'\n') {
554                if pos > MAX_LINE_LENGTH {
555                    return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
556                        "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
557                    )))));
558                }
559                let line_bytes = self.buffer.split_to(pos + 1);
560                let mut line = &line_bytes[..line_bytes.len() - 1]; // strip \n
561                if line.ends_with(b"\r") {
562                    line = &line[..line.len() - 1];
563                }
564                let line = std::str::from_utf8(line)
565                    .map(str::to_owned)
566                    .map_err(|e| crate::error::Error::Body(e.to_string()))?;
567                return Poll::Ready(Some(Ok(line)));
568            }
569
570            // No newline yet; a partially assembled line must stay bounded.
571            if self.buffer.len() > MAX_LINE_LENGTH {
572                return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
573                    "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
574                )))));
575            }
576
577            // Pull more data. Ready chunks are drained in this loop rather
578            // than rescheduling through the executor per chunk.
579            match Pin::new(&mut self.stream).poll_next(cx) {
580                Poll::Ready(Some(Ok(chunk))) => {
581                    if chunk.is_empty() {
582                        // Empty chunks carry no data. Ignore them and let the
583                        // upstream stream determine whether another wake is
584                        // needed.
585                        continue;
586                    }
587                    // Bound the buffer before extending: a single chunk of
588                    // size `MAX+1` without a newline would otherwise push the
589                    // buffer to `MAX + chunk` before the next loop's limit
590                    // check. If the combined buffer would exceed `MAX` and
591                    // there is no newline within the first `MAX` bytes, reject
592                    // immediately without allocating the oversize tail.
593                    if self.buffer.len().saturating_add(chunk.len()) > MAX_LINE_LENGTH {
594                        let remaining = MAX_LINE_LENGTH.saturating_sub(self.buffer.len());
595                        // `buffer` is known to contain no newline (otherwise
596                        // we would have emitted at the top of the loop), so
597                        // only need to inspect the prefix of `chunk` that fits
598                        // within the limit.
599                        let prefix = &chunk[..remaining.min(chunk.len())];
600                        if !prefix.contains(&b'\n') {
601                            // Also check if chunk itself contains an overlong
602                            // line that straddles the boundary: if the first
603                            // newline in the full chunk is beyond `MAX`, the
604                            // first line is already overlong.
605                            if let Some(pos) = chunk.iter().position(|&b| b == b'\n') {
606                                if self.buffer.len() + pos > MAX_LINE_LENGTH {
607                                    return Poll::Ready(Some(Err(
608                                        crate::error::Error::Body(format!(
609                                            "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
610                                        )),
611                                    )));
612                                }
613                            } else {
614                                return Poll::Ready(Some(Err(
615                                    crate::error::Error::Body(format!(
616                                        "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
617                                    )),
618                                )));
619                            }
620                        }
621                    }
622                    self.buffer.extend_from_slice(&chunk);
623                }
624                Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))),
625                Poll::Ready(None) => {
626                    // Stream ended; flush remaining buffer as a final line.
627                    if self.buffer.is_empty() {
628                        return Poll::Ready(None);
629                    }
630                    let remaining = std::mem::take(&mut self.buffer);
631                    if remaining.len() > MAX_LINE_LENGTH {
632                        return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
633                            "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
634                        )))));
635                    }
636                    let mut line = &remaining[..remaining.len()];
637                    if line.ends_with(b"\r") {
638                        line = &line[..line.len() - 1];
639                    }
640                    let line = std::str::from_utf8(line)
641                        .map(str::to_owned)
642                        .map_err(|e| crate::error::Error::Body(e.to_string()))?;
643                    return Poll::Ready(Some(Ok(line)));
644                }
645                Poll::Pending => return Poll::Pending,
646            }
647        }
648    }
649}
650
651#[cfg(test)]
652mod tests {
653    use super::*;
654    use http::HeaderValue;
655
656    #[test]
657    fn response_debug_redacts_sensitive_headers_and_url_parts() {
658        let mut headers = HeaderMap::new();
659        headers.insert("authorization", HeaderValue::from_static("Bearer secret"));
660        headers.insert("set-cookie", HeaderValue::from_static("session=secret"));
661        let response = Response::new(
662            StatusCode::FOUND,
663            Version::HTTP_11,
664            headers,
665            Url::parse("https://user:pass@example.com/path?token=secret#fragment").unwrap(),
666            ResponseBody::buffered(Bytes::new()),
667        );
668        let rendered = format!("{response:?}");
669        assert!(!rendered.contains("secret"));
670        assert!(!rendered.contains("user:pass"));
671        assert!(rendered.contains("https://example.com/path"));
672    }
673
674    #[test]
675    fn response_retains_wire_content_metadata_without_changing_headers() {
676        let mut headers = HeaderMap::new();
677        headers.insert("content-encoding", HeaderValue::from_static("gzip"));
678        headers.insert("content-length", HeaderValue::from_static("42"));
679        let response = Response::new(
680            StatusCode::OK,
681            Version::HTTP_11,
682            headers,
683            Url::parse("http://example.com").unwrap(),
684            ResponseBody::buffered(Bytes::new()),
685        );
686
687        assert_eq!(response.wire_content_encoding(), Some("gzip"));
688        assert_eq!(response.wire_content_length(), Some("42"));
689        assert_eq!(
690            response.headers().get("content-encoding"),
691            Some(&HeaderValue::from_static("gzip"))
692        );
693        assert_eq!(
694            response.headers().get("content-length"),
695            Some(&HeaderValue::from_static("42"))
696        );
697        assert_eq!(response.content_length(), Some(42));
698    }
699
700    #[test]
701    fn invalid_wire_content_length_is_not_inferred() {
702        let mut headers = HeaderMap::new();
703        headers.insert("content-length", HeaderValue::from_static("not-a-number"));
704        let response = Response::new(
705            StatusCode::OK,
706            Version::HTTP_11,
707            headers,
708            Url::parse("http://example.com").unwrap(),
709            ResponseBody::buffered(Bytes::from_static(b"1234")),
710        );
711        assert_eq!(response.content_length(), None);
712    }
713
714    #[cfg(feature = "json")]
715    #[tokio::test]
716    async fn response_json_deserializes_once_and_classifies_parse_errors() {
717        let mut response = Response::new(
718            StatusCode::OK,
719            Version::HTTP_11,
720            HeaderMap::new(),
721            Url::parse("http://example.com").unwrap(),
722            ResponseBody::buffered(Bytes::from_static(br#"{"key":"value"}"#)),
723        );
724        let value: serde_json::Value = response.json().await.unwrap();
725        assert_eq!(value["key"], "value");
726        assert!(matches!(response.bytes().await, Err(crate::Error::Body(_))));
727
728        let mut invalid = Response::new(
729            StatusCode::OK,
730            Version::HTTP_11,
731            HeaderMap::new(),
732            Url::parse("http://example.com").unwrap(),
733            ResponseBody::buffered(Bytes::from_static(b"{not-json")),
734        );
735        let error = invalid.json::<serde_json::Value>().await.unwrap_err();
736        assert_eq!(error.kind(), "json_deserialize");
737        assert!(!error.to_string().contains("not-json"));
738    }
739    use futures_util::StreamExt;
740
741    #[tokio::test]
742    async fn response_bytes_and_text() {
743        let mut resp = Response::new(
744            StatusCode::OK,
745            Version::HTTP_11,
746            HeaderMap::new(),
747            Url::parse("http://example.com").unwrap(),
748            ResponseBody::buffered(Bytes::from("hello")),
749        );
750        assert!(resp.is_success());
751        let text = resp.text().await.unwrap();
752        assert_eq!(text, "hello");
753    }
754
755    #[tokio::test]
756    async fn response_streaming_bytes_collect() {
757        let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
758        let stream = Box::pin(futures_util::stream::iter(chunks));
759        let mut resp = Response::new(
760            StatusCode::OK,
761            Version::HTTP_11,
762            HeaderMap::new(),
763            Url::parse("http://example.com").unwrap(),
764            ResponseBody::streaming(stream),
765        );
766        let bytes = resp.bytes().await.unwrap();
767        assert_eq!(bytes, "hello world");
768    }
769
770    #[tokio::test]
771    async fn response_text_lines_rejects_unbounded_line() {
772        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(vec![
773            b'a'; MAX_LINE_LENGTH + 1
774        ]))]));
775        let mut response = Response::new(
776            StatusCode::OK,
777            Version::HTTP_11,
778            HeaderMap::new(),
779            Url::parse("http://example.com").unwrap(),
780            ResponseBody::streaming(stream),
781        );
782        let mut lines = response.text_lines().unwrap();
783        let error = lines.next().await.unwrap().unwrap_err();
784        assert!(matches!(error, crate::error::Error::Body(_)));
785    }
786
787    #[tokio::test]
788    async fn response_text_lines_rejects_overlong_line_after_valid_line() {
789        // A single chunk carrying a valid line followed by an over-long
790        // line must not smuggle the second line past MAX_LINE_LENGTH via
791        // the buffered fast path.
792        let mut payload = b"ok\n".to_vec();
793        payload.extend_from_slice(&vec![b'a'; MAX_LINE_LENGTH + 1]);
794        payload.push(b'\n');
795        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(payload))]));
796        let mut resp = Response::new(
797            StatusCode::OK,
798            Version::HTTP_11,
799            HeaderMap::new(),
800            Url::parse("http://example.com").unwrap(),
801            ResponseBody::streaming(stream),
802        );
803        let mut lines = resp.text_lines().unwrap();
804        assert_eq!(lines.next().await.unwrap().unwrap(), "ok");
805        let error = lines.next().await.unwrap().unwrap_err();
806        assert!(matches!(error, crate::error::Error::Body(_)));
807    }
808
809    #[tokio::test]
810    async fn response_text_lines_rejects_unbounded_terminal_line() {
811        // The terminal flush path (`Poll::Ready(None)`) must enforce
812        // `MAX_LINE_LENGTH` even when the over-long buffer never sees a
813        // trailing newline.
814        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(vec![
815            b'a'; MAX_LINE_LENGTH + 1
816        ]))]));
817        let mut resp = Response::new(
818            StatusCode::OK,
819            Version::HTTP_11,
820            HeaderMap::new(),
821            Url::parse("http://example.com").unwrap(),
822            ResponseBody::streaming(stream),
823        );
824        let mut lines = resp.text_lines().unwrap();
825        let error = lines.next().await.unwrap().unwrap_err();
826        assert!(matches!(error, crate::error::Error::Body(_)));
827    }
828
829    #[tokio::test]
830    async fn response_bytes_stream_yields_chunks() {
831        let chunks = vec![Ok(Bytes::from("a")), Ok(Bytes::from("b"))];
832        let stream = Box::pin(futures_util::stream::iter(chunks));
833        let mut resp = Response::new(
834            StatusCode::OK,
835            Version::HTTP_11,
836            HeaderMap::new(),
837            Url::parse("http://example.com").unwrap(),
838            ResponseBody::streaming(stream),
839        );
840        let mut stream = resp.bytes_stream().unwrap();
841        assert_eq!(stream.next().await.unwrap().unwrap(), "a");
842        assert_eq!(stream.next().await.unwrap().unwrap(), "b");
843        assert!(stream.next().await.is_none());
844    }
845
846    #[tokio::test]
847    async fn response_text_lines_basic() {
848        let chunks = vec![Ok(Bytes::from("line1\nline2\nline3\n"))];
849        let stream = Box::pin(futures_util::stream::iter(chunks));
850        let mut resp = Response::new(
851            StatusCode::OK,
852            Version::HTTP_11,
853            HeaderMap::new(),
854            Url::parse("http://example.com").unwrap(),
855            ResponseBody::streaming(stream),
856        );
857        let mut lines = resp.text_lines().unwrap();
858        assert_eq!(lines.next().await.unwrap().unwrap(), "line1");
859        assert_eq!(lines.next().await.unwrap().unwrap(), "line2");
860        assert_eq!(lines.next().await.unwrap().unwrap(), "line3");
861        assert!(lines.next().await.is_none());
862    }
863
864    #[tokio::test]
865    async fn response_text_lines_crlf() {
866        let chunks = vec![Ok(Bytes::from("line1\r\nline2\r\n"))];
867        let stream = Box::pin(futures_util::stream::iter(chunks));
868        let mut resp = Response::new(
869            StatusCode::OK,
870            Version::HTTP_11,
871            HeaderMap::new(),
872            Url::parse("http://example.com").unwrap(),
873            ResponseBody::streaming(stream),
874        );
875        let mut lines = resp.text_lines().unwrap();
876        assert_eq!(lines.next().await.unwrap().unwrap(), "line1");
877        assert_eq!(lines.next().await.unwrap().unwrap(), "line2");
878        assert!(lines.next().await.is_none());
879    }
880
881    #[tokio::test]
882    async fn response_text_lines_across_chunks() {
883        let chunks = vec![
884            Ok(Bytes::from("hel")),
885            Ok(Bytes::from("lo\nwor")),
886            Ok(Bytes::from("ld\n")),
887        ];
888        let stream = Box::pin(futures_util::stream::iter(chunks));
889        let mut resp = Response::new(
890            StatusCode::OK,
891            Version::HTTP_11,
892            HeaderMap::new(),
893            Url::parse("http://example.com").unwrap(),
894            ResponseBody::streaming(stream),
895        );
896        let mut lines = resp.text_lines().unwrap();
897        assert_eq!(lines.next().await.unwrap().unwrap(), "hello");
898        assert_eq!(lines.next().await.unwrap().unwrap(), "world");
899        assert!(lines.next().await.is_none());
900    }
901
902    #[tokio::test]
903    async fn response_text_lines_no_trailing_newline() {
904        let chunks = vec![Ok(Bytes::from("hello"))];
905        let stream = Box::pin(futures_util::stream::iter(chunks));
906        let mut resp = Response::new(
907            StatusCode::OK,
908            Version::HTTP_11,
909            HeaderMap::new(),
910            Url::parse("http://example.com").unwrap(),
911            ResponseBody::streaming(stream),
912        );
913        let mut lines = resp.text_lines().unwrap();
914        assert_eq!(lines.next().await.unwrap().unwrap(), "hello");
915        assert!(lines.next().await.is_none());
916    }
917
918    #[test]
919    fn trailers_absent_initially() {
920        let resp = Response::new(
921            StatusCode::OK,
922            Version::HTTP_11,
923            HeaderMap::new(),
924            Url::parse("http://example.com").unwrap(),
925            ResponseBody::buffered(Bytes::new()),
926        );
927        assert!(resp.trailers().is_none());
928    }
929
930    #[test]
931    fn trailers_preserve_duplicate_multi_value() {
932        use crate::body::SharedTrailers;
933        let shared = SharedTrailers::new();
934        let mut map = HeaderMap::new();
935        map.append("x-trailer", HeaderValue::from_static("a"));
936        map.append("x-trailer", HeaderValue::from_static("b"));
937        map.insert("x-single", HeaderValue::from_static("v"));
938        shared.store(map);
939        let mut resp = Response::new(
940            StatusCode::OK,
941            Version::HTTP_11,
942            HeaderMap::new(),
943            Url::parse("http://example.com").unwrap(),
944            ResponseBody::buffered(Bytes::new()),
945        );
946        resp.set_trailers(shared);
947        let trailers = resp.trailers().expect("trailers present");
948        let values: Vec<_> = trailers
949            .get_all("x-trailer")
950            .iter()
951            .map(|v| v.to_str().unwrap().to_owned())
952            .collect();
953        assert_eq!(values, vec!["a", "b"]);
954        assert_eq!(trailers.get("x-single").unwrap(), "v");
955    }
956
957    #[tokio::test]
958    async fn trailers_available_after_bytes_collection() {
959        use crate::body::SharedTrailers;
960        // Simulate a transport that populates trailers when the body is
961        // driven to EOF (same contract as `wrap_incoming`).
962        let shared = SharedTrailers::new();
963        let shared_for_stream = shared.clone();
964        let stream = Box::pin(futures_util::stream::unfold(0, move |step| {
965            let shared_for_stream = shared_for_stream.clone();
966            async move {
967                if step == 0 {
968                    Some((Ok(Bytes::from("hello")), 1))
969                } else if step == 1 {
970                    let mut map = HeaderMap::new();
971                    map.insert("x-done", HeaderValue::from_static("yes"));
972                    shared_for_stream.store(map);
973                    None
974                } else {
975                    None
976                }
977            }
978        }));
979        let mut resp = Response::new(
980            StatusCode::OK,
981            Version::HTTP_11,
982            HeaderMap::new(),
983            Url::parse("http://example.com").unwrap(),
984            ResponseBody::streaming(stream),
985        );
986        resp.set_trailers(shared);
987        assert!(
988            resp.trailers().is_none(),
989            "partial consumption reports None"
990        );
991        let bytes = resp.bytes().await.unwrap();
992        assert_eq!(bytes, "hello");
993        assert_eq!(
994            resp.trailers().unwrap().get("x-done").unwrap(),
995            "yes",
996            "trailers available after body completion"
997        );
998    }
999
1000    #[tokio::test]
1001    async fn partial_consumption_does_not_falsely_report_trailers() {
1002        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("a"))]));
1003        let mut resp = Response::new(
1004            StatusCode::OK,
1005            Version::HTTP_11,
1006            HeaderMap::new(),
1007            Url::parse("http://example.com").unwrap(),
1008            ResponseBody::streaming(stream),
1009        );
1010        // No trailers wired; even after partial read there must be none.
1011        let mut s = resp.bytes_stream().unwrap();
1012        assert_eq!(s.next().await.unwrap().unwrap(), "a");
1013        assert!(resp.trailers().is_none());
1014    }
1015
1016    #[tokio::test]
1017    async fn body_error_before_trailers_leaves_no_trailers() {
1018        use crate::body::SharedTrailers;
1019        let shared = SharedTrailers::new();
1020        let stream = Box::pin(futures_util::stream::iter(vec![
1021            Ok(Bytes::from("a")),
1022            Err(crate::error::Error::Body("boom".into())),
1023        ]));
1024        let mut resp = Response::new(
1025            StatusCode::OK,
1026            Version::HTTP_11,
1027            HeaderMap::new(),
1028            Url::parse("http://example.com").unwrap(),
1029            ResponseBody::streaming(stream),
1030        );
1031        resp.set_trailers(shared);
1032        let err = resp.bytes().await.unwrap_err();
1033        assert!(matches!(err, crate::error::Error::Body(_)));
1034        assert!(
1035            resp.trailers().is_none(),
1036            "body errors must not fabricate trailer state"
1037        );
1038    }
1039}