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