diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-02-15 16:37:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 16:37:41 +0100 |
commit | c7535950b6de086fce741809728129c79288dee8 (patch) | |
tree | 016b274136e8f656d420f6bda79740dfc3fb09f7 | |
parent | 4104a674c7955eb7811b241b2e18453ac96faaf5 (diff) |
feat(flash): add 2nd param to handler to get remote address (#17633)
Closes #17583
-rw-r--r-- | cli/tests/unit/flash_test.ts | 4 | ||||
-rw-r--r-- | cli/tsc/dts/lib.deno.unstable.d.ts | 2 | ||||
-rw-r--r-- | ext/flash/01_http.js | 11 | ||||
-rw-r--r-- | ext/flash/lib.rs | 24 | ||||
-rw-r--r-- | ext/flash/socket.rs | 1 |
5 files changed, 38 insertions, 4 deletions
diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts index 9ed0276a6..0924aab66 100644 --- a/cli/tests/unit/flash_test.ts +++ b/cli/tests/unit/flash_test.ts @@ -89,12 +89,14 @@ Deno.test({ permissions: { net: true } }, async function httpServerBasic() { const listeningPromise = deferred(); const server = Deno.serve({ - handler: async (request) => { + handler: async (request, getRemoteAddr) => { // FIXME(bartlomieju): // make sure that request can be inspected console.log(request); assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/"); assertEquals(await request.text(), ""); + const addr = getRemoteAddr(); + assertEquals(addr.hostname, "127.0.0.1"); promise.resolve(); return new Response("Hello World", { headers: { "foo": "bar" } }); }, diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index 822425d05..0362b416d 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -1126,7 +1126,7 @@ declare namespace Deno { * * @category HTTP Server */ - export type ServeHandler = (request: Request) => Response | Promise<Response>; + export type ServeHandler = (request: Request, getRemoteAddr: () => Deno.NetAddr) => Response | Promise<Response>; /** **UNSTABLE**: New API, yet to be vetted. * diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index 5152fd9e5..ff2eab63c 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -570,7 +570,16 @@ function createServe(opFn) { let resp; try { - resp = handler(req); + resp = handler(req, () => { + const { 0: hostname, 1: port } = core.ops.op_flash_addr( + serverId, + i, + ); + return { + hostname, + port, + }; + }); if (ObjectPrototypeIsPrototypeOf(PromisePrototype, resp)) { PromisePrototypeCatch( PromisePrototypeThen( diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index f92aa3ea3..4ae064173 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -665,6 +665,26 @@ fn op_flash_headers( ) } +#[op] +fn op_flash_addr( + state: Rc<RefCell<OpState>>, + server_id: u32, + token: u32, +) -> Result<(String, u16), AnyError> { + let mut op_state = state.borrow_mut(); + let flash_ctx = op_state.borrow_mut::<FlashContext>(); + let ctx = flash_ctx + .servers + .get_mut(&server_id) + .ok_or_else(|| type_error("server closed"))?; + let req = &ctx + .requests + .get(&token) + .ok_or_else(|| type_error("request closed"))?; + let socket = req.socket(); + Ok((socket.addr.ip().to_string(), socket.addr.port())) +} + // Remember the first packet we read? It probably also has some body data. This op quickly copies it into // a buffer and sets up channels for streaming the rest. #[op] @@ -934,7 +954,7 @@ fn run_server( match token { Token(0) => loop { match listener.accept() { - Ok((mut socket, _)) => { + Ok((mut socket, addr)) => { counter += 1; let token = Token(counter); poll @@ -960,6 +980,7 @@ fn run_server( read_lock: Arc::new(Mutex::new(())), parse_done: ParseStatus::None, buffer: UnsafeCell::new(vec![0_u8; 1024]), + addr, }); trace!("New connection: {}", token.0); @@ -1524,6 +1545,7 @@ pub fn init<P: FlashPermissions + 'static>(unstable: bool) -> Extension { op_flash_method::decl(), op_flash_path::decl(), op_flash_headers::decl(), + op_flash_addr::decl(), op_flash_next::decl(), op_flash_next_server::decl(), op_flash_next_async::decl(), diff --git a/ext/flash/socket.rs b/ext/flash/socket.rs index 27906e74e..cf9501634 100644 --- a/ext/flash/socket.rs +++ b/ext/flash/socket.rs @@ -29,6 +29,7 @@ pub struct Stream { pub parse_done: ParseStatus, pub buffer: UnsafeCell<Vec<u8>>, pub read_lock: Arc<Mutex<()>>, + pub addr: std::net::SocketAddr, } impl Stream { |