Skip to content

Commit f3fbc3d

Browse files
authored
fix: expose content-range header (#93)
otherwise, browsers cannot access it, because it's not among the CORS-safelisted response headers: https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header ## What I'm changing Responses to [range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests) already include the [`Content-Range`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Range) header. But browsers cannot read its value, because it's not among the [CORS-safelisted response headers](https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header). With this PR, the responses also include the header `Access-Control-Expose-Headers: Content-Range`, to allow browsers to access the `Content-Range` header. ## How I did it I add the header, when the request was a range request. ## How to test it Go to https://observablehq.com/@severo/bug-in-source-coop-content-range-is-not-accessible Or test the following in a browser (not in Node.js) ```js function test(url) { return fetch(url, { headers: { range: "bytes=0-100" } }).then((r) => r.headers.get("Content-Range") === "bytes 0-100/70940" ? "Success": "Error" ); } ``` This function should return "Success" for any data file hosted by the proxy. For example: ```js test("https://data.source.coop/severo/csv-papaparse-test-files/verylong-sample.csv") // "Success" // by comparison: test("https://huggingface.co/datasets/severo/csv-papaparse-test-files/resolve/main/verylong-sample.csv") // "Success", since HF allows accessing this header test("https://s3.us-west-2.amazonaws.com/us-west-2.opendata.source.coop/severo/csv-papaparse-test-files/verylong-sample.csv") // "Error", since S3 does not allow accessing this header ``` ## PR Checklist - [x] This PR has **no** breaking changes. - [ ] I have updated or added new tests to cover the changes in this PR. > I'm not a Rust developer, and the main.rs file has no tests, so: I don't feel comfortable creating tests in that file from scratch. - [ ] This PR affects the [Source Cooperative Frontend & API](https://github.com/source-cooperative/source.coop), and I have opened issue/PR #XXX to track the change. ## Related Issues <!-- Reference any existing related GitHub Issues -->
1 parent 13d92a1 commit f3fbc3d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,17 @@ async fn get_object(
123123
.insert_header(("ETag", res.etag));
124124

125125
if is_range_request {
126-
response = response.insert_header((
127-
"Content-Range",
128-
format!(
129-
"bytes {}-{}/{}",
130-
range_start,
131-
range_start + res.content_length - 1,
132-
content_length
133-
),
134-
));
126+
response = response
127+
.insert_header((
128+
"Content-Range",
129+
format!(
130+
"bytes {}-{}/{}",
131+
range_start,
132+
range_start + res.content_length - 1,
133+
content_length
134+
),
135+
))
136+
.insert_header(("Access-Control-Expose-Headers", "Content-Range"));
135137
}
136138

137139
Ok(response.body(streaming_response))

0 commit comments

Comments
 (0)