mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-09 03:02:45 +00:00
4c6562cd29
This allows responses to be sent to the client even when data is only partially read, significantly improving the experience for the client from one with a "connection closed" error to one with a proper response. The consequence is a lifetime in 'Data'. Though other non-lifetime-introducing solutions exist, the introduction of a lifetime to 'Data' is a longstanding desire as it prevents smuggling 'Data' into a longer-lived context. Use of 'Data' in that context was unspecified with various runtime consequences. The addition of a lifetime bound by the request prevents this error statically. In summary, the changes are: * Clients receive responses even when data isn't fully read. * 'Data' becomes 'Data<'r>'. 'FromData' changes accordingly. * Route 'Outcome's are strictly tied to the request lifetime. Tangentially, the invalid length form field validation error message has improved to format length in byte units if it exceeds 1024.
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::{Request, Data};
|
|
use rocket::local::blocking::Client;
|
|
use rocket::data::{self, FromData};
|
|
use rocket::http::ContentType;
|
|
use rocket::form::Form;
|
|
|
|
// Test that the data parameters works as expected.
|
|
|
|
#[derive(FromForm)]
|
|
struct Inner<'r> {
|
|
field: &'r str
|
|
}
|
|
|
|
struct Simple<'r>(&'r str);
|
|
|
|
#[async_trait]
|
|
impl<'r> FromData<'r> for Simple<'r> {
|
|
type Error = std::io::Error;
|
|
|
|
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> data::Outcome<'r, Self> {
|
|
<&'r str>::from_data(req, data).await.map(Simple)
|
|
}
|
|
}
|
|
|
|
#[post("/f", data = "<form>")]
|
|
fn form<'r>(form: Form<Inner<'r>>) -> &'r str { form.into_inner().field }
|
|
|
|
#[post("/s", data = "<simple>")]
|
|
fn simple<'r>(simple: Simple<'r>) -> &'r str { simple.0 }
|
|
|
|
#[test]
|
|
fn test_data() {
|
|
let rocket = rocket::build().mount("/", routes![form, simple]);
|
|
let client = Client::debug(rocket).unwrap();
|
|
|
|
let response = client.post("/f")
|
|
.header(ContentType::Form)
|
|
.body("field=this%20is%20here")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "this is here");
|
|
|
|
let response = client.post("/s").body("this is here").dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "this is here");
|
|
|
|
let response = client.post("/s").body("this%20is%20here").dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "this%20is%20here");
|
|
}
|