1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::time::{SystemTime, UNIX_EPOCH};
use headers::Header;
use http::Method as httpMethod;
use crate::body::Body;
use crate::errors::DavError;
use crate::DavResult;
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[repr(u32)]
pub enum Method {
Head = 0x0001,
Get = 0x0002,
Put = 0x0004,
Patch = 0x0008,
Options = 0x0010,
PropFind = 0x0020,
PropPatch = 0x0040,
MkCol = 0x0080,
Copy = 0x0100,
Move = 0x0200,
Delete = 0x0400,
Lock = 0x0800,
Unlock = 0x1000,
}
pub(crate) fn dav_method(m: &http::Method) -> DavResult<Method> {
let m = match m {
&httpMethod::HEAD => Method::Head,
&httpMethod::GET => Method::Get,
&httpMethod::PUT => Method::Put,
&httpMethod::PATCH => Method::Patch,
&httpMethod::DELETE => Method::Delete,
&httpMethod::OPTIONS => Method::Options,
_ => {
match m.as_str() {
"PROPFIND" => Method::PropFind,
"PROPPATCH" => Method::PropPatch,
"MKCOL" => Method::MkCol,
"COPY" => Method::Copy,
"MOVE" => Method::Move,
"LOCK" => Method::Lock,
"UNLOCK" => Method::Unlock,
_ => {
return Err(DavError::UnknownMethod);
},
}
},
};
Ok(m)
}
#[derive(Clone, Copy)]
pub struct AllowedMethods(u32);
impl AllowedMethods {
pub fn all() -> AllowedMethods {
AllowedMethods(0xffffffff)
}
pub fn none() -> AllowedMethods {
AllowedMethods(0)
}
pub fn add(&mut self, m: Method) -> &Self {
self.0 |= m as u32;
self
}
pub fn remove(&mut self, m: Method) -> &Self {
self.0 &= !(m as u32);
self
}
pub fn allowed(&self, m: Method) -> bool {
self.0 & (m as u32) > 0
}
}
pub(crate) fn notfound() -> http::Response<Body> {
http::Response::builder()
.status(404)
.header("connection", "close")
.body(Body::from("Not Found"))
.unwrap()
}
pub(crate) fn dav_xml_error(body: &str) -> Body {
let xml = format!(
"{}\n{}\n{}\n{}\n",
r#"<?xml version="1.0" encoding="utf-8" ?>"#, r#"<D:error xmlns:D="DAV:">"#, body, r#"</D:error>"#
);
Body::from(xml)
}
pub(crate) fn systemtime_to_timespec(t: SystemTime) -> time::Timespec {
match t.duration_since(UNIX_EPOCH) {
Ok(t) => {
time::Timespec {
sec: t.as_secs() as i64,
nsec: 0,
}
},
Err(_) => time::Timespec { sec: 0, nsec: 0 },
}
}
pub(crate) fn systemtime_to_httpdate(t: SystemTime) -> String {
let d = headers::Date::from(t);
let mut v = Vec::new();
d.encode(&mut v);
v[0].to_str().unwrap().to_owned()
}
pub(crate) fn systemtime_to_rfc3339(t: SystemTime) -> String {
let ts = systemtime_to_timespec(t);
format!("{}", time::at_utc(ts).rfc3339())
}