Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add trailers implementation for responses.
  • Loading branch information
brendandburns committed Apr 5, 2023
commit e24232fba50723f90facd6d5194ca687c7146ef8
18 changes: 18 additions & 0 deletions crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bytes::{BufMut, Bytes, BytesMut};
use http_body_util::{BodyExt, Full};
use hyper::Method;
use hyper::Request;
use std::collections::HashMap;
#[cfg(not(any(target_arch = "riscv64", target_arch = "s390x")))]
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -201,6 +202,23 @@ impl WasiHttp {
if let Some(chunk) = frame.data_ref() {
buf.put(chunk.clone());
}
if let Some(trailers) = frame.trailers_ref() {
response.trailers = self.fields_id_base;
self.fields_id_base += 1;
let mut map: HashMap<String, Vec<String>> = HashMap::new();
for (name, value) in trailers.iter() {
let key = name.to_string();
match map.get_mut(&key) {
Some(vec) => vec.push(value.to_str()?.to_string()),
None => {
let mut vec = Vec::new();
vec.push(value.to_str()?.to_string());
map.insert(key, vec);
}
};
}
self.fields.insert(response.trailers, map);
}
}
response.body = self.streams_id_base;
self.streams_id_base = self.streams_id_base + 1;
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/streams_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::poll::Pollable;
use crate::streams::{InputStream, OutputStream, StreamError};
use crate::WasiHttp;
use anyhow::{anyhow, bail};
use std::vec::Vec;
use bytes::BufMut;
use std::vec::Vec;

impl crate::streams::Host for WasiHttp {
fn read(
Expand Down Expand Up @@ -67,10 +67,10 @@ impl crate::streams::Host for WasiHttp {
new.put(data.clone());
new.put(bytes::Bytes::from(buf.clone()));
self.streams.insert(this, new.freeze());
},
}
None => {
self.streams.insert(this, bytes::Bytes::from(buf.clone()));
},
}
}
Ok(Ok(buf.len().try_into()?))
}
Expand Down
2 changes: 2 additions & 0 deletions crates/wasi-http/src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct ActiveResponse {
pub status: u16,
pub body: u32,
pub response_headers: HashMap<String, Vec<String>>,
pub trailers: u32,
}

impl ActiveRequest {
Expand All @@ -60,6 +61,7 @@ impl ActiveResponse {
status: 0,
body: 0,
response_headers: HashMap::new(),
trailers: 0,
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ impl crate::types::Host for WasiHttp {
self.fields.insert(id, m.clone());
Ok(id)
}
fn finish_incoming_stream(&mut self, _s: IncomingStream) -> wasmtime::Result<Option<Trailers>> {
bail!("unimplemented: finish_incoming_stream")
fn finish_incoming_stream(&mut self, s: IncomingStream) -> wasmtime::Result<Option<Trailers>> {
for (_, value) in self.responses.iter() {
if value.body == s {
return match value.trailers {
0 => Ok(None),
_ => Ok(Some(value.trailers)),
};
}
}
bail!("unknown stream!")
}
fn finish_outgoing_stream(
&mut self,
Expand Down