hyperium / h2

HTTP 2.0 client & server implementation for Rust.
MIT License
1.34k stars 266 forks source link

Modifying the `akamai` example to send to `www.google.com` and adding a `host` header and errors #715

Closed hatoo closed 9 months ago

hatoo commented 9 months ago

This is very weird to me, I'm not sure this is an issue of h2.

I modified the akamai example to send a request to www.google.com which works, and added a host header host: www.google.com then broke.

use h2::client;
use http::{Method, Request};
use tokio::net::TcpStream;
use tokio_rustls::TlsConnector;

use tokio_rustls::rustls::{OwnedTrustAnchor, RootCertStore, ServerName};

use std::convert::TryFrom;
use std::error::Error;
use std::net::ToSocketAddrs;

const ALPN_H2: &str = "h2";

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
    let _ = env_logger::try_init();

    let tls_client_config = std::sync::Arc::new({
        let mut root_store = RootCertStore::empty();
        root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
            OwnedTrustAnchor::from_subject_spki_name_constraints(
                ta.subject,
                ta.spki,
                ta.name_constraints,
            )
        }));

        let mut c = tokio_rustls::rustls::ClientConfig::builder()
            .with_safe_defaults()
            .with_root_certificates(root_store)
            .with_no_client_auth();
        c.alpn_protocols.push(ALPN_H2.as_bytes().to_owned());
        c
    });

    // Sync DNS resolution.
    let addr = "www.google.com:443"
        .to_socket_addrs()
        .unwrap()
        .next()
        .unwrap();

    println!("ADDR: {:?}", addr);

    let tcp = TcpStream::connect(&addr).await?;
    let dns_name = ServerName::try_from("www.google.com").unwrap();
    let connector = TlsConnector::from(tls_client_config);
    let res = connector.connect(dns_name, tcp).await;
    let tls = res.unwrap();
    {
        let (_, session) = tls.get_ref();
        let negotiated_protocol = session.alpn_protocol();
        assert_eq!(Some(ALPN_H2.as_bytes()), negotiated_protocol);
    }

    println!("Starting client handshake");
    let (mut client, h2) = client::handshake(tls).await?;

    println!("building request");
    let request = Request::builder()
        .method(Method::GET)
        .uri("https://www.google.com/")
        .header(http::header::HOST, "www.google.com") // works without this line
        .body(())
        .unwrap();

    println!("sending request");
    let (response, other) = client.send_request(request, true).unwrap();

    tokio::spawn(async move {
        if let Err(e) = h2.await {
            println!("GOT ERR={:?}", e);
        }
    });

    println!("waiting on response : {:?}", other);
    let (_, mut body) = response.await?.into_parts();
    println!("processing body");
    while let Some(chunk) = body.data().await {
        println!("RX: {:?}", chunk?);
    }
    Ok(())
}
❯ cargo run --example akamai
   Compiling h2 v0.3.21 (/home/hatoo/h2)
    Finished dev [unoptimized + debuginfo] target(s) in 1.45s
     Running `target/debug/examples/akamai`
ADDR: 142.250.207.36:443
Starting client handshake
building request
sending request
waiting on response : SendStream { inner: StreamRef { opaque: OpaqueStreamRef { stream_id: StreamId(1), ref_count: 2 }, send_buffer: SendBuffer { inner: Mutex { data: Buffer { slab: Slab { len: 1, cap: 4 } }, poisoned: false, .. } } } }
Error: Error { kind: Reset(StreamId(1), PROTOCOL_ERROR, Remote) }

However, curl succeeded it.

curl -v -H 'host: www.google.com'  https://www.google.com
...
> GET / HTTP/2
> Host: www.google.com
> user-agent: curl/7.81.0
> accept: */*
>
...
it works...

Again, I have no idea whether it's a h2's issue or www.google.com simply broken. But I think it's worth reporting here, because www.google.com is so popular, so it's likely other people will face the same issue.

Thanks.

seanmonstar commented 9 months ago

The remote is sending a reset error, most likely it doesn't like that you included a host header, which is deprecated in http2.

curl filters that header out for you, h2 sends exactly what you ask.

hatoo commented 9 months ago

I'm glad to hear that host header is deprecated in http2.

Thank you!