host: expose harness state and actions over the world API
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
//! A world HTTP client that forwards each response chunk without curl's body buffer.
|
||||
use std::io::Write;
|
||||
use wasi::http::{outgoing_handler, types::*};
|
||||
fn main() {
|
||||
let request = OutgoingRequest::new(Fields::new());
|
||||
request.set_method(&Method::Get).unwrap();
|
||||
request.set_scheme(Some(&Scheme::Http)).unwrap();
|
||||
request.set_authority(Some("host.sausage")).unwrap();
|
||||
request
|
||||
.set_path_with_query(Some(&std::env::args().nth(1).unwrap()))
|
||||
.unwrap();
|
||||
let outgoing = request.body().unwrap();
|
||||
let future = outgoing_handler::handle(request, None).unwrap();
|
||||
OutgoingBody::finish(outgoing, None).unwrap();
|
||||
future.subscribe().block();
|
||||
let response = future.get().unwrap().unwrap().unwrap();
|
||||
assert_eq!(response.status(), 200);
|
||||
let incoming = response.consume().unwrap();
|
||||
let stream = incoming.stream().unwrap();
|
||||
let mut output = std::io::stdout().lock();
|
||||
loop {
|
||||
stream.subscribe().block();
|
||||
match stream.read(65536) {
|
||||
Ok(bytes) => {
|
||||
output.write_all(&bytes).unwrap();
|
||||
output.flush().unwrap();
|
||||
}
|
||||
Err(wasi::io::streams::StreamError::Closed) => break,
|
||||
Err(error) => panic!("stream: {error:?}"),
|
||||
}
|
||||
}
|
||||
drop(stream);
|
||||
let trailers = IncomingBody::finish(incoming);
|
||||
trailers.subscribe().block();
|
||||
trailers.get().unwrap().unwrap().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user