aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorSven-Hendrik Haase <svenstaro@gmail.com>2021-02-27 16:04:48 +0000
committerGitHub <noreply@github.com>2021-02-27 16:04:48 +0000
commit0366bb0cf3a64ce9675402b973d5f978b05fa882 (patch)
tree8a77dee3301dca6e7a65a136b8d6e0b11034167f /src/main.rs
parentAdd CHANGELOG entries for recent changes (diff)
parentMultiple headers support for custom headers (diff)
downloadminiserve-0366bb0cf3a64ce9675402b973d5f978b05fa882.tar.gz
miniserve-0366bb0cf3a64ce9675402b973d5f978b05fa882.zip
Merge pull request #452 from deantvv/custom-header
Allow set custom headers from CLI
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index c55e77f..44298d7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,6 +5,7 @@ use actix_web::{
};
use actix_web::{middleware, App, HttpRequest, HttpResponse};
use actix_web_httpauth::middleware::HttpAuthentication;
+use http::header::HeaderMap;
use std::io::{self, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::thread;
@@ -88,6 +89,9 @@ pub struct MiniserveConfig {
/// Shown instead of host in page title and heading
pub title: Option<String>,
+
+ /// If specified, header will be added
+ pub header: Option<HeaderMap>,
}
fn main() {
@@ -248,6 +252,7 @@ async fn run() -> Result<(), ContextualError> {
let srv = actix_web::HttpServer::new(move || {
App::new()
+ .wrap(configure_header(&inside_config.clone()))
.app_data(inside_config.clone())
.wrap(middleware::Condition::new(
!inside_config.auth.is_empty(),
@@ -279,6 +284,23 @@ async fn run() -> Result<(), ContextualError> {
.map_err(|e| ContextualError::IoError("".to_owned(), e))
}
+fn configure_header(conf: &MiniserveConfig) -> middleware::DefaultHeaders {
+ let headers = conf.clone().header;
+
+ match headers {
+ Some(headers) => {
+ let mut default_headers = middleware::DefaultHeaders::new();
+ for (header_name, header_value) in headers.into_iter() {
+ if let Some(header_name) = header_name {
+ default_headers = default_headers.header(&header_name, header_value);
+ }
+ }
+ default_headers
+ }
+ _ => middleware::DefaultHeaders::new(),
+ }
+}
+
/// Configures the Actix application
fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let random_route = conf.random_route.clone().unwrap_or_default();