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

feat(webdav): add list and improve create #1330

Merged
merged 60 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
9e57129
add initial
imWildCat Feb 8, 2023
253a701
add list response
imWildCat Feb 9, 2023
51f0087
add webdav list_response parser
imWildCat Feb 12, 2023
2b56014
add more response
imWildCat Feb 12, 2023
9660bfb
RFR: making PUT more generic
imWildCat Feb 12, 2023
7124140
progress implementatin
imWildCat Feb 12, 2023
a587aec
add dir_stream
imWildCat Feb 12, 2023
fc278a2
make fields public
imWildCat Feb 12, 2023
651fd39
finish backend
imWildCat Feb 12, 2023
9a7460a
finish
imWildCat Feb 12, 2023
35c713f
rename to DirStream
imWildCat Feb 12, 2023
099049d
add file header
imWildCat Feb 12, 2023
975461e
fix lint
imWildCat Feb 13, 2023
d6fb97a
update comments
imWildCat Feb 13, 2023
9b80cf5
set_capabilities
imWildCat Feb 13, 2023
04dd2e9
address backend feedbacks
imWildCat Feb 13, 2023
0a361f3
refine lock & clean up
imWildCat Feb 13, 2023
3af0ddd
fix logic
imWildCat Feb 13, 2023
f458d12
fix lint: dir_stream.rs
imWildCat Feb 14, 2023
9514fd0
trying to fix
imWildCat Feb 14, 2023
7e64e93
Update src/services/webdav/dir_stream.rs per comment from @ClSlaid
imWildCat Feb 14, 2023
aca39e6
dav_ext_methods PROPFIND;
imWildCat Feb 15, 2023
ca2b122
PROPFIND
imWildCat Feb 15, 2023
ed5ee6e
fix formatting
imWildCat Feb 15, 2023
8c9be54
improve nginx config
imWildCat Feb 15, 2023
006f30d
Install nginx full
Xuanwo Feb 15, 2023
e8af447
Fix apt install
Xuanwo Feb 15, 2023
ca51c84
import module
Xuanwo Feb 15, 2023
08a0bf1
fix lint
imWildCat Feb 15, 2023
3fbc28b
nginx
imWildCat Feb 17, 2023
052baa4
build relative path
imWildCat Feb 17, 2023
34f8698
Merge remote-tracking branch 'upstream/main' into webdav-list-op
imWildCat Feb 17, 2023
1f7b647
trying to fix self.root
imWildCat Feb 17, 2023
59c3123
add default depth for propfind
imWildCat Feb 17, 2023
09d89f8
feat: support auth for HttpBackend (#1359)
Young-Flash Feb 17, 2023
e9e18f6
feat: Add batch delete support (#1357)
Xuanwo Feb 17, 2023
69b40d4
docs: clarify about opendal user defined client (#1356)
ClSlaid Feb 17, 2023
1b2589e
fix(webhdfs): should prepend http:// scheme (#1354)
ClSlaid Feb 17, 2023
538fb18
ci: Pin time <= 0.3.17 until we decide to bump MSRV (#1361)
Xuanwo Feb 17, 2023
9da4832
ci: Only run service test on changing (#1363)
Xuanwo Feb 17, 2023
2ebd0fe
Merge branch 'main' into webdav-list-op
imWildCat Feb 17, 2023
2abeb6a
add auth for propfind
imWildCat Feb 17, 2023
cf9f215
fix list op
imWildCat Feb 17, 2023
1c621af
fix root path
imWildCat Feb 17, 2023
cd67d5d
add xml header
imWildCat Feb 17, 2023
9ed2094
fix prop xml
imWildCat Feb 17, 2023
6b42f6b
remove TODO
imWildCat Feb 17, 2023
b04b300
skip current path while listing
imWildCat Feb 17, 2023
1ff5211
Merge branch 'main' into webdav-list-op
imWildCat Feb 18, 2023
a86f1a8
handle 404 for dir
imWildCat Feb 18, 2023
7f48a5c
add MKCOL to fix mkdir
imWildCat Feb 18, 2023
2e1e966
add MKCOL for dirs
imWildCat Feb 18, 2023
0ad0624
Merge remote-tracking branch 'upstream/main' into webdav-list-op
imWildCat Feb 18, 2023
ab27326
end
imWildCat Feb 18, 2023
ee9d7b4
create dir recursively
imWildCat Feb 18, 2023
ef69945
handle StatusCode::METHOD_NOT_ALLOWED
imWildCat Feb 18, 2023
bf97bcc
fix iteration
imWildCat Feb 18, 2023
aa4d2b4
fix typo
imWildCat Feb 18, 2023
63acd2d
fix dir creation
imWildCat Feb 18, 2023
56a88ea
fix write
imWildCat Feb 18, 2023
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
6 changes: 6 additions & 0 deletions .github/workflows/service_test_webdav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Install nginx full for dav_ext modules
run: sudo apt install nginx-full

- name: Start nginx
shell: bash
run: |
Expand All @@ -51,6 +54,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Install nginx full for dav_ext modules
run: sudo apt install nginx-full

- name: Start nginx
shell: bash
run: |
Expand Down
134 changes: 121 additions & 13 deletions src/services/webdav/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;

use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine;
use bytes::Buf;
use http::header::AUTHORIZATION;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
Expand All @@ -27,8 +24,13 @@ use http::Request;
use http::Response;
use http::StatusCode;
use log::debug;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;

use super::dir_stream::DirStream;
use super::error::parse_error;
use super::list_response::Multistatus;
use crate::ops::*;
use crate::raw::*;
use crate::*;
Expand All @@ -41,7 +43,7 @@ use crate::*;
///
/// - [x] read
/// - [x] write
/// - [ ] list
/// - [x] list
/// - [ ] ~~scan~~
/// - [ ] ~~presign~~
/// - [ ] ~~multipart~~
Expand All @@ -52,10 +54,6 @@ use crate::*;
/// Bazel Remote Caching and Ccache HTTP Storage is also part of this service.
/// Users can use `webdav` to connect those services.
///
/// # Status
///
/// - `list` is not supported so far.
///
/// # Configuration
///
/// - `endpoint`: set the endpoint for webdav
Expand Down Expand Up @@ -274,23 +272,72 @@ impl Debug for WebdavBackend {
impl Accessor for WebdavBackend {
type Reader = IncomingAsyncBody;
type BlockingReader = ();
type Pager = ();
type Pager = DirStream;
imWildCat marked this conversation as resolved.
Show resolved Hide resolved
type BlockingPager = ();

fn metadata(&self) -> AccessorMetadata {
let mut ma = AccessorMetadata::default();
ma.set_scheme(Scheme::Webdav)
.set_root(&self.root)
.set_capabilities(AccessorCapability::Read | AccessorCapability::Write)
.set_capabilities(
AccessorCapability::Read | AccessorCapability::Write | AccessorCapability::List,
)
.set_hints(AccessorHint::ReadStreamable);

ma
}

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Pager)> {
// XML body must start without a new line. Otherwise, the server will panic: `xmlParseChunk() failed`
let all_prop_xml_body = r#"<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:">
<D:allprop/>
</D:propfind>
"#;

let async_body = AsyncBody::Bytes(bytes::Bytes::from(all_prop_xml_body));
let resp = self
.webdav_put(path, Some(0), None, None, AsyncBody::Empty)
.webdav_propfind(path, None, "application/xml".into(), async_body)
.await?;
let status = resp.status();

match status {
StatusCode::OK | StatusCode::MULTI_STATUS => {
let bs = resp.into_body().bytes().await?;
let result: Multistatus =
quick_xml::de::from_reader(bs.reader()).map_err(|err| {
Error::new(ErrorKind::Unexpected, &err.to_string())
.with_context("service", Scheme::Webdav)
})?;

Ok((
RpList::default(),
DirStream::new(&self.root, path, result, args.limit()),
))
}
StatusCode::NOT_FOUND if path.ends_with('/') => Ok((
RpList::default(),
DirStream::new(
&self.root,
path,
Multistatus {
response: Vec::new(),
},
args.limit(),
),
)),
_ => Err(parse_error(resp).await?),
}
}

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let resp = if path.ends_with("/") {
self.webdav_mkcol(path, None, None, AsyncBody::Empty)
imWildCat marked this conversation as resolved.
Show resolved Hide resolved
.await?
} else {
self.webdav_put(path, Some(0), None, None, AsyncBody::Empty)
.await?
};

let status = resp.status();

Expand Down Expand Up @@ -440,6 +487,67 @@ impl WebdavBackend {
self.client.send_async(req).await
}

async fn webdav_mkcol(
&self,
path: &str,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

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

let mut req = Request::builder().method("MKCOL").uri(&url);
if !self.authorization.is_empty() {
req = req.header(AUTHORIZATION, &self.authorization);
}

if let Some(mime) = content_type {
req = req.header(CONTENT_TYPE, mime)
}

if let Some(cd) = content_disposition {
req = req.header(CONTENT_DISPOSITION, cd)
}

let req = req.body(body).map_err(new_request_build_error)?;

self.client.send_async(req).await
}

async fn webdav_propfind(
&self,
path: &str,
size: Option<u64>,
content_type: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));
let mut req = Request::builder()
.method("PROPFIND")
.uri(&url)
.header("Depth", "1");

if !self.authorization.is_empty() {
req = req.header(AUTHORIZATION, &self.authorization);
};

if let Some(size) = size {
req = req.header(CONTENT_LENGTH, size)
}

if let Some(mime) = content_type {
req = req.header(CONTENT_TYPE, mime)
}

let req = req.body(body).map_err(new_request_build_error)?;

self.client.send_async(req).await
}

async fn webdav_head(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
let p = build_rooted_abs_path(&self.root, path);

Expand Down
71 changes: 71 additions & 0 deletions src/services/webdav/dir_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::raw::build_rel_path;
use crate::Result;
use crate::{raw::output, ObjectMetadata, ObjectMode};
use async_trait::async_trait;

use super::list_response::Multistatus;

pub struct DirStream {
root: String,
path: String,
size: usize,
multistates: Multistatus,
}

impl DirStream {
pub fn new(root: &str, path: &str, multistates: Multistatus, limit: Option<usize>) -> Self {
Self {
root: root.into(),
path: path.into(),
size: limit.unwrap_or(1000),
multistates,
}
}
}

#[async_trait]
impl output::Page for DirStream {
async fn next_page(&mut self) -> Result<Option<Vec<output::Entry>>> {
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let mut oes: Vec<output::Entry> = Vec::new();
for _ in 0..self.size {
if let Some(de) = self.multistates.response.pop() {
let path = de.href.clone();
let normalized_path = &if self.root != path {
build_rel_path(&self.root, &path)
} else {
path
};

if normalized_path.eq(&self.path) {
// WebDav server may return the current path as an entry.
continue;
}

let entry = if de.propstat.prop.resourcetype.value
== Some(super::list_response::ResourceType::Collection)
{
output::Entry::new(normalized_path, ObjectMetadata::new(ObjectMode::DIR))
} else {
output::Entry::new(normalized_path, ObjectMetadata::new(ObjectMode::FILE))
};
oes.push(entry);
}
}

Ok(if oes.is_empty() { None } else { Some(oes) })
}
}
5 changes: 4 additions & 1 deletion src/services/webdav/fixtures/nginx-with-basic-auth.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load_module /usr/lib/nginx/modules/ngx_http_dav_ext_module.so;

error_log /tmp/error.log;
pid /tmp/nginx.pid;

Expand All @@ -15,7 +17,8 @@ http {
location / {
client_body_temp_path /tmp;
log_not_found off;
dav_methods PUT DELETE;
dav_methods PUT DELETE MKCOL;
dav_ext_methods PROPFIND;
create_full_put_path on;
client_max_body_size 1024M;
auth_basic "Administrator’s Area";
Expand Down
5 changes: 4 additions & 1 deletion src/services/webdav/fixtures/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load_module /usr/lib/nginx/modules/ngx_http_dav_ext_module.so;

error_log /tmp/error.log;
pid /tmp/nginx.pid;

Expand All @@ -15,7 +17,8 @@ http {
location / {
client_body_temp_path /tmp;
log_not_found off;
dav_methods PUT DELETE;
dav_methods PUT DELETE MKCOL;
dav_ext_methods PROPFIND;
create_full_put_path on;
client_max_body_size 1024M;
}
Expand Down
Loading