From b4d988d366fbd0116b2ceac78e127f1ed0bcaf9b Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Wed, 31 Jan 2024 11:58:54 -0500 Subject: [PATCH] Add "Server" header to all responses --- Cargo.toml | 2 +- src/consts.rs | 6 ++++++ src/main.rs | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6acb913..74610ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ thiserror = "1.0.56" time = { version = "0.3.31", features = ["formatting", "macros", "parsing", "serde"] } tokio = { version = "1.35.1", features = ["macros", "net", "rt-multi-thread", "sync"] } tower = "0.4.13" -tower-http = { version = "0.5.1", features = ["trace"] } +tower-http = { version = "0.5.1", features = ["set-header", "trace"] } tracing-subscriber = "0.3.18" url = { version = "2.5.0", features = ["serde"] } xml-rs = "0.8.19" diff --git a/src/consts.rs b/src/consts.rs index 04e20a0..8d267c1 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,6 +1,8 @@ use std::num::NonZeroUsize; use time::{format_description::FormatItem, macros::format_description}; +/// The value of the "User-Agent" header sent in requests to the Dandi Archive +/// and S3 pub(crate) static USER_AGENT: &str = concat!( env!("CARGO_PKG_NAME"), "/", @@ -10,6 +12,10 @@ pub(crate) static USER_AGENT: &str = concat!( ")", ); +/// The value of the "Server" header returned in all responses from dandidav +pub(crate) static SERVER_VALUE: &str = + concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); + pub(crate) static DEFAULT_API_URL: &str = "https://api.dandiarchive.org/api"; // Case sensitive: diff --git a/src/main.rs b/src/main.rs index 3beb24b..bbc56a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,18 @@ mod dandi; mod dav; mod paths; mod s3; -use crate::consts::{CSS_CONTENT_TYPE, DEFAULT_API_URL}; +use crate::consts::{CSS_CONTENT_TYPE, DEFAULT_API_URL, SERVER_VALUE}; use crate::dandi::Client; use crate::dav::{DandiDav, Templater}; use anyhow::Context; use axum::{ body::Body, extract::Request, - http::{header::CONTENT_TYPE, response::Response, Method}, + http::{ + header::{HeaderValue, CONTENT_TYPE, SERVER}, + response::Response, + Method, + }, middleware::{self, Next}, routing::get, Router, @@ -19,7 +23,7 @@ use clap::Parser; use std::net::IpAddr; use std::sync::Arc; use tower::service_fn; -use tower_http::trace::TraceLayer; +use tower_http::{set_header::response::SetResponseHeaderLayer, trace::TraceLayer}; use tracing_subscriber::filter::LevelFilter; static STYLESHEET: &str = include_str!("dav/static/styles.css"); @@ -73,6 +77,10 @@ async fn main() -> anyhow::Result<()> { }), ) .layer(middleware::from_fn(handle_head)) + .layer(SetResponseHeaderLayer::if_not_present( + SERVER, + HeaderValue::from_static(SERVER_VALUE), + )) .layer(TraceLayer::new_for_http()); let listener = tokio::net::TcpListener::bind((args.ip_addr, args.port)) .await