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(
217            clippy::unnecessary_lazy_evaluations,
218            reason = "the trace event must fire only when the value is dropped, so the lazy closure is not equivalent to eager evaluation"
219        )]
220        let wire_content_encoding = headers
221            .get("content-encoding")
222            .and_then(|value| {
223                value.to_str().ok().or_else(|| {
224                    #[cfg(feature = "tracing")]
225                    tracing::debug!("eggfetch: dropped non-UTF8 content-encoding");
226                    None
227                })
228            })
229            .map(ToOwned::to_owned);
230        // Laziness is load-bearing here as above: eager evaluation would
231        // emit the drop trace event even for valid values.
232        #[allow(
233            clippy::unnecessary_lazy_evaluations,
234            reason = "the trace event must fire only when the value is dropped, so the lazy closure is not equivalent to eager evaluation"
235        )]
236        let wire_content_length = headers
237            .get("content-length")
238            .and_then(|value| {
239                value.to_str().ok().or_else(|| {
240                    #[cfg(feature = "tracing")]
241                    tracing::debug!("eggfetch: dropped non-UTF8 content-length");
242                    None
243                })
244            })
245            .map(ToOwned::to_owned);
246        Self {
247            status,
248            version,
249            headers,
250            url,
251            wire_content_encoding,
252            wire_content_length,
253            wire_reason_phrase: None,
254            body,
255            history: Vec::new(),
256            trailers: SharedTrailers::new(),
257            network_stream: None,
258        }
259    }
260
261    /// Replace the body of this response. Crate-internal: used to attach
262    /// a leased body after pool acquisition.
263    pub(crate) fn set_body(&mut self, body: ResponseBody) {
264        self.body = body;
265    }
266
267    /// Attach the shared trailer store linked to the body stream.
268    ///
269    /// Transports create one [`SharedTrailers`], hand a clone to the
270    /// streaming adapter, and install the same instance here so
271    /// [`Self::trailers`] observes trailers populated during streaming.
272    pub(crate) fn set_trailers(&mut self, trailers: SharedTrailers) {
273        self.trailers = trailers;
274    }
275
276    /// Returns received HTTP trailers, if any.
277    ///
278    /// Returns `None` until the body has been fully consumed and trailers
279    /// have arrived, when the response carried no trailers, or when a body
280    /// error occurred before trailers. Duplicate/multi-value trailing
281    /// headers are preserved in the returned [`HeaderMap`].
282    ///
283    /// This never buffers the body: drive the body to EOF via `bytes()` or
284    /// `bytes_stream()` first, then call this method.
285    #[must_use]
286    pub fn trailers(&self) -> Option<HeaderMap> {
287        self.trailers.get()
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(
357        dead_code,
358        reason = "the wire reason phrase is only set by the proxy and CONNECT transports, which are compiled out when the proxy feature is disabled"
359    )]
360    pub(crate) fn set_wire_reason_phrase(&mut self, reason: Option<String>) {
361        self.wire_reason_phrase = reason;
362    }
363
364    /// Returns the final URL of the response (after any redirects).
365    #[must_use]
366    pub fn url(&self) -> &Url {
367        &self.url
368    }
369
370    /// Returns the redirect history (prior responses in order).
371    #[must_use]
372    pub fn history(&self) -> &[HistoryEntry] {
373        &self.history
374    }
375
376    /// Returns a mutable reference to the redirect history.
377    pub fn history_mut(&mut self) -> &mut Vec<HistoryEntry> {
378        &mut self.history
379    }
380
381    /// Set the redirect history.
382    ///
383    /// Only available with the `redirects` feature; the lean profile leaves
384    /// history empty.
385    #[cfg(feature = "redirects")]
386    pub(crate) fn set_history(&mut self, history: Vec<HistoryEntry>) {
387        self.history = history;
388    }
389
390    /// Returns `true` if the status code indicates success (2xx).
391    #[must_use]
392    pub fn is_success(&self) -> bool {
393        self.status.is_success()
394    }
395
396    /// Returns a reference to the response body.
397    #[must_use]
398    pub fn body(&self) -> &ResponseBody {
399        &self.body
400    }
401
402    /// Returns a mutable reference to the response body.
403    pub fn body_mut(&mut self) -> &mut ResponseBody {
404        &mut self.body
405    }
406
407    /// Consume the response and return the body bytes.
408    ///
409    /// For buffered bodies, this is O(1). For streaming bodies, this
410    /// collects all chunks.
411    ///
412    /// # Errors
413    ///
414    /// Returns an error if the body has already been consumed or if a
415    /// stream chunk fails.
416    pub async fn bytes(&mut self) -> Result<Bytes> {
417        self.body.bytes().await
418    }
419
420    /// Consume the response and return the body as a UTF-8 string.
421    ///
422    /// # Errors
423    ///
424    /// Returns an error if the body is not valid UTF-8 or has already
425    /// been consumed.
426    pub async fn text(&mut self) -> Result<String> {
427        self.body.text().await
428    }
429
430    /// Consume and deserialize the response body as JSON.
431    ///
432    /// This follows [`Self::bytes`] single-consumption and decoded-body-limit
433    /// semantics. Media type is not checked; calling this method is explicit
434    /// caller intent. The body is parsed directly from bytes without a lossy
435    /// UTF-8 conversion.
436    ///
437    /// # Errors
438    ///
439    /// Returns a body error when the response cannot be consumed, or
440    /// [`crate::Error::JsonDeserialize`] when the bytes are not valid JSON.
441    #[cfg(feature = "json")]
442    pub async fn json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
443        let bytes = self.bytes().await?;
444        serde_json::from_slice(&bytes).map_err(|error| {
445            crate::Error::JsonDeserialize(format!(
446                "invalid JSON at line {}, column {}",
447                error.line(),
448                error.column()
449            ))
450        })
451    }
452
453    /// Consume the response body and return a stream of byte chunks.
454    ///
455    /// For buffered bodies, yields the entire body as a single chunk.
456    /// For streaming bodies, yields chunks incrementally.
457    ///
458    /// # Errors
459    ///
460    /// Returns an error if the body has already been consumed.
461    pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
462        self.body.bytes_stream()
463    }
464
465    /// Consume the response body and return encoded byte chunks.
466    ///
467    /// Compressed streaming responses select the original encoded transport
468    /// body. Raw and decoded body selection are mutually exclusive and
469    /// single-consumption operations.
470    ///
471    /// # Errors
472    ///
473    /// Returns an error if the response body has already been consumed.
474    pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
475        self.body.raw_bytes_stream()
476    }
477
478    /// Consume the response body and return a stream of text lines.
479    ///
480    /// Each line is decoded as UTF-8 without the trailing newline.
481    /// Incomplete lines at chunk boundaries are buffered until the
482    /// next chunk completes them.
483    ///
484    /// # Errors
485    ///
486    /// Returns an error if the body has already been consumed or if a
487    /// chunk contains invalid UTF-8.
488    pub fn text_lines(&mut self) -> Result<impl Stream<Item = Result<String>>> {
489        let byte_stream = self.bytes_stream()?;
490        Ok(LineStream::new(byte_stream))
491    }
492
493    /// Returns a reference to the network stream handle, if available.
494    ///
495    /// For upgraded connections (101/CONNECT), this provides full
496    /// IO access. For ordinary pooled connections, this provides
497    /// read-only metadata.
498    #[must_use]
499    pub fn network_stream(&self) -> Option<&NetworkStream> {
500        self.network_stream.as_ref()
501    }
502
503    /// Returns a mutable reference to the network stream handle.
504    pub fn network_stream_mut(&mut self) -> Option<&mut NetworkStream> {
505        self.network_stream.as_mut()
506    }
507
508    /// Take the network stream out of the response, leaving `None` in
509    /// its place. This is the canonical way to obtain ownership of an
510    /// upgraded stream while still keeping the rest of the response.
511    #[must_use]
512    pub fn take_network_stream(&mut self) -> Option<NetworkStream> {
513        self.network_stream.take()
514    }
515
516    /// Set the network stream handle for this response.
517    pub(crate) fn set_network_stream(&mut self, stream: NetworkStream) {
518        self.network_stream = Some(stream);
519    }
520
521    /// Consume the response and return the network stream, if any.
522    ///
523    /// This is the primary way to obtain ownership of an upgraded
524    /// stream (101 Switching Protocols or successful CONNECT).
525    #[must_use]
526    pub fn into_network_stream(self) -> Option<NetworkStream> {
527        self.network_stream
528    }
529}
530
531/// Stream adapter that splits a byte stream into text lines.
532struct LineStream {
533    stream: BoxBytesStream,
534    buffer: BytesMut,
535}
536
537const MAX_LINE_LENGTH: usize = 1024 * 1024;
538
539impl LineStream {
540    fn new(stream: BoxBytesStream) -> Self {
541        Self {
542            stream,
543            buffer: BytesMut::new(),
544        }
545    }
546}
547
548impl Stream for LineStream {
549    type Item = Result<String>;
550
551    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
552        loop {
553            // Fast path: emit a line already sitting in the buffer. The
554            // length cap applies here too — a single chunk may carry an
555            // over-long line piggybacked behind a valid one.
556            if let Some(pos) = self.buffer.iter().position(|&b| b == b'\n') {
557                if pos > MAX_LINE_LENGTH {
558                    return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
559                        "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
560                    )))));
561                }
562                let line_bytes = self.buffer.split_to(pos + 1);
563                let mut line = &line_bytes[..line_bytes.len() - 1]; // strip \n
564                if line.ends_with(b"\r") {
565                    line = &line[..line.len() - 1];
566                }
567                let line = std::str::from_utf8(line)
568                    .map(str::to_owned)
569                    .map_err(|e| crate::error::Error::Body(e.to_string()))?;
570                return Poll::Ready(Some(Ok(line)));
571            }
572
573            // No newline yet; a partially assembled line must stay bounded.
574            if self.buffer.len() > MAX_LINE_LENGTH {
575                return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
576                    "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
577                )))));
578            }
579
580            // Pull more data. Ready chunks are drained in this loop rather
581            // than rescheduling through the executor per chunk.
582            match Pin::new(&mut self.stream).poll_next(cx) {
583                Poll::Ready(Some(Ok(chunk))) => {
584                    if chunk.is_empty() {
585                        // Empty chunks carry no data. Ignore them and let the
586                        // upstream stream determine whether another wake is
587                        // needed.
588                        continue;
589                    }
590                    // Bound the buffer before extending: a single chunk of
591                    // size `MAX+1` without a newline would otherwise push the
592                    // buffer to `MAX + chunk` before the next loop's limit
593                    // check. If the combined buffer would exceed `MAX` and
594                    // there is no newline within the first `MAX` bytes, reject
595                    // immediately without allocating the oversize tail.
596                    if self.buffer.len().saturating_add(chunk.len()) > MAX_LINE_LENGTH {
597                        let remaining = MAX_LINE_LENGTH.saturating_sub(self.buffer.len());
598                        // `buffer` is known to contain no newline (otherwise
599                        // we would have emitted at the top of the loop), so
600                        // only need to inspect the prefix of `chunk` that fits
601                        // within the limit.
602                        let prefix = &chunk[..remaining.min(chunk.len())];
603                        if !prefix.contains(&b'\n') {
604                            // Also check if chunk itself contains an overlong
605                            // line that straddles the boundary: if the first
606                            // newline in the full chunk is beyond `MAX`, the
607                            // first line is already overlong.
608                            if let Some(pos) = chunk.iter().position(|&b| b == b'\n') {
609                                if self.buffer.len() + pos > MAX_LINE_LENGTH {
610                                    return Poll::Ready(Some(Err(
611                                        crate::error::Error::Body(format!(
612                                            "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
613                                        )),
614                                    )));
615                                }
616                            } else {
617                                return Poll::Ready(Some(Err(
618                                    crate::error::Error::Body(format!(
619                                        "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
620                                    )),
621                                )));
622                            }
623                        }
624                    }
625                    self.buffer.extend_from_slice(&chunk);
626                }
627                Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))),
628                Poll::Ready(None) => {
629                    // Stream ended; flush remaining buffer as a final line.
630                    if self.buffer.is_empty() {
631                        return Poll::Ready(None);
632                    }
633                    let remaining = std::mem::take(&mut self.buffer);
634                    if remaining.len() > MAX_LINE_LENGTH {
635                        return Poll::Ready(Some(Err(crate::error::Error::Body(format!(
636                            "response line exceeded maximum length of {MAX_LINE_LENGTH} bytes"
637                        )))));
638                    }
639                    let mut line = &remaining[..remaining.len()];
640                    if line.ends_with(b"\r") {
641                        line = &line[..line.len() - 1];
642                    }
643                    let line = std::str::from_utf8(line)
644                        .map(str::to_owned)
645                        .map_err(|e| crate::error::Error::Body(e.to_string()))?;
646                    return Poll::Ready(Some(Ok(line)));
647                }
648                Poll::Pending => return Poll::Pending,
649            }
650        }
651    }
652}
653
654#[cfg(test)]
655mod tests {
656    use super::*;
657    use http::HeaderValue;
658
659    #[test]
660    fn response_debug_redacts_sensitive_headers_and_url_parts() {
661        let mut headers = HeaderMap::new();
662        headers.insert("authorization", HeaderValue::from_static("Bearer secret"));
663        headers.insert("set-cookie", HeaderValue::from_static("session=secret"));
664        let response = Response::new(
665            StatusCode::FOUND,
666            Version::HTTP_11,
667            headers,
668            Url::parse("https://user:pass@example.com/path?token=secret#fragment").unwrap(),
669            ResponseBody::buffered(Bytes::new()),
670        );
671        let rendered = format!("{response:?}");
672        assert!(!rendered.contains("secret"));
673        assert!(!rendered.contains("user:pass"));
674        assert!(rendered.contains("https://example.com/path"));
675    }
676
677    #[test]
678    fn response_retains_wire_content_metadata_without_changing_headers() {
679        let mut headers = HeaderMap::new();
680        headers.insert("content-encoding", HeaderValue::from_static("gzip"));
681        headers.insert("content-length", HeaderValue::from_static("42"));
682        let response = Response::new(
683            StatusCode::OK,
684            Version::HTTP_11,
685            headers,
686            Url::parse("http://example.com").unwrap(),
687            ResponseBody::buffered(Bytes::new()),
688        );
689
690        assert_eq!(response.wire_content_encoding(), Some("gzip"));
691        assert_eq!(response.wire_content_length(), Some("42"));
692        assert_eq!(
693            response.headers().get("content-encoding"),
694            Some(&HeaderValue::from_static("gzip"))
695        );
696        assert_eq!(
697            response.headers().get("content-length"),
698            Some(&HeaderValue::from_static("42"))
699        );
700        assert_eq!(response.content_length(), Some(42));
701    }
702
703    #[test]
704    fn invalid_wire_content_length_is_not_inferred() {
705        let mut headers = HeaderMap::new();
706        headers.insert("content-length", HeaderValue::from_static("not-a-number"));
707        let response = Response::new(
708            StatusCode::OK,
709            Version::HTTP_11,
710            headers,
711            Url::parse("http://example.com").unwrap(),
712            ResponseBody::buffered(Bytes::from_static(b"1234")),
713        );
714        assert_eq!(response.content_length(), None);
715    }
716
717    #[cfg(feature = "json")]
718    #[tokio::test]
719    async fn response_json_deserializes_once_and_classifies_parse_errors() {
720        let mut response = Response::new(
721            StatusCode::OK,
722            Version::HTTP_11,
723            HeaderMap::new(),
724            Url::parse("http://example.com").unwrap(),
725            ResponseBody::buffered(Bytes::from_static(br#"{"key":"value"}"#)),
726        );
727        let value: serde_json::Value = response.json().await.unwrap();
728        assert_eq!(value["key"], "value");
729        assert!(matches!(response.bytes().await, Err(crate::Error::Body(_))));
730
731        let mut invalid = Response::new(
732            StatusCode::OK,
733            Version::HTTP_11,
734            HeaderMap::new(),
735            Url::parse("http://example.com").unwrap(),
736            ResponseBody::buffered(Bytes::from_static(b"{not-json")),
737        );
738        let error = invalid.json::<serde_json::Value>().await.unwrap_err();
739        assert_eq!(error.kind(), "json_deserialize");
740        assert!(!error.to_string().contains("not-json"));
741    }
742    use futures_util::StreamExt;
743
744    #[tokio::test]
745    async fn response_bytes_and_text() {
746        let mut resp = Response::new(
747            StatusCode::OK,
748            Version::HTTP_11,
749            HeaderMap::new(),
750            Url::parse("http://example.com").unwrap(),
751            ResponseBody::buffered(Bytes::from("hello")),
752        );
753        assert!(resp.is_success());
754        let text = resp.text().await.unwrap();
755        assert_eq!(text, "hello");
756    }
757
758    #[tokio::test]
759    async fn response_streaming_bytes_collect() {
760        let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
761        let stream = Box::pin(futures_util::stream::iter(chunks));
762        let mut resp = Response::new(
763            StatusCode::OK,
764            Version::HTTP_11,
765            HeaderMap::new(),
766            Url::parse("http://example.com").unwrap(),
767            ResponseBody::streaming(stream),
768        );
769        let bytes = resp.bytes().await.unwrap();
770        assert_eq!(bytes, "hello world");
771    }
772
773    #[tokio::test]
774    async fn response_text_lines_rejects_unbounded_line() {
775        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(vec![
776            b'a'; MAX_LINE_LENGTH + 1
777        ]))]));
778        let mut response = Response::new(
779            StatusCode::OK,
780            Version::HTTP_11,
781            HeaderMap::new(),
782            Url::parse("http://example.com").unwrap(),
783            ResponseBody::streaming(stream),
784        );
785        let mut lines = response.text_lines().unwrap();
786        let error = lines.next().await.unwrap().unwrap_err();
787        assert!(matches!(error, crate::error::Error::Body(_)));
788    }
789
790    #[tokio::test]
791    async fn response_text_lines_rejects_overlong_line_after_valid_line() {
792        // A single chunk carrying a valid line followed by an over-long
793        // line must not smuggle the second line past MAX_LINE_LENGTH via
794        // the buffered fast path.
795        let mut payload = b"ok\n".to_vec();
796        payload.extend_from_slice(&vec![b'a'; MAX_LINE_LENGTH + 1]);
797        payload.push(b'\n');
798        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(payload))]));
799        let mut resp = Response::new(
800            StatusCode::OK,
801            Version::HTTP_11,
802            HeaderMap::new(),
803            Url::parse("http://example.com").unwrap(),
804            ResponseBody::streaming(stream),
805        );
806        let mut lines = resp.text_lines().unwrap();
807        assert_eq!(lines.next().await.unwrap().unwrap(), "ok");
808        let error = lines.next().await.unwrap().unwrap_err();
809        assert!(matches!(error, crate::error::Error::Body(_)));
810    }
811
812    #[tokio::test]
813    async fn response_text_lines_rejects_unbounded_terminal_line() {
814        // The terminal flush path (`Poll::Ready(None)`) must enforce
815        // `MAX_LINE_LENGTH` even when the over-long buffer never sees a
816        // trailing newline.
817        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from(vec![
818            b'a'; MAX_LINE_LENGTH + 1
819        ]))]));
820        let mut resp = Response::new(
821            StatusCode::OK,
822            Version::HTTP_11,
823            HeaderMap::new(),
824            Url::parse("http://example.com").unwrap(),
825            ResponseBody::streaming(stream),
826        );
827        let mut lines = resp.text_lines().unwrap();
828        let error = lines.next().await.unwrap().unwrap_err();
829        assert!(matches!(error, crate::error::Error::Body(_)));
830    }
831
832    #[tokio::test]
833    async fn response_bytes_stream_yields_chunks() {
834        let chunks = vec![Ok(Bytes::from("a")), Ok(Bytes::from("b"))];
835        let stream = Box::pin(futures_util::stream::iter(chunks));
836        let mut resp = Response::new(
837            StatusCode::OK,
838            Version::HTTP_11,
839            HeaderMap::new(),
840            Url::parse("http://example.com").unwrap(),
841            ResponseBody::streaming(stream),
842        );
843        let mut stream = resp.bytes_stream().unwrap();
844        assert_eq!(stream.next().await.unwrap().unwrap(), "a");
845        assert_eq!(stream.next().await.unwrap().unwrap(), "b");
846        assert!(stream.next().await.is_none());
847    }
848
849    #[tokio::test]
850    async fn response_text_lines_basic() {
851        let chunks = vec![Ok(Bytes::from("line1\nline2\nline3\n"))];
852        let stream = Box::pin(futures_util::stream::iter(chunks));
853        let mut resp = Response::new(
854            StatusCode::OK,
855            Version::HTTP_11,
856            HeaderMap::new(),
857            Url::parse("http://example.com").unwrap(),
858            ResponseBody::streaming(stream),
859        );
860        let mut lines = resp.text_lines().unwrap();
861        assert_eq!(lines.next().await.unwrap().unwrap(), "line1");
862        assert_eq!(lines.next().await.unwrap().unwrap(), "line2");
863        assert_eq!(lines.next().await.unwrap().unwrap(), "line3");
864        assert!(lines.next().await.is_none());
865    }
866
867    #[tokio::test]
868    async fn response_text_lines_crlf() {
869        let chunks = vec![Ok(Bytes::from("line1\r\nline2\r\n"))];
870        let stream = Box::pin(futures_util::stream::iter(chunks));
871        let mut resp = Response::new(
872            StatusCode::OK,
873            Version::HTTP_11,
874            HeaderMap::new(),
875            Url::parse("http://example.com").unwrap(),
876            ResponseBody::streaming(stream),
877        );
878        let mut lines = resp.text_lines().unwrap();
879        assert_eq!(lines.next().await.unwrap().unwrap(), "line1");
880        assert_eq!(lines.next().await.unwrap().unwrap(), "line2");
881        assert!(lines.next().await.is_none());
882    }
883
884    #[tokio::test]
885    async fn response_text_lines_across_chunks() {
886        let chunks = vec![
887            Ok(Bytes::from("hel")),
888            Ok(Bytes::from("lo\nwor")),
889            Ok(Bytes::from("ld\n")),
890        ];
891        let stream = Box::pin(futures_util::stream::iter(chunks));
892        let mut resp = Response::new(
893            StatusCode::OK,
894            Version::HTTP_11,
895            HeaderMap::new(),
896            Url::parse("http://example.com").unwrap(),
897            ResponseBody::streaming(stream),
898        );
899        let mut lines = resp.text_lines().unwrap();
900        assert_eq!(lines.next().await.unwrap().unwrap(), "hello");
901        assert_eq!(lines.next().await.unwrap().unwrap(), "world");
902        assert!(lines.next().await.is_none());
903    }
904
905    #[tokio::test]
906    async fn response_text_lines_no_trailing_newline() {
907        let chunks = vec![Ok(Bytes::from("hello"))];
908        let stream = Box::pin(futures_util::stream::iter(chunks));
909        let mut resp = Response::new(
910            StatusCode::OK,
911            Version::HTTP_11,
912            HeaderMap::new(),
913            Url::parse("http://example.com").unwrap(),
914            ResponseBody::streaming(stream),
915        );
916        let mut lines = resp.text_lines().unwrap();
917        assert_eq!(lines.next().await.unwrap().unwrap(), "hello");
918        assert!(lines.next().await.is_none());
919    }
920
921    #[test]
922    fn trailers_absent_initially() {
923        let resp = Response::new(
924            StatusCode::OK,
925            Version::HTTP_11,
926            HeaderMap::new(),
927            Url::parse("http://example.com").unwrap(),
928            ResponseBody::buffered(Bytes::new()),
929        );
930        assert!(resp.trailers().is_none());
931    }
932
933    #[test]
934    fn trailers_preserve_duplicate_multi_value() {
935        use crate::body::SharedTrailers;
936        let shared = SharedTrailers::new();
937        let mut map = HeaderMap::new();
938        map.append("x-trailer", HeaderValue::from_static("a"));
939        map.append("x-trailer", HeaderValue::from_static("b"));
940        map.insert("x-single", HeaderValue::from_static("v"));
941        shared.store(map);
942        let mut resp = Response::new(
943            StatusCode::OK,
944            Version::HTTP_11,
945            HeaderMap::new(),
946            Url::parse("http://example.com").unwrap(),
947            ResponseBody::buffered(Bytes::new()),
948        );
949        resp.set_trailers(shared);
950        let trailers = resp.trailers().expect("trailers present");
951        let values: Vec<_> = trailers
952            .get_all("x-trailer")
953            .iter()
954            .map(|v| v.to_str().unwrap().to_owned())
955            .collect();
956        assert_eq!(values, vec!["a", "b"]);
957        assert_eq!(trailers.get("x-single").unwrap(), "v");
958    }
959
960    #[tokio::test]
961    async fn trailers_available_after_bytes_collection() {
962        use crate::body::SharedTrailers;
963        // Simulate a transport that populates trailers when the body is
964        // driven to EOF (same contract as `wrap_incoming`).
965        let shared = SharedTrailers::new();
966        let shared_for_stream = shared.clone();
967        let stream = Box::pin(futures_util::stream::unfold(0, move |step| {
968            let shared_for_stream = shared_for_stream.clone();
969            async move {
970                if step == 0 {
971                    Some((Ok(Bytes::from("hello")), 1))
972                } else if step == 1 {
973                    let mut map = HeaderMap::new();
974                    map.insert("x-done", HeaderValue::from_static("yes"));
975                    shared_for_stream.store(map);
976                    None
977                } else {
978                    None
979                }
980            }
981        }));
982        let mut resp = Response::new(
983            StatusCode::OK,
984            Version::HTTP_11,
985            HeaderMap::new(),
986            Url::parse("http://example.com").unwrap(),
987            ResponseBody::streaming(stream),
988        );
989        resp.set_trailers(shared);
990        assert!(
991            resp.trailers().is_none(),
992            "partial consumption reports None"
993        );
994        let bytes = resp.bytes().await.unwrap();
995        assert_eq!(bytes, "hello");
996        assert_eq!(
997            resp.trailers().unwrap().get("x-done").unwrap(),
998            "yes",
999            "trailers available after body completion"
1000        );
1001    }
1002
1003    #[tokio::test]
1004    async fn partial_consumption_does_not_falsely_report_trailers() {
1005        let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("a"))]));
1006        let mut resp = Response::new(
1007            StatusCode::OK,
1008            Version::HTTP_11,
1009            HeaderMap::new(),
1010            Url::parse("http://example.com").unwrap(),
1011            ResponseBody::streaming(stream),
1012        );
1013        // No trailers wired; even after partial read there must be none.
1014        let mut s = resp.bytes_stream().unwrap();
1015        assert_eq!(s.next().await.unwrap().unwrap(), "a");
1016        assert!(resp.trailers().is_none());
1017    }
1018
1019    #[tokio::test]
1020    async fn body_error_before_trailers_leaves_no_trailers() {
1021        use crate::body::SharedTrailers;
1022        let shared = SharedTrailers::new();
1023        let stream = Box::pin(futures_util::stream::iter(vec![
1024            Ok(Bytes::from("a")),
1025            Err(crate::error::Error::Body("boom".into())),
1026        ]));
1027        let mut resp = Response::new(
1028            StatusCode::OK,
1029            Version::HTTP_11,
1030            HeaderMap::new(),
1031            Url::parse("http://example.com").unwrap(),
1032            ResponseBody::streaming(stream),
1033        );
1034        resp.set_trailers(shared);
1035        let err = resp.bytes().await.unwrap_err();
1036        assert!(matches!(err, crate::error::Error::Body(_)));
1037        assert!(
1038            resp.trailers().is_none(),
1039            "body errors must not fabricate trailer state"
1040        );
1041    }
1042}