diff --git a/Cargo.toml b/Cargo.toml index 91daf6ccf0..07ad13cec0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,7 +110,7 @@ lsp-types = "0.95.0" mach2 = "0.4" md5 = { version = "0.10", package = "md-5" } miette = "7.2" -mime = "0.3" +mime = "0.3.17" mime_guess = "2.0" mockito = { version = "1.5", default-features = false } multipart-rs = "0.1.11" diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 2459d74ad9..475f595ef5 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -50,7 +50,7 @@ encoding_rs = { workspace = true } fancy-regex = { workspace = true } filesize = { workspace = true } filetime = { workspace = true } -fs_extra = { workspace = true } +fs_extra = { workspace = true } human-date-parser = { workspace = true } indexmap = { workspace = true } indicatif = { workspace = true } @@ -88,7 +88,7 @@ sysinfo = { workspace = true } tabled = { workspace = true, features = ["color"], default-features = false } terminal_size = { workspace = true } titlecase = { workspace = true } -toml = { workspace = true, features = ["preserve_order"]} +toml = { workspace = true, features = ["preserve_order"] } unicode-segmentation = { workspace = true } ureq = { workspace = true, default-features = false, features = ["charset", "gzip", "json", "native-tls"] } url = { workspace = true } diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 7bb63a8fbd..0027101528 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -4,9 +4,10 @@ use base64::{ engine::{general_purpose::PAD, GeneralPurpose}, Engine, }; +use fancy_regex::Regex; use multipart_rs::MultipartWriter; use nu_engine::command_prelude::*; -use nu_protocol::{ByteStream, Signals}; +use nu_protocol::{ByteStream, LabeledError, Signals}; use std::{ collections::HashMap, io::Cursor, @@ -560,23 +561,25 @@ fn transform_response_using_content_type( resp: Response, content_type: &str, ) -> Result { - let content_type = - mime::Mime::from_str(content_type).map_err(|_| ShellError::GenericError { - error: format!("MIME type unknown: {content_type}"), - msg: "".into(), - span: None, - help: Some("given unknown MIME type".into()), - inner: vec![], - })?; + let regex = Regex::new("\"").expect("Failed to create regex"); + let content_type_trim = regex.replace_all(content_type, "").to_string(); + + let content_type = mime::Mime::from_str(&content_type_trim).map_err(|err| { + LabeledError::new(err.to_string()) + .with_help("given unknown MIME type, or error parsing MIME type") + .with_label(format!("MIME type unknown: {content_type_trim}"), span) + })?; + let ext = match (content_type.type_(), content_type.subtype()) { (mime::TEXT, mime::PLAIN) => { let path_extension = url::Url::parse(requested_url) - .map_err(|_| ShellError::GenericError { - error: format!("Cannot parse URL: {requested_url}"), - msg: "".into(), - span: None, - help: Some("cannot parse".into()), - inner: vec![], + .map_err(|err| { + LabeledError::new(err.to_string()) + .with_help("cannot parse") + .with_label( + format!("Cannot parse URL: {requested_url}"), + Span::unknown(), + ) })? .path_segments() .and_then(|segments| segments.last())