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]
impl<'a, 'b> BodyReader<'a, 'b>
ⓘImportant traits for BodyReader<'a, 'b>pub fn mock(headers: &'b Headers) -> BodyReader<'static, 'static>
[src]
pub fn mock(headers: &'b Headers) -> BodyReader<'static, 'static>
Create a non-functional body reader for testing purposes.
impl<'a, 'b> BodyReader<'a, 'b>
[src]
impl<'a, 'b> BodyReader<'a, 'b>
pub fn as_multipart<'r>(
&'r mut self
) -> Option<Multipart<MultipartRequest<'r, 'a, 'b>>>
[src]
pub fn as_multipart<'r>(
&'r mut self
) -> Option<Multipart<MultipartRequest<'r, 'a, 'b>>>
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); } }
pub fn read_query_body(&mut self) -> Result<Parameters>
[src]
pub fn read_query_body(&mut self) -> Result<Parameters>
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]
impl<'a, 'b> Read for BodyReader<'a, 'b>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Read the request body.
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
read_initializer
)Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
ⓘImportant traits for &'a mut Rfn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
ⓘImportant traits for Bytes<R>fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
ⓘImportant traits for Chars<R>fn chars(self) -> Chars<Self>
[src]
fn chars(self) -> Chars<Self>
🔬 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>fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
Creates an adaptor which will chain this stream with another. Read more
ⓘImportant traits for Take<T>fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
fn take(self, limit: u64) -> Take<Self>
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> Send for BodyReader<'a, 'b>
impl<'a, 'b> !Sync for BodyReader<'a, 'b>
impl<'a, 'b> !Sync for BodyReader<'a, 'b>