Skip to content

Commit

Permalink
docs: use http::* qualified import instead of an alias (#7873)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Sep 21, 2023
1 parent a2021c3 commit c3ac1f8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
7 changes: 3 additions & 4 deletions core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
Dispatch, Runtime, UserEvent, WindowBuilder,
};

use http::{Request as HttpRequest, Response as HttpResponse};
use serde::{Deserialize, Deserializer};
use tauri_utils::{config::WindowConfig, Theme};
use url::Url;
Expand All @@ -25,13 +24,13 @@ use std::{

use self::dpi::PhysicalPosition;

type UriSchemeProtocol = dyn Fn(HttpRequest<Vec<u8>>, Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>) + Send>)
type UriSchemeProtocol = dyn Fn(http::Request<Vec<u8>>, Box<dyn FnOnce(http::Response<Cow<'static, [u8]>>) + Send>)
+ Send
+ Sync
+ 'static;

type WebResourceRequestHandler =
dyn Fn(HttpRequest<Vec<u8>>, &mut HttpResponse<Cow<'static, [u8]>>) + Send + Sync;
dyn Fn(http::Request<Vec<u8>>, &mut http::Response<Cow<'static, [u8]>>) + Send + Sync;

type NavigationHandler = dyn Fn(&Url) -> bool + Send;

Expand Down Expand Up @@ -310,7 +309,7 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {

pub fn register_uri_scheme_protocol<
N: Into<String>,
H: Fn(HttpRequest<Vec<u8>>, Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>) + Send>)
H: Fn(http::Request<Vec<u8>>, Box<dyn FnOnce(http::Response<Cow<'static, [u8]>>) + Send>)
+ Send
+ Sync
+ 'static,
Expand Down
19 changes: 9 additions & 10 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
channel::ChannelDataIpcQueue, CallbackFn, Invoke, InvokeError, InvokeHandler, InvokeResponder,
InvokeResponse,
},
manager::{Asset, CustomProtocol, WindowManager},
manager::{Asset, UriSchemeProtocol, WindowManager},
plugin::{Plugin, PluginStore},
runtime::{
webview::WebviewAttributes,
Expand All @@ -32,7 +32,6 @@ use crate::menu::{Menu, MenuEvent};
use crate::tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconId};
#[cfg(desktop)]
use crate::window::WindowMenu;
use http::{Request as HttpRequest, Response as HttpResponse};
use raw_window_handle::HasRawDisplayHandle;
use serde::Deserialize;
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
Expand Down Expand Up @@ -991,7 +990,7 @@ pub struct Builder<R: Runtime> {
plugins: PluginStore<R>,

/// The webview protocols available to all windows.
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol<R>>>,
uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>,

/// App state.
state: StateManager,
Expand Down Expand Up @@ -1373,15 +1372,15 @@ impl<R: Runtime> Builder<R> {
pub fn register_uri_scheme_protocol<
N: Into<String>,
T: Into<Cow<'static, [u8]>>,
H: Fn(&AppHandle<R>, HttpRequest<Vec<u8>>) -> HttpResponse<T> + Send + Sync + 'static,
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>) -> http::Response<T> + Send + Sync + 'static,
>(
mut self,
uri_scheme: N,
protocol: H,
) -> Self {
self.uri_scheme_protocols.insert(
uri_scheme.into(),
Arc::new(CustomProtocol {
Arc::new(UriSchemeProtocol {
protocol: Box::new(move |app, request, responder| {
responder.respond(protocol(app, request))
}),
Expand Down Expand Up @@ -1421,15 +1420,15 @@ impl<R: Runtime> Builder<R> {
#[must_use]
pub fn register_asynchronous_uri_scheme_protocol<
N: Into<String>,
H: Fn(&AppHandle<R>, HttpRequest<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
>(
mut self,
uri_scheme: N,
protocol: H,
) -> Self {
self.uri_scheme_protocols.insert(
uri_scheme.into(),
Arc::new(CustomProtocol {
Arc::new(UriSchemeProtocol {
protocol: Box::new(protocol),
}),
);
Expand Down Expand Up @@ -1645,14 +1644,14 @@ impl<R: Runtime> Builder<R> {
}
}

pub(crate) type UriSchemeResponderFn = Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>) + Send>;
pub(crate) type UriSchemeResponderFn = Box<dyn FnOnce(http::Response<Cow<'static, [u8]>>) + Send>;
pub struct UriSchemeResponder(pub(crate) UriSchemeResponderFn);

impl UriSchemeResponder {
/// Resolves the request with the given response.
pub fn respond<T: Into<Cow<'static, [u8]>>>(self, response: HttpResponse<T>) {
pub fn respond<T: Into<Cow<'static, [u8]>>>(self, response: http::Response<T>) {
let (parts, body) = response.into_parts();
(self.0)(HttpResponse::from_parts(parts, body.into()))
(self.0)(http::Response::from_parts(parts, body.into()))
}
}

Expand Down
23 changes: 12 additions & 11 deletions core/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::borrow::Cow;

use http::{
header::{ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE},
HeaderValue, Method, Request as HttpRequest, Response as HttpResponse, StatusCode,
HeaderValue, Method, StatusCode,
};

use crate::{
Expand Down Expand Up @@ -49,15 +49,16 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
Box::new(move |_window, _cmd, response, _callback, _error| {
let (mut response, mime_type) = match response {
InvokeResponse::Ok(InvokeBody::Json(v)) => (
HttpResponse::new(serde_json::to_vec(&v).unwrap().into()),
http::Response::new(serde_json::to_vec(&v).unwrap().into()),
mime::APPLICATION_JSON,
),
InvokeResponse::Ok(InvokeBody::Raw(v)) => {
(HttpResponse::new(v.into()), mime::APPLICATION_OCTET_STREAM)
}
InvokeResponse::Ok(InvokeBody::Raw(v)) => (
http::Response::new(v.into()),
mime::APPLICATION_OCTET_STREAM,
),
InvokeResponse::Err(e) => {
let mut response =
HttpResponse::new(serde_json::to_vec(&e.0).unwrap().into());
http::Response::new(serde_json::to_vec(&e.0).unwrap().into());
*response.status_mut() = StatusCode::BAD_REQUEST;
(response, mime::TEXT_PLAIN)
}
Expand All @@ -74,7 +75,7 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
}
Err(e) => {
respond(
HttpResponse::builder()
http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
.body(e.as_bytes().to_vec().into())
Expand All @@ -84,7 +85,7 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
}
} else {
respond(
HttpResponse::builder()
http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
.body(
Expand All @@ -99,7 +100,7 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
}

Method::OPTIONS => {
let mut r = HttpResponse::new(Vec::new().into());
let mut r = http::Response::new(Vec::new().into());
r.headers_mut().insert(
ACCESS_CONTROL_ALLOW_HEADERS,
HeaderValue::from_static("Content-Type, Tauri-Callback, Tauri-Error, Tauri-Channel-Id"),
Expand All @@ -108,7 +109,7 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
}

_ => {
let mut r = HttpResponse::new(
let mut r = http::Response::new(
"only POST and OPTIONS are allowed"
.as_bytes()
.to_vec()
Expand Down Expand Up @@ -286,7 +287,7 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &WindowManager<R>, l

fn parse_invoke_request<R: Runtime>(
#[allow(unused_variables)] manager: &WindowManager<R>,
request: HttpRequest<Vec<u8>>,
request: http::Request<Vec<u8>>,
) -> std::result::Result<InvokeRequest, String> {
#[allow(unused_mut)]
let (parts, mut body) = request.into_parts();
Expand Down
10 changes: 5 additions & 5 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use serde::Serialize;
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
use url::Url;

use http::Request as HttpRequest;
use tauri_macros::default_runtime;
use tauri_utils::debug_eprintln;
use tauri_utils::{
Expand Down Expand Up @@ -236,7 +235,7 @@ pub struct InnerWindowManager<R: Runtime> {

package_info: PackageInfo,
/// The webview protocols available to all windows.
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol<R>>>,
uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>,
/// A set containing a reference to the active menus, including
/// the app-wide menu and the window-specific menus
///
Expand Down Expand Up @@ -305,10 +304,11 @@ pub struct Asset {
}

/// Uses a custom URI scheme handler to resolve file requests
pub struct CustomProtocol<R: Runtime> {
pub struct UriSchemeProtocol<R: Runtime> {
/// Handler for protocol
#[allow(clippy::type_complexity)]
pub protocol: Box<dyn Fn(&AppHandle<R>, HttpRequest<Vec<u8>>, UriSchemeResponder) + Send + Sync>,
pub protocol:
Box<dyn Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>,
}

#[default_runtime(crate::Wry, wry)]
Expand All @@ -332,7 +332,7 @@ impl<R: Runtime> WindowManager<R> {
plugins: PluginStore<R>,
invoke_handler: Box<InvokeHandler<R>>,
on_page_load: Box<OnPageLoad<R>>,
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol<R>>>,
uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>,
state: StateManager,
window_event_listeners: Vec<GlobalWindowEventListener<R>>,
#[cfg(desktop)] window_menu_event_listeners: HashMap<
Expand Down
7 changes: 3 additions & 4 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use crate::{
CursorIcon, Icon,
};

use http::{Request as HttpRequest, Response as HttpResponse};
use serde::Serialize;
#[cfg(windows)]
use windows::Win32::Foundation::HWND;
Expand All @@ -60,10 +59,10 @@ use std::{
};

pub(crate) type WebResourceRequestHandler =
dyn Fn(HttpRequest<Vec<u8>>, &mut HttpResponse<Cow<'static, [u8]>>) + Send + Sync;
dyn Fn(http::Request<Vec<u8>>, &mut http::Response<Cow<'static, [u8]>>) + Send + Sync;
pub(crate) type NavigationHandler = dyn Fn(&Url) -> bool + Send;
pub(crate) type UriSchemeProtocolHandler =
Box<dyn Fn(HttpRequest<Vec<u8>>, UriSchemeResponder) + Send + Sync>;
Box<dyn Fn(http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>;

#[derive(Clone, Serialize)]
struct WindowCreatedEvent {
Expand Down Expand Up @@ -292,7 +291,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// });
/// ```
pub fn on_web_resource_request<
F: Fn(HttpRequest<Vec<u8>>, &mut HttpResponse<Cow<'static, [u8]>>) + Send + Sync + 'static,
F: Fn(http::Request<Vec<u8>>, &mut http::Response<Cow<'static, [u8]>>) + Send + Sync + 'static,
>(
mut self,
f: F,
Expand Down

0 comments on commit c3ac1f8

Please sign in to comment.