Skip to content

Commit

Permalink
use Url::path_segments_mut to create container & blob URLs
Browse files Browse the repository at this point in the history
This moves away from using `format`, which causes blob names to not be
URL encoded appropriately.

This should address Azure#1350
  • Loading branch information
Brian Caswell committed Sep 13, 2023
1 parent b06278e commit b9c6ceb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
24 changes: 18 additions & 6 deletions sdk/storage_blobs/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ impl BlobClient {

/// Full URL for the blob.
pub fn url(&self) -> azure_core::Result<url::Url> {
let blob_name = self
.blob_name()
.strip_prefix('/')
.unwrap_or_else(|| self.blob_name());
let url = format!("{}/{}", self.container_client().url()?, blob_name);
Ok(url::Url::parse(&url)?)
let mut url = self.container_client().url()?;
let parts = self.blob_name().trim_matches('/').split('/');
url.path_segments_mut()
.map_err(|_| Error::message(ErrorKind::DataConversion, "Invalid url"))?
.extend(parts);
Ok(url)
}

pub(crate) fn finalize_request(
Expand Down Expand Up @@ -399,5 +399,17 @@ mod tests {
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d?fake_token"
);

let url = build_url("a", "/b/c/d", &sas);
assert_eq!(
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d?fake_token"
);

let url = build_url("a", "b/c/d/hi there", &sas);
assert_eq!(
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d/hi%20there?fake_token"
);
}
}
17 changes: 5 additions & 12 deletions sdk/storage_blobs/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,11 @@ impl ContainerClient {

/// Full URL for the container.
pub fn url(&self) -> azure_core::Result<url::Url> {
let container_name = self
.container_name()
.strip_prefix('/')
.unwrap_or_else(|| self.container_name());
let sep = if self.service_client.url()?.path().ends_with('/') {
""
} else {
"/"
};

let url = format!("{}{}{}", self.service_client.url()?, sep, container_name);
Ok(url::Url::parse(&url)?)
let mut url = self.service_client.url()?;
url.path_segments_mut()
.map_err(|_| Error::message(ErrorKind::DataConversion, "Invalid url"))?
.push(self.container_name());
Ok(url)
}

pub(crate) fn credentials(&self) -> &StorageCredentials {
Expand Down

0 comments on commit b9c6ceb

Please sign in to comment.