Skip to content

Commit

Permalink
Bump tungstenite to 0.26
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Jan 8, 2025
1 parent f374b4e commit 115bc85
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
34 changes: 27 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ socket2 = { version = "0.5", features = ["all"] }
tls-listener = { version = "=0.10", features = ["rustls-ring"] }
tokio = { version = "1.40", features = ["full"] }
tokio-stream = "0.1"
tokio-tungstenite = "=0.24"
tokio-tungstenite = "=0.26"
tokio-util = { version = "0.7", features = ["codec"] }

[build-dependencies]
Expand Down
15 changes: 9 additions & 6 deletions src/asgi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::{
types::ASGIMessageType,
};
use crate::{
conversion::BytesToPy,
conversion::{BytesToPy, Utf8BytesToPy},
http::{response_404, HTTPResponse, HTTPResponseBody, HV_SERVER},
runtime::{empty_future_into_py, future_into_py_futlike, future_into_py_iter, Runtime, RuntimeRef},
ws::{HyperWebsocket, UpgradeData, WSRxStream, WSTxStream},
Expand Down Expand Up @@ -542,10 +542,13 @@ fn ws_message_into_rs(py: Python, message: &Bound<PyDict>) -> PyResult<Message>
let data: Cow<[u8]> = item.extract().unwrap_or(EMPTY_BYTES);
Ok(data[..].into())
}
(None, Some(item)) => Ok(Message::Text(item.extract().unwrap_or(EMPTY_STRING))),
(Some(itemb), Some(itemt)) => match (itemb.extract().unwrap_or(None), itemt.extract().unwrap_or(None)) {
(Some(msgb), None) => Ok(Message::Binary(msgb)),
(None, Some(msgt)) => Ok(Message::Text(msgt)),
(None, Some(item)) => Ok(Message::Text(item.extract::<String>().unwrap_or(EMPTY_STRING).into())),
(Some(itemb), Some(itemt)) => match (itemb.is_none(), itemt.is_none()) {
(false, true) => {
let data: Box<[u8]> = itemb.extract::<Cow<[u8]>>()?.into();
Ok(Message::Binary(body::Bytes::from(data)))
}
(true, false) => Ok(itemt.extract::<String>()?.into()),
_ => error_message!(),
},
_ => error_message!(),
Expand All @@ -564,7 +567,7 @@ fn ws_message_into_py(message: Message) -> PyResult<PyObject> {
Message::Text(message) => Python::with_gil(|py| {
let dict = PyDict::new(py);
dict.set_item(pyo3::intern!(py, "type"), pyo3::intern!(py, "websocket.receive"))?;
dict.set_item(pyo3::intern!(py, "text"), message)?;
dict.set_item(pyo3::intern!(py, "text"), Utf8BytesToPy(message))?;
Ok(dict.into_any().unbind())
}),
Message::Close(frame) => Python::with_gil(|py| {
Expand Down
11 changes: 11 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use pyo3::{prelude::*, IntoPyObjectExt};
use crate::workers::{HTTP1Config, HTTP2Config};

pub(crate) struct BytesToPy(pub hyper::body::Bytes);
pub(crate) struct Utf8BytesToPy(pub tokio_tungstenite::tungstenite::Utf8Bytes);

impl<'p> IntoPyObject<'p> for BytesToPy {
type Target = PyAny;
Expand All @@ -14,6 +15,16 @@ impl<'p> IntoPyObject<'p> for BytesToPy {
}
}

impl<'p> IntoPyObject<'p> for Utf8BytesToPy {
type Target = PyAny;
type Output = Bound<'p, Self::Target>;
type Error = PyErr;

fn into_pyobject(self, py: Python<'p>) -> Result<Self::Output, Self::Error> {
self.0.as_str().into_bound_py_any(py)
}
}

pub(crate) fn worker_http1_config_from_py(py: Python, cfg: Option<PyObject>) -> PyResult<HTTP1Config> {
let ret = match cfg {
Some(cfg) => HTTP1Config {
Expand Down
2 changes: 1 addition & 1 deletion src/rsgi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl RSGIWebsocketTransport {

future_into_py_futlike(self.rt.clone(), py, async move {
if let Ok(mut stream) = transport.try_lock() {
return match stream.send(Message::Text(data)).await {
return match stream.send(data.into()).await {
Ok(()) => Ok(pynone),
_ => {
Python::with_gil(|_| drop(pynone));
Expand Down

0 comments on commit 115bc85

Please sign in to comment.