Don't let surf panic on invalid URIs (#1205)
This commit is contained in:
parent
5fe1fd0227
commit
fba743ef0b
2 changed files with 7 additions and 2 deletions
|
@ -34,11 +34,12 @@ pub async fn delete((_, _): (Value, Option<Value>)) -> Result<Value, Error> {
|
||||||
|
|
||||||
fn try_as_uri(fn_name: &str, value: Value) -> Result<Strand, Error> {
|
fn try_as_uri(fn_name: &str, value: Value) -> Result<Strand, Error> {
|
||||||
match value {
|
match value {
|
||||||
Value::Strand(uri) => Ok(uri),
|
// Avoid surf crate panic by pre-checking URI.
|
||||||
|
Value::Strand(uri) if crate::fnc::util::http::uri_is_valid(&uri) => Ok(uri),
|
||||||
_ => Err(Error::InvalidArguments {
|
_ => Err(Error::InvalidArguments {
|
||||||
name: fn_name.to_owned(),
|
name: fn_name.to_owned(),
|
||||||
// Assumption is that URI is first argument.
|
// Assumption is that URI is first argument.
|
||||||
message: String::from("The first argument should be a string."),
|
message: String::from("The first argument should be a string containing a valid URI."),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,10 @@ use crate::sql::value::Value;
|
||||||
use surf::Client;
|
use surf::Client;
|
||||||
use surf::Config;
|
use surf::Config;
|
||||||
|
|
||||||
|
pub(crate) fn uri_is_valid(uri: &str) -> bool {
|
||||||
|
surf::Url::parse(uri).is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn head(uri: Strand, opts: impl Into<Object>) -> Result<Value, Error> {
|
pub async fn head(uri: Strand, opts: impl Into<Object>) -> Result<Value, Error> {
|
||||||
// Set a default client with no timeout
|
// Set a default client with no timeout
|
||||||
let cli: Client = Config::new().set_timeout(None).try_into().unwrap();
|
let cli: Client = Config::new().set_timeout(None).try_into().unwrap();
|
||||||
|
|
Loading…
Reference in a new issue