Skip to content

Commit

Permalink
fix headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed May 12, 2022
1 parent 3744073 commit 5f3cf4b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 136 deletions.
29 changes: 13 additions & 16 deletions iroh-gateway/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use crate::constants::*;
use axum::http::header::*;

pub const DEFAULT_PORT: u16 = 9050;
Expand All @@ -13,7 +12,7 @@ pub struct Config {
/// flag to toggle whether the gateway enables/utilizes caching
pub cache: bool,
/// set of user provided headers to attach to all responses
pub headers: HashMap<String, String>, //todo(arqu): convert to use axum::http::header
pub headers: HeaderMap,
/// default port to listen on
pub port: u16,
}
Expand All @@ -24,21 +23,19 @@ impl Config {
writeable,
fetch,
cache,
headers: HashMap::new(),
headers: HeaderMap::new(),
port,
}
}

pub fn set_default_headers(&mut self) {
let mut headers = HashMap::new();
headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN.to_string(), "*".to_string());
headers.insert(ACCESS_CONTROL_ALLOW_HEADERS.to_string(), "*".to_string());
headers.insert(ACCESS_CONTROL_ALLOW_METHODS.to_string(), "*".to_string());
headers.insert(
CACHE_CONTROL.to_string(),
"no-cache, no-transform".to_string(),
);
headers.insert(ACCEPT_RANGES.to_string(), "none".to_string());
let mut headers = HeaderMap::new();
headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, VALUE_STAR.clone());
headers.insert(ACCESS_CONTROL_ALLOW_HEADERS, VALUE_STAR.clone());
headers.insert(ACCESS_CONTROL_ALLOW_METHODS, VALUE_STAR.clone());
// todo(arqu): remove these once propperly implmented
headers.insert(CACHE_CONTROL, VALUE_NO_CACHE_NO_TRANSFORM.clone());
headers.insert(ACCEPT_RANGES, VALUE_NONE.clone());
self.headers = headers;
}
}
Expand All @@ -49,7 +46,7 @@ impl Default for Config {
writeable: false,
fetch: false,
cache: false,
headers: HashMap::new(),
headers: HeaderMap::new(),
port: DEFAULT_PORT,
};
t.set_default_headers();
Expand All @@ -67,8 +64,8 @@ mod tests {
config.set_default_headers();
assert_eq!(config.headers.len(), 5);
assert_eq!(
config.headers.get(&ACCESS_CONTROL_ALLOW_ORIGIN.to_string()),
Some(&"*".to_string())
config.headers.get(&ACCESS_CONTROL_ALLOW_ORIGIN),
Some(&VALUE_STAR)
);
}

Expand Down
38 changes: 24 additions & 14 deletions iroh-gateway/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
use axum::http::{header::HeaderName, HeaderValue};

// Headers
pub const HEADER_X_IPFS_PATH: &str = "X-Ipfs-Path";
pub const HEADER_X_CONTENT_TYPE_OPTIONS: &str = "X-Content-Type-Options";
pub const HEADER_X_TRACE_ID: &str = "X-Trace-Id";
pub const HEADER_X_IPFS_GATEWAY_PREFIX: &str = "X-Ipfs-Gateway-Prefix";
pub const HEADER_SERVICE_WORKER: &str = "Service-Worker";
pub static HEADER_X_IPFS_PATH: HeaderName = HeaderName::from_static("x-ipfs-path");
pub static HEADER_X_CONTENT_TYPE_OPTIONS: HeaderName =
HeaderName::from_static("x-content-type-options");
pub static HEADER_X_TRACE_ID: HeaderName = HeaderName::from_static("x-trace-id");
pub static HEADER_X_IPFS_GATEWAY_PREFIX: HeaderName =
HeaderName::from_static("x-ipfs-gateway-prefix");
pub static HEADER_SERVICE_WORKER: HeaderName = HeaderName::from_static("service-worker");

// Common Header Values
pub const VALUE_XCTO_NOSNIFF: &str = "nosniff";
pub static VALUE_XCTO_NOSNIFF: HeaderValue = HeaderValue::from_static("nosniff");
pub static VALUE_STAR: HeaderValue = HeaderValue::from_static("*");
pub static VALUE_NONE: HeaderValue = HeaderValue::from_static("none");
pub static VALUE_NO_CACHE_NO_TRANSFORM: HeaderValue =
HeaderValue::from_static("no-cache, no-transform");
pub static VAL_IMMUTABLE_MAX_AGE: HeaderValue =
HeaderValue::from_static("public, max-age=31536000, immutable");

