Skip to content

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

@jannes

Description

@jannes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug. Something is wrong. This is bad!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions