mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
seaweed-volume: serve decoded bytes for a range over a compressed needle
Mirrors the Go volume server: a Range with Accept-Encoding: gzip got Content-Encoding: gzip and a slice of the compressed bytes. Pass gzip through only for a whole read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
This commit is contained in:
1 parent
eb77d803d1
commit
bae650770f
2 files changed
+59
-1
No files matched your search
@@ -1342,6 +1342,7 @@ async fn get_or_head_handler_inner(
|
||||
&query,
|
||||
&ext,
|
||||
request_kind.has_image_ops,
|
||||
request_kind.has_range,
|
||||
&mut response_headers,
|
||||
) {
|
||||
ControlFlow::Continue(data) => data,
|
||||
@@ -2039,6 +2040,7 @@ fn buffered_payload(
|
||||
query: &ReadQueryParams,
|
||||
ext: &str,
|
||||
needs_image_ops: bool,
|
||||
has_range: bool,
|
||||
response_headers: &mut HeaderMap,
|
||||
) -> ControlFlow<Response, Vec<u8>> {
|
||||
// Handle compressed data: if needle is compressed, either pass through or decompress
|
||||
@@ -2067,7 +2069,9 @@ fn buffered_payload(
|
||||
.get(header::ACCEPT_ENCODING)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("");
|
||||
if accept_encoding.contains("gzip")
|
||||
// A range is over the decoded bytes, so only a whole read passes gzip through.
|
||||
if !has_range
|
||||
&& accept_encoding.contains("gzip")
|
||||
&& data.len() >= 2
|
||||
&& data[0] == 0x1f
|
||||
&& data[1] == 0x8b
|
||||
|
||||
@@ -1436,3 +1436,57 @@ async fn write_queue_upload_returns_same_etag_as_direct_write() {
|
||||
}
|
||||
assert_eq!(etags[1], etags[0], "unchanged upload ETag differs");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// A Range over a compressed needle is answered from the decoded bytes
|
||||
// ============================================================================
|
||||
|
||||
#[tokio::test]
|
||||
async fn range_with_accept_gzip_on_compressed_needle_serves_decoded_bytes() {
|
||||
let (state, _tmp) = test_state();
|
||||
let uri = "/1/03637037d6/notes.txt";
|
||||
let plain: Vec<u8> = (0..400)
|
||||
.flat_map(|i| format!("line {i:06}: the quick brown fox\n").into_bytes())
|
||||
.collect();
|
||||
|
||||
let app = build_admin_router(state.clone());
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri(uri)
|
||||
.body(Body::from(plain.clone()))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::CREATED);
|
||||
|
||||
let read = |range: Option<&str>| {
|
||||
let mut request = Request::builder()
|
||||
.uri(uri)
|
||||
.header("Accept-Encoding", "gzip");
|
||||
if let Some(range) = range {
|
||||
request = request.header("Range", range);
|
||||
}
|
||||
build_admin_router(state.clone()).oneshot(request.body(Body::empty()).unwrap())
|
||||
};
|
||||
|
||||
// The whole read passes the stored gzip through, so its length is the compressed size.
|
||||
let response = read(None).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.headers()["Content-Encoding"], "gzip");
|
||||
let compressed_len = body_bytes(response).await.len();
|
||||
assert!(compressed_len < plain.len() / 4);
|
||||
|
||||
for (start, end) in [(10, 69), (compressed_len + 10, compressed_len + 69)] {
|
||||
let response = read(Some(&format!("bytes={start}-{end}"))).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
|
||||
assert!(response.headers().get("Content-Encoding").is_none());
|
||||
assert_eq!(
|
||||
response.headers()["Content-Range"],
|
||||
format!("bytes {start}-{end}/{}", plain.len()).as_str()
|
||||
);
|
||||
assert_eq!(body_bytes(response).await, &plain[start..=end]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user