Use 'Content-Type' for format routing. Simplify 'raw_upload' example.

This commit is contained in:
Sergio Benitez 2016-10-12 19:08:19 -07:00
parent 4769b17d13
commit b4305cb430
3 changed files with 12 additions and 12 deletions

View File

@ -3,19 +3,14 @@
extern crate rocket;
use std::io;
use rocket::request::Data;
use rocket::response::Failure;
use rocket::http::StatusCode;
use rocket::response::data::Plain;
#[post("/upload", format = "text/plain", data = "<data>")]
fn upload(data: Data) -> Result<String, Failure> {
match data.stream_to_file("/tmp/upload.txt") {
Ok(n) => Ok(format!("OK: {} bytes uploaded.", n)),
Err(e) => {
println!(" => Failed writing to file: {:?}.", e);
return Err(Failure(StatusCode::InternalServerError));
}
}
fn upload(data: Data) -> io::Result<Plain<String>> {
data.stream_to_file("/tmp/upload.txt").map(|n| Plain(n.to_string()))
}
#[get("/")]

View File

@ -220,6 +220,11 @@ impl fmt::Display for Request {
/// Pretty prints a Request. This is primarily used by Rocket's logging
/// infrastructure.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", Green.paint(&self.method), Blue.paint(&self.uri))
write!(f, "{} {}", Green.paint(&self.method), Blue.paint(&self.uri))?;
if self.method.supports_payload() && !self.content_type().is_any() {
write!(f, " {}", Yellow.paint(self.content_type()))?;
}
Ok(())
}
}

View File

@ -138,6 +138,6 @@ impl Collider<Request> for Route {
fn collides_with(&self, req: &Request) -> bool {
self.method == req.method
&& req.uri().collides_with(&self.path.as_uri())
&& req.accepts().collides_with(&self.content_type)
&& req.content_type().collides_with(&self.content_type)
}
}