mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
Modernize web_util
This commit is contained in:
parent
068aa513a0
commit
bf7c0cb32d
1 changed files with 9 additions and 32 deletions
|
|
@ -12,24 +12,17 @@ static MAX_BODY_SIZE: usize = 100 * 1024 * 1024; // 100 MiB
|
||||||
/// type of the request and read the request's body.
|
/// type of the request and read the request's body.
|
||||||
fn read_json_text(request: &rouille::Request) -> Option<String> {
|
fn read_json_text(request: &rouille::Request) -> Option<String> {
|
||||||
// Bail out if the request body isn't marked as JSON
|
// Bail out if the request body isn't marked as JSON
|
||||||
match request.header("Content-Type") {
|
let content_type = request.header("Content-Type")?;
|
||||||
Some(header) => if !header.starts_with("application/json") {
|
|
||||||
return None;
|
if !content_type.starts_with("application/json") {
|
||||||
},
|
return None;
|
||||||
None => return None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = match request.data() {
|
let body = request.data()?;
|
||||||
Some(v) => v,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Allocate a buffer and read up to MAX_BODY_SIZE+1 bytes into it.
|
// Allocate a buffer and read up to MAX_BODY_SIZE+1 bytes into it.
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
match body.take(MAX_BODY_SIZE.saturating_add(1) as u64).read_to_end(&mut out) {
|
body.take(MAX_BODY_SIZE.saturating_add(1) as u64).read_to_end(&mut out).ok()?;
|
||||||
Ok(_) => {},
|
|
||||||
Err(_) => return None,
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the body was too big (MAX_BODY_SIZE+1), we abort instead of trying to
|
// If the body was too big (MAX_BODY_SIZE+1), we abort instead of trying to
|
||||||
// process it.
|
// process it.
|
||||||
|
|
@ -37,12 +30,7 @@ fn read_json_text(request: &rouille::Request) -> Option<String> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsed = match String::from_utf8(out) {
|
String::from_utf8(out).ok()
|
||||||
Ok(v) => v,
|
|
||||||
Err(_) => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(parsed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the body out of a Rouille Request and attempts to turn it into JSON.
|
/// Reads the body out of a Rouille Request and attempts to turn it into JSON.
|
||||||
|
|
@ -50,17 +38,6 @@ pub fn read_json<T>(request: &rouille::Request) -> Option<T>
|
||||||
where
|
where
|
||||||
T: serde::de::DeserializeOwned,
|
T: serde::de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
let body = match read_json_text(&request) {
|
let body = read_json_text(&request)?;
|
||||||
Some(v) => v,
|
serde_json::from_str(&body).ok()?
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let parsed = match serde_json::from_str(&body) {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(_) => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Change return type to some sort of Result
|
|
||||||
|
|
||||||
Some(parsed)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue