pub struct Response {
pub http_version: String,
pub status_code: i16,
pub reason_phrase: String,
pub headers: Vec<Header>,
pub content_range_list: Vec<ContentRange>,
pub stream_file: Option<String>,
pub stream_pipe: Option<Box<dyn Read + Send>>,
pub trailers: Option<Trailers>,
}Expand description
An HTTP response. Build one inside [Controller::process] and return it.
Set status_code and reason_phrase from STATUS_CODE_REASON_PHRASE,
push body content via Range::get_content_range into content_range_list,
and add any extra headers to headers.
For large files, set stream_file to the absolute file path instead of loading
the body into content_range_list. The server will stream it with
Transfer-Encoding: chunked so memory usage stays constant regardless of file size.
For proxy passthrough (SSE, AI token streams, large downloads), set stream_pipe
to a boxed Read source instead. The server streams its output with
Transfer-Encoding: chunked immediately after sending the response headers.
Fields§
§http_version: StringHTTP version string, e.g. "HTTP/1.1".
status_code: i16Numeric status code, e.g. 200. Set from STATUS_CODE_REASON_PHRASE.
reason_phrase: StringReason phrase, e.g. "OK". Set from STATUS_CODE_REASON_PHRASE.
headers: Vec<Header>Response headers. Pre-populated with standard headers by Header::get_header_list.
content_range_list: Vec<ContentRange>Response body as a list of content ranges (supports multipart and range responses).
stream_file: Option<String>If set, the server streams this file with Transfer-Encoding: chunked
instead of using content_range_list. Use for files larger than ~8 MB.
stream_pipe: Option<Box<dyn Read + Send>>If set, the server pipes bytes from this reader with Transfer-Encoding: chunked
immediately after sending the response headers. Takes precedence over
content_range_list and stream_file. The reader is consumed and not cloned.
trailers: Option<Trailers>Header fields sent after the body (HTTP/2 and HTTP/3; HTTP/1.1 drops
them) — gRPC’s grpc-status and grpc-message. A streamed body’s
producer fills the slot before it ends the stream. Since 18.3.0.
Implementations§
Source§impl Response
impl Response
pub const _ERROR_UNABLE_TO_PARSE_HTTP_VERSION_STATUS_CODE: &'static str = "Unable to parse status code"
pub const _HTTP_VERSION_AND_STATUS_CODE_AND_REASON_PHRASE_REGEX: &'static str = "(?P<http_version>\\w+/\\w+.\\w)\\s(?P<status_code>\\w+)\\s(?P<reason_phrase>.+)"
Sourcepub fn with_status(status: &StatusCodeReasonPhrase) -> Response
pub fn with_status(status: &StatusCodeReasonPhrase) -> Response
A response with status, no headers and no body; set fields or push
headers and body parts after.
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
let mut response = Response::with_status(STATUS_CODE_REASON_PHRASE.n204_no_content);
response.headers.push(rust_web_server::header::Header::new("X-Done", "yes"));
assert_eq!(response.status_code, 204);pub fn build( status: StatusCodeReasonPhrase, header_list: Vec<Header>, body: Vec<ContentRange>, ) -> Response
pub fn status_code_reason_phrase_list() -> Vec<&'static StatusCodeReasonPhrase>
pub fn _get_header(&self, name: String) -> Option<&Header>
Sourcepub fn status_has_no_content(status_code: i16) -> bool
pub fn status_has_no_content(status_code: i16) -> bool
true for a status that never has content: 1xx, 204, 304
(RFC 9110 §6.4.1, §15.3.5, §15.4.5).
Sourcepub fn drop_content_for_status(&mut self)
pub fn drop_content_for_status(&mut self)
For a status without content (Response::status_has_no_content),
drop the body — buffered, file or pipe — and any Content-Length or
Transfer-Encoding, which those responses must not carry (#54).
Every connection writer calls it before framing the response.
pub fn generate_body(content_range_list: Vec<ContentRange>) -> Vec<u8> ⓘ
Sourcepub fn generate_response(response: Response, request: Request) -> Vec<u8> ⓘ
pub fn generate_response(response: Response, request: Request) -> Vec<u8> ⓘ
The serialized response: status line + headers, then the body.
Sourcepub fn generate_response_parts(
response: Response,
request: Request,
) -> (Vec<u8>, Vec<u8>)
pub fn generate_response_parts( response: Response, request: Request, ) -> (Vec<u8>, Vec<u8>)
Like generate_response, but returns the
head (status line + headers + blank line) and the body separately, so
a connection writer can send them back to back without first copying
a large body into one combined buffer. The body is empty for HEAD and
for a status without content.
pub fn _parse_response(response_vec_u8: &[u8]) -> Response
pub fn _parse_http_version_status_code_reason_phrase_string( http_version_status_code_reason_phrase: &str, ) -> Result<(String, i16, String), String>
pub fn _parse_http_response_header_string(header_string: &str) -> Header
pub fn _parse_raw_response_via_cursor( cursor: &mut Cursor<&[u8]>, iteration_number: usize, response: &mut Response, content_length: usize, )
pub fn _is_multipart_byteranges_content_type(content_type: &Header) -> bool
pub fn get_response( status_code_reason_phrase: &StatusCodeReasonPhrase, boxed_header_list: Option<Vec<Header>>, boxed_content_range_list: Option<Vec<ContentRange>>, ) -> Response
Sourcepub fn json(
status_code_reason_phrase: &StatusCodeReasonPhrase,
body: Vec<u8>,
) -> Response
pub fn json( status_code_reason_phrase: &StatusCodeReasonPhrase, body: Vec<u8>, ) -> Response
Build a Content-Type: application/json response from already-serialized bytes.
Takes raw bytes rather than a Serialize value so it carries no dependency on the
serde feature — pair it with serde_json::to_vec (or hand-rolled JSON) to build body.
For a one-line serialize-and-respond helper on a typed value, see [crate::json::Json]
(serde feature).
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
let response = Response::json(STATUS_CODE_REASON_PHRASE.n200_ok, br#"{"ok":true}"#.to_vec());Sourcepub fn text(
status_code_reason_phrase: &StatusCodeReasonPhrase,
body: &str,
) -> Response
pub fn text( status_code_reason_phrase: &StatusCodeReasonPhrase, body: &str, ) -> Response
Build a Content-Type: text/plain response from a body string.
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
let response = Response::text(STATUS_CODE_REASON_PHRASE.n400_bad_request, "invalid input");pub fn generate(&mut self) -> Vec<u8> ⓘ
pub fn get_header(&self, name: String) -> Option<&Header>
Sourcepub fn header(&self, name: &str) -> Option<&Header>
pub fn header(&self, name: &str) -> Option<&Header>
The first header named name (case-insensitive), without allocating.
pub fn parse(response_vec_u8: &[u8]) -> Result<Response, String>
pub fn parse_raw_response_via_cursor( cursor: &mut Cursor<&[u8]>, iteration_number: usize, response: &mut Response, _content_length: usize, total_bytes: i32, bytes_read: i32, ) -> Result<(), String>
pub fn parse_http_response_header_string( header_string: &str, ) -> Result<Header, String>
Trait Implementations§
impl Eq for Response
Source§impl IntoResponse for Response
impl IntoResponse for Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl !RefUnwindSafe for Response
impl !Sync for Response
impl !UnwindSafe for Response
impl Freeze for Response
impl Send for Response
impl Unpin for Response
impl UnsafeUnpin 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§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.