mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
refactor(core): http_next generic over request extractor (#19071)
This commit is contained in:
parent
a650318c0c
commit
e6e0fa752d
7 changed files with 57 additions and 48 deletions
|
@ -9,6 +9,7 @@ use deno_core::Extension;
|
|||
use deno_core::ExtensionFileSource;
|
||||
use deno_core::ExtensionFileSourceCode;
|
||||
use deno_runtime::deno_cache::SqliteBackedCache;
|
||||
use deno_runtime::deno_http::DefaultHttpPropertyExtractor;
|
||||
use deno_runtime::deno_kv::sqlite::SqliteDbHandler;
|
||||
use deno_runtime::permissions::PermissionsContainer;
|
||||
use deno_runtime::*;
|
||||
|
@ -361,7 +362,7 @@ fn create_cli_snapshot(snapshot_path: PathBuf) {
|
|||
false, // No --unstable.
|
||||
),
|
||||
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
|
||||
deno_http::deno_http::init_ops(),
|
||||
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(),
|
||||
deno_io::deno_io::init_ops(Default::default()),
|
||||
deno_fs::deno_fs::init_ops::<PermissionsContainer>(false, fs.clone()),
|
||||
deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs),
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
use crate::compressible::is_content_compressible;
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
use crate::compressible::is_content_compressible;
|
||||
use crate::extract_network_stream;
|
||||
use crate::network_buffered_stream::NetworkStreamPrefixCheck;
|
||||
use crate::request_body::HttpRequestBody;
|
||||
use crate::request_properties::DefaultHttpRequestProperties;
|
||||
use crate::request_properties::HttpConnectionProperties;
|
||||
use crate::request_properties::HttpListenProperties;
|
||||
use crate::request_properties::HttpPropertyExtractor;
|
||||
|
@ -375,12 +374,15 @@ pub fn op_http_set_promise_complete(index: u32, status: u16) {
|
|||
}
|
||||
|
||||
#[op]
|
||||
pub fn op_http_get_request_method_and_url(
|
||||
pub fn op_http_get_request_method_and_url<HTTP>(
|
||||
index: u32,
|
||||
) -> (String, Option<String>, String, String, Option<u16>) {
|
||||
) -> (String, Option<String>, String, String, Option<u16>)
|
||||
where
|
||||
HTTP: HttpPropertyExtractor,
|
||||
{
|
||||
// TODO(mmastrac): Passing method can be optimized
|
||||
with_http(index, |http| {
|
||||
let request_properties = DefaultHttpRequestProperties::request_properties(
|
||||
let request_properties = HTTP::request_properties(
|
||||
&http.request_info,
|
||||
&http.request_parts.uri,
|
||||
&http.request_parts.headers,
|
||||
|
@ -825,12 +827,15 @@ fn serve_http(
|
|||
spawn_local(serve_http2_autodetect(io, svc).try_or_cancel(cancel))
|
||||
}
|
||||
|
||||
fn serve_http_on(
|
||||
fn serve_http_on<HTTP>(
|
||||
network_stream: NetworkStream,
|
||||
listen_properties: &HttpListenProperties,
|
||||
cancel: Rc<CancelHandle>,
|
||||
tx: tokio::sync::mpsc::Sender<u32>,
|
||||
) -> JoinHandle<Result<(), AnyError>> {
|
||||
) -> JoinHandle<Result<(), AnyError>>
|
||||
where
|
||||
HTTP: HttpPropertyExtractor,
|
||||
{
|
||||
// We always want some sort of peer address. If we can't get one, just make up one.
|
||||
let peer_address = network_stream.peer_address().unwrap_or_else(|_| {
|
||||
NetworkStreamAddress::Ip(SocketAddr::V4(SocketAddrV4::new(
|
||||
|
@ -839,10 +844,7 @@ fn serve_http_on(
|
|||
)))
|
||||
});
|
||||
let connection_properties: HttpConnectionProperties =
|
||||
DefaultHttpRequestProperties::connection_properties(
|
||||
listen_properties,
|
||||
&peer_address,
|
||||
);
|
||||
HTTP::connection_properties(listen_properties, &peer_address);
|
||||
|
||||
match network_stream {
|
||||
NetworkStream::Tcp(conn) => {
|
||||
|
@ -889,21 +891,21 @@ impl Drop for HttpJoinHandle {
|
|||
}
|
||||
|
||||
#[op(v8)]
|
||||
pub fn op_http_serve(
|
||||
pub fn op_http_serve<HTTP>(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
listener_rid: ResourceId,
|
||||
) -> Result<(ResourceId, &'static str, String), AnyError> {
|
||||
let listener =
|
||||
DefaultHttpRequestProperties::get_network_stream_listener_for_rid(
|
||||
&mut state.borrow_mut(),
|
||||
listener_rid,
|
||||
)?;
|
||||
) -> Result<(ResourceId, &'static str, String), AnyError>
|
||||
where
|
||||
HTTP: HttpPropertyExtractor,
|
||||
{
|
||||
let listener = HTTP::get_network_stream_listener_for_rid(
|
||||
&mut state.borrow_mut(),
|
||||
listener_rid,
|
||||
)?;
|
||||
|
||||
let local_address = listener.listen_address()?;
|
||||
let listen_properties = DefaultHttpRequestProperties::listen_properties(
|
||||
listener.stream(),
|
||||
&local_address,
|
||||
);
|
||||
let listen_properties =
|
||||
HTTP::listen_properties(listener.stream(), &local_address);
|
||||
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(10);
|
||||
let resource: Rc<HttpJoinHandle> = Rc::new(HttpJoinHandle(
|
||||
|
@ -920,7 +922,7 @@ pub fn op_http_serve(
|
|||
.accept()
|
||||
.try_or_cancel(cancel_clone.clone())
|
||||
.await?;
|
||||
serve_http_on(
|
||||
serve_http_on::<HTTP>(
|
||||
conn,
|
||||
&listen_properties_clone,
|
||||
cancel_clone.clone(),
|
||||
|
@ -944,21 +946,19 @@ pub fn op_http_serve(
|
|||
}
|
||||
|
||||
#[op(v8)]
|
||||
pub fn op_http_serve_on(
|
||||
pub fn op_http_serve_on<HTTP>(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
conn: ResourceId,
|
||||
) -> Result<(ResourceId, &'static str, String), AnyError> {
|
||||
) -> Result<(ResourceId, &'static str, String), AnyError>
|
||||
where
|
||||
HTTP: HttpPropertyExtractor,
|
||||
{
|
||||
let network_stream: NetworkStream =
|
||||
DefaultHttpRequestProperties::get_network_stream_for_rid(
|
||||
&mut state.borrow_mut(),
|
||||
conn,
|
||||
)?;
|
||||
HTTP::get_network_stream_for_rid(&mut state.borrow_mut(), conn)?;
|
||||
|
||||
let local_address = network_stream.local_address()?;
|
||||
let listen_properties = DefaultHttpRequestProperties::listen_properties(
|
||||
network_stream.stream(),
|
||||
&local_address,
|
||||
);
|
||||
let listen_properties =
|
||||
HTTP::listen_properties(network_stream.stream(), &local_address);
|
||||
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(10);
|
||||
let resource: Rc<HttpJoinHandle> = Rc::new(HttpJoinHandle(
|
||||
|
@ -967,12 +967,13 @@ pub fn op_http_serve_on(
|
|||
AsyncRefCell::new(rx),
|
||||
));
|
||||
|
||||
let handle: JoinHandle<Result<(), deno_core::anyhow::Error>> = serve_http_on(
|
||||
network_stream,
|
||||
&listen_properties,
|
||||
resource.cancel_handle(),
|
||||
tx,
|
||||
);
|
||||
let handle: JoinHandle<Result<(), deno_core::anyhow::Error>> =
|
||||
serve_http_on::<HTTP>(
|
||||
network_stream,
|
||||
&listen_properties,
|
||||
resource.cancel_handle(),
|
||||
tx,
|
||||
);
|
||||
|
||||
// Set the handle after we start the future
|
||||
*RcRef::map(&resource, |this| &this.0)
|
||||
|
|
|
@ -73,6 +73,7 @@ use tokio::task::spawn_local;
|
|||
use crate::network_buffered_stream::NetworkBufferedStream;
|
||||
use crate::reader_stream::ExternallyAbortableReaderStream;
|
||||
use crate::reader_stream::ShutdownHandle;
|
||||
use crate::request_properties::HttpPropertyExtractor;
|
||||
|
||||
pub mod compressible;
|
||||
mod http_next;
|
||||
|
@ -83,9 +84,12 @@ mod request_properties;
|
|||
mod response_body;
|
||||
mod websocket_upgrade;
|
||||
|
||||
pub use request_properties::DefaultHttpPropertyExtractor;
|
||||
|
||||
deno_core::extension!(
|
||||
deno_http,
|
||||
deps = [deno_web, deno_net, deno_fetch, deno_websocket],
|
||||
parameters = [ HTTP: HttpPropertyExtractor ],
|
||||
ops = [
|
||||
op_http_accept,
|
||||
op_http_headers,
|
||||
|
@ -97,10 +101,10 @@ deno_core::extension!(
|
|||
op_http_write,
|
||||
http_next::op_http_get_request_header,
|
||||
http_next::op_http_get_request_headers,
|
||||
http_next::op_http_get_request_method_and_url,
|
||||
http_next::op_http_get_request_method_and_url<HTTP>,
|
||||
http_next::op_http_read_request_body,
|
||||
http_next::op_http_serve_on,
|
||||
http_next::op_http_serve,
|
||||
http_next::op_http_serve_on<HTTP>,
|
||||
http_next::op_http_serve<HTTP>,
|
||||
http_next::op_http_set_promise_complete,
|
||||
http_next::op_http_set_response_body_bytes,
|
||||
http_next::op_http_set_response_body_resource,
|
||||
|
|
|
@ -70,9 +70,9 @@ pub trait HttpPropertyExtractor {
|
|||
) -> HttpRequestProperties;
|
||||
}
|
||||
|
||||
pub struct DefaultHttpRequestProperties {}
|
||||
pub struct DefaultHttpPropertyExtractor {}
|
||||
|
||||
impl HttpPropertyExtractor for DefaultHttpRequestProperties {
|
||||
impl HttpPropertyExtractor for DefaultHttpPropertyExtractor {
|
||||
fn get_network_stream_for_rid(
|
||||
state: &mut OpState,
|
||||
rid: ResourceId,
|
||||
|
|
|
@ -18,6 +18,7 @@ mod startup_snapshot {
|
|||
use deno_core::Extension;
|
||||
use deno_core::ExtensionFileSource;
|
||||
use deno_core::ModuleCode;
|
||||
use deno_http::DefaultHttpPropertyExtractor;
|
||||
use std::path::Path;
|
||||
|
||||
fn transpile_ts_for_snapshotting(
|
||||
|
@ -319,7 +320,7 @@ mod startup_snapshot {
|
|||
false, // No --unstable
|
||||
),
|
||||
deno_napi::deno_napi::init_ops_and_esm::<Permissions>(),
|
||||
deno_http::deno_http::init_ops_and_esm(),
|
||||
deno_http::deno_http::init_ops_and_esm::<DefaultHttpPropertyExtractor>(),
|
||||
deno_io::deno_io::init_ops_and_esm(Default::default()),
|
||||
deno_fs::deno_fs::init_ops_and_esm::<Permissions>(false, fs.clone()),
|
||||
runtime::init_ops_and_esm(),
|
||||
|
|
|
@ -35,6 +35,7 @@ use deno_core::SharedArrayBufferStore;
|
|||
use deno_core::Snapshot;
|
||||
use deno_core::SourceMapGetter;
|
||||
use deno_fs::FileSystem;
|
||||
use deno_http::DefaultHttpPropertyExtractor;
|
||||
use deno_io::Stdio;
|
||||
use deno_kv::sqlite::SqliteDbHandler;
|
||||
use deno_tls::RootCertStoreProvider;
|
||||
|
@ -439,7 +440,7 @@ impl WebWorker {
|
|||
unstable,
|
||||
),
|
||||
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
|
||||
deno_http::deno_http::init_ops(),
|
||||
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(),
|
||||
deno_io::deno_io::init_ops(Some(options.stdio)),
|
||||
deno_fs::deno_fs::init_ops::<PermissionsContainer>(
|
||||
unstable,
|
||||
|
|
|
@ -31,6 +31,7 @@ use deno_core::SharedArrayBufferStore;
|
|||
use deno_core::Snapshot;
|
||||
use deno_core::SourceMapGetter;
|
||||
use deno_fs::FileSystem;
|
||||
use deno_http::DefaultHttpPropertyExtractor;
|
||||
use deno_io::Stdio;
|
||||
use deno_kv::sqlite::SqliteDbHandler;
|
||||
use deno_tls::RootCertStoreProvider;
|
||||
|
@ -264,7 +265,7 @@ impl MainWorker {
|
|||
unstable,
|
||||
),
|
||||
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
|
||||
deno_http::deno_http::init_ops(),
|
||||
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(),
|
||||
deno_io::deno_io::init_ops(Some(options.stdio)),
|
||||
deno_fs::deno_fs::init_ops::<PermissionsContainer>(
|
||||
unstable,
|
||||
|
|
Loading…
Reference in a new issue