tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
825 stars 227 forks source link

[bug] wrong content-type returned via tiny-http (Rust) #880

Open liudonghua123 opened 8 months ago

liudonghua123 commented 8 months ago

Describe the bug

Hi, I tried to use tauri to package some static files (webvm), and I found one resource /debian_mini_20240103_7393161196.ext2 request returned Content-Type: text/html and Transfer-Encoding: chunked.

The mime type of unknown file maybe represent as binary file (Content-Type: application/octet-stream).

Reproduction

No response

Expected behavior

I expected the server returned Content-Type: application/octet-stream and Content-Range: bytes 0-1/629145600 for /debian_mini_20240103_7393161196.ext2.

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.22631 X64
    ✔ WebView2: 120.0.2210.91
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.75.0 (82e1608df 2023-12-21)
    ✔ cargo: 1.75.0 (1d8b05cdd 2023-11-20)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.18.2
    - pnpm: 8.9.0
    - yarn: 1.22.17
    - npm: 10.2.4

[-] Packages
    - tauri [RUST]: 1.5.3
    - tauri-build [RUST]: 1.5.0
    - wry [RUST]: 0.24.6
    - tao [RUST]: 0.16.5
    - tauri-cli [RUST]: 1.3.1
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 1.5.7

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../../../../Downloads/msi/webvm
    - devPath: ../src

Stack trace

No response

Additional context

How can I configure/overwrite the mime type of a specified resource? And is there any way to make the response return Content-Length and Content-Range instead of Transfer-Encoding: chunked.

I also posted a discussion on https://github.com/tauri-apps/tauri/discussions/8533.

amrbashir commented 8 months ago

Please provide more information and a reproduction repo and how tiny-http is used

liudonghua123 commented 8 months ago

Please provide more information and a reproduction repo and how tiny-http is used

I did not use tiny-http, it's just the response header (Server: tiny-http (Rust)).

The only changes I made is the following after I execute yarn create tauri-app.

  1. Cargo.toml, add tauri-plugin-localhost and portpicker dependencies
[package]
name = "tauri-app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.5", features = [] }

[dependencies]
# https://docs.rs/tauri/latest/tauri/
tauri = { version = "1.5", features = ["devtools", "http-multipart", "shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri-plugin-localhost = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
# tauri-plugin-shell = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
portpicker = "0.1" # used in the example to pick a random free port

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]
  1. main.rs, tauri-plugin-localhost tauri-plugin-localhost and portpicker plugins.
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{utils::config::AppUrl, WindowUrl};

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

fn main() {
    let port = portpicker::pick_unused_port().expect("failed to find unused port");

    let mut context = tauri::generate_context!();
    let url = format!("http://localhost:{}", port).parse().unwrap();
    let window_url = WindowUrl::External(url);
    // rewrite the config so the IPC is enabled on this URL
    context.config_mut().build.dist_dir = AppUrl::Url(window_url.clone());

    tauri::Builder::default()
        //.plugin(tauri_plugin_shell::init())
        .plugin(
            tauri_plugin_localhost::Builder::new(port)
                .on_request(|_req, resp| {
                    resp.add_header("Cross-Origin-Opener-Policy", "same-origin");
                    resp.add_header("Cross-Origin-Embedder-Policy", "require-corp");
                })
                .build(),
        )
        .invoke_handler(tauri::generate_handler![greet])
        .run(context)
        .expect("error while running tauri application");
}
  1. And update the build.distDir of tauri.conf.json.
liudonghua123 commented 8 months ago

I read https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding, it seems the default configured tauri serve do not support Accept-Ranges: bytes.

liudonghua123 commented 8 months ago

I found the doc https://docs.rs/tauri/latest/tauri/api/http/header/constant.ACCEPT_RANGES.html, and I tried to add "http-api" features, but it's the same, the server did not reply Accept-Ranges and did not support requesting a specific range of resource. Maybe I need to go through the code and find what I missed.

liudonghua123 commented 8 months ago

Please provide more information and a reproduction repo and how tiny-http is used

@amrbashir Hi, I use tauri-plugin-localhost plugin which supports adding Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp and serve the static files via http://localhost:<some_port> which is needed to enable SharedArrayBuffer.

But the tiny-http lib does not support ranges features currently.

Is there any method which I can enable SharedArrayBuffer in tauri and support http range feature.

I found maybe I can replace the tiny-http with http-serve which provides more complete http server features like range in a forked tauri-plugin-localhost plugin. But I am not familiar with rust.

amrbashir commented 8 months ago

I see, I will move the issue to plugins-workspace then