Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gateway upgrade #36

Merged
merged 31 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions iroh-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ git-version = "0.3.5"
rand = "0.8.5"
tracing-opentelemetry = "0.17.2"
opentelemetry = { version = "0.17.0", features = ["rt-tokio"] }
chrono = "0.4.19"
Arqu marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
axum-macros = "0.2.0" # use #[axum_macros::debug_handler] for better error messages on handlers
3 changes: 2 additions & 1 deletion iroh-gateway/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::metrics::*;
use crate::response::ResponseFormat;
use axum::body::Body;
use cid::Cid;
use metrics::{counter, gauge, histogram, increment_counter};
use rand::{prelude::StdRng, Rng, SeedableRng};
use std::{fs::File, io::Read, path::Path, time::Duration};
Expand Down Expand Up @@ -86,7 +87,7 @@ impl Client {
#[derive(Debug, Clone)]
pub struct Request {
pub format: ResponseFormat,
pub cid: String,
pub cid: Cid,
pub full_content_path: String,
pub query_file_name: String,
pub content_path: String,
Expand Down
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());
Arqu marked this conversation as resolved.
Show resolved Hide resolved
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
34 changes: 24 additions & 10 deletions iroh-gateway/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,17 +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 static HEADER_X_IPFS_PATH: HeaderName = HeaderName::from_static("x-ipfs-path");
Arqu marked this conversation as resolved.
Show resolved Hide resolved
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";
pub const CONTENT_TYPE_HTML: &str = "text/html";
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");
Loading