Skip to content

Commit

Permalink
Implemented CORS header handler middleware (#262)
Browse files Browse the repository at this point in the history
* feat: Add cors middleware

* test: Add cors test code

* feat: export CorsMiddleware

* feat: Add cors example

* $cargo fmt

* doc: move example/cors.rs

* doc: Add README

* doc: Add doc comment

* fix example doc comment

* fix: Changed type to option

* feat: Add credentials header at preflight request

* fix: Attach an expose header when requesting preflight

* $cargo fmt

* fix: remove debug pring

* refactor: move build preflight response

* fix: Remove echo_back option

* fix: fix type OPTION -> OPTIONS

* refactor: tide-cors to optional dependence

* $cargo fmt

* refactor: imple arg to generic argument

* fix typo

* refactor: using 'match'
  • Loading branch information
k-nasa authored and yoshuawuyts committed Jun 6, 2019
1 parent 2504828 commit 1fb4ab3
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ repository = "https://github.com/rustasync/tide"
version = "0.2.0"

[features]
default = ["hyper", "cookies"]
default = ["hyper", "cookies", "cors"]
cookies = ["tide-cookies"]
cors = ["tide-cors"]
hyper = ["tide-core/http-service-hyper"]

[dependencies]
futures-preview = "0.3.0-alpha.16"
http = "0.1"
http-service = "0.2.0"
tide-cookies = { path = "./tide-cookies", optional = true }
tide-cors = { path = "./tide-cors", optional = true }
tide-core = { path = "./tide-core" }
tide-headers = { path = "./tide-headers" }
tide-log = { path = "./tide-log" }
Expand Down Expand Up @@ -55,6 +57,7 @@ members = [
"tide-compression",
"tide-cookies",
"tide-core",
"tide-cors",
"tide-forms",
"tide-headers",
"tide-log",
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub use http;
#[doc(inline)]
pub use tide_cookies as cookies;

#[cfg(feature = "cors")]
#[doc(inline)]
pub use tide_cors as cors;

#[doc(inline)]
pub use tide_core::{
err_fmt,
Expand Down Expand Up @@ -62,6 +66,9 @@ pub mod middleware {
pub use tide_headers::DefaultHeaders;
pub use tide_log::RequestLogger;

#[cfg(feature = "cors")]
pub use tide_cors::CorsMiddleware;

#[cfg(feature = "cookies")]
pub use tide_cookies::CookiesMiddleware;
}
24 changes: 24 additions & 0 deletions tide-cors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "tide-cors"
version = "0.2.0"
authors = [
"Tide Developers",
]
description = "Cors middleware and extensions for Tide"
documentation = "https://docs.rs/tide-cors"
readme = "README.md"
repository = "https://github.com/rustasync/tide"
license = "MIT OR Apache-2.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures-preview = "0.3.0-alpha.16"
http = "0.1"
http-service = "0.2.0"
tide-core = { path = "../tide-core" }

[dev-dependencies]
tide = { path = "../" }
http-service-mock = "0.2.0"
38 changes: 38 additions & 0 deletions tide-cors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# tide-cors

This crate provides cors-related middleware for Tide.

## Examples

Examples are in the `/examples` folder of this crate.

```rust
#![feature(async_await)]

use http::header::HeaderValue;
use tide::middleware::CorsMiddleware;

fn main() {
let mut app = tide::App::new();

app.middleware(
CorsMiddleware::new()
.allow_origin(HeaderValue::from_static("*"))
.allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")),
);

app.at("/").get(async move |_| "Hello, world!");

app.run("127.0.0.1:8000").unwrap();
}
```

**Simple Example**

You can test the simple example by running `cargo run --example cors` while in this crate's directory, and then running this script in the browser console:

```console
fetch("http://127.0.0.1:8000")
```

You will probably get a browser alert when running without cors middleware
18 changes: 18 additions & 0 deletions tide-cors/examples/cors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(async_await)]

use http::header::HeaderValue;
use tide::middleware::CorsMiddleware;

fn main() {
let mut app = tide::App::new();

app.middleware(
CorsMiddleware::new()
.allow_origin(HeaderValue::from_static("*"))
.allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")),
);

app.at("/").get(async move |_| "Hello, world!");

app.run("127.0.0.1:8000").unwrap();
}
54 changes: 54 additions & 0 deletions tide-cors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Cors middleware and extensions for Tide
//!
//! # tide-cors
//!
//! This crate provides cors-related middleware for Tide.
//!
//! ## Examples
//!
//! Examples are in the `/examples` folder of this crate.
//!
//! ```rust, no_run
//! #![feature(async_await)]
//!
//! use http::header::HeaderValue;
//! use tide::middleware::CorsMiddleware;
//!
//! fn main() {
//! let mut app = tide::App::new();
//!
//! app.middleware(
//! CorsMiddleware::new()
//! .allow_origin(HeaderValue::from_static("*"))
//! .allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")),
//! );
//!
//! app.at("/").get(async move |_| "Hello, world!");
//!
//! app.run("127.0.0.1:8000").unwrap();
//! }
//! ```
//!
//! **Simple Example**
//!
//! You can test the simple example by running `cargo run --example cors` while in this crate's directory, and then running this script in the browser console:
//!
//! ```console
//! fetch("http://127.0.0.1:8000")
//! ```
//!
//! You will probably get a browser alert when running without cors middleware
//!
#![feature(async_await)]
#![warn(
nonstandard_style,
rust_2018_idioms,
future_incompatible,
missing_debug_implementations,
missing_docs
)]

mod middleware;

pub use self::middleware::CorsMiddleware;
Loading

0 comments on commit 1fb4ab3

Please sign in to comment.