-
Notifications
You must be signed in to change notification settings - Fork 431
HTTP add stream support #2479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP add stream support #2479
Changes from 6 commits
643039c
5edea81
bdbb3d7
39639ea
44ee279
fd7bd41
ca3651b
0a0a428
ba122e6
0a2645f
e747212
9b472c2
57eb09b
acafdd8
d75de7b
194fdee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
||
use std::{future::Future, pin::Pin, str::FromStr, sync::Arc, time::Duration}; | ||
|
||
use http::{header, HeaderMap, HeaderName, HeaderValue, Method, StatusCode}; | ||
|
@@ -10,7 +11,7 @@ use serde::{Deserialize, Serialize}; | |
use tauri::{ | ||
async_runtime::Mutex, | ||
command, | ||
ipc::{CommandScope, GlobalScope}, | ||
ipc::{Channel, CommandScope, GlobalScope}, | ||
Manager, ResourceId, ResourceTable, Runtime, State, Webview, | ||
}; | ||
use tokio::sync::oneshot::{channel, Receiver, Sender}; | ||
|
@@ -22,6 +23,8 @@ use crate::{ | |
|
||
const HTTP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); | ||
|
||
// reqwest::Response is never read, but might be needed for future use. | ||
#[allow(dead_code)] | ||
struct ReqwestResponse(reqwest::Response); | ||
impl tauri::Resource for ReqwestResponse {} | ||
|
||
|
@@ -126,6 +129,12 @@ pub struct BasicAuth { | |
password: String, | ||
} | ||
|
||
#[derive(Clone, Serialize)] | ||
pub struct StreamMessage { | ||
value: Option<Vec<u8>>, | ||
done: bool, | ||
} | ||
|
||
|
||
#[inline] | ||
fn proxy_creator( | ||
url_or_config: UrlOrConfig, | ||
|
@@ -181,6 +190,7 @@ pub async fn fetch<R: Runtime>( | |
client_config: ClientConfig, | ||
command_scope: CommandScope<Entry>, | ||
global_scope: GlobalScope<Entry>, | ||
stream_channel: Channel<StreamMessage> | ||
) -> crate::Result<ResourceId> { | ||
let ClientConfig { | ||
method, | ||
|
@@ -314,7 +324,24 @@ pub async fn fetch<R: Runtime>( | |
#[cfg(feature = "tracing")] | ||
tracing::trace!("{:?}", request); | ||
|
||
let fut = async move { request.send().await.map_err(Into::into) }; | ||
let fut = async move { | ||
let mut res = request.send().await?; | ||
|
||
// send response through IPC channel | ||
while let Some(chunk) = res.chunk().await? { | ||
stream_channel.send(StreamMessage{ | ||
value: Some(chunk.to_vec()), | ||
done: false, | ||
})?; | ||
} | ||
|
||
stream_channel.send(StreamMessage { value: None, done: true })?; | ||
|
||
// return that response | ||
Ok(res) | ||
}; | ||
|
||
|
||
let mut resources_table = webview.resources_table(); | ||
let rid = resources_table.add_request(Box::pin(fut)); | ||
|
||
|
@@ -410,19 +437,6 @@ pub async fn fetch_send<R: Runtime>( | |
}) | ||
} | ||
|
||
#[tauri::command] | ||
pub(crate) async fn fetch_read_body<R: Runtime>( | ||
webview: Webview<R>, | ||
rid: ResourceId, | ||
) -> crate::Result<tauri::ipc::Response> { | ||
let res = { | ||
let mut resources_table = webview.resources_table(); | ||
resources_table.take::<ReqwestResponse>(rid)? | ||
}; | ||
let res = Arc::into_inner(res).unwrap().0; | ||
Ok(tauri::ipc::Response::new(res.bytes().await?.to_vec())) | ||
} | ||
|
||
// forbidden headers per fetch spec https://fetch.spec.whatwg.org/#terminology-headers | ||
#[cfg(not(feature = "unsafe-headers"))] | ||
fn is_unsafe_header(header: &HeaderName) -> bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove, no need to keep it around anymore