Don't let surf panic on invalid URIs (#1205)

This commit is contained in:
Finn Bear 2022-09-23 17:25:05 -07:00 committed by GitHub
parent 5fe1fd0227
commit fba743ef0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -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> {
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 {
name: fn_name.to_owned(),
// 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."),
}),
}
}

View file

@ -6,6 +6,10 @@ use crate::sql::value::Value;
use surf::Client;
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> {
// Set a default client with no timeout
let cli: Client = Config::new().set_timeout(None).try_into().unwrap();