Skip to content

Commit

Permalink
Add Cf context to http::Request
Browse files Browse the repository at this point in the history
  • Loading branch information
kflansburg committed Mar 12, 2024
1 parent 71385bf commit 7dbf6bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
13 changes: 12 additions & 1 deletion worker/src/cf.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/// In addition to the methods on the `Request` struct, the `Cf` struct on an inbound Request contains information about the request provided by Cloudflare’s edge.
///
/// [Details](https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties)
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Cf {
inner: worker_sys::IncomingRequestCfProperties,
}

unsafe impl Send for Cf {}
unsafe impl Sync for Cf {}

impl Cf {
pub(crate) fn new(inner: worker_sys::IncomingRequestCfProperties) -> Self {
Self { inner }
}

pub(crate) fn inner(&self) -> &worker_sys::IncomingRequestCfProperties {
&self.inner
}

/// The three-letter airport code (e.g. `ATX`, `LUX`) representing
/// the colocation which processed the request
pub fn colo(&self) -> String {
Expand Down
33 changes: 33 additions & 0 deletions worker/src/http/request.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use crate::http::body::Body;
use crate::Cf;
use crate::{http::redirect::RequestRedirect, AbortSignal};
use worker_sys::ext::RequestExt;

use crate::http::header::{header_map_from_web_sys_headers, web_sys_headers_from_header_map};

fn version_from_string(version: &str) -> http::Version {
match version {
"HTTP/0.9" => http::Version::HTTP_09,
"HTTP/1.0" => http::Version::HTTP_10,
"HTTP/1.1" => http::Version::HTTP_11,
"HTTP/2" => http::Version::HTTP_2,
"HTTP/3" => http::Version::HTTP_3,
_ => unreachable!("no other versions exist"),
}
}

pub fn from_wasm(req: web_sys::Request) -> http::Request<Body> {
let mut builder = http::request::Builder::new()
.uri(req.url())
Expand All @@ -12,6 +25,12 @@ pub fn from_wasm(req: web_sys::Request) -> http::Request<Body> {

header_map_from_web_sys_headers(req.headers(), builder.headers_mut().unwrap());

if let Some(cf) = req.cf() {
builder = builder
.version(version_from_string(&cf.http_protocol()))
.extension(Cf::new(cf));
}

if let Some(body) = req.body() {
builder.body(Body::new(body)).unwrap()
} else {
Expand All @@ -32,6 +51,20 @@ pub fn to_wasm(mut req: http::Request<Body>) -> web_sys::Request {
init.redirect(redirect.into());
}

if let Some(cf) = req.extensions_mut().remove::<Cf>() {
// TODO: this should be handled in worker-sys
let r = ::js_sys::Reflect::set(
init.as_ref(),
&wasm_bindgen::JsValue::from("cf"),
&wasm_bindgen::JsValue::from(cf.inner()),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
}

if let Some(readable_stream) = req.into_body().into_inner() {
init.body(Some(readable_stream.as_ref()));
}
Expand Down

0 comments on commit 7dbf6bb

Please sign in to comment.