// Dispositions
pub const DISPOSITION_ATTACHMENT: &str = "attachment";
pub const DISPOSITION_INLINE: &str = "inline";
pub static DISPOSITION_ATTACHMENT: &str = "attachment";
pub static DISPOSITION_INLINE: &str = "inline";

// Content Types
pub const CONTENT_TYPE_IPLD_RAW: &str = "application/vnd.ipld.raw";
pub const CONTENT_TYPE_IPLD_CAR: &str = "application/vnd.ipld.car; version=1";
pub const CONTENT_TYPE_OCTET_STREAM: &str = "application/octet-stream";

// Values
pub const VAL_IMMUTABLE_MAX_AGE: &str = "public, max-age=31536000, immutable";
pub static CONTENT_TYPE_IPLD_RAW: HeaderValue =
HeaderValue::from_static("application/vnd.ipld.raw");
pub static CONTENT_TYPE_IPLD_CAR: HeaderValue =
HeaderValue::from_static("application/vnd.ipld.car; version=1");
pub static CONTENT_TYPE_OCTET_STREAM: HeaderValue =
HeaderValue::from_static("application/octet-stream");
21 changes: 12 additions & 9 deletions iroh-gateway/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ async fn get_ipfs(
let cpath = "".to_string();
let cpath = params.get("cpath").unwrap_or(&cpath);

if request_headers.contains_key(HEADER_SERVICE_WORKER) {
let sw = request_headers.get(HEADER_SERVICE_WORKER).unwrap();
if request_headers.contains_key(&HEADER_SERVICE_WORKER) {
let sw = request_headers.get(&HEADER_SERVICE_WORKER).unwrap();
if sw.to_str().unwrap() == "script" && cpath.is_empty() {
return error(StatusCode::BAD_REQUEST, "Service Worker not supported");
}
}
if request_headers.contains_key(HEADER_X_IPFS_GATEWAY_PREFIX) {
if request_headers.contains_key(&HEADER_X_IPFS_GATEWAY_PREFIX) {
return error(StatusCode::BAD_REQUEST, "Unsupported HTTP header");
}

Expand All @@ -174,7 +174,7 @@ async fn get_ipfs(
let query_file_name = query_params.filename.unwrap_or_default();
let download = query_params.download.unwrap_or_default();

let mut headers = HashMap::new();
let mut headers = HeaderMap::new();

if request_headers.contains_key("If-None-Match") {
// todo(arqu): handle dir etags
Expand All @@ -192,7 +192,10 @@ async fn get_ipfs(
// init headers
format.write_headers(&mut headers);
add_user_headers(&mut headers, config.headers.clone());
headers.insert(HEADER_X_IPFS_PATH.to_string(), full_content_path.clone());
headers.insert(
&HEADER_X_IPFS_PATH,
HeaderValue::from_str(&full_content_path).unwrap(),
);
// todo(arqu): add X-Ipfs-Roots

// handle request and fetch data
Expand Down Expand Up @@ -221,7 +224,7 @@ async fn resolve_cid(cid: &Cid) -> Result<Cid, String> {
async fn serve_raw(
req: &Request,
client: Client,
mut headers: HashMap<String, String>,
mut headers: HeaderMap,
start_time: std::time::Instant,
) -> Result<GatewayResponse, GatewayError> {
let body = client
Expand All @@ -247,7 +250,7 @@ async fn serve_raw(
async fn serve_car(
req: &Request,
client: Client,
mut headers: HashMap<String, String>,
mut headers: HeaderMap,
start_time: std::time::Instant,
) -> Result<GatewayResponse, GatewayError> {
let body = client
Expand Down Expand Up @@ -275,7 +278,7 @@ async fn serve_car(
async fn serve_fs(
req: &Request,
client: Client,
mut headers: HashMap<String, String>,
mut headers: HeaderMap,
start_time: std::time::Instant,
) -> Result<GatewayResponse, GatewayError> {
let body = client
Expand Down Expand Up @@ -303,7 +306,7 @@ async fn serve_fs(
fn response(
status_code: StatusCode,
body: BoxBody,
headers: HashMap<String, String>,
headers: HeaderMap,
) -> Result<GatewayResponse, GatewayError> {
Ok(GatewayResponse {
status_code,
Expand Down
1 change: 0 additions & 1 deletion iroh-gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl IntoResponse for GatewayError {
"message": self.message,
"trace_id": self.trace_id,
}));
// todo(arqu): add headers
(self.status_code, body).into_response()
}
}
Loading

0 comments on commit 5f3cf4b

Please sign in to comment.