1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-04 17:18:23 -05:00
denoland-deno/runtime/ops/http.rs

94 lines
3.2 KiB
Rust
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2021-07-12 06:44:49 -04:00
use std::rc::Rc;
use deno_core::op2;
2021-07-12 06:44:49 -04:00
use deno_core::OpState;
use deno_core::ResourceId;
use deno_http::http_create_conn_resource;
2021-07-12 06:44:49 -04:00
use deno_net::io::TcpStreamResource;
use deno_net::ops_tls::TlsStreamResource;
2021-07-12 06:44:49 -04:00
pub const UNSTABLE_FEATURE_NAME: &str = "http";
deno_core::extension!(deno_http_runtime, ops = [op_http_start],);
2021-07-12 06:44:49 -04:00
#[derive(Debug, thiserror::Error)]
pub enum HttpStartError {
#[error("TCP stream is currently in use")]
TcpStreamInUse,
#[error("TLS stream is currently in use")]
TlsStreamInUse,
#[error("Unix socket is currently in use")]
UnixSocketInUse,
#[error(transparent)]
ReuniteTcp(#[from] tokio::net::tcp::ReuniteError),
#[cfg(unix)]
#[error(transparent)]
ReuniteUnix(#[from] tokio::net::unix::ReuniteError),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
Other(deno_core::error::AnyError),
}
#[op2(fast)]
#[smi]
2021-07-12 06:44:49 -04:00
fn op_http_start(
state: &mut OpState,
#[smi] tcp_stream_rid: ResourceId,
) -> Result<ResourceId, HttpStartError> {
if let Ok(resource_rc) = state
2021-07-12 06:44:49 -04:00
.resource_table
.take::<TcpStreamResource>(tcp_stream_rid)
{
// 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 Busy 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)
.map_err(|_| HttpStartError::TcpStreamInUse)?;
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()?;
return Ok(http_create_conn_resource(state, tcp_stream, addr, "http"));
2021-07-12 06:44:49 -04:00
}
if let Ok(resource_rc) = state
2021-07-12 06:44:49 -04:00
.resource_table
.take::<TlsStreamResource>(tcp_stream_rid)
{
// 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 Busy 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)
.map_err(|_| HttpStartError::TlsStreamInUse)?;
2021-07-12 06:44:49 -04:00
let (read_half, write_half) = resource.into_inner();
let tls_stream = read_half.unsplit(write_half);
let addr = tls_stream.local_addr()?;
return Ok(http_create_conn_resource(state, tls_stream, addr, "https"));
2021-07-12 06:44:49 -04: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)
{
// 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 Busy error.
// See also: https://github.com/denoland/deno/pull/16242
let resource = Rc::try_unwrap(resource_rc)
.map_err(|_| HttpStartError::UnixSocketInUse)?;
let (read_half, write_half) = resource.into_inner();
let unix_stream = read_half.reunite(write_half)?;
let addr = unix_stream.local_addr()?;
return Ok(http_create_conn_resource(
state,
unix_stream,
addr,
"http+unix",
));
}
Err(HttpStartError::Other(deno_core::error::bad_resource_id()))
2021-07-12 06:44:49 -04:00
}