-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): added proxy support during packages installation (#2535)
Migrated to `isahc` crate that uses `libcurl`, which gives us more consistent package installation including support of `http_proxy` and `https_proxy` environment variables. Fixes #2517
- Loading branch information
Alexander Galibey
committed
Aug 5, 2022
1 parent
9eb34e4
commit 3bd9aaa
Showing
15 changed files
with
272 additions
and
663 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,95 @@ | ||
use std::io::{ErrorKind, Error as IoError}; | ||
use async_h1::client; | ||
use http_types::{Error, Request, Response, StatusCode}; | ||
use std::fmt::Debug; | ||
use http::uri::Scheme; | ||
use isahc::{AsyncBody, Request, Response}; | ||
use tracing::{debug, error, instrument}; | ||
use crate::error::HttpError; | ||
|
||
#[instrument( | ||
skip(request), | ||
fields(url = %request.url()) | ||
fields(uri = %request.uri()) | ||
)] | ||
pub async fn execute(request: Request) -> Result<Response, Error> { | ||
pub async fn execute<B: Into<AsyncBody> + Debug>( | ||
request: Request<B>, | ||
) -> Result<Response<AsyncBody>, HttpError> { | ||
debug!(?request, "Executing http request:"); | ||
|
||
if request.url().scheme() != "https" { | ||
if request.uri().scheme() != Some(&Scheme::HTTPS) { | ||
error!("CLI http executor only accepts https!"); | ||
return Err(IoError::new(ErrorKind::InvalidInput, "Must use https").into()); | ||
return Err(HttpError::InvalidInput("Must use https".to_string())); | ||
} | ||
|
||
let host = request | ||
.url() | ||
.host_str() | ||
.ok_or_else(|| Error::from_str(StatusCode::BadRequest, "missing hostname"))? | ||
.uri() | ||
.host() | ||
.ok_or_else(|| HttpError::InvalidInput("missing hostname".to_string()))? | ||
.to_string(); | ||
debug!(%host, "Valid hostname:"); | ||
|
||
let addr: (&str, u16) = (&host, request.url().port_or_known_default().unwrap_or(443)); | ||
let tcp_stream = fluvio_future::net::TcpStream::connect(addr).await?; | ||
debug!("Established TCP stream"); | ||
let tls_connector = create_tls().await; | ||
debug!("Created TLS connector"); | ||
let tls_stream = tls_connector.connect(host, tcp_stream).await?; | ||
debug!("Opened TLS stream from TCP stream"); | ||
let response = client::connect(tls_stream, request).await?; | ||
let response = isahc::send_async(request).await?; | ||
|
||
debug!(?response, "Http response:"); | ||
Ok(response) | ||
} | ||
async fn create_tls() -> fluvio_future::native_tls::TlsConnector { | ||
fluvio_future::native_tls::TlsConnector::default() | ||
|
||
pub async fn read_to_end(response: Response<AsyncBody>) -> std::io::Result<Vec<u8>> { | ||
use futures::io::AsyncReadExt; | ||
let mut async_body = response.into_body(); | ||
let mut body = Vec::with_capacity(async_body.len().unwrap_or_default() as usize); | ||
async_body.read_to_end(&mut body).await?; | ||
Ok(body) | ||
} | ||
|
||
#[cfg(test)] | ||
#[fluvio_future::test] | ||
async fn test_web_request() { | ||
use fluvio_index::HttpAgent; | ||
use http_types::StatusCode; | ||
let agent = HttpAgent::default(); | ||
let index = agent.request_index().expect("Failed to get request index"); | ||
let response = execute(index).await.expect("Failed to execute request"); | ||
assert_eq!(response.status(), StatusCode::Ok); | ||
mod tests { | ||
use super::*; | ||
|
||
#[fluvio_future::test] | ||
async fn test_web_request() { | ||
use fluvio_index::HttpAgent; | ||
use http::StatusCode; | ||
let agent = HttpAgent::default(); | ||
let index = agent.request_index().expect("Failed to get request index"); | ||
let response = execute(index).await.expect("Failed to execute request"); | ||
assert_eq!(response.status(), StatusCode::OK); | ||
} | ||
|
||
#[fluvio_future::test] | ||
async fn test_https_required() { | ||
//given | ||
let request = Request::get("http://fluvio.io") | ||
.body(()) | ||
.expect("valid url"); | ||
|
||
//when | ||
let result = execute(request).await; | ||
|
||
//then | ||
assert!(matches!(result, Err(HttpError::InvalidInput(_)))); | ||
} | ||
|
||
#[fluvio_future::test] | ||
async fn test_read_empty_body() { | ||
//given | ||
let body = AsyncBody::empty(); | ||
let response = Response::new(body); | ||
|
||
//when | ||
let result = read_to_end(response).await.expect("read body"); | ||
|
||
//then | ||
assert!(result.is_empty()); | ||
} | ||
|
||
#[fluvio_future::test] | ||
async fn test_read_body() { | ||
//given | ||
let body = AsyncBody::from("content"); | ||
let response = Response::new(body); | ||
|
||
//when | ||
let result = read_to_end(response).await.expect("read body"); | ||
|
||
//then | ||
assert_eq!(&result, b"content"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "fluvio-extension-common" | ||
version = "0.8.1" | ||
version = "0.8.2" | ||
edition = "2021" | ||
authors = ["Fluvio Contributors <[email protected]>"] | ||
description = "Fluvio extension common" | ||
|
@@ -27,4 +27,4 @@ thiserror = "1.0.20" | |
semver = { version = "1.0.0", features = ["serde"] } | ||
|
||
fluvio = { version = "0.13.0", path = "../fluvio", optional = true } | ||
fluvio-package-index = { version = "0.6", path = "../fluvio-package-index" } | ||
fluvio-package-index = { path = "../fluvio-package-index" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "fluvio-package-index" | ||
version = "0.6.1" | ||
version = "0.7.0" | ||
authors = ["Fluvio Contributors <[email protected]>"] | ||
description = "Fluvio Package Index" | ||
repository = "https://github.com/infinyon/fluvio" | ||
|
@@ -12,6 +12,9 @@ build = "build.rs" | |
name = "fluvio_index" | ||
path = "src/lib.rs" | ||
|
||
[features] | ||
http_agent = ["http"] | ||
|
||
[dependencies] | ||
tracing = "0.1.21" | ||
thiserror = "1.0.21" | ||
|
@@ -20,4 +23,4 @@ serde_json = "1.0.59" | |
semver = { version = "1.0.0", features = ["serde"] } | ||
url = { version = "2.1.1", features = ["serde"] } | ||
lazy_static = "1.4.0" | ||
http-types = "2.6.0" | ||
http = { version = "0.2", default-features = false, optional = true} |
Oops, something went wrong.