Browse Source

stash code

lloydzhou 1 year ago
parent
commit
f9d4105170
2 changed files with 44 additions and 8 deletions
  1. 20 2
      app/utils/stream.ts
  2. 24 6
      src-tauri/src/stream.rs

+ 20 - 2
app/utils/stream.ts

@@ -21,7 +21,12 @@ type StreamResponse = {
 
 export function fetch(url: string, options?: RequestInit): Promise<any> {
   if (window.__TAURI__) {
-    const { signal, method = "GET", headers = {}, body = [] } = options || {};
+    const {
+      signal,
+      method = "GET",
+      headers: _headers = {},
+      body = [],
+    } = options || {};
     let unlisten: Function | undefined;
     let request_id = 0;
     const ts = new TransformStream();
@@ -32,10 +37,10 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
       writer.ready.then(() => {
         try {
           writer.releaseLock();
+          ts.writable.close();
         } catch (e) {
           console.error(e);
         }
-        ts.writable.close();
       });
     };
 
@@ -61,6 +66,19 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
       })
       .then((u: Function) => (unlisten = u));
 
+    const headers = {
+      Accept: "*",
+      Connection: "close",
+      Origin: "http://localhost:3000",
+      Referer: "http://localhost:3000/",
+      "Sec-Fetch-Dest": "empty",
+      "Sec-Fetch-Mode": "cors",
+      "Sec-Fetch-Site": "cross-site",
+      "User-Agent": navigator.userAgent,
+    };
+    for (const item of new Headers(_headers || {})) {
+      headers[item[0]] = item[1];
+    }
     return window.__TAURI__
       .invoke("stream_fetch", {
         method,

+ 24 - 6
src-tauri/src/stream.rs

@@ -53,15 +53,33 @@ pub async fn stream_fetch(
   }
 
   let mut _headers = HeaderMap::new();
-  for (key, value) in headers {
-      _headers.insert(key.parse::<HeaderName>().unwrap(), value.parse().unwrap());
+  for (key, value) in &headers {
+    _headers.insert(key.parse::<HeaderName>().unwrap(), value.parse().unwrap());
   }
-  let body = bytes::Bytes::from(body);
 
-  let response_future = Client::new().request(
-    method.parse::<reqwest::Method>().map_err(|err| format!("failed to parse method: {}", err))?,
+  println!("method: {:?}", method);
+  println!("url: {:?}", url);
+  println!("headers: {:?}", headers);
+  println!("headers: {:?}", _headers);
+
+  let method = method.parse::<reqwest::Method>().map_err(|err| format!("failed to parse method: {}", err))?;
+  let client = Client::builder()
+    .user_agent("Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15")
+    .default_headers(_headers)
+    .build()
+    .map_err(|err| format!("failed to generate client: {}", err))?;
+
+  let mut request = client.request(
+    method.clone(),
     url.parse::<reqwest::Url>().map_err(|err| format!("failed to parse url: {}", err))?
-  ).headers(_headers).body(body).send();
+  );
+
+  if method == reqwest::Method::POST {
+    let body = bytes::Bytes::from(body);
+    println!("body: {:?}", body);
+    request = request.body(body);
+  }
+  let response_future = request.send();
 
   let res = response_future.await;
   let response = match res {