Struct rustful::context::body::BodyReader [] [src]

pub struct BodyReader<'a, 'b: 'a> { /* fields omitted */ }

A reader for a request body.

Methods

impl<'a, 'b> BodyReader<'a, 'b>
[src]

Important traits for BodyReader<'a, 'b>

Create a non-functional body reader for testing purposes.

impl<'a, 'b> BodyReader<'a, 'b>
[src]

Try to create a multipart/form-data reader from the request body.

use std::fmt::Write;
use std::io::Read;
use rustful::{Context, Response};
use rustful::StatusCode::BadRequest;
use multipart::server::MultipartData;

fn my_handler(mut context: Context, mut response: Response) {
    if let Some(mut multipart) = context.body.as_multipart() {
        let mut result = String::new();

        //Iterate over the multipart entries and print info about them in `result`
        multipart.foreach_entry(|mut entry| {
            if let Some(content_mime) = entry.headers.content_type {
                if content_mime.type_() == mime::TEXT {
                    //Found data from a text field
                    let mut text = String::new();
                    entry.data.read_to_string(&mut text);
                    writeln!(&mut result, "{}: '{}'", entry.headers.name, text);
                }
                else {
                    //Found an uploaded file
                    if let Some(file_name) = entry.headers.filename {
                        writeln!(&mut result, "{}: a file called '{}'", entry.headers.name, file_name);
                    } else {
                        writeln!(&mut result, "{}: a nameless file", entry.headers.name);
                    }
                }
             }
             else {
                 //Content-type not supplied, default to text/plain as per IETF RFC 7578, section 4.4
                 let mut text = String::new();
                 entry.data.read_to_string(&mut text);
                 writeln!(&mut result, "{}: '{}'", entry.headers.name, text);
             }
        });

        response.send(result);
    } else {
        //We expected it to be a valid `multipart/form-data` request, but it was not
        response.set_status(BadRequest);
    }
}

Read and parse the request body as a query string. The body will be decoded as UTF-8 and plain '+' characters will be replaced with spaces.

A simplified example of how to parse a=number&b=number:

use rustful::{Context, Response};

fn my_handler(mut context: Context, response: Response) {
    //Parse the request body as a query string
    let query = context.body.read_query_body().unwrap();

    //Find "a" and "b" and assume that they are numbers
    let a: f64 = query.get("a").and_then(|number| number.parse().ok()).unwrap();
    let b: f64 = query.get("b").and_then(|number| number.parse().ok()).unwrap();

    response.send(format!("{} + {} = {}", a, b, a + b));
}

Trait Implementations

impl<'a, 'b> Read for BodyReader<'a, 'b>
[src]

Read the request body.

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, appending them to buf. Read more

Read the exact number of bytes required to fill buf. Read more

Important traits for &'a mut R

Creates a "by reference" adaptor for this instance of Read. Read more

Important traits for Bytes<R>

Transforms this Read instance to an [Iterator] over its bytes. Read more

Important traits for Chars<R>

🔬 This is a nightly-only experimental API. (io)

the semantics of a partial read/write of where errors happen is currently unclear and may change

Transforms this Read instance to an [Iterator] over [char]s. Read more

Important traits for Chain<T, U>

Creates an adaptor which will chain this stream with another. Read more

Important traits for Take<T>

Creates an adaptor which will read at most limit bytes from it. Read more

Auto Trait Implementations

impl<'a, 'b> Send for BodyReader<'a, 'b>

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