Struct rouille::Response[][src]

pub struct Response {
    pub status_code: u16,
    pub headers: Vec<(Cow<'static, str>, Cow<'static, str>)>,
    pub data: ResponseBody,
    pub upgrade: Option<Box<dyn Upgrade + Send>>,
}
Expand description

Contains a prototype of a response.

The response is only sent to the client when you return the Response object from your request handler. This means that you are free to create as many Response objects as you want.

Fields

status_code: u16

The status code to return to the user.

headers: Vec<(Cow<'static, str>, Cow<'static, str>)>

List of headers to be returned in the response.

The value of the following headers will be ignored from this list, even if present:

  • Accept-Ranges
  • Connection
  • Content-Length
  • Content-Range
  • Trailer
  • Transfer-Encoding

Additionnaly, the Upgrade header is ignored as well unless the upgrade field of the Response is set to something.

The reason for this is that these headers are too low-level and are directly handled by the underlying HTTP response system.

The value of Content-Length is automatically determined by the ResponseBody object of the data member.

If you want to send back Connection: upgrade, you should set the value of the upgrade field to something.

data: ResponseBody

An opaque type that contains the body of the response.

upgrade: Option<Box<dyn Upgrade + Send>>

If set, rouille will give ownership of the client socket to the Upgrade object.

In all circumstances, the value of the Connection header is managed by the framework and cannot be customized. If this value is set, the response will automatically contain Connection: Upgrade.

Implementations

Returns true if the status code of this Response indicates success.

This is the range [200-399].

Example

use rouille::Response;
let response = Response::text("hello world");
assert!(response.is_success());

Shortcut for !response.is_success().

Example

use rouille::Response;
let response = Response::empty_400();
assert!(response.is_error());

Builds a Response that redirects the user to another URL with a 301 status code. This semantically means a permanent redirect.

Note: If you’re uncertain about which status code to use for a redirection, 303 is the safest choice.

Example

use rouille::Response;
let response = Response::redirect_301("/foo");

Builds a Response that redirects the user to another URL with a 302 status code. This semantically means a temporary redirect.

Note: If you’re uncertain about which status code to use for a redirection, 303 is the safest choice.

Example

use rouille::Response;
let response = Response::redirect_302("/bar");

Builds a Response that redirects the user to another URL with a 303 status code. This means “See Other” and is usually used to indicate where the response of a query is located.

For example when a user sends a POST request to URL /foo the server can return a 303 response with a target to /bar, in which case the browser will automatically change the page to /bar (with a GET request to /bar).

Note: If you’re uncertain about which status code to use for a redirection, 303 is the safest choice.

Example

use rouille::Response;
let user_id = 5;
let response = Response::redirect_303(format!("/users/{}", user_id));

Builds a Response that redirects the user to another URL with a 307 status code. This semantically means a permanent redirect.

The difference between 307 and 301 is that the client must keep the same method after the redirection. For example if the browser sends a POST request to /foo and that route returns a 307 redirection to /bar, then the browser will make a POST request to /bar. With a 301 redirection it would use a GET request instead.

Note: If you’re uncertain about which status code to use for a redirection, 303 is the safest choice.

Example

use rouille::Response;
let response = Response::redirect_307("/foo");

Builds a Response that redirects the user to another URL with a 302 status code. This semantically means a temporary redirect.

The difference between 308 and 302 is that the client must keep the same method after the redirection. For example if the browser sends a POST request to /foo and that route returns a 308 redirection to /bar, then the browser will make a POST request to /bar. With a 302 redirection it would use a GET request instead.

Note: If you’re uncertain about which status code to use for a redirection, 303 is the safest choice.

Example

use rouille::Response;
let response = Response::redirect_302("/bar");

Builds a 200 Response with data.

Example

use rouille::Response;
let response = Response::from_data("application/octet-stream", vec![1, 2, 3, 4]);

Builds a 200 Response with the content of a file.

Example

use std::fs::File;
use rouille::Response;

let file = File::open("image.png").unwrap();
let response = Response::from_file("image/png", file);

Builds a Response that outputs HTML.

Example

use rouille::Response;
let response = Response::html("<p>hello <strong>world</strong></p>");

Builds a Response that outputs SVG.

Example

use rouille::Response;
let response = Response::svg("<svg xmlns='http://www.w3.org/2000/svg'/>");

Builds a Response that outputs plain text.

Example

use rouille::Response;
let response = Response::text("hello world");

Builds a Response that outputs JSON.

Example

extern crate serde;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate rouille;
use rouille::Response;

#[derive(Serialize)]
struct MyStruct {
    field1: String,
    field2: i32,
}

let response = Response::json(&MyStruct { field1: "hello".to_owned(), field2: 5 });
// The Response will contain something like `{ field1: "hello", field2: 5 }`

Builds a Response that returns a 401 Not Authorized status and a WWW-Authenticate header.

Example

use rouille::Response;
let response = Response::basic_http_auth_login_required("realm");

Builds an empty Response with a 204 status code.

Example

use rouille::Response;
let response = Response::empty_204();

Builds an empty Response with a 400 status code.

Example

use rouille::Response;
let response = Response::empty_400();

Builds an empty Response with a 404 status code.

Example

use rouille::Response;
let response = Response::empty_404();

Builds an empty Response with a 406 status code.

Example

use rouille::Response;
let response = Response::empty_406();

Changes the status code of the response.

Example

use rouille::Response;
let response = Response::text("hello world").with_status_code(500);

Removes all headers from the response that match header.

Adds an additional header to the response.

Removes all headers from the response whose names are header, and replaces them .

Adds or replaces a ETag header to the response, and turns the response into an empty 304 response if the ETag matches a If-None-Match header of the request.

An ETag is a unique representation of the content of a resource. If the content of the resource changes, the ETag should change as well. The purpose of using ETags is that a client can later ask the server to send the body of a response only if it still matches a certain ETag the client has stored in memory.

Note: You should always try to specify an ETag for responses that have a large body.

Example

use rouille::Request;
use rouille::Response;

fn handle(request: &Request) -> Response {
    Response::text("hello world").with_etag(request, "my-etag-1234")
}

Turns the response into an empty 304 response if the ETag that is stored in it matches a If-None-Match header of the request.

Adds a ETag header to the response, or replaces an existing header if there is one.

Note: Contrary to with_etag, this function doesn’t try to turn the response into a 304 response. If you’re unsure of what to do, prefer with_etag.

Adds or replace a Content-Disposition header of the response. Tells the browser that the body of the request should fire a download popup instead of being shown in the browser.

Example

use rouille::Request;
use rouille::Response;

fn handle(request: &Request) -> Response {
    Response::text("hello world").with_content_disposition_attachment("book.txt")
}

When the response is sent back to the browser, it will show a popup asking the user to download the file “book.txt” whose content will be “hello world”.

Adds or replaces a Cache-Control header that specifies that the resource is public and can be cached for the given number of seconds.

Note: This function doesn’t do any caching itself. It just indicates that clients that receive this response are allowed to cache it.

Adds or replaces a Cache-Control header that specifies that the resource is private and can be cached for the given number of seconds.

Only the browser or the final client is authorized to cache the resource. Intermediate proxies must not cache it.

Note: This function doesn’t do any caching itself. It just indicates that clients that receive this response are allowed to cache it.

Adds or replaces a Cache-Control header that specifies that the client must not cache the resource.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.