2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-22 13:48:21 -04:00
|
|
|
use bytes::Buf;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use deno_net::raw::NetworkStream;
|
2023-12-27 08:38:44 -05:00
|
|
|
use h2::RecvStream;
|
|
|
|
use h2::SendStream;
|
2023-12-27 11:59:57 -05:00
|
|
|
use hyper::upgrade::Upgraded;
|
2023-12-26 05:20:49 -05:00
|
|
|
use hyper_util::rt::TokioIo;
|
2023-11-01 17:11:01 -04:00
|
|
|
use std::io::ErrorKind;
|
2023-04-22 13:48:21 -04:00
|
|
|
use std::pin::Pin;
|
2023-11-01 17:11:01 -04:00
|
|
|
use std::task::ready;
|
2023-04-22 13:48:21 -04:00
|
|
|
use std::task::Poll;
|
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use tokio::io::AsyncWrite;
|
|
|
|
use tokio::io::ReadBuf;
|
|
|
|
|
|
|
|
// TODO(bartlomieju): remove this
|
|
|
|
pub(crate) enum WsStreamKind {
|
2023-12-26 05:20:49 -05:00
|
|
|
Upgraded(TokioIo<Upgraded>),
|
2023-04-22 13:48:21 -04:00
|
|
|
Network(NetworkStream),
|
2023-11-01 17:11:01 -04:00
|
|
|
H2(SendStream<Bytes>, RecvStream),
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct WebSocketStream {
|
|
|
|
stream: WsStreamKind,
|
|
|
|
pre: Option<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebSocketStream {
|
|
|
|
pub fn new(stream: WsStreamKind, buffer: Option<Bytes>) -> Self {
|
|
|
|
Self {
|
|
|
|
stream,
|
|
|
|
pre: buffer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for WebSocketStream {
|
|
|
|
// From hyper's Rewind (https://github.com/hyperium/hyper), MIT License, Copyright (c) Sean McArthur
|
|
|
|
fn poll_read(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
) -> Poll<std::io::Result<()>> {
|
|
|
|
if let Some(mut prefix) = self.pre.take() {
|
|
|
|
// If there are no remaining bytes, let the bytes get dropped.
|
|
|
|
if !prefix.is_empty() {
|
|
|
|
let copy_len = std::cmp::min(prefix.len(), buf.remaining());
|
|
|
|
// TODO: There should be a way to do following two lines cleaner...
|
|
|
|
buf.put_slice(&prefix[..copy_len]);
|
|
|
|
prefix.advance(copy_len);
|
|
|
|
// Put back what's left
|
|
|
|
if !prefix.is_empty() {
|
|
|
|
self.pre = Some(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &mut self.stream {
|
|
|
|
WsStreamKind::Network(stream) => Pin::new(stream).poll_read(cx, buf),
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => Pin::new(stream).poll_read(cx, buf),
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(_, recv) => {
|
|
|
|
let data = ready!(recv.poll_data(cx));
|
|
|
|
let Some(data) = data else {
|
|
|
|
// EOF
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
};
|
|
|
|
let mut data = data.map_err(|e| {
|
|
|
|
std::io::Error::new(std::io::ErrorKind::InvalidData, e)
|
|
|
|
})?;
|
|
|
|
recv.flow_control().release_capacity(data.len()).unwrap();
|
|
|
|
// This looks like the prefix code above -- can we share this?
|
|
|
|
let copy_len = std::cmp::min(data.len(), buf.remaining());
|
|
|
|
// TODO: There should be a way to do following two lines cleaner...
|
|
|
|
buf.put_slice(&data[..copy_len]);
|
|
|
|
data.advance(copy_len);
|
|
|
|
// Put back what's left
|
|
|
|
if !data.is_empty() {
|
|
|
|
self.pre = Some(data);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncWrite for WebSocketStream {
|
|
|
|
fn poll_write(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
buf: &[u8],
|
|
|
|
) -> std::task::Poll<Result<usize, std::io::Error>> {
|
|
|
|
match &mut self.stream {
|
|
|
|
WsStreamKind::Network(stream) => Pin::new(stream).poll_write(cx, buf),
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => Pin::new(stream).poll_write(cx, buf),
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(send, _) => {
|
|
|
|
// Zero-length write succeeds
|
|
|
|
if buf.is_empty() {
|
|
|
|
return Poll::Ready(Ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
send.reserve_capacity(buf.len());
|
|
|
|
let res = ready!(send.poll_capacity(cx));
|
|
|
|
|
|
|
|
// TODO(mmastrac): the documentation is not entirely clear what to do here, so we'll continue
|
|
|
|
_ = res;
|
|
|
|
|
|
|
|
// We'll try to send whatever we have capacity for
|
|
|
|
let size = std::cmp::min(buf.len(), send.capacity());
|
|
|
|
assert!(size > 0);
|
|
|
|
|
|
|
|
let buf: Bytes = Bytes::copy_from_slice(&buf[0..size]);
|
|
|
|
let len = buf.len();
|
|
|
|
// TODO(mmastrac): surface the h2 error?
|
|
|
|
let res = send
|
|
|
|
.send_data(buf, false)
|
|
|
|
.map_err(|_| std::io::Error::from(ErrorKind::Other));
|
|
|
|
Poll::Ready(res.map(|_| len))
|
|
|
|
}
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_flush(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Result<(), std::io::Error>> {
|
|
|
|
match &mut self.stream {
|
|
|
|
WsStreamKind::Network(stream) => Pin::new(stream).poll_flush(cx),
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => Pin::new(stream).poll_flush(cx),
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(..) => Poll::Ready(Ok(())),
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_shutdown(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Result<(), std::io::Error>> {
|
|
|
|
match &mut self.stream {
|
|
|
|
WsStreamKind::Network(stream) => Pin::new(stream).poll_shutdown(cx),
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => Pin::new(stream).poll_shutdown(cx),
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(send, _) => {
|
|
|
|
// TODO(mmastrac): surface the h2 error?
|
|
|
|
let res = send
|
|
|
|
.send_data(Bytes::new(), false)
|
|
|
|
.map_err(|_| std::io::Error::from(ErrorKind::Other));
|
|
|
|
Poll::Ready(res)
|
|
|
|
}
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
match &self.stream {
|
|
|
|
WsStreamKind::Network(stream) => stream.is_write_vectored(),
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => stream.is_write_vectored(),
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(..) => false,
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_write_vectored(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
bufs: &[std::io::IoSlice<'_>],
|
|
|
|
) -> std::task::Poll<Result<usize, std::io::Error>> {
|
|
|
|
match &mut self.stream {
|
|
|
|
WsStreamKind::Network(stream) => {
|
|
|
|
Pin::new(stream).poll_write_vectored(cx, bufs)
|
|
|
|
}
|
2023-04-23 16:07:37 -04:00
|
|
|
WsStreamKind::Upgraded(stream) => {
|
2023-04-22 13:48:21 -04:00
|
|
|
Pin::new(stream).poll_write_vectored(cx, bufs)
|
|
|
|
}
|
2023-11-01 17:11:01 -04:00
|
|
|
WsStreamKind::H2(..) => {
|
|
|
|
// TODO(mmastrac): this is possibly just too difficult, but we'll never call it
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|