From 0ae4c0db603b2eaa891eaae2d7714b1ce4390daf Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 26 Aug 2020 17:34:52 -0700 Subject: CORS headers --- src/main.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a1e8c5b..5f6b574 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use hyper::service::{make_service_fn, service_fn}; -use hyper::{Body, Client, Request, Response, Server, StatusCode}; +use hyper::{Body, Client, Request, Response, Server, StatusCode, header::HeaderValue}; use std::env; use std::net::SocketAddr; use toml; @@ -15,9 +15,9 @@ async fn upstream_req( req: Request, config: ProxyConfig, ) -> Result, hyper::Error> { - info!("hit: {}", req.uri()); + info!("request: {} {}", req.method(), req.uri()); let parsed = filter_request(req, &config).await; - let resp = match parsed { + let mut resp = match parsed { Ok(upstream_req) => { debug!("sending request..."); Client::new().request(upstream_req).await? @@ -28,6 +28,24 @@ async fn upstream_req( .body(serde_json::to_string(&other.to_json()).unwrap().into()) .unwrap(), }; + resp.headers_mut().insert( + "Via", + HeaderValue::from_static("es-public-proxy"), + ); + if config.enable_cors == Some(true) { + resp.headers_mut().insert( + "Access-Control-Allow-Origin", + HeaderValue::from_static("*"), + ); + resp.headers_mut().insert( + "Access-Control-Allow-Origin", + HeaderValue::from_static("GET, POST, DELETE, HEAD, OPTIONS"), + ); + resp.headers_mut().insert( + "Access-Control-Allow-Headers", + HeaderValue::from_static("DNT,User-Agent,Content-Type"), + ); + } debug!("resp!"); Ok(resp) } -- cgit v1.2.3