allow asset handlers to read the whole request
This commit is contained in:
1 parent
1002bc955b
commit
7649ad84a1
3 files changed
+61
-19
No files matched your search
@@ -32,7 +32,7 @@ use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent};
|
|||||||
use element::DesktopElement;
|
use element::DesktopElement;
|
||||||
use eval::init_eval;
|
use eval::init_eval;
|
||||||
use futures_util::{pin_mut, FutureExt};
|
use futures_util::{pin_mut, FutureExt};
|
||||||
pub use protocol::{use_asset_handler, AssetFuture, AssetHandler, AssetResponse};
|
pub use protocol::{use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse};
|
||||||
use shortcut::ShortcutRegistry;
|
use shortcut::ShortcutRegistry;
|
||||||
pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError};
|
pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use dioxus_interpreter_js::{COMMON_JS, INTERPRETER_JS};
|
|||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
collections::HashMap,
|
|
||||||
future::Future,
|
future::Future,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
@@ -72,13 +72,57 @@ pub type AssetResponse = Response<Cow<'static, [u8]>>;
|
|||||||
pub trait AssetFuture: Future<Output = Option<AssetResponse>> + Send + Sync + 'static {}
|
pub trait AssetFuture: Future<Output = Option<AssetResponse>> + Send + Sync + 'static {}
|
||||||
impl<T: Future<Output = Option<AssetResponse>> + Send + Sync + 'static> AssetFuture for T {}
|
impl<T: Future<Output = Option<AssetResponse>> + Send + Sync + 'static> AssetFuture for T {}
|
||||||
|
|
||||||
/// A handler that takes an asset [`Path`] and returns a future that loads the path.
|
/// A request for an asset. This is a wrapper around [`Request<Vec<u8>>`] that provides methods specific to asset requests.
|
||||||
|
pub struct AssetRequest {
|
||||||
|
path: PathBuf,
|
||||||
|
request: Request<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AssetRequest {
|
||||||
|
/// Get the path the asset request is for
|
||||||
|
pub fn path(&self) -> &Path {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Request<Vec<u8>>> for AssetRequest {
|
||||||
|
fn from(request: Request<Vec<u8>>) -> Self {
|
||||||
|
let decoded = urlencoding::decode(request.uri().path().trim_start_matches('/'))
|
||||||
|
.expect("expected URL to be UTF-8 encoded");
|
||||||
|
let path = PathBuf::from(&*decoded);
|
||||||
|
Self { request, path }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for AssetRequest {
|
||||||
|
type Target = Request<Vec<u8>>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for AssetRequest {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A handler that takes an [`AssetRequest`] and returns a future that either loads the asset, or returns `None`.
|
||||||
/// This handler is stashed indefinitely in a context object, so it must be `'static`.
|
/// This handler is stashed indefinitely in a context object, so it must be `'static`.
|
||||||
pub trait AssetHandler<F: AssetFuture>: Fn(&Path) -> F + Send + Sync + 'static {}
|
pub trait AssetHandler<F: AssetFuture>: Send + Sync + 'static {
|
||||||
impl<F: AssetFuture, T: Fn(&Path) -> F + Send + Sync + 'static> AssetHandler<F> for T {}
|
/// Handle an asset request, returning a future that either loads the asset, or returns `None`
|
||||||
|
fn handle_request(&self, request: &AssetRequest) -> F;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: AssetFuture, T: Fn(&AssetRequest) -> F + Send + Sync + 'static> AssetHandler<F> for T {
|
||||||
|
fn handle_request(&self, request: &AssetRequest) -> F {
|
||||||
|
self(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type AssetHandlerRegistryInner =
|
type AssetHandlerRegistryInner =
|
||||||
Slab<Box<dyn Fn(&Path) -> Pin<Box<dyn AssetFuture>> + Send + Sync + 'static>>;
|
Slab<Box<dyn Fn(&AssetRequest) -> Pin<Box<dyn AssetFuture>> + Send + Sync + 'static>>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AssetHandlerRegistry(Arc<RwLock<AssetHandlerRegistryInner>>);
|
pub struct AssetHandlerRegistry(Arc<RwLock<AssetHandlerRegistryInner>>);
|
||||||
@@ -90,7 +134,7 @@ impl AssetHandlerRegistry {
|
|||||||
|
|
||||||
pub async fn register_handler<F: AssetFuture>(&self, f: impl AssetHandler<F>) -> usize {
|
pub async fn register_handler<F: AssetFuture>(&self, f: impl AssetHandler<F>) -> usize {
|
||||||
let mut registry = self.0.write().await;
|
let mut registry = self.0.write().await;
|
||||||
registry.insert(Box::new(move |path| Box::pin(f(path))))
|
registry.insert(Box::new(move |req| Box::pin(f.handle_request(req))))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_handler(&self, id: usize) -> Option<()> {
|
pub async fn remove_handler(&self, id: usize) -> Option<()> {
|
||||||
@@ -98,10 +142,10 @@ impl AssetHandlerRegistry {
|
|||||||
registry.try_remove(id).map(|_| ())
|
registry.try_remove(id).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn try_handlers(&self, path: &Path) -> Option<AssetResponse> {
|
pub async fn try_handlers(&self, req: &AssetRequest) -> Option<AssetResponse> {
|
||||||
let registry = self.0.read().await;
|
let registry = self.0.read().await;
|
||||||
for (_, handler) in registry.iter() {
|
for (_, handler) in registry.iter() {
|
||||||
if let Some(response) = handler(path).await {
|
if let Some(response) = handler(req).await {
|
||||||
return Some(response);
|
return Some(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,12 +208,14 @@ pub fn use_asset_handler<F: AssetFuture>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn desktop_handler(
|
pub(super) async fn desktop_handler(
|
||||||
request: &Request<Vec<u8>>,
|
request: Request<Vec<u8>>,
|
||||||
custom_head: Option<String>,
|
custom_head: Option<String>,
|
||||||
custom_index: Option<String>,
|
custom_index: Option<String>,
|
||||||
root_name: &str,
|
root_name: &str,
|
||||||
asset_handlers: &AssetHandlerRegistry,
|
asset_handlers: &AssetHandlerRegistry,
|
||||||
) -> Result<AssetResponse> {
|
) -> Result<AssetResponse> {
|
||||||
|
let request = AssetRequest::from(request);
|
||||||
|
|
||||||
// If the request is for the root, we'll serve the index.html file.
|
// If the request is for the root, we'll serve the index.html file.
|
||||||
if request.uri().path() == "/" {
|
if request.uri().path() == "/" {
|
||||||
// If a custom index is provided, just defer to that, expecting the user to know what they're doing.
|
// If a custom index is provided, just defer to that, expecting the user to know what they're doing.
|
||||||
@@ -204,25 +250,21 @@ pub(super) async fn desktop_handler(
|
|||||||
.map_err(From::from);
|
.map_err(From::from);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Else, try to serve a file from the filesystem.
|
|
||||||
let decoded = urlencoding::decode(request.uri().path().trim_start_matches('/'))
|
|
||||||
.expect("expected URL to be UTF-8 encoded");
|
|
||||||
let path = PathBuf::from(&*decoded);
|
|
||||||
|
|
||||||
// If the user provided a custom asset handler, then call it and return the response
|
// If the user provided a custom asset handler, then call it and return the response
|
||||||
// if the request was handled.
|
// if the request was handled.
|
||||||
if let Some(response) = asset_handlers.try_handlers(&path).await {
|
if let Some(response) = asset_handlers.try_handlers(&request).await {
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Else, try to serve a file from the filesystem.
|
// Else, try to serve a file from the filesystem.
|
||||||
|
|
||||||
// If the path is relative, we'll try to serve it from the assets directory.
|
// If the path is relative, we'll try to serve it from the assets directory.
|
||||||
let mut asset = get_asset_root()
|
let mut asset = get_asset_root()
|
||||||
.unwrap_or_else(|| Path::new(".").to_path_buf())
|
.unwrap_or_else(|| Path::new(".").to_path_buf())
|
||||||
.join(&path);
|
.join(&request.path);
|
||||||
|
|
||||||
if !asset.exists() {
|
if !asset.exists() {
|
||||||
asset = PathBuf::from("/").join(path);
|
asset = PathBuf::from("/").join(request.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if asset.exists() {
|
if asset.exists() {
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ pub fn build(
|
|||||||
let asset_handlers_ref = asset_handlers_ref.clone();
|
let asset_handlers_ref = asset_handlers_ref.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let response_res = protocol::desktop_handler(
|
let response_res = protocol::desktop_handler(
|
||||||
&request,
|
request,
|
||||||
custom_head.clone(),
|
custom_head.clone(),
|
||||||
index_file.clone(),
|
index_file.clone(),
|
||||||
&root_name,
|
&root_name,
|
||||||
|
|||||||
Reference in new issue
Block a user