Make following redirects explicit in HttpClient::get

This commit is contained in:
Antonio Scandurra 2022-04-27 13:14:45 +02:00
parent 78afbb3599
commit 0e1e5b7d55
7 changed files with 21 additions and 15 deletions

View file

@ -137,6 +137,7 @@ impl AutoUpdater {
.get( .get(
&format!("{server_url}/api/releases/latest?token={ACCESS_TOKEN}&asset=Zed.dmg"), &format!("{server_url}/api/releases/latest?token={ACCESS_TOKEN}&asset=Zed.dmg"),
Default::default(), Default::default(),
true,
) )
.await?; .await?;
@ -173,7 +174,7 @@ impl AutoUpdater {
.map_or_else(|| cx.platform().app_path(), Ok)?; .map_or_else(|| cx.platform().app_path(), Ok)?;
let mut dmg_file = File::create(&dmg_path).await?; let mut dmg_file = File::create(&dmg_path).await?;
let mut response = client.get(&release.url, Default::default()).await?; let mut response = client.get(&release.url, Default::default(), true).await?;
smol::io::copy(response.body_mut(), &mut dmg_file).await?; smol::io::copy(response.body_mut(), &mut dmg_file).await?;
log::info!("downloaded update. path:{:?}", dmg_path); log::info!("downloaded update. path:{:?}", dmg_path);

View file

@ -781,7 +781,7 @@ impl Client {
let http = self.http.clone(); let http = self.http.clone();
cx.background().spawn(async move { cx.background().spawn(async move {
let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL); let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL);
let rpc_response = http.get(&rpc_url, Default::default()).await?; let rpc_response = http.get(&rpc_url, Default::default(), false).await?;
if rpc_response.status().is_redirection() { if rpc_response.status().is_redirection() {
rpc_url = rpc_response rpc_url = rpc_response
.headers() .headers()

View file

@ -1,15 +1,14 @@
pub use anyhow::{anyhow, Result};
use futures::future::BoxFuture; use futures::future::BoxFuture;
use isahc::{ use isahc::{
config::{Configurable, RedirectPolicy}, config::{Configurable, RedirectPolicy},
AsyncBody, AsyncBody,
}; };
use std::sync::Arc;
pub use anyhow::{anyhow, Result};
pub use isahc::{ pub use isahc::{
http::{Method, Uri}, http::{Method, Uri},
Error, Error,
}; };
use std::sync::Arc;
pub use url::Url; pub use url::Url;
pub type Request = isahc::Request<AsyncBody>; pub type Request = isahc::Request<AsyncBody>;
@ -18,9 +17,19 @@ pub type Response = isahc::Response<AsyncBody>;
pub trait HttpClient: Send + Sync { pub trait HttpClient: Send + Sync {
fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result<Response, Error>>; fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result<Response, Error>>;
fn get<'a>(&'a self, uri: &str, body: AsyncBody) -> BoxFuture<'a, Result<Response, Error>> { fn get<'a>(
&'a self,
uri: &str,
body: AsyncBody,
follow_redirects: bool,
) -> BoxFuture<'a, Result<Response, Error>> {
self.send( self.send(
isahc::Request::builder() isahc::Request::builder()
.redirect_policy(if follow_redirects {
RedirectPolicy::Follow
} else {
RedirectPolicy::None
})
.method(Method::GET) .method(Method::GET)
.uri(uri) .uri(uri)
.body(body) .body(body)
@ -30,12 +39,7 @@ pub trait HttpClient: Send + Sync {
} }
pub fn client() -> Arc<dyn HttpClient> { pub fn client() -> Arc<dyn HttpClient> {
Arc::new( Arc::new(isahc::HttpClient::builder().build().unwrap())
isahc::HttpClient::builder()
.redirect_policy(RedirectPolicy::Follow)
.build()
.unwrap(),
)
} }
impl HttpClient for isahc::HttpClient { impl HttpClient for isahc::HttpClient {

View file

@ -253,7 +253,7 @@ impl Contact {
async fn fetch_avatar(http: &dyn HttpClient, url: &str) -> Result<Arc<ImageData>> { async fn fetch_avatar(http: &dyn HttpClient, url: &str) -> Result<Arc<ImageData>> {
let mut response = http let mut response = http
.get(url, Default::default()) .get(url, Default::default(), true)
.await .await
.map_err(|e| anyhow!("failed to send user avatar request: {}", e))?; .map_err(|e| anyhow!("failed to send user avatar request: {}", e))?;

View file

@ -42,7 +42,7 @@ impl super::LspAdapter for CLspAdapter {
if fs::metadata(&binary_path).await.is_err() { if fs::metadata(&binary_path).await.is_err() {
let mut response = http let mut response = http
.get(&version.url, Default::default()) .get(&version.url, Default::default(), true)
.await .await
.context("error downloading release")?; .context("error downloading release")?;
let mut file = File::create(&zip_path).await?; let mut file = File::create(&zip_path).await?;

View file

@ -87,6 +87,7 @@ pub async fn latest_github_release(
.get( .get(
&format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"), &format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),
Default::default(), Default::default(),
true,
) )
.await .await
.context("error fetching latest release")?; .context("error fetching latest release")?;

View file

@ -43,7 +43,7 @@ impl LspAdapter for RustLspAdapter {
if fs::metadata(&destination_path).await.is_err() { if fs::metadata(&destination_path).await.is_err() {
let mut response = http let mut response = http
.get(&version.url, Default::default()) .get(&version.url, Default::default(), true)
.await .await
.map_err(|err| anyhow!("error downloading release: {}", err))?; .map_err(|err| anyhow!("error downloading release: {}", err))?;
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut())); let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));