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
PUSH example #412
Comments
This comment has been minimized.
This comment has been minimized.
Could it be that you're ending the initial stream (second argument to
and then pushing a request on it? Try pushing before sending end of stream. |
The error becomes CANCEL. Sending only the PUSH, the error is still a CANCEL. Reverting the operations order, sending first the PUSH and then the standard response, there is no error, but the client doesn't see the PUSH, only the standard response. |
I used wireshark to look at the data transmitted (sorry for not thinking about it before). |
What exactly don't you see being transmitted? Perhaps your client doesn't support push messages properly. See When using |
The client I'm using is the client example from this crate, improved with push promises:
|
Ok, so, having your working example, turns out the only thing to change in my server was the building of the PUSH Request, using the incoming URI as base:
And the client was working correctly. |
The example works now, and I have a new problem: Adding another push request will result an error. |
use h2::server;
use bytes::*;
use http::uri;
use http::{Request, Response, StatusCode};
use std::error::Error;
use tokio::net::{TcpListener, TcpStream};
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
let _ = env_logger::try_init();
let mut listener = TcpListener::bind("127.0.0.1:5928").await?;
println!("listening on {:?}", listener.local_addr());
loop {
if let Ok((socket, _peer_addr)) = listener.accept().await {
tokio::spawn(async move {
if let Err(e) = handle(socket).await {
println!(" -> err={:?}", e);
}
});
}
}
}
async fn handle(socket: TcpStream) -> Result<(), Box<dyn Error>> {
let mut connection = server::handshake(socket).await?;
println!("H2 connection bound");
while let Some(result) = connection.accept().await {
let (request, mut respond) = result?;
println!("GOT request: {:?}", request);
let response = Response::builder().status(StatusCode::OK).body(()).unwrap();
let mut pushed_uri_parts: uri::Parts = request.into_parts().0.uri.into();
pushed_uri_parts.path_and_query = uri::PathAndQuery::from_static("/pushed").into();
let uri1 = uri::Uri::from_parts(pushed_uri_parts).unwrap();
println!("uri1 {}", uri1);
let uri2 = uri::Uri::from_static("http://127.0.0.1:5928/pushed2");
//let uri2 = uri::Uri::from_static("https://http2.akamai.com/pushed2");
println!("uri2 {}", uri2);
let pushed_req = Request::builder()
.uri(uri1)
.body(())
.unwrap();
let pushed_req2 = Request::builder()
.uri(uri2)
.body(())
.unwrap();
let pushed_rsp = http::Response::builder().status(200).body(()).unwrap();
let pushed_rsp2 = http::Response::builder().status(200).body(()).unwrap();
let mut send_pushed = respond
.push_request(pushed_req)
.unwrap()
.send_response(pushed_rsp, false)
.unwrap();
let mut send_pushed2 = respond
.push_request(pushed_req2)
.unwrap()
.send_response(pushed_rsp2, false)
.unwrap();
let mut send = respond.send_response(response, false)?;
println!(">>>> pushing data");
send_pushed.send_data(Bytes::from_static(b"Pushed data!\n"), false)?;
send_pushed2.send_data(Bytes::from_static(b"Another Pushed data!\n"), false)?;
println!(">>>> sending data");
send.send_data(Bytes::from_static(b"hello world"), true)?;
}
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
Ok(())
} Using nghttp to get a response >nghttp http://127.0.0.1:5928
Some requests were not processed. total=1, processed=0 The error from the server says:
|
FWIW, this was where the overflow happened back in 2019: h2/src/proto/streams/streams.rs Line 1269 in fac165e
|
That issue was fixed with #479, this issue can probably be closed. |
Hellom
I'm trying to use the new push_promise feature, I edited the server example to reply a index.html page and push a script.js file, but it ends up with a PROTOCOL_ERROR and I'm unable to debug it:
Can you please help me creating a working PUSH example?
Thank you
The text was updated successfully, but these errors were encountered: