add CORS support

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-10-30 16:29:04 +05:30
parent 8902d63b5a
commit afd4b4e913
4 changed files with 287 additions and 6 deletions

107
Cargo.lock generated
View File

@@ -37,6 +37,15 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "aho-corasick"
version = "0.7.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.3" version = "0.3.3"
@@ -285,6 +294,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "form_urlencoded"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8"
dependencies = [
"percent-encoding",
]
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.25" version = "0.3.25"
@@ -510,6 +528,16 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "idna"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.1" version = "1.9.1"
@@ -875,6 +903,8 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
] ]
@@ -956,6 +986,22 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "rocket_cors"
version = "0.6.0-alpha1"
source = "git+https://github.com/lawliet89/rocket_cors?branch=master#c17e8145baa4790319fdb6a473e465b960f55e7c"
dependencies = [
"http",
"log",
"regex",
"rocket",
"serde",
"serde_derive",
"unicase",
"unicase_serde",
"url",
]
[[package]] [[package]]
name = "rocket_http" name = "rocket_http"
version = "0.5.0-rc.2" version = "0.5.0-rc.2"
@@ -1073,6 +1119,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"rand", "rand",
"rocket", "rocket",
"rocket_cors",
] ]
[[package]] [[package]]
@@ -1193,6 +1240,21 @@ dependencies = [
"time-core", "time-core",
] ]
[[package]]
name = "tinyvec"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.21.2" version = "1.21.2"
@@ -1356,12 +1418,46 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicase_serde"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ef53697679d874d69f3160af80bc28de12730a985d57bdf2b47456ccb8b11f1"
dependencies = [
"serde",
"unicase",
]
[[package]]
name = "unicode-bidi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.5" version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "unicode-normalization"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
dependencies = [
"tinyvec",
]
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.4" version = "0.2.4"
@@ -1378,6 +1474,17 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "url"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"

168
Cargo.nix
View File

@@ -88,6 +88,20 @@ in
}; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".aho-corasick."0.7.19" = overridableMkRustCrate (profileName: rec {
name = "aho-corasick";
version = "0.7.19";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"; };
features = builtins.concatLists [
[ "default" ]
[ "std" ]
];
dependencies = {
memchr = rustPackages."registry+https://github.com/rust-lang/crates.io-index".memchr."2.5.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".async-stream."0.3.3" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".async-stream."0.3.3" = overridableMkRustCrate (profileName: rec {
name = "async-stream"; name = "async-stream";
version = "0.3.3"; version = "0.3.3";
@@ -434,6 +448,16 @@ in
]; ];
}); });
"registry+https://github.com/rust-lang/crates.io-index".form_urlencoded."1.1.0" = overridableMkRustCrate (profileName: rec {
name = "form_urlencoded";
version = "1.1.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8"; };
dependencies = {
percent_encoding = rustPackages."registry+https://github.com/rust-lang/crates.io-index".percent-encoding."2.2.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".futures."0.3.25" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".futures."0.3.25" = overridableMkRustCrate (profileName: rec {
name = "futures"; name = "futures";
version = "0.3.25"; version = "0.3.25";
@@ -751,6 +775,17 @@ in
}; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".idna."0.3.0" = overridableMkRustCrate (profileName: rec {
name = "idna";
version = "0.3.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"; };
dependencies = {
unicode_bidi = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicode-bidi."0.3.8" { inherit profileName; };
unicode_normalization = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicode-normalization."0.1.22" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".indexmap."1.9.1" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".indexmap."1.9.1" = overridableMkRustCrate (profileName: rec {
name = "indexmap"; name = "indexmap";
version = "1.9.1"; version = "1.9.1";
@@ -1249,9 +1284,27 @@ in
registry = "registry+https://github.com/rust-lang/crates.io-index"; registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"; }; src = fetchCratesIo { inherit name version; sha256 = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"; };
features = builtins.concatLists [ features = builtins.concatLists [
[ "aho-corasick" ]
[ "default" ]
[ "memchr" ]
[ "perf" ]
[ "perf-cache" ]
[ "perf-dfa" ]
[ "perf-inline" ]
[ "perf-literal" ]
[ "std" ] [ "std" ]
[ "unicode" ]
[ "unicode-age" ]
[ "unicode-bool" ]
[ "unicode-case" ]
[ "unicode-gencat" ]
[ "unicode-perl" ]
[ "unicode-script" ]
[ "unicode-segment" ]
]; ];
dependencies = { dependencies = {
aho_corasick = rustPackages."registry+https://github.com/rust-lang/crates.io-index".aho-corasick."0.7.19" { inherit profileName; };
memchr = rustPackages."registry+https://github.com/rust-lang/crates.io-index".memchr."2.5.0" { inherit profileName; };
regex_syntax = rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex-syntax."0.6.27" { inherit profileName; }; regex_syntax = rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex-syntax."0.6.27" { inherit profileName; };
}; };
}); });
@@ -1361,6 +1414,36 @@ in
}; };
}); });
"git+https://github.com/lawliet89/rocket_cors".rocket_cors."0.6.0-alpha1" = overridableMkRustCrate (profileName: rec {
name = "rocket_cors";
version = "0.6.0-alpha1";
registry = "git+https://github.com/lawliet89/rocket_cors";
src = fetchCrateGit {
url = https://github.com/lawliet89/rocket_cors;
name = "rocket_cors";
version = "0.6.0-alpha1";
rev = "c17e8145baa4790319fdb6a473e465b960f55e7c";
ref = "master";};
features = builtins.concatLists [
[ "default" ]
[ "serde" ]
[ "serde_derive" ]
[ "serialization" ]
[ "unicase_serde" ]
];
dependencies = {
http = rustPackages."registry+https://github.com/rust-lang/crates.io-index".http."0.2.8" { inherit profileName; };
log = rustPackages."registry+https://github.com/rust-lang/crates.io-index".log."0.4.17" { inherit profileName; };
regex = rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex."1.6.0" { inherit profileName; };
rocket = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rocket."0.5.0-rc.2" { inherit profileName; };
serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.147" { inherit profileName; };
serde_derive = buildRustPackages."registry+https://github.com/rust-lang/crates.io-index".serde_derive."1.0.147" { profileName = "__noProfile"; };
unicase = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicase."2.6.0" { inherit profileName; };
unicase_serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicase_serde."0.1.0" { inherit profileName; };
url = rustPackages."registry+https://github.com/rust-lang/crates.io-index".url."2.3.1" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".rocket_http."0.5.0-rc.2" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".rocket_http."0.5.0-rc.2" = overridableMkRustCrate (profileName: rec {
name = "rocket_http"; name = "rocket_http";
version = "0.5.0-rc.2"; version = "0.5.0-rc.2";
@@ -1515,6 +1598,7 @@ in
dependencies = { dependencies = {
rand = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rand."0.8.5" { inherit profileName; }; rand = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rand."0.8.5" { inherit profileName; };
rocket = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rocket."0.5.0-rc.2" { inherit profileName; }; rocket = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rocket."0.5.0-rc.2" { inherit profileName; };
rocket_cors = rustPackages."git+https://github.com/lawliet89/rocket_cors".rocket_cors."0.6.0-alpha1" { inherit profileName; };
}; };
}); });
@@ -1694,6 +1778,28 @@ in
}; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".tinyvec."1.6.0" = overridableMkRustCrate (profileName: rec {
name = "tinyvec";
version = "1.6.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"; };
features = builtins.concatLists [
[ "alloc" ]
[ "default" ]
[ "tinyvec_macros" ]
];
dependencies = {
tinyvec_macros = rustPackages."registry+https://github.com/rust-lang/crates.io-index".tinyvec_macros."0.1.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".tinyvec_macros."0.1.0" = overridableMkRustCrate (profileName: rec {
name = "tinyvec_macros";
version = "0.1.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"; };
});
"registry+https://github.com/rust-lang/crates.io-index".tokio."1.21.2" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".tokio."1.21.2" = overridableMkRustCrate (profileName: rec {
name = "tokio"; name = "tokio";
version = "1.21.2"; version = "1.21.2";
@@ -1956,6 +2062,39 @@ in
}; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".unicase."2.6.0" = overridableMkRustCrate (profileName: rec {
name = "unicase";
version = "2.6.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"; };
buildDependencies = {
version_check = buildRustPackages."registry+https://github.com/rust-lang/crates.io-index".version_check."0.9.4" { profileName = "__noProfile"; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".unicase_serde."0.1.0" = overridableMkRustCrate (profileName: rec {
name = "unicase_serde";
version = "0.1.0";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "6ef53697679d874d69f3160af80bc28de12730a985d57bdf2b47456ccb8b11f1"; };
dependencies = {
serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.147" { inherit profileName; };
unicase = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicase."2.6.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".unicode-bidi."0.3.8" = overridableMkRustCrate (profileName: rec {
name = "unicode-bidi";
version = "0.3.8";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"; };
features = builtins.concatLists [
[ "default" ]
[ "hardcoded-data" ]
[ "std" ]
];
});
"registry+https://github.com/rust-lang/crates.io-index".unicode-ident."1.0.5" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".unicode-ident."1.0.5" = overridableMkRustCrate (profileName: rec {
name = "unicode-ident"; name = "unicode-ident";
version = "1.0.5"; version = "1.0.5";
@@ -1963,6 +2102,20 @@ in
src = fetchCratesIo { inherit name version; sha256 = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"; }; src = fetchCratesIo { inherit name version; sha256 = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".unicode-normalization."0.1.22" = overridableMkRustCrate (profileName: rec {
name = "unicode-normalization";
version = "0.1.22";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"; };
features = builtins.concatLists [
[ "default" ]
[ "std" ]
];
dependencies = {
tinyvec = rustPackages."registry+https://github.com/rust-lang/crates.io-index".tinyvec."1.6.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".unicode-xid."0.2.4" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".unicode-xid."0.2.4" = overridableMkRustCrate (profileName: rec {
name = "unicode-xid"; name = "unicode-xid";
version = "0.2.4"; version = "0.2.4";
@@ -1984,6 +2137,21 @@ in
}; };
}); });
"registry+https://github.com/rust-lang/crates.io-index".url."2.3.1" = overridableMkRustCrate (profileName: rec {
name = "url";
version = "2.3.1";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"; };
features = builtins.concatLists [
[ "default" ]
];
dependencies = {
form_urlencoded = rustPackages."registry+https://github.com/rust-lang/crates.io-index".form_urlencoded."1.1.0" { inherit profileName; };
idna = rustPackages."registry+https://github.com/rust-lang/crates.io-index".idna."0.3.0" { inherit profileName; };
percent_encoding = rustPackages."registry+https://github.com/rust-lang/crates.io-index".percent-encoding."2.2.0" { inherit profileName; };
};
});
"registry+https://github.com/rust-lang/crates.io-index".valuable."0.1.0" = overridableMkRustCrate (profileName: rec { "registry+https://github.com/rust-lang/crates.io-index".valuable."0.1.0" = overridableMkRustCrate (profileName: rec {
name = "valuable"; name = "valuable";
version = "0.1.0"; version = "0.1.0";

View File

@@ -7,4 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
rocket = "0.5.0-rc.2" rocket = "0.5.0-rc.2"
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
rand = "0.8.5" rand = "0.8.5"

View File

@@ -25,7 +25,6 @@ async fn post_file(mut upload: Form<Upload<'_>>) -> status::Custom<String> {
} }
if let Some(name) = upload.file.raw_name() { if let Some(name) = upload.file.raw_name() {
let new_name = format!( let new_name = format!(
"{}-{}", "{}-{}",
Alphanumeric.sample_string(&mut thread_rng(), 4), Alphanumeric.sample_string(&mut thread_rng(), 4),
@@ -51,7 +50,7 @@ async fn post_file(mut upload: Form<Upload<'_>>) -> status::Custom<String> {
} }
} }
#[get("/<filename>", format = "text/html")] #[get("/<filename>")]
async fn get_file(filename: String) -> Option<NamedFile> { async fn get_file(filename: String) -> Option<NamedFile> {
NamedFile::open(format!("{}/{}", env_root_dir(), filename)) NamedFile::open(format!("{}/{}", env_root_dir(), filename))
.await .await
@@ -87,14 +86,20 @@ fn env_key() -> String {
fn env_user_url() -> String { fn env_user_url() -> String {
let default_config = Config::default(); let default_config = Config::default();
env::var("USER_URL").unwrap_or(format!("http://{}:{}", env::var("USER_URL").unwrap_or(format!(
default_config.address, "http://{}:{}",
default_config.port)) default_config.address, default_config.port
))
} }
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
let cors = rocket_cors::CorsOptions::default().to_cors().unwrap();
fs::create_dir_all(env_root_dir()).unwrap(); fs::create_dir_all(env_root_dir()).unwrap();
println!("Starting"); println!("Starting");
rocket::build().mount("/", routes![post_file, get_file, index])
rocket::build()
.attach(cors)
.mount("/", routes![post_file, get_file, index])
} }