fix: overwrite request headers

This commit is contained in:
daimond113 2024-08-12 23:16:26 +02:00
parent 836870f1ce
commit 83286ff758
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
3 changed files with 47 additions and 9 deletions

View file

@ -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>,

View file

@ -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;
@ -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());
@ -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),
}
}

View file

@ -5,6 +5,7 @@ use std::{
use gix::Url;
use relative_path::RelativePathBuf;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use serde::Deserialize;
use tempfile::tempdir;
@ -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))?;
@ -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),
}
}