From 6d72d15b3ba579e1d911bc7dada4aa5a0cb35977 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Wed, 28 Jun 2023 11:09:59 +0100 Subject: [PATCH] Fill downstream_client_request_id in Instead of returning an empty string, use the request id to mimic prod closer. Prompted by @GeeWee in https://github.com/fastly/Viceroy/pull/276. --- lib/src/wiggle_abi/req_impl.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/src/wiggle_abi/req_impl.rs b/lib/src/wiggle_abi/req_impl.rs index ceb62274..e730bb6a 100644 --- a/lib/src/wiggle_abi/req_impl.rs +++ b/lib/src/wiggle_abi/req_impl.rs @@ -89,14 +89,33 @@ impl FastlyHttpReq for Session { } } - #[allow(unused_variables)] // FIXME FGS 2023-06-14: Remove this directive once implemented. fn downstream_client_request_id( &mut self, reqid_out: &GuestPtr, reqid_max_len: u32, nwritten_out: &GuestPtr, ) -> Result<(), Error> { - nwritten_out.write(0)?; + let reqid_bytes = format!("{:032x}", self.req_id()).into_bytes(); + + if reqid_bytes.len() > reqid_max_len as usize { + // Write out the number of bytes necessary to fit the value, or zero on overflow to + // signal an error condition. + nwritten_out.write(reqid_bytes.len().try_into().unwrap_or(0))?; + return Err(Error::BufferLengthError { + buf: "reqid_out", + len: "reqid_max_len", + }); + } + + let reqid_len = + u32::try_from(reqid_bytes.len()).expect("smaller u32::MAX means it must fit"); + + let mut reqid_slice = reqid_out + .as_array(reqid_len) + .as_slice_mut()? + .ok_or(Error::SharedMemory)?; + reqid_slice.copy_from_slice(&reqid_bytes); + nwritten_out.write(reqid_len)?; Ok(()) }