Skip to content

Commit

Permalink
refacto: use Url in upstream_base_url config and ensure trailing '/'
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed Jul 3, 2023
1 parent d717766 commit 29c399d
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct EncryptConfig {

#[derive(Debug, Clone)]
pub struct HttpConfig {
pub upstream_base_url: String,
pub upstream_base_url: Url,
pub keyring: Keyring,
pub chunk_size: usize,
pub address: SocketAddr,
Expand Down Expand Up @@ -121,14 +121,16 @@ impl Config {
)
});

let upstream_base_url = match &args.flag_upstream_url {
let raw_upstream_base_url = match &args.flag_upstream_url {
Some(upstream_url) => Some(upstream_url.to_string()),
None => Some(env::var("DS_UPSTREAM_URL").expect(
"Missing upstream_url, use DS_UPSTREAM_URL env or --upstream-url cli argument",
)),
}
.unwrap();

let upstream_base_url = normalize_and_parse_upstream_url(raw_upstream_base_url);

let address = match &args.flag_address {
Some(address) => match address.to_socket_addrs() {
Ok(mut sockets) => Some(sockets.next().unwrap()),
Expand All @@ -155,6 +157,18 @@ impl Config {
}
}

// ensure upstream_url ends with a "/ to avoid
// upstream url: "https://upstream/dir"
// request: "https://proxy/file"
// "https://upstream/dir".join('file') => https://upstream/file
// instead ".../upstream/dir/".join('file') => https://upstream/dir/file
fn normalize_and_parse_upstream_url(mut url: String) -> Url {
if !url.ends_with('/') {
url.push('/');
}
Url::parse(&url).unwrap()
}

impl HttpConfig {
pub fn create_upstream_url(&self, req: &HttpRequest) -> String {
let base = Url::parse(self.upstream_base_url.as_ref()).unwrap();
Expand Down Expand Up @@ -189,6 +203,14 @@ mod tests {
use super::*;
use actix_web::test::TestRequest;

#[test]
fn test_normalize_and_parse_upstream_url() {
assert_eq!(
normalize_and_parse_upstream_url("https://upstream.com/dir".to_string()),
Url::parse("https://upstream.com/dir/").unwrap()
);
}

#[test]
fn test_create_upstream_url() {
let req = TestRequest::default()
Expand Down Expand Up @@ -228,7 +250,7 @@ mod tests {
HttpConfig {
keyring,
chunk_size: DEFAULT_CHUNK_SIZE,
upstream_base_url: upstream_base_url.to_string(),
upstream_base_url: normalize_and_parse_upstream_url(upstream_base_url.to_string()),
address: "127.0.0.1:1234".to_socket_addrs().unwrap().next().unwrap(),
local_encryption_directory: PathBuf::from(DEFAULT_LOCAL_ENCRYPTION_DIRECTORY),
}
Expand Down

0 comments on commit 29c399d

Please sign in to comment.