2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
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;
|
|
|
|
use deno_core::error::AnyError;
|
2023-09-21 10:08:23 -04:00
|
|
|
use deno_core::op2;
|
2021-07-12 06:44:49 -04:00
|
|
|
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;
|
2021-10-19 19:30:04 -04:00
|
|
|
use deno_net::ops_tls::TlsStreamResource;
|
2021-07-12 06:44:49 -04:00
|
|
|
|
2023-10-12 11:55:50 -04:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "http";
|
|
|
|
|
2024-01-22 16:35:39 -05:00
|
|
|
deno_core::extension!(deno_http_runtime, ops = [op_http_start],);
|
2021-07-12 06:44:49 -04:00
|
|
|
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
2021-07-12 06:44:49 -04:00
|
|
|
fn op_http_start(
|
|
|
|
state: &mut OpState,
|
2023-09-21 10:08:23 -04:00
|
|
|
#[smi] tcp_stream_rid: ResourceId,
|
2021-07-12 06:44:49 -04:00
|
|
|
) -> 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();
|
2023-11-15 18:12:46 -05:00
|
|
|
let tls_stream = read_half.unsplit(write_half);
|
|
|
|
let addr = tls_stream.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
|
|
|
{
|
2023-10-12 11:55:50 -04:00
|
|
|
super::check_unstable(state, UNSTABLE_FEATURE_NAME, "Deno.serveHttp");
|
2022-02-15 18:16:12 -05:00
|
|
|
|
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())
|
|
|
|
}
|