Add CORS layer to reply to OPTIONS requests
Diff
Cargo.lock | 1 +
Cargo.toml | 1 +
src/main.rs | 6 +++++-
src/methods/repo/mod.rs | 5 ++++-
src/methods/repo/smart_git.rs | 11 +++++++++++
5 files changed, 19 insertions(+), 5 deletions(-)
@@ -2073,6 +2073,7 @@
"tokio",
"tokio-util",
"tower",
"tower-http",
"tower-layer",
"tower-service",
"tracing",
@@ -34,6 +34,7 @@
tower = "0.4"
tower-service = "0.3"
tower-layer = "0.3"
tower-http = { version = "0.3.4", features = ["cors"] }
tracing = "0.1"
tracing-subscriber = "0.3"
unix_mode = "0.1"
@@ -1,8 +1,8 @@
#![deny(clippy::pedantic)]
use std::{sync::Arc, time::Duration};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::{sync::Arc, time::Duration};
use askama::Template;
use axum::{
@@ -17,6 +17,7 @@
use bat::assets::HighlightingAssets;
use clap::Parser;
use syntect::html::ClassStyle;
use tower_http::cors::CorsLayer;
use tower_layer::layer_fn;
use tracing::{info, instrument};
@@ -102,7 +103,8 @@
.layer(layer_fn(LoggingMiddleware))
.layer(Extension(Arc::new(Git::new(syntax_set))))
.layer(Extension(db))
.layer(Extension(Arc::new(args.scan_path)));
.layer(Extension(Arc::new(args.scan_path)))
.layer(CorsLayer::new());
axum::Server::bind(&args.bind_address)
.serve(app.into_make_service_with_connect_info::<std::net::SocketAddr>())
@@ -50,7 +50,10 @@
bytes::Bytes: From<ReqBody::Data>,
<ReqBody as HttpBody>::Error: std::error::Error + Send + Sync,
{
let scan_path = request.extensions().get::<Arc<PathBuf>>().expect("scan_path missing");
let scan_path = request
.extensions()
.get::<Arc<PathBuf>>()
.expect("scan_path missing");
let mut uri_parts: Vec<&str> = request
.uri()
@@ -63,7 +63,10 @@
let resp = cgi_to_response(&out.stdout)?;
if out.stderr.len() > 0 {
warn!("Git returned an error: `{}`", String::from_utf8_lossy(&out.stderr));
warn!(
"Git returned an error: `{}`",
String::from_utf8_lossy(&out.stderr)
);
}
Ok(resp)
@@ -100,7 +103,11 @@
);
}
if let Some(status) = response.headers_mut().remove("Status").filter(|s| s.len() >= 3) {
if let Some(status) = response
.headers_mut()
.remove("Status")
.filter(|s| s.len() >= 3)
{
let status = &status.as_ref()[..3];
if let Ok(status) = StatusCode::from_bytes(status) {