Skip to content

Commit

Permalink
Use url instead of http.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Sep 27, 2024
1 parent 2d1cc7f commit 60b91c6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 48 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ ed25519-dalek = { workspace = true }
reqwest = { version = "0.12.7", features = ["json", "blocking", "stream"] }
jsonrpsee-http-client = "0.20.1"
jsonrpsee-core = "0.20.1"
http = "1.1.0"
regex = "1.6.0"
wasm-opt = { version = "0.114.0", optional = true }
chrono = { version = "0.4.27", features = ["serde"]}
chrono = { version = "0.4.27", features = ["serde"] }
rpassword = "7.2.0"
dirs = "4.0.0"
toml = "0.5.9"
Expand All @@ -106,12 +105,12 @@ gix = { version = "0.58.0", default-features = false, features = [
"blocking-http-transport-reqwest-rust-tls",
"worktree-mutation",
] }
async-compression = { version = "0.4.12", features = [ "tokio", "gzip" ] }
async-compression = { version = "0.4.12", features = ["tokio", "gzip"] }

tempfile = "3.8.1"
toml_edit = "0.21.0"
rust-embed = { version = "8.2.0", features = ["debug-embed"] }
bollard = { workspace=true }
bollard = { workspace = true }
futures-util = "0.3.30"
futures = "0.3.30"
home = "0.5.9"
Expand All @@ -125,7 +124,12 @@ open = "5.3.0"
url = "2.5.2"

[target.'cfg(unix)'.dependencies]
reqwest = { version = "0.12.7", features = ["json", "blocking", "stream", "native-tls-vendored"] }
reqwest = { version = "0.12.7", features = [
"json",
"blocking",
"stream",
"native-tls-vendored",
] }

