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