pub struct Response { /* private fields */ }Expand description
Parsed HTTP response.
§Examples
use barehttp::Response;
let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok";
let response = Response::parse(raw).map_err(barehttp::Error::from)?;
assert_eq!(response.status_code(), 200);
assert_eq!(response.header("content-length"), Some("2"));
assert_eq!(response.to_text()?, "ok");Implementations§
Source§impl Response
impl Response
Sourcepub const fn status_code(&self) -> u16
pub const fn status_code(&self) -> u16
Status code (e.g. 200).
Self::status is a deprecated alias.
Sourcepub fn body(&self) -> &[u8]
pub fn body(&self) -> &[u8]
Response body bytes.
Self::as_bytes is a deprecated alias.
Sourcepub const fn trailers(&self) -> &Headers
pub const fn trailers(&self) -> &Headers
Trailer fields from chunked responses (RFC 9112 §7.1.2).
Same type as Self::headers: case-insensitive lookup, ordered iteration.
Sourcepub fn parse(input: &[u8]) -> Result<Self, ParseError>
pub fn parse(input: &[u8]) -> Result<Self, ParseError>
Parse a complete buffered HTTP/1.1 response.
One-pass header materialize + slice body decode (Callgrind-sensitive). The
live receive path keeps owned-buffer adoption via parse_body_from_owned
/ wire spans on the connection buffer.
§Errors
ParseError when the status line, headers, framing, or body are illegal.
Sourcepub const fn status(&self) -> u16
👎Deprecated: use status_code
pub const fn status(&self) -> u16
use status_code
Deprecated alias of Self::status_code.
Sourcepub const fn is_success(&self) -> bool
pub const fn is_success(&self) -> bool
Status is 2xx.
Sourcepub const fn is_redirect(&self) -> bool
pub const fn is_redirect(&self) -> bool
Status is 3xx.
Sourcepub const fn is_client_error(&self) -> bool
pub const fn is_client_error(&self) -> bool
Status is 4xx.
Sourcepub const fn is_server_error(&self) -> bool
pub const fn is_server_error(&self) -> bool
Status is 5xx.
Sourcepub fn as_bytes(&self) -> &[u8]
👎Deprecated: use body
pub fn as_bytes(&self) -> &[u8]
use body
Deprecated alias of Self::body.
Sourcepub fn to_text(&self) -> Result<&str, Utf8Error>
pub fn to_text(&self) -> Result<&str, Utf8Error>
Body as UTF-8 text (borrowed).
Leaves self intact on failure, so status and headers stay available.
Error type is core::str::Utf8Error; it converts via From / ? into
crate::Error::Utf8Error.
§Errors
core::str::Utf8Error if the body is not valid UTF-8.
Sourcepub fn into_string(self) -> Result<String, IntoStringError>
pub fn into_string(self) -> Result<String, IntoStringError>
Consume the response; return the body as a UTF-8 String.
Non-UTF-8 body: IntoStringError holds the original Response
(status, headers, body). Converting that error to crate::Error via
From drops the response; recover with IntoStringError::into_response
or IntoStringError::response first.
Unique body buffer: success builds the String with no copy.
§Errors
IntoStringError if the body is not valid UTF-8.
§Examples
use barehttp::Response;
let bad = Response::parse(b"HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\n\xff")
.map_err(barehttp::Error::from)?;
if let Err(err) = bad.into_string() {
assert_eq!(err.response().status_code(), 200);
assert_eq!(err.into_response().body(), &[0xff]);
}Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consume the response; return body bytes as a Vec<u8>.
For a borrowed view, use Self::body. Reclaims the allocation without a
copy when the internal buffer is uniquely owned.
Trait Implementations§
impl Eq for Response
impl StructuralPartialEq for Response
Auto Trait Implementations§
impl !Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.