diff --git a/src/wsgi/callbacks.rs b/src/wsgi/callbacks.rs index 1716a00e..c547117b 100644 --- a/src/wsgi/callbacks.rs +++ b/src/wsgi/callbacks.rs @@ -29,6 +29,13 @@ fn run_callback(callback: PyObject, scope: Scope) -> PyResult<(i32, Vec<(String, }) } -pub(crate) fn call_http(cb: CallbackWrapper, scope: Scope) -> JoinHandle, Body)>> { +pub(crate) fn call_rtb_http(cb: CallbackWrapper, scope: Scope) -> PyResult<(i32, Vec<(String, String)>, Body)> { + run_callback(cb.callback, scope) +} + +pub(crate) fn call_rtt_http( + cb: CallbackWrapper, + scope: Scope, +) -> JoinHandle, Body)>> { tokio::task::spawn_blocking(move || run_callback(cb.callback, scope)) } diff --git a/src/wsgi/http.rs b/src/wsgi/http.rs index 3a46cccd..e74882a7 100644 --- a/src/wsgi/http.rs +++ b/src/wsgi/http.rs @@ -4,7 +4,10 @@ use hyper::{ }; use std::net::SocketAddr; -use super::{callbacks::call_http, types::WSGIScope as Scope}; +use super::{ + callbacks::{call_rtb_http, call_rtt_http}, + types::WSGIScope as Scope, +}; use crate::{ callbacks::CallbackWrapper, http::{response_500, HV_SERVER}, @@ -26,7 +29,7 @@ fn build_response(status: i32, pyheaders: Vec<(String, String)>, body: Body) -> res } -pub(crate) async fn handle( +pub(crate) async fn handle_rtt( _rt: RuntimeRef, callback: CallbackWrapper, server_addr: SocketAddr, @@ -34,8 +37,7 @@ pub(crate) async fn handle( req: Request, scheme: &str, ) -> Response { - let scope = Scope::new(scheme, server_addr, client_addr, req).await; - if let Ok(res) = call_http(callback, scope).await { + if let Ok(res) = call_rtt_http(callback, Scope::new(scheme, server_addr, client_addr, req).await).await { if let Ok((status, headers, body)) = res { return build_response(status, headers, body); } @@ -45,3 +47,20 @@ pub(crate) async fn handle( } response_500() } + +pub(crate) async fn handle_rtb( + _rt: RuntimeRef, + callback: CallbackWrapper, + server_addr: SocketAddr, + client_addr: SocketAddr, + req: Request, + scheme: &str, +) -> Response { + match call_rtb_http(callback, Scope::new(scheme, server_addr, client_addr, req).await) { + Ok((status, headers, body)) => build_response(status, headers, body), + _ => { + log::warn!("Application callable raised an exception"); + response_500() + } + } +} diff --git a/src/wsgi/serve.rs b/src/wsgi/serve.rs index 33f3ff72..0b60e8b3 100644 --- a/src/wsgi/serve.rs +++ b/src/wsgi/serve.rs @@ -1,6 +1,6 @@ use pyo3::prelude::*; -use super::http::handle; +use super::http::{handle_rtb, handle_rtt}; use crate::workers::{serve_rth, serve_rth_ssl, serve_wth, serve_wth_ssl, WorkerConfig}; #[pyclass(module = "granian._granian")] @@ -9,10 +9,10 @@ pub struct WSGIWorker { } impl WSGIWorker { - serve_rth!(_serve_rth, handle); - serve_wth!(_serve_wth, handle); - serve_rth_ssl!(_serve_rth_ssl, handle); - serve_wth_ssl!(_serve_wth_ssl, handle); + serve_rth!(_serve_rth, handle_rtb); + serve_wth!(_serve_wth, handle_rtt); + serve_rth_ssl!(_serve_rth_ssl, handle_rtb); + serve_wth_ssl!(_serve_wth_ssl, handle_rtt); } #[pymethods]