-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
actix-files: Range request for static HTML file results in Content Encoding Error #3191
Comments
I'm experiencing a similar problem with actix-files 0.6.5 and Firefox. The browser fails to display an image when the server returns It's not easy to reproduce. I think Firefox adds the |
I've managed to find a hack workaround: use futures::future::FutureExt;
use actix_web::{dev::Service as _};
app.wrap_fn(|request, service| {
let path = request.path().to_owned();
service.call(request).map(move |response| {
if path.contains("/static") {
response.and_then(|mut response| {
response.headers_mut().remove("Content-Encoding");
Ok(response)
})
} else {
response
}
})
}); I'm serving files from the |
Per RFC 2616 (https://datatracker.ietf.org/doc/html/rfc2616#section-3.5),
Actix is doing the wrong thing here, regardless of how any browser behaves or misbehaves. One could argue that Firefox should silently ignore The most reliable way to reproduce this using Firefox is probably to have
Minimal reproduction
[package]
name = "example"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "=4.5.1"
actix-files = "=0.6.5"
use actix_files::NamedFile;
use actix_web::{get, Responder};
#[get("/")]
async fn index() -> impl Responder {
NamedFile::open("test_file")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
#!/bin/bash
echo foo > test_file
cargo b
cargo r&
sleep 1
nc 127.0.0.1 8080 << EOF
GET / HTTP/1.1
Range: bytes=0-
Accept-Encoding:
EOF
kill %1 Create these files, then run |
I've made a following workaround:
And where you serve the file you make:
Now all |
Expected Behavior
Serving static HTML files via actix-files should allow viewing these files in a web browser.
Current Behavior
Serving static HTML files via actix-files works partially, but under some circumstances, trying to view a file results in a "Content Encoding Error" (tested with Firefox).
Possible Solution
I investigated the error using Firefox' developer tools and it turned out that it occurs if the browser sends a range request for the HTML file. Not being a web developer, I am not aware of why the browser sometimes sends a range request when normally browsing to a file; they seemed to be conditional range requests related to the ETag, which seems like a caching mechanism.
The responses to the range requests contained an HTTP header "Content-Encoding: identity". Looking at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding, "identity" does not appear on this page. Thus, it seems to me that "identity" is not a valid value for the Content-Encoding header, which likely causes the "Content Encoding Error" display.
I had a look at the actix-files code and found that this snippet is responsible for adding that header to replies to range requests:
actix-web/actix-files/src/named.rs
Line 545 in e50bceb
Testing with a patched version of actix-files, commenting out this snippet seemed to solve the issue. However, according to the comment in the code, it seems that this will break cases where compression middleware is used.
I am not familiar with how actix-web works internally, but given the above, it seems to me that a proper solution might be to either:
Steps to Reproduce (for bugs)
Serve static HTML files via actix-files and view them in a browser, having the browser use range requests. It is not clear to me under which conditions browsers use range requests for HTML files.
Context
Serving some static HTML files via actix-files.
Your Environment
rustc -V
): rustc 1.73.0 (cc66ad468 2023-10-03)The text was updated successfully, but these errors were encountered: