Replies: 7 comments 6 replies
-
You're using an |
Beta Was this translation helpful? Give feedback.
-
The idea of using But on the other hand, I have seen a strange thing, if I modify the example and, instead of sending a file, I send a #[macro_use] extern crate rocket;
use rocket::form::Form;
#[derive(FromForm)]
pub struct FormData {
pub text: Option<String>,
}
#[post("/", data = "<form>")]
async fn index(form: Form<FormData>) -> &'static str {
match &form.text {
Some(_) => "You have sent text!",
None => "No data"
}
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
} ROCKET_LIMITS={form="1"} ROCKET_LOG_LEVEL=debug cargo run &
% curl -v -d 'text=abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyz' http://127.0.0.1:8000
* Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000
> POST / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.5.0
> Accept: */*
> Content-Length: 130
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 413 Payload Too Large
... etc ... As far as I can see the data guard does handle the data limit correctly when it is a |
Beta Was this translation helpful? Give feedback.
-
It depends what you mean by "check". Rocket will never give you a file larger than what you've configured via limits. That's why they exist. This is why you get a 413 when you use just If you mean you want to check, then use a
You're sending/expecting different kinds of data which means different limits are in effect. See the limits docs. |
Beta Was this translation helpful? Give feedback.
-
But that's precisely where the problem lies (or maybe it's just me not understanding something). If I use directly a For example, with this code: #[macro_use] extern crate rocket;
use rocket::fs::TempFile;
use rocket::form::Form;
use std::io::Error;
#[derive(FromForm)]
pub struct FormData<'a> {
pub data: Option<TempFile<'a>>
}
async fn save_data(data: &mut TempFile<'_>) -> Result<(), Error> {
data.persist_to("/tmp/file.raw").await?;
Ok(())
}
#[post("/", data = "<form>")]
async fn index(mut form: Form<FormData<'_>>) -> &'static str {
match form.data.as_mut() {
Some(data) => {
match save_data(data).await {
Ok(()) => "Data saved",
Err(_) => "Error"
}
},
None => "No data"
}
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index]) I run it: ROCKET_LIMITS={data-form="1"} ROCKET_LOG_LEVEL=debug cargo run And send a big file:
It does not return 413 and the log says:
|
Beta Was this translation helpful? Give feedback.
-
But you're not doing that. You're using an #[derive(FromForm)]
pub struct FormData<'a> {
- pub data: Option<TempFile<'a>>
+ pub data: TempFile<'a>
} |
Beta Was this translation helpful? Give feedback.
-
I understand what you are saying, error 413 becomes None and since that is controlled, it ends with a 200. And is there any way to differentiate between a 413 error or that the user is not submitting that part of the form? I think it was what you posted here:
But I don't quite understand how to apply it, I don't know if you have a simple example of how to do it. |
Beta Was this translation helpful? Give feedback.
-
#[derive(FromForm)]
pub struct FormData<'a> {
- pub data: Option<TempFile<'a>>
+ pub data: rocket::form::Result<TempFile<'a>>
} |
Beta Was this translation helpful? Give feedback.
-
Rocket Version
0.5.0
Operating System
Arch Linux
Rust Toolchain Version
rustc 1.74.0 (79e9716c9 2023-11-13)
What happened?
When data is sent in a multipart form and the amount of data exceeds the limit imposed in the
data-form
the system returns a 200 instead of a 413.Test Case
Log Output
Additional Context
Rocket is launched as follows:
And in this case a file of almost 2MiB is being sent with this command:
UPDATE: I forgot to comment that if you don't use a
Capped<TempFile>
it also happens.System Checks
rustc
toolchain.Beta Was this translation helpful? Give feedback.
All reactions