Struct rustful::response::Response
[−]
[src]
pub struct Response<'a, 'b> { /* fields omitted */ }
An interface for sending data to the client.
This is where the status code and response headers are set, as well as the
response body. The body can be directly written through the Response
if
its size is known.
Methods
impl<'a, 'b> Response<'a, 'b>
[src]
impl<'a, 'b> Response<'a, 'b>
pub fn mock(global: &'b Global) -> Response<'static, 'b>
[src]
pub fn mock(global: &'b Global) -> Response<'static, 'b>
Create a non-functional Response
for testing purposes.
pub fn status(&self) -> StatusCode
[src]
pub fn status(&self) -> StatusCode
Get the current status code.
pub fn set_status(&mut self, status: StatusCode)
[src]
pub fn set_status(&mut self, status: StatusCode)
Change the status code. Ok (200)
is the default.
pub fn headers(&self) -> &Headers
[src]
pub fn headers(&self) -> &Headers
Get a reference to the headers.
pub fn headers_mut(&mut self) -> &mut Headers
[src]
pub fn headers_mut(&mut self) -> &mut Headers
Get a mutable reference to the headers.
pub fn filter_storage(&self) -> &AnyMap
[src]
pub fn filter_storage(&self) -> &AnyMap
Get a reference to the filter storage.
pub fn filter_storage_mut(&mut self) -> &mut AnyMap
[src]
pub fn filter_storage_mut(&mut self) -> &mut AnyMap
Get a mutable reference to the filter storage. It can be used to communicate with the response filters.
pub fn send<Content: SendResponse<'a, 'b>>(self, content: Content) where
Content::Error: ResponseError,
[src]
pub fn send<Content: SendResponse<'a, 'b>>(self, content: Content) where
Content::Error: ResponseError,
Send content to the client and finish the response, ignoring eventual
errors. Higher level content may manipulate the response before sending
it. Use send_data
to restrict the content to lower level data.
use rustful::{Context, Response}; use std::io; fn make_data() -> Result<String, io::Error> { let number = try!(read_number()); Ok(format!("got number {}", number)) } fn my_handler(context: Context, response: Response) { response.send(make_data()); }
pub fn try_send<Content: SendResponse<'a, 'b>>(
self,
content: Content
) -> Result<(), Content::Error>
[src]
pub fn try_send<Content: SendResponse<'a, 'b>>(
self,
content: Content
) -> Result<(), Content::Error>
Send content to the client and finish the response. Higher level
content may manipulate the response before sending it. Use
try_send_data
to restrict the content to lower level data.
#[macro_use] extern crate log; use rustful::{Context, Response}; use rustful::response::Error; use std::io; fn make_data() -> Result<String, io::Error> { let number = try!(read_number()); Ok(format!("got number {}", number)) } fn my_handler(context: Context, response: Response) { if let Err(Error::Filter(e)) = response.try_send(make_data()) { error!("a filter failed: {}", e); } }
pub fn send_data<'d, Content: Into<Data<'d>>>(self, content: Content)
[src]
pub fn send_data<'d, Content: Into<Data<'d>>>(self, content: Content)
Send simple data to the client and finish the response, ignoring
eventual errors. Use try_send_data
to get error information.
use rustful::{Context, Response}; fn my_handler(context: Context, response: Response) { response.send_data("hello"); }
pub fn try_send_data<'d, Content: Into<Data<'d>>>(
self,
content: Content
) -> Result<(), Error>
[src]
pub fn try_send_data<'d, Content: Into<Data<'d>>>(
self,
content: Content
) -> Result<(), Error>
Try to send simple data to the client and finish the response. This is
the same as send_data
, but errors are not ignored.
#[macro_use] extern crate log; use rustful::{Context, Response}; use rustful::response::Error; fn my_handler(context: Context, response: Response) { if let Err(Error::Filter(e)) = response.try_send_data("hello") { error!("a filter failed: {}", e); } }
pub fn send_file_with_mime<P, F>(
self,
path: P,
to_mime: F
) -> Result<(), FileError<'a, 'b>> where
P: AsRef<Path>,
F: FnOnce(&str) -> Option<Mime>,
[src]
pub fn send_file_with_mime<P, F>(
self,
path: P,
to_mime: F
) -> Result<(), FileError<'a, 'b>> where
P: AsRef<Path>,
F: FnOnce(&str) -> Option<Mime>,
Send a static file with a specified MIME type to the client.
This can be used instead of send_file
to control what MIME type the
file will be sent as. This can be useful if, for example, the MIME guesser
happens to be wrong about some file extension.
An error is returned upon failure and the response may be recovered from there if the file could not be opened.
#[macro_use] extern crate log; use std::path::Path; use rustful::{Context, Response}; use rustful::StatusCode; use rustful::file; fn my_handler(mut context: Context, mut response: Response) { if let Some(file) = context.variables.get("file") { let file_path = Path::new(file.as_ref()); //Check if the path is valid if file::check_path(file_path).is_ok() { //Make a full path from the filename let path = Path::new("path/to/files").join(file_path); //Send .rs files as Rust files and do the usual guessing for the rest let res = response.send_file_with_mime(&path, |ext| { if ext == "rs" { Some(content_type!(Text / "rust"; Charset = Utf8)) } else { file::ext_to_mime(ext) } }).or_else(|e| e.send_not_found("the file was not found")) .or_else(|e| e.ignore_send_error()); //Check if a more fatal file error than "not found" occurred if let Err((e, mut response)) = res { //Something went horribly wrong error!("failed to open '{}': {}", file, e); response.set_status(StatusCode::InternalServerError); } } else { //Accessing parent directories is forbidden response.set_status(StatusCode::Forbidden); } } else { //No filename was specified response.set_status(StatusCode::Forbidden); } }
ⓘImportant traits for Chunked<'a, 'b>pub fn into_chunked(self) -> Chunked<'a, 'b>
[src]
pub fn into_chunked(self) -> Chunked<'a, 'b>
Write the status code and headers to the client and turn the Response
into a Chunked
response.
ⓘImportant traits for Raw<'a>pub unsafe fn into_raw(self, content_length: u64) -> Raw<'a>
[src]
pub unsafe fn into_raw(self, content_length: u64) -> Raw<'a>
Write the status code and headers to the client and turn the Response
into a Raw
response. Any eventual response filters are bypassed to
make sure that the data is not modified.
Unsafety: The content length is set beforehand, which makes it possible to send responses that are too short.