mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
port cors middleware
This commit is contained in:
parent
60050307bd
commit
5b06f2bee5
23
README.md
23
README.md
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
||||||
|
|
||||||
* Supported *HTTP/1.x* and [*HTTP/2.0*](https://actix.rs/docs/http2/) protocols
|
* Supported *HTTP/1.x* and *HTTP/2.0* protocols
|
||||||
* Streaming and pipelining
|
* Streaming and pipelining
|
||||||
* Keep-alive and slow requests handling
|
* Keep-alive and slow requests handling
|
||||||
* Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
* Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
||||||
@ -13,33 +13,33 @@ Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
|||||||
* SSL support with OpenSSL or `native-tls`
|
* SSL support with OpenSSL or `native-tls`
|
||||||
* Middlewares ([Logger, Session, CORS, CSRF, etc](https://actix.rs/docs/middleware/))
|
* Middlewares ([Logger, Session, CORS, CSRF, etc](https://actix.rs/docs/middleware/))
|
||||||
* Includes an asynchronous [HTTP client](https://actix.rs/actix-web/actix_web/client/index.html)
|
* Includes an asynchronous [HTTP client](https://actix.rs/actix-web/actix_web/client/index.html)
|
||||||
* Built on top of [Actix actor framework](https://github.com/actix/actix)
|
* Supports [Actix actor framework](https://github.com/actix/actix)
|
||||||
* Experimental [Async/Await](https://github.com/mehcode/actix-web-async-await) support.
|
* Experimental [Async/Await](https://github.com/mehcode/actix-web-async-await) support.
|
||||||
|
|
||||||
## Documentation & community resources
|
## Documentation & community resources
|
||||||
|
|
||||||
* [User Guide](https://actix.rs/docs/)
|
* [User Guide](https://actix.rs/docs/)
|
||||||
* [API Documentation (Development)](https://actix.rs/actix-web/actix_web/)
|
* [API Documentation (Development)](https://actix.rs/actix-web/actix_web/)
|
||||||
* [API Documentation (Releases)](https://actix.rs/api/actix-web/stable/actix_web/)
|
* [API Documentation (Releases)](https://docs.rs/actix-web/)
|
||||||
* [Chat on gitter](https://gitter.im/actix/actix)
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
* Cargo package: [actix-web](https://crates.io/crates/actix-web)
|
* Cargo package: [actix-web](https://crates.io/crates/actix-web)
|
||||||
* Minimum supported Rust version: 1.31 or later
|
* Minimum supported Rust version: 1.32 or later
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
extern crate actix_web;
|
use actix_web::{web, App, HttpServer, Responder};
|
||||||
use actix_web::{http, server, App, Path, Responder};
|
|
||||||
|
|
||||||
fn index(info: Path<(u32, String)>) -> impl Responder {
|
fn index(info: web::Path<(u32, String)>) -> impl Responder {
|
||||||
format!("Hello {}! id:{}", info.1, info.0)
|
format!("Hello {}! id:{}", info.1, info.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
server::new(
|
HttpServer::new(
|
||||||
|| App::new()
|
|| App::new()
|
||||||
.route("/{id}/{name}/index.html", http::Method::GET, index))
|
.service(web::resource("/{id}/{name}/index.html")
|
||||||
.bind("127.0.0.1:8080").unwrap()
|
.route(web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8080")?
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -48,7 +48,6 @@ fn main() {
|
|||||||
|
|
||||||
* [Basics](https://github.com/actix/examples/tree/master/basics/)
|
* [Basics](https://github.com/actix/examples/tree/master/basics/)
|
||||||
* [Stateful](https://github.com/actix/examples/tree/master/state/)
|
* [Stateful](https://github.com/actix/examples/tree/master/state/)
|
||||||
* [Protobuf support](https://github.com/actix/examples/tree/master/protobuf/)
|
|
||||||
* [Multipart streams](https://github.com/actix/examples/tree/master/multipart/)
|
* [Multipart streams](https://github.com/actix/examples/tree/master/multipart/)
|
||||||
* [Simple websocket](https://github.com/actix/examples/tree/master/websocket/)
|
* [Simple websocket](https://github.com/actix/examples/tree/master/websocket/)
|
||||||
* [Tera](https://github.com/actix/examples/tree/master/template_tera/) /
|
* [Tera](https://github.com/actix/examples/tree/master/template_tera/) /
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -154,18 +154,15 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::dev::ServiceRequest;
|
use crate::dev::ServiceRequest;
|
||||||
use crate::http::header::CONTENT_TYPE;
|
use crate::http::header::CONTENT_TYPE;
|
||||||
use crate::test::{block_on, TestRequest};
|
use crate::test::{block_on, ok_service, TestRequest};
|
||||||
use crate::HttpResponse;
|
use crate::HttpResponse;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_headers() {
|
fn test_default_headers() {
|
||||||
let srv = FnService::new(|req: ServiceRequest<_>| {
|
|
||||||
req.into_response(HttpResponse::Ok().finish())
|
|
||||||
});
|
|
||||||
let mut mw = block_on(
|
let mut mw = block_on(
|
||||||
DefaultHeaders::new()
|
DefaultHeaders::new()
|
||||||
.header(CONTENT_TYPE, "0001")
|
.header(CONTENT_TYPE, "0001")
|
||||||
.new_transform(srv),
|
.new_transform(ok_service()),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ mod compress;
|
|||||||
#[cfg(any(feature = "brotli", feature = "flate2"))]
|
#[cfg(any(feature = "brotli", feature = "flate2"))]
|
||||||
pub use self::compress::Compress;
|
pub use self::compress::Compress;
|
||||||
|
|
||||||
|
pub mod cors;
|
||||||
mod defaultheaders;
|
mod defaultheaders;
|
||||||
mod errhandlers;
|
mod errhandlers;
|
||||||
mod logger;
|
mod logger;
|
||||||
|
27
src/test.rs
27
src/test.rs
@ -3,13 +3,13 @@ use std::cell::RefCell;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::http::header::{Header, HeaderName, IntoHeaderValue};
|
use actix_http::http::header::{Header, HeaderName, IntoHeaderValue};
|
||||||
use actix_http::http::{HttpTryFrom, Method, Version};
|
use actix_http::http::{HttpTryFrom, Method, StatusCode, Version};
|
||||||
use actix_http::test::TestRequest as HttpTestRequest;
|
use actix_http::test::TestRequest as HttpTestRequest;
|
||||||
use actix_http::{Extensions, PayloadStream, Request};
|
use actix_http::{Extensions, PayloadStream, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_rt::Runtime;
|
use actix_rt::Runtime;
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::ServerConfig;
|
||||||
use actix_service::{IntoNewService, NewService, Service};
|
use actix_service::{FnService, IntoNewService, NewService, Service};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use cookie::Cookie;
|
use cookie::Cookie;
|
||||||
@ -17,9 +17,10 @@ use futures::future::{lazy, Future};
|
|||||||
|
|
||||||
use crate::config::{AppConfig, AppConfigInner};
|
use crate::config::{AppConfig, AppConfigInner};
|
||||||
use crate::data::RouteData;
|
use crate::data::RouteData;
|
||||||
|
use crate::dev::Body;
|
||||||
use crate::rmap::ResourceMap;
|
use crate::rmap::ResourceMap;
|
||||||
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
||||||
use crate::{HttpRequest, HttpResponse};
|
use crate::{Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static RT: RefCell<Runtime> = {
|
static RT: RefCell<Runtime> = {
|
||||||
@ -55,6 +56,26 @@ where
|
|||||||
RT.with(move |rt| rt.borrow_mut().block_on(lazy(f)))
|
RT.with(move |rt| rt.borrow_mut().block_on(lazy(f)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ok_service() -> impl Service<
|
||||||
|
Request = ServiceRequest<PayloadStream>,
|
||||||
|
Response = ServiceResponse<Body>,
|
||||||
|
Error = Error,
|
||||||
|
> {
|
||||||
|
default_service(StatusCode::OK)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_service(
|
||||||
|
status_code: StatusCode,
|
||||||
|
) -> impl Service<
|
||||||
|
Request = ServiceRequest<PayloadStream>,
|
||||||
|
Response = ServiceResponse<Body>,
|
||||||
|
Error = Error,
|
||||||
|
> {
|
||||||
|
FnService::new(move |req: ServiceRequest<PayloadStream>| {
|
||||||
|
req.into_response(HttpResponse::build(status_code).finish())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// This method accepts application builder instance, and constructs
|
/// This method accepts application builder instance, and constructs
|
||||||
/// service.
|
/// service.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user