Change content length limits of http routes

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-10 01:07:03 +01:00
parent d67ea597e3
commit 69b18cc659
5 changed files with 60 additions and 39 deletions

View file

@ -1,6 +1,8 @@
use warp::http; use warp::http;
use warp::Filter; use warp::Filter;
const MAX: u64 = 1024 * 1024 * 1024 * 4; // 4 GiB
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// Set base path // Set base path
let base = warp::path("import").and(warp::path::end()); let base = warp::path("import").and(warp::path::end());
@ -9,7 +11,7 @@ pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
// Set post method // Set post method
let post = base let post = base
.and(warp::post()) .and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024 * 1024)) // 1GiB .and(warp::body::content_length_limit(MAX))
.and_then(handler); .and_then(handler);
// Specify route // Specify route
opts.or(post) opts.or(post)

View file

@ -11,8 +11,10 @@ use surrealdb::Session;
use warp::path; use warp::path;
use warp::Filter; use warp::Filter;
const MAX: u64 = 1024 * 16; // 16 KiB
#[derive(Default, Deserialize, Debug, Clone)] #[derive(Default, Deserialize, Debug, Clone)]
pub struct Query { struct Query {
pub limit: Option<String>, pub limit: Option<String>,
pub start: Option<String>, pub start: Option<String>,
} }
@ -30,24 +32,30 @@ pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
// Routes for a table // Routes for a table
// ------------------------------ // ------------------------------
// All methods
let base = warp::any();
// Get session config
let base = base.and(conf::build());
// Get content type header
let base = base.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()));
// Set base path for all
let base = base.and(path!("key" / String).and(warp::path::end()));
// Set select method // Set select method
let select = base.and(warp::get()).and(warp::query()).and_then(select_all); let select = warp::any()
.and(warp::get())
.and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and(warp::query())
.and_then(select_all);
// Set create method // Set create method
let create = base let create = warp::any()
.and(warp::post()) .and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB .and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes()) .and(warp::body::bytes())
.and_then(create_all); .and_then(create_all);
// Set delete method // Set delete method
let delete = base.and(warp::delete()).and_then(delete_all); let delete = warp::any()
.and(warp::delete())
.and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and_then(delete_all);
// Specify route // Specify route
let all = select.or(create).or(delete); let all = select.or(create).or(delete);
@ -55,36 +63,47 @@ pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
// Routes for a thing // Routes for a thing
// ------------------------------ // ------------------------------
// All methods
let base = warp::any();
// Get session config
let base = base.and(conf::build());
// Get content type header
let base = base.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()));
// Set base path for one
let base = base.and(path!("key" / String / String).and(warp::path::end()));
// Set select method // Set select method
let select = base.and(warp::get()).and_then(select_one); let select = warp::any()
.and(warp::get())
.and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and_then(select_one);
// Set create method // Set create method
let create = base let create = warp::any()
.and(warp::post()) .and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB .and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes()) .and(warp::body::bytes())
.and_then(create_one); .and_then(create_one);
// Set update method // Set update method
let update = base let update = warp::any()
.and(warp::put()) .and(warp::put())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB .and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes()) .and(warp::body::bytes())
.and_then(update_one); .and_then(update_one);
// Set modify method // Set modify method
let modify = base let modify = warp::any()
.and(warp::patch()) .and(warp::patch())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB .and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes()) .and(warp::body::bytes())
.and_then(modify_one); .and_then(modify_one);
// Set delete method // Set delete method
let delete = base.and(warp::delete()).and_then(delete_one); let delete = warp::any()
.and(warp::delete())
.and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and_then(delete_one);
// Specify route // Specify route
let one = select.or(create).or(update).or(modify).or(delete); let one = select.or(create).or(update).or(modify).or(delete);

View file

@ -2,16 +2,15 @@ use crate::net::head;
use warp::http; use warp::http;
use warp::Filter; use warp::Filter;
const MAX: u64 = 1024; // 1 KiB
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// Set base path // Set base path
let base = warp::path("signin").and(warp::path::end()); let base = warp::path("signin").and(warp::path::end());
// Set opts method // Set opts method
let opts = base.and(warp::options()).map(warp::reply); let opts = base.and(warp::options()).map(warp::reply);
// Set post method // Set post method
let post = base let post = base.and(warp::post()).and(warp::body::content_length_limit(MAX)).and_then(handler);
.and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB
.and_then(handler);
// Specify route // Specify route
opts.or(post).with(head::cors()) opts.or(post).with(head::cors())
} }

View file

@ -2,16 +2,15 @@ use crate::net::head;
use warp::http; use warp::http;
use warp::Filter; use warp::Filter;
const MAX: u64 = 1024; // 1 KiB
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// Set base path // Set base path
let base = warp::path("signup").and(warp::path::end()); let base = warp::path("signup").and(warp::path::end());
// Set opts method // Set opts method
let opts = base.and(warp::options()).map(warp::reply); let opts = base.and(warp::options()).map(warp::reply);
// Set post method // Set post method
let post = base let post = base.and(warp::post()).and(warp::body::content_length_limit(MAX)).and_then(handler);
.and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB
.and_then(handler);
// Specify route // Specify route
opts.or(post).with(head::cors()) opts.or(post).with(head::cors())
} }

View file

@ -8,6 +8,8 @@ use futures::{FutureExt, StreamExt};
use surrealdb::Session; use surrealdb::Session;
use warp::Filter; use warp::Filter;
const MAX: u64 = 1024 * 1024; // 1 MiB
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// Set base path // Set base path
let base = warp::path("sql").and(warp::path::end()); let base = warp::path("sql").and(warp::path::end());
@ -18,7 +20,7 @@ pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::post()) .and(warp::post())
.and(conf::build()) .and(conf::build())
.and(warp::header::<String>(http::header::CONTENT_TYPE.as_str())) .and(warp::header::<String>(http::header::CONTENT_TYPE.as_str()))
.and(warp::body::content_length_limit(1024 * 1024)) // 1MiB .and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes()) .and(warp::body::bytes())
.and_then(handler); .and_then(handler);
// Set sock method // Set sock method