Merge pull request #426 from EdouardVanbelle/refactor/etag-centralize
refactor & normalize etag for Nextcloud + fix NFC string (important fix)
This commit is contained in:
@@ -20,6 +20,7 @@ use crate::application::dtos::folder_dto::FolderDto;
|
||||
use crate::application::dtos::grant_dto::{ResourceContentDto, ResourceTypeDto};
|
||||
use crate::application::ports::favorites_ports::FavoritesUseCase;
|
||||
use crate::application::services::favorites_service::FavoritesService;
|
||||
use crate::domain::entities::file::File;
|
||||
use crate::interfaces::errors::AppError;
|
||||
use crate::interfaces::middleware::auth::AuthUser;
|
||||
|
||||
@@ -253,8 +254,10 @@ pub async fn list_favorites_resources(
|
||||
};
|
||||
|
||||
if row.resource_type == "folder" {
|
||||
let resource_id = row.resource_id.to_string();
|
||||
let dto = FolderDto {
|
||||
id: row.resource_id.to_string(),
|
||||
etag: resource_id.clone(),
|
||||
id: resource_id,
|
||||
name: row.name.clone(),
|
||||
path,
|
||||
parent_id: row.parent_id.map(|u| u.to_string()),
|
||||
@@ -277,6 +280,18 @@ pub async fn list_favorites_resources(
|
||||
.as_deref()
|
||||
.unwrap_or("application/octet-stream");
|
||||
let size_bytes = row.size.max(0) as u64;
|
||||
// Route ETag through `File::compute_etag` so
|
||||
// this listing's `etag` byte-equals what
|
||||
// GET/HEAD/PROPFIND would return for the same
|
||||
// file. `blob_hash` is `None` only for
|
||||
// folder rows, which take the other branch.
|
||||
let modified_at_u = row.modified_at.timestamp() as u64;
|
||||
let content_hash = row.blob_hash.clone().unwrap_or_default();
|
||||
let etag = if content_hash.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
File::compute_etag(&content_hash, modified_at_u)
|
||||
};
|
||||
let dto = FileDto {
|
||||
id: row.resource_id.to_string(),
|
||||
name: row.name.clone(),
|
||||
@@ -285,7 +300,7 @@ pub async fn list_favorites_resources(
|
||||
mime_type: std::sync::Arc::from(mime),
|
||||
folder_id: row.parent_id.map(|u| u.to_string()),
|
||||
created_at: row.resource_created_at.timestamp() as u64,
|
||||
modified_at: row.modified_at.timestamp() as u64,
|
||||
modified_at: modified_at_u,
|
||||
icon_class: std::sync::Arc::from(icon_class_for(&row.name, mime)),
|
||||
icon_special_class: std::sync::Arc::from(icon_special_class_for(
|
||||
&row.name, mime,
|
||||
@@ -294,7 +309,8 @@ pub async fn list_favorites_resources(
|
||||
size_formatted: format_file_size(size_bytes),
|
||||
owner_id: Some(row.owner_id.to_string()),
|
||||
sort_date: None,
|
||||
etag: String::new(),
|
||||
content_hash,
|
||||
etag,
|
||||
};
|
||||
FavoritesResourceItemDto {
|
||||
resource_type: ResourceTypeDto::File,
|
||||
|
||||
@@ -593,7 +593,11 @@ impl FileHandler {
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let etag = format!("\"{}-{}\"", id, file_dto.modified_at);
|
||||
// Route through `FileDto::etag` so this REST download
|
||||
// endpoint, WebDAV/NextCloud GET, HEAD, PROPFIND, and PUT all
|
||||
// emit the same opaque token for the same file — see
|
||||
// `File::etag` for the formula.
|
||||
let etag = format!("\"{}\"", file_dto.etag);
|
||||
|
||||
// ── ETag (304 Not Modified) ──────────────────────────────────
|
||||
if let Some(inm) = headers.get(header::IF_NONE_MATCH)
|
||||
|
||||
@@ -26,6 +26,7 @@ use crate::application::ports::folder_ports::FolderUseCase;
|
||||
use crate::application::ports::trash_ports::TrashUseCase;
|
||||
use crate::application::services::folder_service::FolderService;
|
||||
use crate::common::di::AppState as GlobalAppState;
|
||||
use crate::domain::entities::file::File;
|
||||
use crate::interfaces::errors::AppError;
|
||||
use crate::interfaces::middleware::auth::AuthUser;
|
||||
|
||||
@@ -692,8 +693,10 @@ pub async fn list_folder_resources(
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
if row.resource_type == "folder" {
|
||||
let resource_id = row.id.to_string();
|
||||
let dto = FolderDto {
|
||||
id: row.id.to_string(),
|
||||
etag: resource_id.clone(),
|
||||
id: resource_id,
|
||||
name: row.name.clone(),
|
||||
path: String::new(), // cleared — share recipients must not see hierarchy
|
||||
parent_id: row.parent_id.map(|u| u.to_string()),
|
||||
@@ -715,6 +718,20 @@ pub async fn list_folder_resources(
|
||||
.as_deref()
|
||||
.unwrap_or("application/octet-stream");
|
||||
let size_bytes = row.size.max(0) as u64;
|
||||
// `blob_hash` is `Some(_)` for file rows in the
|
||||
// UNION ALL (`NULL` for folders). Route the
|
||||
// ETag formula through `File::compute_etag` —
|
||||
// the single source of truth shared with
|
||||
// GET/HEAD/PROPFIND/PUT response — so this
|
||||
// listing's `etag` byte-equals what a
|
||||
// conditional request would compare against.
|
||||
let modified_at_u = row.modified_at.timestamp() as u64;
|
||||
let content_hash = row.blob_hash.clone().unwrap_or_default();
|
||||
let etag = if content_hash.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
File::compute_etag(&content_hash, modified_at_u)
|
||||
};
|
||||
let dto = FileDto {
|
||||
id: row.id.to_string(),
|
||||
name: row.name.clone(),
|
||||
@@ -730,7 +747,8 @@ pub async fn list_folder_resources(
|
||||
size_formatted: format_file_size(size_bytes),
|
||||
owner_id: Some(row.owner_id.to_string()),
|
||||
sort_date: None,
|
||||
etag: String::new(),
|
||||
content_hash,
|
||||
etag,
|
||||
};
|
||||
FolderResourceItemDto {
|
||||
resource_type: ResourceTypeDto::File,
|
||||
|
||||
@@ -19,6 +19,7 @@ use crate::application::dtos::recent_dto::{
|
||||
};
|
||||
use crate::application::ports::recent_ports::RecentItemsUseCase;
|
||||
use crate::application::services::recent_service::RecentService;
|
||||
use crate::domain::entities::file::File;
|
||||
use crate::interfaces::errors::AppError;
|
||||
use crate::interfaces::middleware::auth::AuthUser;
|
||||
use uuid::Uuid;
|
||||
@@ -283,8 +284,10 @@ pub async fn list_recent_resources(
|
||||
};
|
||||
|
||||
if row.resource_type == "folder" {
|
||||
let resource_id = row.resource_id.to_string();
|
||||
let dto = FolderDto {
|
||||
id: row.resource_id.to_string(),
|
||||
etag: resource_id.clone(),
|
||||
id: resource_id,
|
||||
name: row.name.clone(),
|
||||
path,
|
||||
parent_id: row.parent_id.map(|u| u.to_string()),
|
||||
@@ -307,6 +310,16 @@ pub async fn list_recent_resources(
|
||||
.as_deref()
|
||||
.unwrap_or("application/octet-stream");
|
||||
let size_bytes = row.size.max(0) as u64;
|
||||
// Route ETag through `File::compute_etag` so this
|
||||
// listing matches GET/HEAD/PROPFIND byte-for-byte
|
||||
// for the same file.
|
||||
let modified_at_u = row.modified_at.timestamp() as u64;
|
||||
let content_hash = row.blob_hash.clone().unwrap_or_default();
|
||||
let etag = if content_hash.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
File::compute_etag(&content_hash, modified_at_u)
|
||||
};
|
||||
let dto = FileDto {
|
||||
id: row.resource_id.to_string(),
|
||||
name: row.name.clone(),
|
||||
@@ -315,7 +328,7 @@ pub async fn list_recent_resources(
|
||||
mime_type: std::sync::Arc::from(mime),
|
||||
folder_id: row.parent_id.map(|u| u.to_string()),
|
||||
created_at: row.resource_created_at.timestamp() as u64,
|
||||
modified_at: row.modified_at.timestamp() as u64,
|
||||
modified_at: modified_at_u,
|
||||
icon_class: std::sync::Arc::from(icon_class_for(&row.name, mime)),
|
||||
icon_special_class: std::sync::Arc::from(icon_special_class_for(
|
||||
&row.name, mime,
|
||||
@@ -324,7 +337,8 @@ pub async fn list_recent_resources(
|
||||
size_formatted: format_file_size(size_bytes),
|
||||
owner_id: Some(row.owner_id.to_string()),
|
||||
sort_date: None,
|
||||
etag: String::new(),
|
||||
content_hash,
|
||||
etag,
|
||||
};
|
||||
RecentResourceItemDto {
|
||||
resource_type: ResourceTypeDto::File,
|
||||
|
||||
@@ -69,6 +69,37 @@ pub(crate) fn encode_uri_path(path: &str) -> String {
|
||||
.join("/")
|
||||
}
|
||||
|
||||
/// Build the `<D:href>` value for a non-collection (file) resource.
|
||||
///
|
||||
/// RFC 4918 §5.2 distinguishes collection (folder) URLs from
|
||||
/// non-collection URLs by a trailing `/`. Files use NO trailing
|
||||
/// slash. Mirror of [`webdav_collection_href`] — keep both arms
|
||||
/// of the choice on the same screen so an "is it a file or a
|
||||
/// folder?" reviewer can verify both branches at once.
|
||||
fn webdav_href(path: &str) -> String {
|
||||
format!("/webdav/{}", encode_uri_path(path))
|
||||
}
|
||||
|
||||
/// Build the `<D:href>` value for a collection (folder) resource.
|
||||
///
|
||||
/// Always terminates with `/` — RFC 4918 §5.2 requires collection
|
||||
/// URLs to end in a slash, and strict WebDAV clients (notably the
|
||||
/// NextCloud desktop sync engine, which also speaks to this
|
||||
/// endpoint) abort multi-status parses with
|
||||
/// `Invalid href "<…>" expected starting with "<requested-url>"`
|
||||
/// when the response's own-entry href is missing the trailing `/`.
|
||||
/// PROPPATCH and LOCK responses on folders MUST use this — using
|
||||
/// [`webdav_href`] for a folder is the bug class this helper
|
||||
/// exists to prevent.
|
||||
fn webdav_collection_href(path: &str) -> String {
|
||||
let h = webdav_href(path);
|
||||
if h.ends_with('/') {
|
||||
h
|
||||
} else {
|
||||
format!("{}/", h)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a custom DAV header since it's not in the standard headers
|
||||
const HEADER_DAV: HeaderName = HeaderName::from_static("dav");
|
||||
const HEADER_LOCK_TOKEN: HeaderName = HeaderName::from_static("lock-token");
|
||||
@@ -353,6 +384,7 @@ async fn handle_propfind(
|
||||
// Root folder
|
||||
let root_folder = FolderDto {
|
||||
id: "root".to_string(),
|
||||
etag: "root".to_string(),
|
||||
name: "".to_string(),
|
||||
path: "".to_string(),
|
||||
parent_id: None,
|
||||
@@ -604,12 +636,35 @@ async fn build_streaming_propfind_response(
|
||||
* @return XML response with property modification results
|
||||
*/
|
||||
async fn handle_proppatch(
|
||||
_state: Arc<AppState>,
|
||||
state: Arc<AppState>,
|
||||
req: Request<Body>,
|
||||
path: String,
|
||||
) -> Result<Response<Body>, AppError> {
|
||||
let _user = extract_user(&req)?;
|
||||
|
||||
// Resolve the target resource type BEFORE consuming the body so
|
||||
// we can pick the correct href shape in the multi-status
|
||||
// response. RFC 4918 §5.2 + strict WebDAV-client parser rules
|
||||
// require a trailing `/` for collection hrefs; emitting
|
||||
// `/webdav/foo` for a folder breaks NC-desktop / Cyberduck /
|
||||
// other multi-status consumers the same way the NC PROPFIND
|
||||
// bug did. An empty / `/` path is the root, always a
|
||||
// collection. A path that resolves to neither file nor folder
|
||||
// (e.g. PROPPATCH on a resource that doesn't exist) defaults
|
||||
// to non-collection — matches the request-line shape the
|
||||
// client used, since collection paths conventionally arrive
|
||||
// with trailing `/` already trimmed by routing.
|
||||
let is_collection = if path.is_empty() || path == "/" {
|
||||
true
|
||||
} else {
|
||||
state
|
||||
.applications
|
||||
.folder_service
|
||||
.get_folder_by_path(&path)
|
||||
.await
|
||||
.is_ok()
|
||||
};
|
||||
|
||||
// Read request body (XML — bounded to 1 MB)
|
||||
let body_bytes = body::to_bytes(req.into_body(), MAX_XML_BODY)
|
||||
.await
|
||||
@@ -635,8 +690,12 @@ async fn handle_proppatch(
|
||||
results.push((prop, true));
|
||||
}
|
||||
|
||||
// Generate response
|
||||
let href = format!("/webdav/{}", encode_uri_path(&path));
|
||||
// Generate response — collection vs file href chosen above.
|
||||
let href = if is_collection {
|
||||
webdav_collection_href(&path)
|
||||
} else {
|
||||
webdav_href(&path)
|
||||
};
|
||||
let mut response_body = Vec::new();
|
||||
WebDavAdapter::generate_proppatch_response(&mut response_body, &href, &results).map_err(
|
||||
|e| AppError::internal_error(format!("Failed to generate PROPPATCH response: {}", e)),
|
||||
@@ -706,7 +765,7 @@ async fn handle_get(
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, &*file.mime_type)
|
||||
.header(header::CONTENT_LENGTH, file.size)
|
||||
.header(header::ETAG, format!("\"{}\"", file.id))
|
||||
.header(header::ETAG, format!("\"{}\"", file.etag))
|
||||
.header(
|
||||
header::LAST_MODIFIED,
|
||||
chrono::DateTime::<Utc>::from_timestamp(file.created_at as i64, 0)
|
||||
@@ -747,7 +806,7 @@ async fn handle_head(
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "httpd/unix-directory")
|
||||
.header(header::CONTENT_LENGTH, 0)
|
||||
.header(header::ETAG, format!("\"{}\"", folder.id))
|
||||
.header(header::ETAG, format!("\"{}\"", folder.etag))
|
||||
.body(Body::empty())
|
||||
.unwrap());
|
||||
}
|
||||
@@ -756,7 +815,7 @@ async fn handle_head(
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, &*file.mime_type)
|
||||
.header(header::CONTENT_LENGTH, file.size)
|
||||
.header(header::ETAG, format!("\"{}\"", file.id))
|
||||
.header(header::ETAG, format!("\"{}\"", file.etag))
|
||||
.header(
|
||||
header::LAST_MODIFIED,
|
||||
chrono::DateTime::<Utc>::from_timestamp(file.created_at as i64, 0)
|
||||
@@ -777,7 +836,7 @@ async fn handle_head(
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "httpd/unix-directory")
|
||||
.header(header::CONTENT_LENGTH, 0)
|
||||
.header(header::ETAG, format!("\"{}\"", folder.id))
|
||||
.header(header::ETAG, format!("\"{}\"", folder.etag))
|
||||
.body(Body::empty())
|
||||
.unwrap());
|
||||
}
|
||||
@@ -793,7 +852,7 @@ async fn handle_head(
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, &*file.mime_type)
|
||||
.header(header::CONTENT_LENGTH, file.size)
|
||||
.header(header::ETAG, format!("\"{}\"", file.id))
|
||||
.header(header::ETAG, format!("\"{}\"", file.etag))
|
||||
.header(
|
||||
header::LAST_MODIFIED,
|
||||
chrono::DateTime::<Utc>::from_timestamp(file.created_at as i64, 0)
|
||||
@@ -1686,6 +1745,24 @@ async fn handle_lock(
|
||||
) -> Result<Response<Body>, AppError> {
|
||||
let user = extract_user(&req)?;
|
||||
|
||||
// Determine collection-vs-file for href shape. Root + known
|
||||
// folders → collection; everything else (existing files,
|
||||
// lock-null on a non-existent path) → file. RFC 4918 §9.10.1
|
||||
// allows LOCK on a non-existent resource (the "lock-null
|
||||
// resource" pattern used by Office save flows) — that arm
|
||||
// falls through to the file href shape, matching the
|
||||
// request-line shape clients send.
|
||||
let is_collection = if path.is_empty() || path == "/" {
|
||||
true
|
||||
} else {
|
||||
state
|
||||
.applications
|
||||
.folder_service
|
||||
.get_folder_by_path(&path)
|
||||
.await
|
||||
.is_ok()
|
||||
};
|
||||
|
||||
// Get the headers that we need
|
||||
let depth = req
|
||||
.headers()
|
||||
@@ -1735,8 +1812,12 @@ async fn handle_lock(
|
||||
AppError::precondition_failed(format!("Lock token not found or expired: {}", token))
|
||||
})?;
|
||||
|
||||
// Generate response
|
||||
let href = format!("/webdav/{}", encode_uri_path(&path));
|
||||
// Generate response — collection vs file href chosen above.
|
||||
let href = if is_collection {
|
||||
webdav_collection_href(&path)
|
||||
} else {
|
||||
webdav_href(&path)
|
||||
};
|
||||
let mut response_body = Vec::new();
|
||||
WebDavAdapter::generate_lock_response(&mut response_body, &entry.info, &href).map_err(
|
||||
|e| AppError::internal_error(format!("Failed to generate LOCK response: {}", e)),
|
||||
@@ -1771,8 +1852,12 @@ async fn handle_lock(
|
||||
))
|
||||
})?;
|
||||
|
||||
// Generate response
|
||||
let href = format!("/webdav/{}", encode_uri_path(&path));
|
||||
// Generate response — collection vs file href chosen above.
|
||||
let href = if is_collection {
|
||||
webdav_collection_href(&path)
|
||||
} else {
|
||||
webdav_href(&path)
|
||||
};
|
||||
let mut response_body = Vec::new();
|
||||
WebDavAdapter::generate_lock_response(&mut response_body, &entry.info, &href).map_err(
|
||||
|e| AppError::internal_error(format!("Failed to generate LOCK response: {}", e)),
|
||||
@@ -1836,3 +1921,49 @@ async fn handle_unlock(
|
||||
.body(Body::empty())
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_webdav_href_no_trailing_slash() {
|
||||
assert_eq!(
|
||||
webdav_href("Documents/report.pdf"),
|
||||
"/webdav/Documents/report.pdf"
|
||||
);
|
||||
assert_eq!(webdav_href("file.txt"), "/webdav/file.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_webdav_collection_href_appends_slash_when_missing() {
|
||||
assert_eq!(webdav_collection_href("Documents"), "/webdav/Documents/");
|
||||
assert_eq!(
|
||||
webdav_collection_href("Documents/subfolder"),
|
||||
"/webdav/Documents/subfolder/"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_webdav_collection_href_idempotent_when_already_slashed() {
|
||||
// `encode_uri_path` never emits a trailing `/` of its own
|
||||
// because the path argument is already trimmed by routing,
|
||||
// but the helper still has to be robust to a path that
|
||||
// happens to end in `/` — exercise the idempotence path.
|
||||
assert_eq!(webdav_collection_href("Documents/"), "/webdav/Documents/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_webdav_href_preserves_url_encoding() {
|
||||
// Spaces and Unicode must percent-encode at the segment level,
|
||||
// not get a verbatim `%20` re-encoded as `%2520`.
|
||||
assert_eq!(
|
||||
webdav_href("My Photos/vacation pic.jpg"),
|
||||
"/webdav/My%20Photos/vacation%20pic.jpg"
|
||||
);
|
||||
assert_eq!(
|
||||
webdav_collection_href("My Photos/2024"),
|
||||
"/webdav/My%20Photos/2024/"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user