Skip to content

Commit

Permalink
fix: overwrite request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
daimond113 committed Aug 12, 2024
1 parent 836870f commit 83286ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct LoginCommand {
/// The index to use. Defaults to `default`, or the configured default index if current directory doesn't have a manifest
#[arg(short, long)]
index: Option<String>,

/// The token to use for authentication, skipping login
#[arg(short, long, conflicts_with = "index")]
token: Option<String>,
Expand Down
27 changes: 23 additions & 4 deletions src/source/pesde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use gix::Url;
use relative_path::RelativePathBuf;
use reqwest::header::ACCEPT;
use reqwest::header::{HeaderMap, ACCEPT, AUTHORIZATION};
use serde::{Deserialize, Serialize};

use pkg_ref::PesdePackageRef;
Expand Down Expand Up @@ -275,14 +275,29 @@ impl PackageSource for PesdePackageSource {
.replace("{PACKAGE_VERSION}", &pkg_ref.version.to_string())
.replace("{PACKAGE_TARGET}", &pkg_ref.target.to_string());

let mut response = reqwest.get(url).header(ACCEPT, "application/octet-stream");
let mut headers = HeaderMap::new();
headers.insert(
ACCEPT,
"application/octet-stream"
.parse()
.map_err(|e| errors::DownloadError::InvalidHeaderValue("Accept".to_string(), e))?,
);

if let Some(token) = project.auth_config.get_token(&self.repo_url) {
log::debug!("using token for pesde package download");
response = response.header("Authorization", token);
headers.insert(
AUTHORIZATION,
token.parse().map_err(|e| {
errors::DownloadError::InvalidHeaderValue("Authorization".to_string(), e)
})?,
);
}

let response = response.send()?.error_for_status()?;
let response = reqwest
.get(url)
.headers(headers)
.send()?
.error_for_status()?;
let bytes = response.bytes()?;

let mut decoder = flate2::read::GzDecoder::new(bytes.as_ref());
Expand Down Expand Up @@ -506,5 +521,9 @@ pub mod errors {
/// Error writing index file
#[error("error reading index file")]
ReadIndex(#[source] std::io::Error),

/// A header value was invalid
#[error("invalid header {0} value")]
InvalidHeaderValue(String, #[source] reqwest::header::InvalidHeaderValue),
}
}
27 changes: 23 additions & 4 deletions src/source/wally/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use gix::Url;
use relative_path::RelativePathBuf;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use serde::Deserialize;
use tempfile::tempdir;

Expand Down Expand Up @@ -177,19 +178,33 @@ impl PackageSource for WallyPackageSource {
pkg_ref.version
);

let mut response = reqwest.get(url).header(
let mut headers = HeaderMap::new();
headers.insert(
"Wally-Version",
std::env::var("PESDE_WALLY_VERSION")
.as_deref()
.unwrap_or("0.3.2"),
.unwrap_or("0.3.2")
.parse()
.map_err(|e| {
errors::DownloadError::InvalidHeaderValue("Wally-Version".to_string(), e)
})?,
);

if let Some(token) = project.auth_config.get_token(&self.repo_url) {
log::debug!("using token for wally package download");
response = response.header("Authorization", token);
headers.insert(
AUTHORIZATION,
token.parse().map_err(|e| {
errors::DownloadError::InvalidHeaderValue("Authorization".to_string(), e)
})?,
);
}

let response = response.send()?.error_for_status()?;
let response = reqwest
.get(url)
.headers(headers)
.send()?
.error_for_status()?;
let bytes = response.bytes()?;

let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes))?;
Expand Down Expand Up @@ -340,5 +355,9 @@ pub mod errors {
/// Error writing index file
#[error("error writing index file")]
WriteIndex(#[source] std::io::Error),

/// A header value was invalid
#[error("invalid header {0} value")]
InvalidHeaderValue(String, #[source] reqwest::header::InvalidHeaderValue),
}
}

0 comments on commit 83286ff

Please sign in to comment.