2021-07-12 06:44:49 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::ResourceId;
|
2021-10-04 21:50:40 -04:00
|
|
|
use deno_http::http_create_conn_resource;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_net::io::TcpStreamResource;
|
2022-02-15 18:16:12 -05:00
|
|
|
use deno_net::io::UnixStreamResource;
|
2021-10-19 19:30:04 -04:00
|
|
|
use deno_net::ops_tls::TlsStreamResource;
|
2021-07-12 06:44:49 -04:00
|
|
|
|
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![("op_http_start", op_sync(op_http_start))])
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_http_start(
|
|
|
|
state: &mut OpState,
|
|
|
|
tcp_stream_rid: ResourceId,
|
|
|
|
_: (),
|
|
|
|
) -> Result<ResourceId, AnyError> {
|
2021-08-15 07:29:19 -04:00
|
|
|
if let Ok(resource_rc) = state
|
2021-07-12 06:44:49 -04:00
|
|
|
.resource_table
|
|
|
|
.take::<TcpStreamResource>(tcp_stream_rid)
|
|
|
|
{
|
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
|
|
|
.expect("Only a single use of this resource should happen");
|
|
|
|
let (read_half, write_half) = resource.into_inner();
|
|
|
|
let tcp_stream = read_half.reunite(write_half)?;
|
|
|
|
let addr = tcp_stream.local_addr()?;
|
2021-10-04 21:50:40 -04:00
|
|
|
return http_create_conn_resource(state, tcp_stream, addr, "http");
|
2021-07-12 06:44:49 -04:00
|
|
|
}
|
|
|
|
|
2021-08-15 07:29:19 -04:00
|
|
|
if let Ok(resource_rc) = state
|
2021-07-12 06:44:49 -04:00
|
|
|
.resource_table
|
|
|
|
.take::<TlsStreamResource>(tcp_stream_rid)
|
|
|
|
{
|
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
|
|
|
.expect("Only a single use of this resource should happen");
|
|
|
|
let (read_half, write_half) = resource.into_inner();
|
|
|
|
let tls_stream = read_half.reunite(write_half);
|
|
|
|
let addr = tls_stream.get_ref().0.local_addr()?;
|
2021-10-04 21:50:40 -04:00
|
|
|
return http_create_conn_resource(state, tls_stream, addr, "https");
|
2021-07-12 06:44:49 -04:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:16:12 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
if let Ok(resource_rc) = state
|
|
|
|
.resource_table
|
|
|
|
.take::<UnixStreamResource>(tcp_stream_rid)
|
|
|
|
{
|
|
|
|
super::check_unstable(state, "Deno.serveHttp");
|
|
|
|
|
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
|
|
|
.expect("Only a single use of this resource should happen");
|
|
|
|
let (read_half, write_half) = resource.into_inner();
|
|
|
|
let unix_stream = read_half.reunite(write_half)?;
|
|
|
|
let addr = unix_stream.local_addr()?;
|
|
|
|
return http_create_conn_resource(state, unix_stream, addr, "http+unix");
|
|
|
|
}
|
|
|
|
|
2021-07-12 06:44:49 -04:00
|
|
|
Err(bad_resource_id())
|
|
|
|
}
|