DAV storage methods

This commit is contained in:
mdecimus
2025-03-06 18:35:46 +01:00
parent 3554dea4cb
commit eadd36f4cb
35 changed files with 1289 additions and 206 deletions

View File

@@ -10,13 +10,11 @@ use http_body_util::BodyExt;
use crate::HttpRequest;
#[inline]
pub fn decode_path_element(item: &str) -> Cow<'_, str> {
// Bit hackish but avoids an extra dependency
form_urlencoded::parse(item.as_bytes())
.into_iter()
.next()
.map(|(k, _)| k)
.unwrap_or_else(|| item.into())
percent_encoding::percent_decode_str(item)
.decode_utf8()
.unwrap_or_else(|_| item.into())
}
pub async fn fetch_body(

View File

@@ -36,6 +36,21 @@ impl HttpResponse {
self
}
pub fn with_content_length(mut self, content_length: usize) -> Self {
self.builder = self.builder.header(header::CONTENT_LENGTH, content_length);
self
}
pub fn with_etag(mut self, etag: u64) -> Self {
self.builder = self.builder.header(header::ETAG, format!("\"{etag}\""));
self
}
pub fn with_last_modified(mut self, last_modified: String) -> Self {
self.builder = self.builder.header(header::LAST_MODIFIED, last_modified);
self
}
pub fn with_header<K, V>(mut self, name: K, value: V) -> Self
where
K: TryInto<HeaderName>,
@@ -47,18 +62,23 @@ impl HttpResponse {
self
}
pub fn with_xml_body(self, body: impl Into<String>) -> Self {
self.with_text_body(body)
.with_content_type("application/xml; charset=utf-8")
}
pub fn with_text_body(mut self, body: impl Into<String>) -> Self {
let body = body.into();
let body_len = body.len();
self.body = HttpResponseBody::Text(body);
self.with_header(header::CONTENT_LENGTH, body_len)
self.with_content_length(body_len)
}
pub fn with_binary_body(mut self, body: impl Into<Vec<u8>>) -> Self {
let body = body.into();
let body_len = body.len();
self.body = HttpResponseBody::Binary(body);
self.with_header(header::CONTENT_LENGTH, body_len)
self.with_content_length(body_len)
}
pub fn with_stream_body(
@@ -94,13 +114,18 @@ impl HttpResponse {
self
}
pub fn with_no_cache(mut self) -> Self {
pub fn with_no_store(mut self) -> Self {
self.builder = self
.builder
.header(header::CACHE_CONTROL, "no-store, no-cache, must-revalidate");
self
}
pub fn with_no_cache(mut self) -> Self {
self.builder = self.builder.header(header::CACHE_CONTROL, "no-cache");
self
}
pub fn with_location<V>(mut self, location: V) -> Self
where
V: TryInto<HeaderValue>,
@@ -170,7 +195,7 @@ impl<T: serde::Serialize> ToHttpResponse for JsonResponse<T> {
.with_text_body(serde_json::to_string(&self.inner).unwrap_or_default());
if self.no_cache {
response.with_no_cache()
response.with_no_store()
} else {
response
}