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]

Create a non-functional Response for testing purposes.

Get the current status code.

Change the status code. Ok (200) is the default.

Get a reference to the headers.

Get a mutable reference to the headers.

Get a reference to the filter storage.

Get a mutable reference to the filter storage. It can be used to communicate with the response filters.

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());
}

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);
    }
}

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");
}

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);
    }
}

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>

Write the status code and headers to the client and turn the Response into a Chunked response.

Important traits for 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.

Trait Implementations

impl<'a, 'b> Drop for Response<'a, 'b>
[src]

Writes status code and headers and closes the connection.

Auto Trait Implementations

impl<'a, 'b> !Send for Response<'a, 'b>

impl<'a, 'b> !Sync for Response<'a, 'b>