[build-dependencies]
crate-git-revision = "0.0.4"
Expand Down
2 changes: 0 additions & 2 deletions cmd/soroban-cli/src/commands/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ pub enum Error {
#[error("network arg or rpc url and network passphrase are required if using the network")]
Network,
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
Rpc(#[from] rpc::Error),
#[error(transparent)]
HttpClient(#[from] reqwest::Error),
Expand Down
22 changes: 11 additions & 11 deletions cmd/soroban-cli/src/commands/snapshot/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use async_compression::tokio::bufread::GzipDecoder;
use bytesize::ByteSize;
use clap::{arg, Parser, ValueEnum};
use futures::StreamExt;
use http::Uri;
use humantime::format_duration;
use itertools::{Either, Itertools};
use reqwest::Url;
use sha2::{Digest, Sha256};
use soroban_ledger_snapshot::LedgerSnapshot;
use std::{
Expand Down Expand Up @@ -86,7 +86,7 @@ pub struct Cmd {
network: config::network::Args,
/// Archive URL
#[arg(long, help_heading = HEADING_RPC, env = "STELLAR_ARCHIVE_URL")]
archive_url: Option<Uri>,
archive_url: Option<Url>,
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -102,7 +102,7 @@ pub enum Error {
#[error("opening cached bucket to read: {0}")]
ReadOpeningCachedBucket(io::Error),
#[error("parsing bucket url: {0}")]
ParsingBucketUrl(http::uri::InvalidUri),
ParsingBucketUrl(url::ParseError),
#[error("getting bucket: {0}")]
GettingBucket(reqwest::Error),
#[error("getting bucket: got status code {0}")]
Expand Down Expand Up @@ -366,7 +366,7 @@ impl Cmd {
Ok(())
}

fn archive_url(&self) -> Result<http::Uri, Error> {
fn archive_url(&self) -> Result<Url, Error> {
// Return the configured archive URL, or if one is not configured, guess
// at an appropriate archive URL given the network passphrase.
self.archive_url
Expand All @@ -384,7 +384,7 @@ impl Cmd {
passphrase::LOCAL => Some("http://localhost:8000/archive"),
_ => None,
}
.map(|s| Uri::from_str(s).expect("archive url valid"))
.map(|s| Url::from_str(s).expect("archive url valid"))
})
})
.ok_or(Error::ArchiveUrlNotConfigured)
Expand All @@ -393,7 +393,7 @@ impl Cmd {

async fn get_history(
print: &print::Print,
archive_url: &Uri,
archive_url: &Url,
ledger: Option<u32>,
) -> Result<History, Error> {
let archive_url = archive_url.to_string();
Expand All @@ -407,11 +407,11 @@ async fn get_history(
} else {
format!("{archive_url}/.well-known/stellar-history.json")
};
let history_url = Uri::from_str(&history_url).unwrap();
let history_url = Url::from_str(&history_url).unwrap();

print.globe(format!("Downloading history {history_url}"));

let response = reqwest::get(&history_url.to_string())
let response = reqwest::get(history_url.as_str())
.await
.map_err(Error::DownloadingHistory)?;

Expand Down Expand Up @@ -445,7 +445,7 @@ async fn get_history(

async fn cache_bucket(
print: &print::Print,
archive_url: &Uri,
archive_url: &Url,
bucket_index: usize,
bucket: &str,
) -> Result<PathBuf, Error> {
Expand All @@ -460,9 +460,9 @@ async fn cache_bucket(

print.globe(format!("Downloading bucket {bucket_index} {bucket}…"));

let bucket_url = Uri::from_str(&bucket_url).map_err(Error::ParsingBucketUrl)?;
let bucket_url = Url::from_str(&bucket_url).map_err(Error::ParsingBucketUrl)?;

let response = reqwest::get(&bucket_url.to_string())
let response = reqwest::get(bucket_url.as_str())
.await
.map_err(Error::GettingBucket)?;

Expand Down
14 changes: 7 additions & 7 deletions cmd/soroban-cli/src/config/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::rpc::{GetTransactionResponse, GetTransactionResponseRaw, SimulateTransactionResponse};
use directories::ProjectDirs;
use http::Uri;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

Expand All @@ -15,7 +15,7 @@ pub enum Error {
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Http(#[from] http::uri::InvalidUri),
InvalidUrl(#[from] url::ParseError),
#[error(transparent)]
Ulid(#[from] ulid::DecodeError),
#[error(transparent)]
Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn bucket_dir() -> Result<std::path::PathBuf, Error> {
Ok(dir)
}

pub fn write(action: Action, rpc_url: &Uri) -> Result<ulid::Ulid, Error> {
pub fn write(action: Action, rpc_url: &Url) -> Result<ulid::Ulid, Error> {
let data = Data {
action,
rpc_url: rpc_url.to_string(),
Expand All @@ -67,10 +67,10 @@ pub fn write(action: Action, rpc_url: &Uri) -> Result<ulid::Ulid, Error> {
Ok(id)
}

pub fn read(id: &ulid::Ulid) -> Result<(Action, Uri), Error> {
pub fn read(id: &ulid::Ulid) -> Result<(Action, Url), Error> {
let file = actions_dir()?.join(id.to_string()).with_extension("json");
let data: Data = serde_json::from_str(&std::fs::read_to_string(file)?)?;
Ok((data.action, http::Uri::from_str(&data.rpc_url)?))
Ok((data.action, Url::from_str(&data.rpc_url)?))
}

pub fn write_spec(hash: &str, spec_entries: &[xdr::ScSpecEntry]) -> Result<(), Error> {
Expand Down Expand Up @@ -117,7 +117,7 @@ pub fn list_actions() -> Result<Vec<DatedAction>, Error> {
.collect::<Result<Vec<_>, Error>>()
}

pub struct DatedAction(ulid::Ulid, Action, Uri);
pub struct DatedAction(ulid::Ulid, Action, Url);

impl std::fmt::Display for DatedAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {
fn test_write_read() {
let t = assert_fs::TempDir::new().unwrap();
std::env::set_var(XDG_DATA_HOME, t.path().to_str().unwrap());
let rpc_uri = http::uri::Uri::from_str("http://localhost:8000").unwrap();
let rpc_uri = Url::from_str("http://localhost:8000").unwrap();
let sim = SimulateTransactionResponse::default();
let original_action: Action = sim.into();

Expand Down
66 changes: 45 additions & 21 deletions cmd/soroban-cli/src/config/network.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::str::FromStr;

use clap::arg;
use phf::phf_map;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::str::FromStr;
use stellar_strkey::ed25519::PublicKey;

use crate::{
Expand All @@ -21,8 +21,6 @@ pub enum Error {
#[error("network arg or rpc url and network passphrase are required if using the network")]
Network,
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
Rpc(#[from] rpc::Error),
#[error(transparent)]
HttpClient(#[from] reqwest::Error),
Expand Down Expand Up @@ -106,28 +104,24 @@ pub struct Network {
}

impl Network {
pub async fn helper_url(&self, addr: &str) -> Result<http::Uri, Error> {
use http::Uri;
pub async fn helper_url(&self, addr: &str) -> Result<Url, Error> {
tracing::debug!("address {addr:?}");
let rpc_uri = Uri::from_str(&self.rpc_url)
let rpc_url = Url::from_str(&self.rpc_url)
.map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))?;
if self.network_passphrase.as_str() == passphrase::LOCAL {
let auth = rpc_uri.authority().unwrap().clone();
let scheme = rpc_uri.scheme_str().unwrap();
Ok(Uri::builder()
.authority(auth)
.scheme(scheme)
.path_and_query(format!("/friendbot?addr={addr}"))
.build()?)
let mut local_url = rpc_url;
local_url.set_path("/friendbot");
local_url.set_query(Some(&format!("addr={addr}")));
Ok(local_url)
} else {
let client = Client::new(&self.rpc_url)?;
let network = client.get_network().await?;
tracing::debug!("network {network:?}");
let uri = client.friendbot_url().await?;
tracing::debug!("URI {uri:?}");
Uri::from_str(&format!("{uri}?addr={addr}")).map_err(|e| {
let url = client.friendbot_url().await?;
tracing::debug!("URL {url:?}");
Url::from_str(&format!("{url}?addr={addr}")).map_err(|e| {
tracing::error!("{e}");
Error::InvalidUrl(uri.to_string())
Error::InvalidUrl(url.to_string())
})
}
}
Expand All @@ -136,7 +130,7 @@ impl Network {
pub async fn fund_address(&self, addr: &PublicKey) -> Result<(), Error> {
let uri = self.helper_url(&addr.to_string()).await?;
tracing::debug!("URL {uri:?}");
let response = reqwest::get(uri.to_string()).await?;
let response = reqwest::get(uri.as_str()).await?;

let request_successful = response.status().is_success();
let body = response.bytes().await?;
Expand All @@ -161,8 +155,8 @@ impl Network {
Ok(())
}

pub fn rpc_uri(&self) -> Result<http::Uri, Error> {
http::Uri::from_str(&self.rpc_url).map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))
pub fn rpc_uri(&self) -> Result<Url, Error> {
Url::from_str(&self.rpc_url).map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))
}
}

Expand Down Expand Up @@ -194,3 +188,33 @@ impl From<&(&str, &str)> for Network {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_helper_url_local_network() {
let network = Network {
rpc_url: "http://localhost:8000".to_string(),
network_passphrase: passphrase::LOCAL.to_string(),
};

let result = network
.helper_url("GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI")
.await;

assert!(result.is_ok());
let url = result.unwrap();
assert_eq!(url.as_str(), "http://localhost:8000/friendbot?addr=GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI");
}

#[tokio::test]
async fn test_helper_url_test_network() {
// It might be a bit difficult to conduct client mock here.
let friendbot_url = "https://friendbot.stellar.org/secret_key";
let addr = "GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
let url = Url::from_str(&format!("{friendbot_url}?addr={addr}")).unwrap();
assert_eq!(url.as_str(), "https://friendbot.stellar.org/secret_key?addr=GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI");
}
}
5 changes: 4 additions & 1 deletion cmd/soroban-cli/src/upgrade_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ async fn fetch_latest_crate_info() -> Result<Crate, Box<dyn Error>> {
.default_headers({
// crates.io requires a User-Agent header
let mut headers = reqwest::header::HeaderMap::new();
headers.insert("User-Agent", reqwest::header::HeaderValue::from_static(crate_name));
headers.insert(
"User-Agent",
reqwest::header::HeaderValue::from_static(crate_name),
);
headers
})
.build()?;
Expand Down

0 comments on commit 60b91c6

Please sign in to comment.