Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Authorization logic for WebdavBackend #1348

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/services/webdav/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ impl Debug for WebdavBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut de = f.debug_struct("Builder");
de.field("endpoint", &self.endpoint);
de.field("username", &self.username);
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
de.field("password", &self.password);
de.field("root", &self.root);

de.finish()
Expand Down Expand Up @@ -247,6 +249,7 @@ impl Debug for WebdavBackend {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Backend")
.field("endpoint", &self.endpoint)
.field("authorization", &self.authorization)
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
.field("root", &self.root)
.field("client", &self.client)
.finish()
Expand Down Expand Up @@ -369,7 +372,11 @@ impl WebdavBackend {

let url = format!("{}{}", self.endpoint, percent_encode_path(&p));

let mut req = Request::get(&url).header(AUTHORIZATION, &self.authorization);
let mut req = if self.authorization.is_empty() {
Request::get(&url)
} else {
Request::get(&url).header(AUTHORIZATION, &self.authorization)
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
};

if !range.is_full() {
req = req.header(http::header::RANGE, range.to_header());
Expand All @@ -393,7 +400,11 @@ impl WebdavBackend {

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));

let mut req = Request::put(&url).header(AUTHORIZATION, &self.authorization);
let mut req = if self.authorization.is_empty() {
Request::put(&url)
} else {
Request::put(&url).header(AUTHORIZATION, &self.authorization)
};

if let Some(size) = size {
req = req.header(CONTENT_LENGTH, size)
Expand All @@ -414,7 +425,11 @@ impl WebdavBackend {

let url = format!("{}{}", self.endpoint, percent_encode_path(&p));

let req = Request::head(&url).header(AUTHORIZATION, &self.authorization);
let req = if self.authorization.is_empty() {
Request::head(&url)
} else {
Request::head(&url).header(AUTHORIZATION, &self.authorization)
};

let req = req
.body(AsyncBody::Empty)
Expand All @@ -428,10 +443,13 @@ impl WebdavBackend {

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));

let req = Request::delete(&url)
.header(AUTHORIZATION, &self.authorization)
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;
let req = if self.authorization.is_empty() {
Request::delete(&url)
} else {
Request::delete(&url).header(AUTHORIZATION, &self.authorization)
}
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

self.client.send_async(req).await
}
Expand Down