Skip to content
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

Server incorrectly adds content-length: 0 to Http1 Head responses #2835

Closed
jannes opened this issue May 18, 2022 · 1 comment · Fixed by #2836
Closed

Server incorrectly adds content-length: 0 to Http1 Head responses #2835

jannes opened this issue May 18, 2022 · 1 comment · Fixed by #2836
Labels
C-bug Category: bug. Something is wrong. This is bad!

Comments

@jannes
Copy link
Contributor

jannes commented May 18, 2022

Version
0.14.18

Platform

Darwin C02DN4K3MD6R 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64 i386 MacBookPro16,1 Darwin

Description
Hyper incorrectly sets content-length: 0 on Head responses without content-length.
According to https://www.ietf.org/archive/id/draft-ietf-httpbis-semantics-19.html#name-content-length:
"A server MAY send a Content-Length header field in a response to a HEAD request (Section 9.3.2); a server MUST NOT send Content-Length in such a response unless its field value equals the decimal number of octets that would have been sent in the content of a response if the same request had used the GET method."

I tried this code:

use std::net::SocketAddr;

use hyper::{
    header::HOST,
    service::{make_service_fn, service_fn},
    Body, Client, Request, Server, Version,
};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {

    let on_request = service_fn(|r| {
        let uri = format!("http://{}", r.headers()[HOST].to_str().unwrap());
        let req = Request::builder()
            .method(r.method())
            .uri(uri)
            .version(Version::HTTP_11)
            .body(Body::empty())
            .unwrap();
        let client = Client::new();
        client.request(req)
    });
    let make_svc = make_service_fn(move |_| async move { Ok::<_, Error>(on_request) });

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    let server = Server::bind(&addr).serve(make_svc);

    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }

    Ok(())
}

and

curl http://localhost:3000 -H "Host: eaufavor.net" -I
curl http://eaufavor.net -I     

I expected to see this happen:
Both curl responses have no content-length header.

Instead, this happened:
The response for the request that goes through hyper has content-length: 0

@jannes jannes added the C-bug Category: bug. Something is wrong. This is bad! label May 18, 2022
@jannes jannes changed the title Server adds content-length: 0 to H1 Head responses without explicit content-length Server adds content-length: 0 to Http1 Head responses May 18, 2022
@jannes jannes changed the title Server adds content-length: 0 to Http1 Head responses Server incorrectly adds content-length: 0 to Http1 Head responses May 18, 2022
@jannes
Copy link
Contributor Author

jannes commented May 18, 2022

related to #2427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug. Something is wrong. This is bad!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant