summaryrefslogtreecommitdiffstats
path: root/third_party/rust/h2/examples/server.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/h2/examples/server.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/h2/examples/server.rs')
-rw-r--r--third_party/rust/h2/examples/server.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/third_party/rust/h2/examples/server.rs b/third_party/rust/h2/examples/server.rs
new file mode 100644
index 0000000000..6d6490db08
--- /dev/null
+++ b/third_party/rust/h2/examples/server.rs
@@ -0,0 +1,65 @@
+use std::error::Error;
+
+use bytes::Bytes;
+use h2::server::{self, SendResponse};
+use h2::RecvStream;
+use http::Request;
+use tokio::net::{TcpListener, TcpStream};
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
+ let _ = env_logger::try_init();
+
+ let 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) = serve(socket).await {
+ println!(" -> err={:?}", e);
+ }
+ });
+ }
+ }
+}
+
+async fn serve(socket: TcpStream) -> Result<(), Box<dyn Error + Send + Sync>> {
+ let mut connection = server::handshake(socket).await?;
+ println!("H2 connection bound");
+
+ while let Some(result) = connection.accept().await {
+ let (request, respond) = result?;
+ tokio::spawn(async move {
+ if let Err(e) = handle_request(request, respond).await {
+ println!("error while handling request: {}", e);
+ }
+ });
+ }
+
+ println!("~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
+ Ok(())
+}
+
+async fn handle_request(
+ mut request: Request<RecvStream>,
+ mut respond: SendResponse<Bytes>,
+) -> Result<(), Box<dyn Error + Send + Sync>> {
+ println!("GOT request: {:?}", request);
+
+ let body = request.body_mut();
+ while let Some(data) = body.data().await {
+ let data = data?;
+ println!("<<<< recv {:?}", data);
+ let _ = body.flow_control().release_capacity(data.len());
+ }
+
+ let response = http::Response::new(());
+ let mut send = respond.send_response(response, false)?;
+ println!(">>>> send");
+ send.send_data(Bytes::from_static(b"hello "), false)?;
+ send.send_data(Bytes::from_static(b"world\n"), true)?;
+
+ Ok(())
+}