Expand description
Require a content-length
header to have a value no greater than some limit.
Rejects if content-length
header is missing, is invalid, or has a number
larger than the limit provided.
Example
use warp::Filter;
// Limit the upload to 4kb...
let upload = warp::body::content_length_limit(4096)
.and(warp::body::aggregate());
Examples found in repository?
More examples
examples/body.rs (line 22)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
async fn main() {
pretty_env_logger::init();
// POST /employees/:rate {"name":"Sean","rate":2}
let promote = warp::post()
.and(warp::path("employees"))
.and(warp::path::param::<u32>())
// Only accept bodies smaller than 16kb...
.and(warp::body::content_length_limit(1024 * 16))
.and(warp::body::json())
.map(|rate, mut employee: Employee| {
employee.rate = rate;
warp::reply::json(&employee)
});
warp::serve(promote).run(([127, 0, 0, 1], 3030)).await
}
src/filters/multipart.rs (line 87)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fn filter(&self, _: Internal) -> Self::Future {
let boundary = super::header::header2::<ContentType>().and_then(|ct| {
let mime = Mime::from(ct);
let mime = mime
.get_param("boundary")
.map(|v| v.to_string())
.ok_or_else(|| reject::invalid_header("content-type"));
future::ready(mime)
});
let filt = super::body::content_length_limit(self.max_length)
.and(boundary)
.and(super::body::bytes())
.map(|boundary, body| FormData {
inner: Multipart::with_body(Cursor::new(body), boundary),
});
let fut = filt.filter(Internal);
Box::pin(fut)
}
examples/sse_chat.rs (line 25)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
async fn main() {
pretty_env_logger::init();
// Keep track of all connected users, key is usize, value
// is an event stream sender.
let users = Arc::new(Mutex::new(HashMap::new()));
// Turn our "state" into a new Filter...
let users = warp::any().map(move || users.clone());
// POST /chat -> send message
let chat_send = warp::path("chat")
.and(warp::post())
.and(warp::path::param::<usize>())
.and(warp::body::content_length_limit(500))
.and(
warp::body::bytes().and_then(|body: bytes::Bytes| async move {
std::str::from_utf8(&body)
.map(String::from)
.map_err(|_e| warp::reject::custom(NotUtf8))
}),
)
.and(users.clone())
.map(|my_id, msg, users| {
user_message(my_id, msg, &users);
warp::reply()
});
// GET /chat -> messages stream
let chat_recv = warp::path("chat").and(warp::get()).and(users).map(|users| {
// reply using server-sent events
let stream = user_connected(users);
warp::sse::reply(warp::sse::keep_alive().stream(stream))
});
// GET / -> index html
let index = warp::path::end().map(|| {
warp::http::Response::builder()
.header("content-type", "text/html; charset=utf-8")
.body(INDEX_HTML)
});
let routes = index.or(chat_recv).or(chat_send);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}