2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
use std::cell::RefCell;
|
2021-07-12 06:44:49 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-10-17 22:28:27 -04:00
|
|
|
use deno_core::error::bad_resource;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_core::error::bad_resource_id;
|
2022-03-16 09:54:18 -04:00
|
|
|
use deno_core::error::custom_error;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_core::OpState;
|
2022-03-16 09:54:18 -04:00
|
|
|
use deno_core::RcRef;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_core::ResourceId;
|
2022-03-16 09:54:18 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2021-10-04 21:50:40 -04:00
|
|
|
use deno_http::http_create_conn_resource;
|
2022-03-16 09:54:18 -04:00
|
|
|
use deno_http::HttpRequestReader;
|
|
|
|
use deno_http::HttpStreamResource;
|
2021-07-12 06:44:49 -04:00
|
|
|
use deno_net::io::TcpStreamResource;
|
2022-03-16 09:54:18 -04:00
|
|
|
use deno_net::ops_tls::TlsStream;
|
2021-10-19 19:30:04 -04:00
|
|
|
use deno_net::ops_tls::TlsStreamResource;
|
2022-03-16 09:54:18 -04:00
|
|
|
use hyper::upgrade::Parts;
|
|
|
|
use serde::Serialize;
|
|
|
|
use tokio::net::TcpStream;
|
2022-03-22 09:19:53 -04:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_net::io::UnixStreamResource;
|
2022-03-19 09:21:49 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::net::UnixStream;
|
2021-07-12 06:44:49 -04:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_http_runtime,
|
|
|
|
ops = [op_http_start, op_http_upgrade, op_flash_upgrade_http],
|
2023-03-18 18:30:04 -04:00
|
|
|
customizer = |ext: &mut deno_core::ExtensionBuilder| {
|
|
|
|
ext.force_op_registration();
|
|
|
|
},
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2021-07-12 06:44:49 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-07-12 06:44:49 -04:00
|
|
|
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)
|
|
|
|
{
|
2022-10-17 22:28:27 -04:00
|
|
|
// This TCP connection might be used somewhere else. If it's the case, we cannot proceed with the
|
|
|
|
// process of starting a HTTP server on top of this TCP connection, so we just return a bad
|
|
|
|
// resource error. See also: https://github.com/denoland/deno/pull/16242
|
2021-07-12 06:44:49 -04:00
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
2022-10-17 22:28:27 -04:00
|
|
|
.map_err(|_| bad_resource("TCP stream is currently in use"))?;
|
2021-07-12 06:44:49 -04:00
|
|
|
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)
|
|
|
|
{
|
2022-10-17 22:28:27 -04:00
|
|
|
// This TLS connection might be used somewhere else. If it's the case, we cannot proceed with the
|
|
|
|
// process of starting a HTTP server on top of this TLS connection, so we just return a bad
|
|
|
|
// resource error. See also: https://github.com/denoland/deno/pull/16242
|
2021-07-12 06:44:49 -04:00
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
2022-10-17 22:28:27 -04:00
|
|
|
.map_err(|_| bad_resource("TLS stream is currently in use"))?;
|
2021-07-12 06:44:49 -04:00
|
|
|
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
|
2022-02-16 13:14:19 -05:00
|
|
|
.take::<deno_net::io::UnixStreamResource>(tcp_stream_rid)
|
2022-02-15 18:16:12 -05:00
|
|
|
{
|
|
|
|
super::check_unstable(state, "Deno.serveHttp");
|
|
|
|
|
2022-10-17 22:28:27 -04:00
|
|
|
// This UNIX socket might be used somewhere else. If it's the case, we cannot proceed with the
|
|
|
|
// process of starting a HTTP server on top of this UNIX socket, so we just return a bad
|
|
|
|
// resource error. See also: https://github.com/denoland/deno/pull/16242
|
2022-02-15 18:16:12 -05:00
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
2022-10-17 22:28:27 -04:00
|
|
|
.map_err(|_| bad_resource("UNIX stream is currently in use"))?;
|
2022-02-15 18:16:12 -05:00
|
|
|
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())
|
|
|
|
}
|
2022-03-16 09:54:18 -04:00
|
|
|
|
2022-08-18 08:05:02 -04:00
|
|
|
#[op]
|
|
|
|
fn op_flash_upgrade_http(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: u32,
|
|
|
|
server_id: u32,
|
|
|
|
) -> Result<deno_core::ResourceId, AnyError> {
|
|
|
|
let flash_ctx = state.borrow_mut::<deno_flash::FlashContext>();
|
|
|
|
let ctx = flash_ctx.servers.get_mut(&server_id).unwrap();
|
|
|
|
|
|
|
|
let tcp_stream = deno_flash::detach_socket(ctx, token)?;
|
|
|
|
Ok(
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split())),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct HttpUpgradeResult {
|
|
|
|
conn_rid: ResourceId,
|
|
|
|
conn_type: &'static str,
|
|
|
|
read_buf: ZeroCopyBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
async fn op_http_upgrade(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
_: (),
|
|
|
|
) -> Result<HttpUpgradeResult, AnyError> {
|
|
|
|
let stream = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<HttpStreamResource>(rid)?;
|
|
|
|
let mut rd = RcRef::map(&stream, |r| &r.rd).borrow_mut().await;
|
|
|
|
|
|
|
|
let request = match &mut *rd {
|
|
|
|
HttpRequestReader::Headers(request) => request,
|
|
|
|
_ => {
|
|
|
|
return Err(custom_error(
|
|
|
|
"Http",
|
|
|
|
"cannot upgrade because request body was used",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let transport = hyper::upgrade::on(request).await?;
|
|
|
|
let transport = match transport.downcast::<TcpStream>() {
|
|
|
|
Ok(Parts {
|
|
|
|
io: tcp_stream,
|
|
|
|
read_buf,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
return Ok(HttpUpgradeResult {
|
|
|
|
conn_type: "tcp",
|
|
|
|
conn_rid: state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split())),
|
|
|
|
read_buf: read_buf.to_vec().into(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Err(transport) => transport,
|
|
|
|
};
|
2022-03-19 09:21:49 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
let transport = match transport.downcast::<UnixStream>() {
|
|
|
|
Ok(Parts {
|
|
|
|
io: unix_stream,
|
|
|
|
read_buf,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
return Ok(HttpUpgradeResult {
|
|
|
|
conn_type: "unix",
|
|
|
|
conn_rid: state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.add(UnixStreamResource::new(unix_stream.into_split())),
|
|
|
|
read_buf: read_buf.to_vec().into(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Err(transport) => transport,
|
|
|
|
};
|
2022-03-16 09:54:18 -04:00
|
|
|
match transport.downcast::<TlsStream>() {
|
|
|
|
Ok(Parts {
|
|
|
|
io: tls_stream,
|
|
|
|
read_buf,
|
|
|
|
..
|
|
|
|
}) => Ok(HttpUpgradeResult {
|
|
|
|
conn_type: "tls",
|
|
|
|
conn_rid: state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.add(TlsStreamResource::new(tls_stream.into_split())),
|
|
|
|
read_buf: read_buf.to_vec().into(),
|
|
|
|
}),
|
|
|
|
Err(_) => Err(custom_error(
|
|
|
|
"Http",
|
|
|
|
"encountered unsupported transport while upgrading",
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|