2020-12-04 14:59:34 +03:00
use anyhow ::Error ;
2019-06-29 11:08:12 +03:00
use futures ::* ;
2020-12-04 14:59:34 +03:00
use hyper ::{ Body , Request , Response } ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
use tokio ::net ::{ TcpListener , TcpStream } ;
2019-06-29 11:08:12 +03:00
2020-01-20 14:52:22 +03:00
fn main ( ) -> Result < ( ) , Error > {
2021-11-19 19:36:06 +03:00
proxmox_async ::runtime ::main ( run ( ) )
2020-01-20 14:52:22 +03:00
}
async fn run ( ) -> Result < ( ) , Error > {
2020-12-04 14:59:34 +03:00
let listener = TcpListener ::bind ( std ::net ::SocketAddr ::from ( ( [ 127 , 0 , 0 , 1 ] , 8008 ) ) ) . await ? ;
2019-06-29 11:08:12 +03:00
println! ( " listening on {:?} " , listener . local_addr ( ) ) ;
2019-12-12 17:27:07 +03:00
loop {
let ( socket , _addr ) = listener . accept ( ) . await ? ;
2020-12-04 14:59:34 +03:00
tokio ::spawn ( handle_connection ( socket ) . map ( | res | {
if let Err ( err ) = res {
eprintln! ( " Error: {} " , err ) ;
}
} ) ) ;
2019-08-28 16:50:40 +03:00
}
}
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
async fn handle_connection ( socket : TcpStream ) -> Result < ( ) , Error > {
socket . set_nodelay ( true ) . unwrap ( ) ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
let mut http = hyper ::server ::conn ::Http ::new ( ) ;
http . http2_only ( true ) ;
// increase window size: todo - find optiomal size
let max_window_size = ( 1 < < 31 ) - 2 ;
http . http2_initial_stream_window_size ( max_window_size ) ;
http . http2_initial_connection_window_size ( max_window_size ) ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
let service = hyper ::service ::service_fn ( | _req : Request < Body > | {
println! ( " Got request " ) ;
let buffer = vec! [ 65 u8 ; 4 * 1024 * 1024 ] ; // nonsense [A,A,A,A...]
let body = Body ::from ( buffer ) ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
let response = Response ::builder ( )
2019-08-28 16:50:40 +03:00
. status ( http ::StatusCode ::OK )
2020-12-04 14:59:34 +03:00
. header ( http ::header ::CONTENT_TYPE , " application/octet-stream " )
. body ( body )
2019-08-28 16:50:40 +03:00
. unwrap ( ) ;
2020-12-04 14:59:34 +03:00
future ::ok ::< _ , Error > ( response )
} ) ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
http . serve_connection ( socket , service )
. map_err ( Error ::from )
. await ? ;
2019-06-29 11:08:12 +03:00
2020-12-04 14:59:34 +03:00
println! ( " H2 connection CLOSE ! " ) ;
2019-06-29 11:08:12 +03:00
Ok ( ( ) )
}