Struct nickel::Response
[−]
[src]
pub struct Response<'a, D: 'a = (), T: 'static + Any = Fresh> { /* fields omitted */ }
A container for the response
Methods
impl<'a, D> Response<'a, D, Fresh>
[src]
fn from_internal<'c, 'd>(
response: HyperResponse<'c, Fresh>,
templates: &'c RwLock<HashMap<String, Template>>,
data: &'c D
) -> Response<'c, D, Fresh>
response: HyperResponse<'c, Fresh>,
templates: &'c RwLock<HashMap<String, Template>>,
data: &'c D
) -> Response<'c, D, Fresh>
fn status_mut(&mut self) -> &mut StatusCode
Get a mutable reference to the status.
fn headers_mut(&mut self) -> &mut Headers
Get a mutable reference to the Headers.
fn set<T: Modifier<Response<'a, D>>>(
&mut self,
attribute: T
) -> &mut Response<'a, D>
&mut self,
attribute: T
) -> &mut Response<'a, D>
Modify the response with the provided data.
Examples
extern crate hyper; #[macro_use] extern crate nickel; use nickel::{Nickel, HttpRouter, MediaType}; use nickel::status::StatusCode; use hyper::header::Location; fn main() { let mut server = Nickel::new(); server.get("/a", middleware! { |_, mut res| // set the Status res.set(StatusCode::PermanentRedirect) // update a Header value .set(Location("http://nickel.rs".into())); "" }); server.get("/b", middleware! { |_, mut res| // setting the content type res.set(MediaType::Json); "{'foo': 'bar'}" }); // ... }
fn send<T: Responder<D>>(self, data: T) -> MiddlewareResult<'a, D>
Writes a response
Examples
use nickel::{Request, Response, MiddlewareResult}; fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> { res.send("hello world") }
fn send_file<P: AsRef<Path>>(self, path: P) -> MiddlewareResult<'a, D>
Writes a file to the output.
Examples
use nickel::{Request, Response, MiddlewareResult}; use std::path::Path; fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> { let favicon = Path::new("/assets/favicon.ico"); res.send_file(favicon) }
fn error<T>(self, status: StatusCode, message: T) -> MiddlewareResult<'a, D> where
T: Into<Cow<'static, str>>,
T: Into<Cow<'static, str>>,
Return an error with the appropriate status code for error handlers to provide output for.
fn set_header_fallback<F, H>(&mut self, f: F) where
H: Header + HeaderFormat,
F: FnOnce() -> H,
H: Header + HeaderFormat,
F: FnOnce() -> H,
Sets the header if not already set.
If the header is not set then f
will be called.
Renders the given template bound with the given data.
Examples
#[macro_use] extern crate nickel; extern crate hyper; use nickel::{Nickel, HttpRouter, MediaType}; use hyper::header::ContentType; fn main() { let mut server = Nickel::new(); server.get("/", middleware! { |_, mut res| res.set(MediaType::Html); res.set_header_fallback(|| { panic!("Should not get called"); ContentType(MediaType::Txt.into()) }); "<h1>Hello World</h1>" }); // ... }
fn render<T, P>(self, path: P, data: &T) -> MiddlewareResult<'a, D> where
T: Encodable,
P: AsRef<str> + Into<String>,
T: Encodable,
P: AsRef<str> + Into<String>,
Renders the given template bound with the given data.
Examples
use std::collections::HashMap; use nickel::{Request, Response, MiddlewareResult}; fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> { let mut data = HashMap::new(); data.insert("name", "user"); res.render("examples/assets/template.tpl", &data) }
fn start(self) -> Result<Response<'a, D, Streaming>, NickelError<'a, D>>
fn server_data(&self) -> &'a D
fn on_send<F>(&mut self, f: F) where
F: FnMut(&mut Response<'a, D, Fresh>) + 'static,
F: FnMut(&mut Response<'a, D, Fresh>) + 'static,
fn next_middleware(self) -> MiddlewareResult<'a, D>
Pass execution off to another Middleware
When returned from a Middleware, it allows computation to continue in any Middleware queued after the active one.
impl<'a, 'b, D> Response<'a, D, Streaming>
[src]
fn bail<T>(self, message: T) -> MiddlewareResult<'a, D> where
T: Into<Cow<'static, str>>,
T: Into<Cow<'static, str>>,
In the case of an unrecoverable error while a stream is already in
progress, there is no standard way to signal to the client that an
error has occurred. bail
will drop the connection and log an error
message.
fn end(self) -> Result<()>
Flushes all writing of a response to the client.
impl<'a, D, T: 'static + Any> Response<'a, D, T>
[src]
fn status(&self) -> StatusCode
The status of this response.
fn headers(&self) -> &Headers
The headers of this response.
fn data(&self) -> &'a D
Trait Implementations
impl<'a, 'b, D> Write for Response<'a, D, Streaming>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Write
. Read more
impl<'a, D, T: 'static + Any> Extensible for Response<'a, D, T>
[src]
fn extensions(&self) -> &TypeMap
Get a reference to the type's extension storage.
fn extensions_mut(&mut self) -> &mut TypeMap
Get a mutable reference to the type's extension storage.
impl<'a, D, T: 'static + Any> Pluggable for Response<'a, D, T>
[src]
fn get<P>(&mut self) -> Result<<P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Clone,
<P as Key>::Value: Any,
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Clone,
<P as Key>::Value: Any,
Return a copy of the plugin's produced value. Read more
fn get_ref<P>(
&mut self
) -> Result<&<P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Any,
&mut self
) -> Result<&<P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Any,
Return a reference to the plugin's produced value. Read more
fn get_mut<P>(
&mut self
) -> Result<&mut <P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Any,
&mut self
) -> Result<&mut <P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Self: Extensible,
<P as Key>::Value: Any,
Return a mutable reference to the plugin's produced value. Read more
fn compute<P>(
&mut self
) -> Result<<P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
&mut self
) -> Result<<P as Key>::Value, <P as Plugin<Self>>::Error> where
P: Plugin<Self>,
Create and evaluate a once-off instance of a plugin.
impl<'a, D> Redirect for Response<'a, D>
[src]
type Result = MiddlewareResult<'a, D>
fn redirect_with<T>(self, target: T, status: StatusCode) -> Self::Result where
T: Into<String>,
T: Into<String>,
fn redirect<T>(self, target: T) -> Self::Result where
T: Into<String>,
T: Into<String>,
Redirect the response to a given target Read more
fn redirect_permanently<T>(self, target: T) -> Self::Result where
T: Into<String>,
T: Into<String>,