diff --git a/Cargo.lock b/Cargo.lock index 8b49394..85e9f8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2168,6 +2168,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -4385,6 +4395,7 @@ dependencies = [ "iroh-io 0.4.0", "lru", "md5", + "mime_guess", "percent-encoding", "quic-rpc", "quinn", @@ -4439,6 +4450,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/Cargo.toml b/Cargo.toml index 60006c6..15e983e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,10 +32,11 @@ thiserror = "1.0" tokio = { version = "1", features = ["full"] } tokio-stream = "0.1" tokio-util = "0.7" -tower-http = { version = "0.5", features = ["trace"] } +tower-http = { version = "0.5", features = ["trace", "cors"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } futures = "0.3" lru = "0.12" tokio-task-pool = "0.1" url = "2.5.0" +mime_guess = "2.0.4" diff --git a/bin/main.rs b/bin/main.rs index 2067b04..f9bf83d 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -25,6 +25,7 @@ use tokio_util::io::{ReaderStream, StreamReader}; use tokio_util::sync::CancellationToken; use tokio_util::task::TaskTracker; use tower_http::trace::{self, TraceLayer}; +use tower_http::cors::CorsLayer; use tracing::{info, info_span, Instrument, Level}; use tracing_subscriber::EnvFilter; use trident_storage::config::{Config, TableConfig}; @@ -166,6 +167,7 @@ async fn app() -> Result<(), Error> { .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), ) + .layer(CorsLayer::permissive()) .with_state(state); // run our app with hyper, listening globally on port 3000 @@ -263,11 +265,13 @@ async fn blobs_get( Ok(Some((reader, file_size))) => match method { Method::HEAD => Response::builder() .header("Content-Length", file_size) + .header("Content-Type", mime_guess::mime::OCTET_STREAM.to_string()) .header("X-Iroh-Hash", hash_str) .body(Body::default()) .unwrap(), Method::GET => Response::builder() .header("Content-Length", file_size) + .header("Content-Type", mime_guess::mime::OCTET_STREAM.to_string()) .header("X-Iroh-Hash", hash_str) .body(Body::from_stream(ReaderStream::new(reader))) .unwrap(), @@ -501,11 +505,13 @@ async fn table_get( Ok(Some((reader, file_size, hash))) => match method { Method::HEAD => Response::builder() .header("Content-Length", file_size) + .header("Content-Type", mime_guess::from_ext(&key).first_or_octet_stream().to_string()) .header("X-Iroh-Hash", hash.to_string()) .body(Body::default()) .unwrap(), Method::GET => Response::builder() .header("Content-Length", file_size) + .header("Content-Type", mime_guess::from_ext(&key).first_or_octet_stream().to_string()) .header("X-Iroh-Hash", hash.to_string()) .body(Body::from_stream(ReaderStream::new(reader))) .unwrap(),