changes Reqwest to Ureq. (#8320)

# Description

_(Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.)_

This pull request removes `Reqwest` and replaces it with `Ureq` to
remove some of our dependencies, giving us faster compile times as well
as smaller binaries. `Ureq` does not have an async runtime included so
we do not need build heavy dependencies such as `Tokio`. From older
tests I had the number of build units be reduced from `430 -> 392`.
The default of `Ureq` uses `Rustls` but it has been configured to
instead use `native_tls` which should work exactly the same as the `tls`
works now.
I removed `content-length` from the http commands as after refactoring i
did not see a reason to have it available, correct me if this is
something we should preserve.

In the medium, to long term, we should maybe consider changing to
`rustls` to have the same `tls` on all platforms.

# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Filip Andersson 2023-03-05 23:48:13 +01:00 committed by GitHub
parent d6141881f2
commit f6ca62384e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 172 additions and 302 deletions

116
Cargo.lock generated
View File

@ -1805,19 +1805,6 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.50" version = "0.1.50"
@ -1934,12 +1921,6 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "ipnet"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b"
[[package]] [[package]]
name = "is-root" name = "is-root"
version = "0.1.2" version = "0.1.2"
@ -2496,9 +2477,9 @@ dependencies = [
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.10" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
@ -2773,6 +2754,7 @@ dependencies = [
"mime", "mime",
"mime_guess", "mime_guess",
"mockito", "mockito",
"native-tls",
"notify", "notify",
"nu-ansi-term", "nu-ansi-term",
"nu-cmd-lang", "nu-cmd-lang",
@ -2808,7 +2790,6 @@ dependencies = [
"rayon", "rayon",
"reedline", "reedline",
"regex", "regex",
"reqwest",
"roxmltree", "roxmltree",
"rstest", "rstest",
"rusqlite", "rusqlite",
@ -2829,13 +2810,14 @@ dependencies = [
"umask", "umask",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
"ureq",
"url", "url",
"users 0.11.0", "users 0.11.0",
"uuid", "uuid",
"wax", "wax",
"which", "which",
"windows", "windows",
"winreg 0.11.0", "winreg",
] ]
[[package]] [[package]]
@ -4309,43 +4291,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "reqwest"
version = "0.11.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "431949c384f4e2ae07605ccaa56d1d9d2ecdb5cadd4f9577ccfab29f2e5149fc"
dependencies = [
"base64 0.13.0",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg 0.10.1",
]
[[package]] [[package]]
name = "retain_mut" name = "retain_mut"
version = "0.1.9" version = "0.1.9"
@ -4583,12 +4528,11 @@ dependencies = [
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.20" version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" checksum = "713cfb06c7059f3588fb8044c0fad1d09e3c01d225e25b9220dbfdcf16dbb1b3"
dependencies = [ dependencies = [
"lazy_static", "windows-sys 0.42.0",
"windows-sys 0.36.1",
] ]
[[package]] [[package]]
@ -4614,9 +4558,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.7.0" version = "2.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c" checksum = "a332be01508d814fed64bf28f798a146d73792121129962fdf335bb3c49a4254"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"core-foundation", "core-foundation",
@ -4627,9 +4571,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework-sys" name = "security-framework-sys"
version = "2.6.1" version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" checksum = "31c9bb296072e961fcbd8853511dd39c2d8be2deb1e17c6860b1d30732b323b4"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -5389,16 +5333,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-native-tls"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.4" version = "0.7.4"
@ -5629,6 +5563,23 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]]
name = "ureq"
version = "2.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "338b31dd1314f68f3aabf3ed57ab922df95ffcd902476ca7ba3c4ce7b908c46d"
dependencies = [
"base64 0.13.0",
"encoding_rs",
"flate2",
"log",
"native-tls",
"once_cell",
"serde",
"serde_json",
"url",
]
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -6085,15 +6036,6 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "winreg"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.11.0" version = "0.11.0"

View File

@ -72,7 +72,8 @@ quick-xml = "0.27"
rand = "0.8" rand = "0.8"
rayon = "1.6.1" rayon = "1.6.1"
regex = "1.7.1" regex = "1.7.1"
reqwest = { version = "0.11", features = ["blocking", "json"] } ureq = { version = "2.6.2", default-features = false, features = ["json", "charset", "native-tls", "gzip"] }
native-tls = "0.2.11"
roxmltree = "0.18.0" roxmltree = "0.18.0"
rust-embed = "6.3.0" rust-embed = "6.3.0"
same-file = "1.0.6" same-file = "1.0.6"

View File

@ -7,8 +7,8 @@ use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
BufferedReader, IntoPipelineData, PipelineData, RawStream, ShellError, Span, Value, BufferedReader, IntoPipelineData, PipelineData, RawStream, ShellError, Span, Value,
}; };
use reqwest::blocking::{RequestBuilder, Response}; use ureq::{Error, ErrorKind, Request, Response};
use reqwest::{blocking, Error, StatusCode};
use std::collections::HashMap; use std::collections::HashMap;
use std::io::BufReader; use std::io::BufReader;
use std::path::PathBuf; use std::path::PathBuf;
@ -25,12 +25,16 @@ pub enum BodyType {
// Only panics if the user agent is invalid but we define it statically so either // Only panics if the user agent is invalid but we define it statically so either
// it always or never fails // it always or never fails
pub fn http_client(allow_insecure: bool) -> blocking::Client { pub fn http_client(allow_insecure: bool) -> ureq::Agent {
blocking::Client::builder() let tls = native_tls::TlsConnector::builder()
.user_agent("nushell")
.danger_accept_invalid_certs(allow_insecure) .danger_accept_invalid_certs(allow_insecure)
.build() .build()
.expect("Failed to build reqwest client") .expect("Failed to build network tls");
ureq::builder()
.user_agent("nushell")
.tls_connector(std::sync::Arc::new(tls))
.build()
} }
pub fn http_parse_url( pub fn http_parse_url(
@ -62,15 +66,9 @@ pub fn response_to_buffer(
) -> PipelineData { ) -> PipelineData {
// Try to get the size of the file to be downloaded. // Try to get the size of the file to be downloaded.
// This is helpful to show the progress of the stream. // This is helpful to show the progress of the stream.
let buffer_size = match &response.headers().get("content-length") { let buffer_size = match response.header("content-length") {
Some(content_length) => { Some(content_length) => {
let content_length = &(*content_length).clone(); // binding let content_length = content_length.parse::<u64>().unwrap_or_default();
let content_length = content_length
.to_str()
.unwrap_or("")
.parse::<u64>()
.unwrap_or(0);
if content_length == 0 { if content_length == 0 {
None None
@ -81,7 +79,8 @@ pub fn response_to_buffer(
_ => None, _ => None,
}; };
let buffered_input = BufReader::new(response); let reader = response.into_reader();
let buffered_input = BufReader::new(reader);
PipelineData::ExternalStream { PipelineData::ExternalStream {
stdout: Some(RawStream::new( stdout: Some(RawStream::new(
@ -103,8 +102,8 @@ pub fn response_to_buffer(
pub fn request_add_authorization_header( pub fn request_add_authorization_header(
user: Option<String>, user: Option<String>,
password: Option<String>, password: Option<String>,
mut request: RequestBuilder, mut request: Request,
) -> RequestBuilder { ) -> Request {
let base64_engine = GeneralPurpose::new(&alphabet::STANDARD, PAD); let base64_engine = GeneralPurpose::new(&alphabet::STANDARD, PAD);
let login = match (user, password) { let login = match (user, password) {
@ -127,71 +126,87 @@ pub fn request_add_authorization_header(
}; };
if let Some(login) = login { if let Some(login) = login {
request = request.header("Authorization", format!("Basic {login}")); request = request.set("Authorization", &format!("Basic {login}"));
} }
request request
} }
pub fn request_set_body( pub fn send_request(
request: Request,
span: Span,
body: Option<Value>,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>, ) -> Result<Response, ShellError> {
body: Value, let request_url = request.url().to_string();
mut request: RequestBuilder, if body.is_none() {
) -> Result<RequestBuilder, ShellError> { return request
// set the content-type header before using e.g., request.json .call()
// because that will avoid duplicating the header value .map_err(|err| handle_response_error(span, &request_url, err));
if let Some(val) = &content_type {
request = request.header("Content-Type", val);
} }
let body = body.expect("Should never be none.");
let body_type = match content_type { let body_type = match content_type {
Some(it) if it == "application/json" => BodyType::Json, Some(it) if it == "application/json" => BodyType::Json,
Some(it) if it == "application/x-www-form-urlencoded" => BodyType::Form, Some(it) if it == "application/x-www-form-urlencoded" => BodyType::Form,
_ => BodyType::Unknown, _ => BodyType::Unknown,
}; };
match body { match body {
Value::Binary { val, .. } => { Value::Binary { val, .. } => request
request = request.body(val); .send_bytes(&val)
} .map_err(|err| handle_response_error(span, &request_url, err)),
Value::String { val, .. } => { Value::String { val, .. } => request
request = request.body(val); .send_string(&val)
} .map_err(|err| handle_response_error(span, &request_url, err)),
Value::Record { .. } if body_type == BodyType::Json => { Value::Record { .. } if body_type == BodyType::Json => {
let data = value_to_json_value(&body)?; let data = value_to_json_value(&body)?;
request = request.json(&data); request
.send_json(data)
.map_err(|err| handle_response_error(span, &request_url, err))
} }
Value::Record { .. } if body_type == BodyType::Form => { Value::Record { cols, vals, .. } if body_type == BodyType::Form => {
let data = value_to_json_value(&body)?; let mut data: Vec<(String, String)> = Vec::with_capacity(cols.len());
request = request.form(&data);
for (col, val) in cols.iter().zip(vals.iter()) {
data.push((col.clone(), val.as_string()?))
}
let data = data
.iter()
.map(|(a, b)| (a.as_str(), b.as_str()))
.collect::<Vec<(&str, &str)>>();
request
.send_form(&data[..])
.map_err(|err| handle_response_error(span, &request_url, err))
} }
Value::List { vals, .. } if body_type == BodyType::Form => { Value::List { vals, .. } if body_type == BodyType::Form => {
if vals.len() % 2 != 0 { if vals.len() % 2 != 0 {
return Err(ShellError::IOError("unsupported body input".into())); return Err(ShellError::IOError("unsupported body input".into()));
} }
let data = vals let data = vals
.chunks(2) .chunks(2)
.map(|it| Ok((it[0].as_string()?, it[1].as_string()?))) .map(|it| Ok((it[0].as_string()?, it[1].as_string()?)))
.collect::<Result<Vec<(String, String)>, ShellError>>()?; .collect::<Result<Vec<(String, String)>, ShellError>>()?;
request = request.form(&data)
}
_ => {
return Err(ShellError::IOError("unsupported body input".into()));
}
};
if let Some(val) = content_length { let data = data
request = request.header("Content-Length", val); .iter()
.map(|(a, b)| (a.as_str(), b.as_str()))
.collect::<Vec<(&str, &str)>>();
request
.send_form(&data)
.map_err(|err| handle_response_error(span, &request_url, err))
}
_ => Err(ShellError::IOError("unsupported body input".into())),
} }
Ok(request)
} }
pub fn request_set_timeout( pub fn request_set_timeout(
timeout: Option<Value>, timeout: Option<Value>,
mut request: RequestBuilder, mut request: Request,
) -> Result<RequestBuilder, ShellError> { ) -> Result<Request, ShellError> {
if let Some(timeout) = timeout { if let Some(timeout) = timeout {
let val = timeout.as_i64()?; let val = timeout.as_i64()?;
if val.is_negative() || val < 1 { if val.is_negative() || val < 1 {
@ -210,8 +225,8 @@ pub fn request_set_timeout(
pub fn request_add_custom_headers( pub fn request_add_custom_headers(
headers: Option<Value>, headers: Option<Value>,
mut request: RequestBuilder, mut request: Request,
) -> Result<RequestBuilder, ShellError> { ) -> Result<Request, ShellError> {
if let Some(headers) = headers { if let Some(headers) = headers {
let mut custom_headers: HashMap<String, Value> = HashMap::new(); let mut custom_headers: HashMap<String, Value> = HashMap::new();
@ -257,7 +272,7 @@ pub fn request_add_custom_headers(
for (k, v) in &custom_headers { for (k, v) in &custom_headers {
if let Ok(s) = v.as_string() { if let Ok(s) = v.as_string() {
request = request.header(k, s); request = request.set(k, &s);
} }
} }
} }
@ -265,48 +280,41 @@ pub fn request_add_custom_headers(
Ok(request) Ok(request)
} }
fn handle_response_error(span: Span, requested_url: &String, response: Error) -> ShellError { fn handle_response_error(span: Span, requested_url: &str, response_err: Error) -> ShellError {
// Explicitly turn 4xx and 5xx statuses into errors. match response_err {
if response.is_timeout() { Error::Status(301, _) => ShellError::NetworkFailure(
ShellError::NetworkFailure(format!("Request to {requested_url} has timed out"), span) format!("Resource moved permanently (301): {requested_url:?}"),
} else if response.is_status() { span,
match response.status() { ),
Some(err_code) if err_code == StatusCode::NOT_FOUND => ShellError::NetworkFailure( Error::Status(400, _) => {
format!("Requested file not found (404): {requested_url:?}"), ShellError::NetworkFailure(format!("Bad request (400) to {requested_url:?}"), span)
span,
),
Some(err_code) if err_code == StatusCode::MOVED_PERMANENTLY => {
ShellError::NetworkFailure(
format!("Resource moved permanently (301): {requested_url:?}"),
span,
)
}
Some(err_code) if err_code == StatusCode::BAD_REQUEST => {
ShellError::NetworkFailure(format!("Bad request (400) to {requested_url:?}"), span)
}
Some(err_code) if err_code == StatusCode::FORBIDDEN => ShellError::NetworkFailure(
format!("Access forbidden (403) to {requested_url:?}"),
span,
),
_ => ShellError::NetworkFailure(
format!(
"Cannot make request to {:?}. Error is {:?}",
requested_url,
response.to_string()
),
span,
),
} }
} else { Error::Status(403, _) => {
ShellError::NetworkFailure( ShellError::NetworkFailure(format!("Access forbidden (403) to {requested_url:?}"), span)
}
Error::Status(404, _) => ShellError::NetworkFailure(
format!("Requested file not found (404): {requested_url:?}"),
span,
),
Error::Status(408, _) => {
ShellError::NetworkFailure(format!("Request timeout (408): {requested_url:?}"), span)
}
Error::Status(_, _) => ShellError::NetworkFailure(
format!( format!(
"Cannot make request to {:?}. Error is {:?}", "Cannot make request to {:?}. Error is {:?}",
requested_url, requested_url,
response.to_string() response_err.to_string()
), ),
span, span,
) ),
Error::Transport(t) => match t {
t if t.kind() == ErrorKind::ConnectionFailed => ShellError::NetworkFailure(
format!("Cannot make request to {requested_url}, there was an error establishing a connection.",),
span,
),
t => ShellError::NetworkFailure(t.to_string(), span),
},
} }
} }
@ -316,20 +324,11 @@ pub fn request_handle_response(
span: Span, span: Span,
requested_url: &String, requested_url: &String,
raw: bool, raw: bool,
response: Result<Response, Error>, response: Result<Response, ShellError>,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
match response { match response {
Ok(resp) => match resp.headers().get("content-type") { Ok(resp) => match resp.header("content-type") {
Some(content_type) => { Some(content_type) => {
let content_type = content_type.to_str().map_err(|e| {
ShellError::GenericError(
e.to_string(),
"".to_string(),
None,
Some("MIME type were invalid".to_string()),
Vec::new(),
)
})?;
let content_type = mime::Mime::from_str(content_type).map_err(|_| { let content_type = mime::Mime::from_str(content_type).map_err(|_| {
ShellError::GenericError( ShellError::GenericError(
format!("MIME type unknown: {content_type}"), format!("MIME type unknown: {content_type}"),
@ -386,29 +385,29 @@ pub fn request_handle_response(
} }
None => Ok(response_to_buffer(resp, engine_state, span)), None => Ok(response_to_buffer(resp, engine_state, span)),
}, },
Err(e) => Err(handle_response_error(span, requested_url, e)), Err(e) => Err(e),
} }
} }
pub fn request_handle_response_headers( pub fn request_handle_response_headers(
span: Span, span: Span,
requested_url: &String, response: Result<Response, ShellError>,
response: Result<Response, Error>,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
match response { match response {
Ok(resp) => { Ok(resp) => {
let cols: Vec<String> = resp.headers().keys().map(|name| name.to_string()).collect(); let cols = resp.headers_names();
let mut vals = Vec::with_capacity(cols.len()); let mut vals = Vec::with_capacity(cols.len());
for value in resp.headers().values() { for key in &cols {
match value.to_str() { match resp.header(key) {
Ok(str_value) => vals.push(Value::String { // match value.to_str() {
Some(str_value) => vals.push(Value::String {
val: str_value.to_string(), val: str_value.to_string(),
span, span,
}), }),
Err(err) => { None => {
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
format!("Failure when converting header value: {err}"), "Failure when converting header value".to_string(),
"".to_string(), "".to_string(),
None, None,
Some("Failure when converting header value".to_string()), Some("Failure when converting header value".to_string()),
@ -420,6 +419,6 @@ pub fn request_handle_response_headers(
Ok(Value::Record { cols, vals, span }.into_pipeline_data()) Ok(Value::Record { cols, vals, span }.into_pipeline_data())
} }
Err(e) => Err(handle_response_error(span, requested_url, e)), Err(e) => Err(e),
} }
} }

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_body, request_set_timeout, request_handle_response, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -46,12 +46,6 @@ impl Command for SubCommand {
"the MIME type of content to post", "the MIME type of content to post",
Some('t'), Some('t'),
) )
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named( .named(
"max-time", "max-time",
SyntaxShape::Int, SyntaxShape::Int,
@ -137,7 +131,6 @@ struct Arguments {
headers: Option<Value>, headers: Option<Value>,
data: Option<Value>, data: Option<Value>,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>,
raw: bool, raw: bool,
insecure: bool, insecure: bool,
user: Option<String>, user: Option<String>,
@ -156,7 +149,6 @@ fn run_delete(
headers: call.get_flag(engine_state, stack, "headers")?, headers: call.get_flag(engine_state, stack, "headers")?,
data: call.get_flag(engine_state, stack, "data")?, data: call.get_flag(engine_state, stack, "data")?,
content_type: call.get_flag(engine_state, stack, "content-type")?, content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
raw: call.has_flag("raw"), raw: call.has_flag("raw"),
insecure: call.has_flag("insecure"), insecure: call.has_flag("insecure"),
user: call.get_flag(engine_state, stack, "user")?, user: call.get_flag(engine_state, stack, "user")?,
@ -176,19 +168,17 @@ fn helper(
args: Arguments, args: Arguments,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.delete(url); let mut request = client.delete(&requested_url);
if let Some(data) = args.data {
request = request_set_body(args.content_type, args.content_length, data, request)?;
}
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, args.data, args.content_type);
request_handle_response( request_handle_response(
engine_state, engine_state,
stack, stack,

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_body, request_set_timeout, request_handle_response, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -46,12 +46,6 @@ impl Command for SubCommand {
"the MIME type of content to post", "the MIME type of content to post",
Some('t'), Some('t'),
) )
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named( .named(
"max-time", "max-time",
SyntaxShape::Int, SyntaxShape::Int,
@ -138,7 +132,6 @@ struct Arguments {
headers: Option<Value>, headers: Option<Value>,
data: Option<Value>, data: Option<Value>,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>,
raw: bool, raw: bool,
insecure: bool, insecure: bool,
user: Option<String>, user: Option<String>,
@ -157,7 +150,6 @@ fn run_get(
headers: call.get_flag(engine_state, stack, "headers")?, headers: call.get_flag(engine_state, stack, "headers")?,
data: call.get_flag(engine_state, stack, "data")?, data: call.get_flag(engine_state, stack, "data")?,
content_type: call.get_flag(engine_state, stack, "content-type")?, content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
raw: call.has_flag("raw"), raw: call.has_flag("raw"),
insecure: call.has_flag("insecure"), insecure: call.has_flag("insecure"),
user: call.get_flag(engine_state, stack, "user")?, user: call.get_flag(engine_state, stack, "user")?,
@ -176,19 +168,16 @@ fn helper(
args: Arguments, args: Arguments,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.get(url); let mut request = client.get(&requested_url);
if let Some(data) = args.data {
request = request_set_body(args.content_type, args.content_length, data, request)?;
}
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, args.data, args.content_type);
request_handle_response( request_handle_response(
engine_state, engine_state,
stack, stack,

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response_headers, request_set_timeout, request_handle_response_headers, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -134,17 +134,17 @@ fn run_head(
// The Option<String> return a possible file extension which can be used in AutoConvert commands // The Option<String> return a possible file extension which can be used in AutoConvert commands
fn helper(call: &Call, args: Arguments) -> Result<PipelineData, ShellError> { fn helper(call: &Call, args: Arguments) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.head(url); let mut request = client.head(&requested_url);
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, None, None);
request_handle_response_headers(span, &requested_url, response) request_handle_response_headers(span, response)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_body, request_set_timeout, request_handle_response, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -42,12 +42,6 @@ impl Command for SubCommand {
"the MIME type of content to post", "the MIME type of content to post",
Some('t'), Some('t'),
) )
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named( .named(
"max-time", "max-time",
SyntaxShape::Int, SyntaxShape::Int,
@ -127,7 +121,6 @@ struct Arguments {
headers: Option<Value>, headers: Option<Value>,
data: Value, data: Value,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>,
raw: bool, raw: bool,
insecure: bool, insecure: bool,
user: Option<String>, user: Option<String>,
@ -146,7 +139,6 @@ fn run_patch(
headers: call.get_flag(engine_state, stack, "headers")?, headers: call.get_flag(engine_state, stack, "headers")?,
data: call.req(engine_state, stack, 1)?, data: call.req(engine_state, stack, 1)?,
content_type: call.get_flag(engine_state, stack, "content-type")?, content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
raw: call.has_flag("raw"), raw: call.has_flag("raw"),
insecure: call.has_flag("insecure"), insecure: call.has_flag("insecure"),
user: call.get_flag(engine_state, stack, "user")?, user: call.get_flag(engine_state, stack, "user")?,
@ -166,17 +158,16 @@ fn helper(
args: Arguments, args: Arguments,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.patch(url); let mut request = client.patch(&requested_url);
request = request_set_body(args.content_type, args.content_length, args.data, request)?;
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, Some(args.data), args.content_type);
request_handle_response( request_handle_response(
engine_state, engine_state,
stack, stack,

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_body, request_set_timeout, request_handle_response, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -42,12 +42,6 @@ impl Command for SubCommand {
"the MIME type of content to post", "the MIME type of content to post",
Some('t'), Some('t'),
) )
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named( .named(
"max-time", "max-time",
SyntaxShape::Int, SyntaxShape::Int,
@ -127,7 +121,6 @@ struct Arguments {
headers: Option<Value>, headers: Option<Value>,
data: Value, data: Value,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>,
raw: bool, raw: bool,
insecure: bool, insecure: bool,
user: Option<String>, user: Option<String>,
@ -146,7 +139,6 @@ fn run_post(
headers: call.get_flag(engine_state, stack, "headers")?, headers: call.get_flag(engine_state, stack, "headers")?,
data: call.req(engine_state, stack, 1)?, data: call.req(engine_state, stack, 1)?,
content_type: call.get_flag(engine_state, stack, "content-type")?, content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
raw: call.has_flag("raw"), raw: call.has_flag("raw"),
insecure: call.has_flag("insecure"), insecure: call.has_flag("insecure"),
user: call.get_flag(engine_state, stack, "user")?, user: call.get_flag(engine_state, stack, "user")?,
@ -166,17 +158,16 @@ fn helper(
args: Arguments, args: Arguments,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.post(url); let mut request = client.post(&requested_url);
request = request_set_body(args.content_type, args.content_length, args.data, request)?;
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, Some(args.data), args.content_type);
request_handle_response( request_handle_response(
engine_state, engine_state,
stack, stack,

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::network::http::client::{ use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers, http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_body, request_set_timeout, request_handle_response, request_set_timeout, send_request,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -42,12 +42,6 @@ impl Command for SubCommand {
"the MIME type of content to post", "the MIME type of content to post",
Some('t'), Some('t'),
) )
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named( .named(
"max-time", "max-time",
SyntaxShape::Int, SyntaxShape::Int,
@ -127,7 +121,6 @@ struct Arguments {
headers: Option<Value>, headers: Option<Value>,
data: Value, data: Value,
content_type: Option<String>, content_type: Option<String>,
content_length: Option<String>,
raw: bool, raw: bool,
insecure: bool, insecure: bool,
user: Option<String>, user: Option<String>,
@ -146,7 +139,6 @@ fn run_put(
headers: call.get_flag(engine_state, stack, "headers")?, headers: call.get_flag(engine_state, stack, "headers")?,
data: call.req(engine_state, stack, 1)?, data: call.req(engine_state, stack, 1)?,
content_type: call.get_flag(engine_state, stack, "content-type")?, content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
raw: call.has_flag("raw"), raw: call.has_flag("raw"),
insecure: call.has_flag("insecure"), insecure: call.has_flag("insecure"),
user: call.get_flag(engine_state, stack, "user")?, user: call.get_flag(engine_state, stack, "user")?,
@ -166,17 +158,16 @@ fn helper(
args: Arguments, args: Arguments,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = args.url.span()?; let span = args.url.span()?;
let (requested_url, url) = http_parse_url(call, span, args.url)?; let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure); let client = http_client(args.insecure);
let mut request = client.put(url); let mut request = client.put(&requested_url);
request = request_set_body(args.content_type, args.content_length, args.data, request)?;
request = request_set_timeout(args.timeout, request)?; request = request_set_timeout(args.timeout, request)?;
request = request_add_authorization_header(args.user, args.password, request); request = request_add_authorization_header(args.user, args.password, request);
request = request_add_custom_headers(args.headers, request)?; request = request_add_custom_headers(args.headers, request)?;
let response = request.send().and_then(|r| r.error_for_status()); let response = send_request(request, span, Some(args.data), args.content_type);
request_handle_response( request_handle_response(
engine_state, engine_state,
stack, stack,

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_delete_is_success() { fn http_delete_is_success() {
@ -25,10 +24,7 @@ fn http_delete_is_success() {
fn http_delete_failed_due_to_server_error() { fn http_delete_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("DELETE", "/").with_status(400).create();
.mock("DELETE", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_get_is_success() { fn http_get_is_success() {
@ -25,10 +24,7 @@ fn http_get_is_success() {
fn http_get_failed_due_to_server_error() { fn http_get_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("GET", "/").with_status(400).create();
.mock("GET", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_head_is_success() { fn http_head_is_success() {
@ -26,10 +25,7 @@ fn http_head_is_success() {
fn http_head_failed_due_to_server_error() { fn http_head_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("HEAD", "/").with_status(400).create();
.mock("HEAD", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_patch_is_success() { fn http_patch_is_success() {
@ -25,10 +24,7 @@ fn http_patch_is_success() {
fn http_patch_failed_due_to_server_error() { fn http_patch_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("PATCH", "/").with_status(400).create();
.mock("PATCH", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_post_is_success() { fn http_post_is_success() {
@ -25,10 +24,7 @@ fn http_post_is_success() {
fn http_post_failed_due_to_server_error() { fn http_post_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("POST", "/").with_status(400).create();
.mock("POST", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(

View File

@ -1,6 +1,5 @@
use mockito::Server; use mockito::Server;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
use reqwest::StatusCode;
#[test] #[test]
fn http_put_is_success() { fn http_put_is_success() {
@ -25,10 +24,7 @@ fn http_put_is_success() {
fn http_put_failed_due_to_server_error() { fn http_put_failed_due_to_server_error() {
let mut server = Server::new(); let mut server = Server::new();
let _mock = server let _mock = server.mock("PUT", "/").with_status(400).create();
.mock("PUT", "/")
.with_status(StatusCode::BAD_REQUEST.as_u16() as usize)
.create();
let actual = nu!(pipeline( let actual = nu!(pipeline(
format!( format!(