Skip to content

Commit 656a5a9

Browse files
committed
Update for review feedback
1 parent 9ab65ba commit 656a5a9

5 files changed

Lines changed: 44 additions & 18 deletions

File tree

crates/adapter/src/fastly/core.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,9 @@ pub mod fastly_http_req {
995995
nwritten: *mut usize,
996996
) -> FastlyStatus {
997997
alloc_result!(region_out, region_max_len, nwritten, {
998-
fastly::api::http_req::downstream_compliance_region()
998+
fastly::api::http_req::downstream_compliance_region(
999+
u64::try_from(region_max_len).trapping_unwrap(),
1000+
)
9991001
})
10001002
}
10011003

lib/src/component/http_req.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,17 @@ impl http_req::Host for Session {
808808
Err(Error::NotAvailable("Client TLS JA4 hash").into())
809809
}
810810

811-
async fn downstream_compliance_region(&mut self) -> Result<Vec<u8>, types::Error> {
812-
Ok(Vec::from(b"none"))
811+
async fn downstream_compliance_region(
812+
&mut self,
813+
region_max_len: u64,
814+
) -> Result<Vec<u8>, types::Error> {
815+
let region = Session::downstream_compliance_region(self);
816+
let region_len = region.len();
817+
818+
match u64::try_from(region_len) {
819+
Ok(region_len) if region_len <= region_max_len => Ok(region.into()),
820+
too_large => Err(types::Error::BufferLen(too_large.unwrap_or(0))),
821+
}
813822
}
814823

815824
async fn original_header_names_get(

lib/src/session.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ use {
3838
tokio::sync::oneshot::Sender,
3939
};
4040

41+
const REGION_NONE: &[u8] = b"none";
42+
4143
/// Data specific to an individual request, including any host-side
4244
/// allocations on behalf of the guest processing the request.
4345
pub struct Session {
4446
/// The downstream IP address and port for this session.
4547
downstream_client_addr: SocketAddr,
4648
/// The IP address and port that received this session.
4749
downstream_server_addr: SocketAddr,
50+
/// The compliance region that this request was received in.
51+
///
52+
/// For now this is just always `"none"`, but we place the field in the session
53+
/// to make it easier to implement custom configuration values later on.
54+
downstream_compliance_region: Vec<u8>,
4855
/// Handle for the downstream request "parts". NB the backing parts data can be mutated
4956
/// or even removed from the relevant map.
5057
downstream_req_handle: RequestHandle,
@@ -165,6 +172,7 @@ impl Session {
165172
Session {
166173
downstream_server_addr: server_addr,
167174
downstream_client_addr: client_addr,
175+
downstream_compliance_region: Vec::from(REGION_NONE),
168176
downstream_req_handle,
169177
downstream_req_body_handle,
170178
downstream_req_original_headers,
@@ -204,6 +212,11 @@ impl Session {
204212
self.downstream_server_addr.ip()
205213
}
206214

215+
/// Retrieve the compliance region that received the request for this session.
216+
pub fn downstream_compliance_region(&self) -> &[u8] {
217+
self.downstream_compliance_region.as_slice()
218+
}
219+
207220
/// Retrieve the handle corresponding to the downstream request.
208221
pub fn downstream_request(&self) -> RequestHandle {
209222
self.downstream_req_handle

lib/src/wiggle_abi/req_impl.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,23 +308,25 @@ impl FastlyHttpReq for Session {
308308
region_max_len: u32,
309309
nwritten_out: GuestPtr<u32>,
310310
) -> Result<(), Error> {
311-
const REGION_NONE: &[u8] = b"none";
312-
const REGION_NONE_LEN: u32 = 4;
311+
let region = Session::downstream_compliance_region(self);
312+
let region_len = region.len();
313313

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

318-
return Err(Error::BufferLengthError {
319-
buf: "region_out",
320-
len: "region_max_len",
321-
});
322-
}
323-
324-
memory.copy_from_slice(REGION_NONE, region_out.as_array(region_max_len))?;
325-
memory.write(nwritten_out, REGION_NONE_LEN)?;
319+
Ok(())
320+
}
321+
too_large => {
322+
memory.write(nwritten_out, too_large.unwrap_or(0))?;
326323

327-
Ok(())
324+
Err(Error::BufferLengthError {
325+
buf: "region_out",
326+
len: "region_max_len",
327+
})
328+
}
329+
}
328330
}
329331

330332
fn framing_headers_mode_set(

lib/wit/deps/fastly/compute.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ interface http-req {
323323

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

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

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

0 commit comments

Comments
 (0)