diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs new file mode 100644 index 000000000..e2133ea3e --- /dev/null +++ b/crates/nu-command/src/network/http/client.rs @@ -0,0 +1,9 @@ +// Only panics if the user agent is invalid but we define it statically so either +// it always or never fails +pub fn http_client(allow_insecure: bool) -> reqwest::blocking::Client { + reqwest::blocking::Client::builder() + .user_agent("nushell") + .danger_accept_invalid_certs(allow_insecure) + .build() + .expect("Failed to build reqwest client") +} diff --git a/crates/nu-command/src/network/http/get.rs b/crates/nu-command/src/network/http/get.rs index 0a51d633d..68cfa15dd 100644 --- a/crates/nu-command/src/network/http/get.rs +++ b/crates/nu-command/src/network/http/get.rs @@ -1,3 +1,4 @@ +use crate::network::http::client::http_client; use base64::{alphabet, engine::general_purpose::PAD, engine::GeneralPurpose, Engine}; use nu_engine::CallExt; use nu_protocol::ast::Call; @@ -60,6 +61,11 @@ impl Command for SubCommand { "fetch contents as text rather than a table", Some('r'), ) + .switch( + "insecure", + "allow insecure server connections when using SSL", + Some('k'), + ) .filter() .category(Category::Network) } @@ -112,6 +118,7 @@ impl Command for SubCommand { struct Arguments { url: Value, raw: bool, + insecure: Option, user: Option, password: Option, timeout: Option, @@ -127,6 +134,7 @@ fn run_fetch( let args = Arguments { url: call.req(engine_state, stack, 0)?, raw: call.has_flag("raw"), + insecure: call.get_flag(engine_state, stack, "insecure")?, user: call.get_flag(engine_state, stack, "user")?, password: call.get_flag(engine_state, stack, "password")?, timeout: call.get_flag(engine_state, stack, "timeout")?, @@ -183,7 +191,7 @@ fn helper( _ => None, }; - let client = http_client(); + let client = http_client(args.insecure.is_some()); let mut request = client.get(url); if let Some(timeout) = timeout { @@ -407,13 +415,3 @@ fn response_to_buffer( trim_end_newline: false, } } - -// Only panics if the user agent is invalid but we define it statically so either -// it always or never fails -#[allow(clippy::unwrap_used)] -fn http_client() -> reqwest::blocking::Client { - reqwest::blocking::Client::builder() - .user_agent("nushell") - .build() - .unwrap() -} diff --git a/crates/nu-command/src/network/http/mod.rs b/crates/nu-command/src/network/http/mod.rs index af807b222..694b1401d 100644 --- a/crates/nu-command/src/network/http/mod.rs +++ b/crates/nu-command/src/network/http/mod.rs @@ -1,3 +1,4 @@ +mod client; mod get; mod http_; mod post; diff --git a/crates/nu-command/src/network/http/post.rs b/crates/nu-command/src/network/http/post.rs index 14a8ea6b3..8352496cc 100644 --- a/crates/nu-command/src/network/http/post.rs +++ b/crates/nu-command/src/network/http/post.rs @@ -14,6 +14,8 @@ use std::io::BufReader; use std::path::PathBuf; use std::str::FromStr; +use crate::network::http::client::http_client; + #[derive(Clone)] pub struct SubCommand; @@ -92,6 +94,7 @@ impl Command for SubCommand { ) -> Result { run_post(engine_state, stack, call, input) } + fn examples(&self) -> Vec { vec![ Example { @@ -430,13 +433,3 @@ fn response_to_buffer( trim_end_newline: false, } } -// Only panics if the user agent is invalid but we define it statically so either -// it always or never fails -#[allow(clippy::unwrap_used)] -fn http_client(allow_insecure: bool) -> reqwest::blocking::Client { - reqwest::blocking::Client::builder() - .user_agent("nushell") - .danger_accept_invalid_certs(allow_insecure) - .build() - .expect("Failed to build reqwest client") -}