Skip to content

Commit

Permalink
Add "Server" header to all responses
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Jan 31, 2024
1 parent 878dc4c commit b4d988d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -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"),
"/",
Expand All @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4d988d

Please sign in to comment.