17 unstable releases (4 breaking)

0.7.0 Jul 17, 2024
0.5.8 Jan 26, 2024
0.5.7 Jul 18, 2023
0.5.4 Mar 14, 2023
0.3.3 Mar 29, 2022

#73 in Web programming

Download history 683/week @ 2024-07-30 820/week @ 2024-08-06 745/week @ 2024-08-13 1076/week @ 2024-08-20 953/week @ 2024-08-27 1132/week @ 2024-09-03 848/week @ 2024-09-10 1064/week @ 2024-09-17 1492/week @ 2024-09-24 1472/week @ 2024-10-01 994/week @ 2024-10-08 1198/week @ 2024-10-15 1204/week @ 2024-10-22 963/week @ 2024-10-29 983/week @ 2024-11-05 1437/week @ 2024-11-12

4,714 downloads per month
Used in 7 crates

Apache-2.0

325KB
7.5K SLoC

dav-server-rs

Apache-2.0 licensed Crates.io docs.rs

A fork of the webdav-handler-rs project.

Generic async HTTP/Webdav handler

Webdav (RFC4918) is defined as HTTP (GET/HEAD/PUT/DELETE) plus a bunch of extension methods (PROPFIND, etc). These extension methods are used to manage collections (like unix directories), get information on collections (like unix ls or readdir), rename and copy items, lock/unlock items, etc.

A handler is a piece of code that takes a http::Request, processes it in some way, and then generates a http::Response. This library is a handler that maps the HTTP/Webdav protocol to the filesystem. Or actually, "a" filesystem. Included is an adapter for the local filesystem (localfs), and an adapter for an in-memory filesystem (memfs).

So this library can be used as a handler with HTTP servers like hyper, warp, actix-web, etc. Either as a correct and complete HTTP handler for files (GET/HEAD) or as a handler for the entire Webdav protocol. In the latter case, you can mount it as a remote filesystem: Linux, Windows, macOS can all mount Webdav filesystems.

Backend interfaces.

The backend interfaces are similar to the ones from the Go x/net/webdav package:

The handler in this library works with the standard http types from the http and http_body crates. That means that you can use it straight away with http libraries / frameworks that also work with those types, like hyper. Compatibility modules for actix-web and warp are also provided.

Implemented standards.

Currently passes the "basic", "copymove", "props", "locks" and "http" checks of the Webdav Litmus Test testsuite. That's all of the base RFC4918 webdav specification.

The litmus test suite also has tests for RFC3744 "acl" and "principal", RFC5842 "bind", and RFC3253 "versioning". Those we do not support right now.

The relevant parts of the HTTP RFCs are also implemented, such as the preconditions (If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since, If-Range), partial transfers (Range).

Also implemented is partial PUT, for which there are currently two non-standard ways to do it: PUT with the Content-Range header, which is what Apache's mod_dav implements, and PATCH with the X-Update-Range header from SabreDav.

Backends.

Included are two filesystems:

  • LocalFs: serves a directory on the local filesystem
  • MemFs: ephemeral in-memory filesystem. supports DAV properties.

Also included are two locksystems:

  • MemLs: ephemeral in-memory locksystem.
  • FakeLs: fake locksystem. just enough LOCK/UNLOCK support for macOS/Windows.

External filesystems:

Example.

Example server using hyper that serves the /tmp directory in r/w mode. You should be able to mount this network share from Linux, macOS and Windows. Examples for other frameworks are also available.

use std::convert::Infallible;
use dav_server::{fakels::FakeLs, localfs::LocalFs, DavHandler};

#[tokio::main]
async fn main() {
    let dir = "/tmp";
    let addr = ([127, 0, 0, 1], 4918).into();

    let dav_server = DavHandler::builder()
        .filesystem(LocalFs::new(dir, false, false, false))
        .locksystem(FakeLs::new())
        .build_handler();

    let make_service = hyper::service::make_service_fn(move |_| {
        let dav_server = dav_server.clone();
        async move {
            let func = move |req| {
                let dav_server = dav_server.clone();
                async move {
                    Ok::<_, Infallible>(dav_server.handle(req).await)
                }
            };
            Ok::<_, Infallible>(hyper::service::service_fn(func))
        }
    });

    println!("Serving {} on {}", dir, addr);
    let _ = hyper::Server::bind(&addr)
        .serve(make_service)
        .await
        .map_err(|e| eprintln!("server error: {}", e));
}

Building.

This crate uses std::future::Future and async/await, so it only works with Rust 1.39 and up.

Testing.

RUST_LOG=dav_server=debug cargo run --example sample-litmus-server

This will start a server on port 4918, serving an in-memory filesystem. For other options, run cargo run --example sample-litmus-server -- --help

  • © 2018, 2019, 2020 XS4ALL Internet bv
  • © 2018, 2019, 2020 Miquel van Smoorenburg
  • © 2021 - 2023 Messense Lv
  • Apache License, Version 2.0

Dependencies

~10–23MB
~323K SLoC