Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub body: Bytes,
    /* private fields */
}
Expand description

HTTP Response representation

Fields§

§status: StatusCode§headers: HeaderMap§body: Bytes

Implementations§

Source§

impl Response

Source

pub fn new(status: StatusCode) -> Self

Create a new Response with the given status code

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::new(StatusCode::OK);
assert_eq!(response.status, StatusCode::OK);
assert!(response.body.is_empty());
Source

pub fn ok() -> Self

Create a Response with HTTP 200 OK status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::ok();
assert_eq!(response.status, StatusCode::OK);
Source

pub fn created() -> Self

Create a Response with HTTP 201 Created status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::created();
assert_eq!(response.status, StatusCode::CREATED);
Source

pub fn no_content() -> Self

Create a Response with HTTP 204 No Content status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::no_content();
assert_eq!(response.status, StatusCode::NO_CONTENT);
Source

pub fn bad_request() -> Self

Create a Response with HTTP 400 Bad Request status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::bad_request();
assert_eq!(response.status, StatusCode::BAD_REQUEST);
Source

pub fn unauthorized() -> Self

Create a Response with HTTP 401 Unauthorized status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::unauthorized();
assert_eq!(response.status, StatusCode::UNAUTHORIZED);
Source

pub fn forbidden() -> Self

Create a Response with HTTP 403 Forbidden status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::forbidden();
assert_eq!(response.status, StatusCode::FORBIDDEN);
Source

pub fn not_found() -> Self

Create a Response with HTTP 404 Not Found status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::not_found();
assert_eq!(response.status, StatusCode::NOT_FOUND);
Source

pub fn internal_server_error() -> Self

Create a Response with HTTP 500 Internal Server Error status

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::internal_server_error();
assert_eq!(response.status, StatusCode::INTERNAL_SERVER_ERROR);
Source

pub fn gone() -> Self

Create a Response with HTTP 410 Gone status

Used when a resource has been permanently removed.

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::gone();
assert_eq!(response.status, StatusCode::GONE);
Source

pub fn permanent_redirect(location: impl AsRef<str>) -> Self

Create a Response with HTTP 301 Moved Permanently (permanent redirect)

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::permanent_redirect("/new-location");
assert_eq!(response.status, StatusCode::MOVED_PERMANENTLY);
assert_eq!(
    response.headers.get("location").unwrap().to_str().unwrap(),
    "/new-location"
);
Source

pub fn temporary_redirect(location: impl AsRef<str>) -> Self

Create a Response with HTTP 302 Found (temporary redirect)

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::temporary_redirect("/temp-location");
assert_eq!(response.status, StatusCode::FOUND);
assert_eq!(
    response.headers.get("location").unwrap().to_str().unwrap(),
    "/temp-location"
);
Source

pub fn temporary_redirect_preserve_method(location: impl AsRef<str>) -> Self

Create a Response with HTTP 307 Temporary Redirect (preserves HTTP method)

Unlike 302, this guarantees the request method is preserved during redirect.

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::temporary_redirect_preserve_method("/temp-location");
assert_eq!(response.status, StatusCode::TEMPORARY_REDIRECT);
assert_eq!(
    response.headers.get("location").unwrap().to_str().unwrap(),
    "/temp-location"
);
Source

pub fn with_body(self, body: impl Into<Bytes>) -> Self

Set the response body

§Examples
use reinhardt_http::Response;
use bytes::Bytes;

let response = Response::ok().with_body("Hello, World!");
assert_eq!(response.body, Bytes::from("Hello, World!"));
Source

pub fn try_with_header(self, name: &str, value: &str) -> Result<Self>

Try to add a custom header to the response, returning an error on invalid inputs.

§Errors

Returns Err if the header name or value is invalid according to HTTP specifications.

§Examples
use reinhardt_http::Response;

let response = Response::ok().try_with_header("X-Custom-Header", "custom-value").unwrap();
assert_eq!(
    response.headers.get("X-Custom-Header").unwrap().to_str().unwrap(),
    "custom-value"
);
use reinhardt_http::Response;

// Invalid header names return an error instead of panicking
let result = Response::ok().try_with_header("Invalid Header", "value");
assert!(result.is_err());
Source

pub fn with_header(self, name: &str, value: &str) -> Self

Add a custom header to the response.

Invalid header names or values are silently ignored. Use try_with_header if you need error reporting.

§Examples
use reinhardt_http::Response;

let response = Response::ok().with_header("X-Custom-Header", "custom-value");
assert_eq!(
    response.headers.get("X-Custom-Header").unwrap().to_str().unwrap(),
    "custom-value"
);
use reinhardt_http::Response;

// Invalid header names are silently ignored (no panic)
let response = Response::ok().with_header("Invalid Header", "value");
assert!(response.headers.is_empty());
Source

pub fn with_location(self, location: &str) -> Self

Add a Location header to the response (typically used for redirects)

§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

let response = Response::new(StatusCode::FOUND).with_location("/redirect-target");
assert_eq!(
    response.headers.get("location").unwrap().to_str().unwrap(),
    "/redirect-target"
);
Source

pub fn with_json<T: Serialize>(self, data: &T) -> Result<Self>

Set the response body to JSON and add appropriate Content-Type header

§Examples
use reinhardt_http::Response;
use serde_json::json;

let data = json!({"message": "Hello, World!"});
let response = Response::ok().with_json(&data).unwrap();

assert_eq!(
    response.headers.get("content-type").unwrap().to_str().unwrap(),
    "application/json"
);
Source

pub fn with_typed_header(self, key: HeaderName, value: HeaderValue) -> Self

Add a custom header using typed HeaderName and HeaderValue

§Examples
use reinhardt_http::Response;
use hyper::header::{HeaderName, HeaderValue};

let header_name = HeaderName::from_static("x-custom-header");
let header_value = HeaderValue::from_static("custom-value");
let response = Response::ok().with_typed_header(header_name, header_value);

assert_eq!(
    response.headers.get("x-custom-header").unwrap().to_str().unwrap(),
    "custom-value"
);
Source

pub fn should_stop_chain(&self) -> bool

Check if this response should stop the middleware chain

When true, no further middleware or handlers will be executed.

§Examples
use reinhardt_http::Response;

let response = Response::ok();
assert!(!response.should_stop_chain());

let stopping_response = Response::ok().with_stop_chain(true);
assert!(stopping_response.should_stop_chain());
Source

pub fn with_stop_chain(self, stop: bool) -> Self

Set whether this response should stop the middleware chain

When set to true, the middleware chain will stop processing and return this response immediately, skipping any remaining middleware and handlers.

This is useful for early returns in middleware, such as:

  • Authentication failures (401 Unauthorized)
  • CORS preflight responses (204 No Content)
  • Rate limiting rejections (429 Too Many Requests)
  • Cache hits (304 Not Modified)
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;

// Early return for authentication failure
let auth_failure = Response::unauthorized()
    .with_body("Authentication required")
    .with_stop_chain(true);
assert!(auth_failure.should_stop_chain());

// CORS preflight response
let preflight = Response::no_content()
    .with_header("Access-Control-Allow-Origin", "*")
    .with_stop_chain(true);
assert!(preflight.should_stop_chain());

Trait Implementations§

Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Error> for Response

Source§

fn from(error: Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more