Skip to content

Commit

Permalink
Update for review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Jul 26, 2024
1 parent 9ab65ba commit 656a5a9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
4 changes: 3 additions & 1 deletion crates/adapter/src/fastly/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,9 @@ pub mod fastly_http_req {
nwritten: *mut usize,
) -> FastlyStatus {
alloc_result!(region_out, region_max_len, nwritten, {
fastly::api::http_req::downstream_compliance_region()
fastly::api::http_req::downstream_compliance_region(
u64::try_from(region_max_len).trapping_unwrap(),
)
})
}

Expand Down
13 changes: 11 additions & 2 deletions lib/src/component/http_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,17 @@ impl http_req::Host for Session {
Err(Error::NotAvailable("Client TLS JA4 hash").into())
}

async fn downstream_compliance_region(&mut self) -> Result<Vec<u8>, types::Error> {
Ok(Vec::from(b"none"))
async fn downstream_compliance_region(
&mut self,
region_max_len: u64,
) -> Result<Vec<u8>, types::Error> {
let region = Session::downstream_compliance_region(self);
let region_len = region.len();

match u64::try_from(region_len) {
Ok(region_len) if region_len <= region_max_len => Ok(region.into()),
too_large => Err(types::Error::BufferLen(too_large.unwrap_or(0))),
}
}

async fn original_header_names_get(
Expand Down
13 changes: 13 additions & 0 deletions lib/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ use {
tokio::sync::oneshot::Sender,
};

const REGION_NONE: &[u8] = b"none";

/// Data specific to an individual request, including any host-side
/// allocations on behalf of the guest processing the request.
pub struct Session {
/// The downstream IP address and port for this session.
downstream_client_addr: SocketAddr,
/// The IP address and port that received this session.
downstream_server_addr: SocketAddr,
/// The compliance region that this request was received in.
///
/// For now this is just always `"none"`, but we place the field in the session
/// to make it easier to implement custom configuration values later on.
downstream_compliance_region: Vec<u8>,
/// Handle for the downstream request "parts". NB the backing parts data can be mutated
/// or even removed from the relevant map.
downstream_req_handle: RequestHandle,
Expand Down Expand Up @@ -165,6 +172,7 @@ impl Session {
Session {
downstream_server_addr: server_addr,
downstream_client_addr: client_addr,
downstream_compliance_region: Vec::from(REGION_NONE),
downstream_req_handle,
downstream_req_body_handle,
downstream_req_original_headers,
Expand Down Expand Up @@ -204,6 +212,11 @@ impl Session {
self.downstream_server_addr.ip()
}

/// Retrieve the compliance region that received the request for this session.
pub fn downstream_compliance_region(&self) -> &[u8] {
self.downstream_compliance_region.as_slice()
}

/// Retrieve the handle corresponding to the downstream request.
pub fn downstream_request(&self) -> RequestHandle {
self.downstream_req_handle
Expand Down
30 changes: 16 additions & 14 deletions lib/src/wiggle_abi/req_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,25 @@ impl FastlyHttpReq for Session {
region_max_len: u32,
nwritten_out: GuestPtr<u32>,
) -> Result<(), Error> {
const REGION_NONE: &[u8] = b"none";
const REGION_NONE_LEN: u32 = 4;
let region = Session::downstream_compliance_region(self);
let region_len = region.len();

if region_max_len < REGION_NONE_LEN {
// Let the guest know how much we want to write.
memory.write(nwritten_out, REGION_NONE_LEN)?;
match u32::try_from(region_len) {
Ok(region_len) if region_len <= region_max_len => {
memory.copy_from_slice(region, region_out.as_array(region_max_len))?;
memory.write(nwritten_out, region_len.try_into().unwrap_or(0))?;

return Err(Error::BufferLengthError {
buf: "region_out",
len: "region_max_len",
});
}

memory.copy_from_slice(REGION_NONE, region_out.as_array(region_max_len))?;
memory.write(nwritten_out, REGION_NONE_LEN)?;
Ok(())
}
too_large => {
memory.write(nwritten_out, too_large.unwrap_or(0))?;

Ok(())
Err(Error::BufferLengthError {
buf: "region_out",
len: "region_max_len",
})
}
}
}

fn framing_headers_mode_set(
Expand Down
2 changes: 1 addition & 1 deletion lib/wit/deps/fastly/compute.wit
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ interface http-req {

downstream-tls-ja4: func(max-len: u64) -> result<list<u8>, error>;

downstream-compliance-region: func() -> result<list<u8>, error>;
downstream-compliance-region: func(max-len: u64) -> result<list<u8>, error>;

new: func() -> result<request-handle, error>;

Expand Down

0 comments on commit 656a5a9

Please sign in to comment.