1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00

port cors middleware

This commit is contained in:
Nikolay Kim 2019-03-23 21:29:16 -07:00
parent 60050307bd
commit 5b06f2bee5
5 changed files with 582 additions and 649 deletions

View file

@ -2,7 +2,7 @@
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
* Keep-alive and slow requests handling
* 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`
* 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)
* 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.
## Documentation & community resources
* [User Guide](https://actix.rs/docs/)
* [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)
* 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
```rust
extern crate actix_web;
use actix_web::{http, server, App, Path, Responder};
use actix_web::{web, App, HttpServer, 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)
}
fn main() {
server::new(
fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new()
.route("/{id}/{name}/index.html", http::Method::GET, index))
.bind("127.0.0.1:8080").unwrap()
.service(web::resource("/{id}/{name}/index.html")
.route(web::get().to(index)))
.bind("127.0.0.1:8080")?
.run();
}
```
@ -48,7 +48,6 @@ fn main() {
* [Basics](https://github.com/actix/examples/tree/master/basics/)
* [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/)
* [Simple websocket](https://github.com/actix/examples/tree/master/websocket/)
* [Tera](https://github.com/actix/examples/tree/master/template_tera/) /

File diff suppressed because it is too large Load diff

View file

@ -154,18 +154,15 @@ mod tests {
use super::*;
use crate::dev::ServiceRequest;
use crate::http::header::CONTENT_TYPE;
use crate::test::{block_on, TestRequest};
use crate::test::{block_on, ok_service, TestRequest};
use crate::HttpResponse;
#[test]
fn test_default_headers() {
let srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().finish())
});
let mut mw = block_on(
DefaultHeaders::new()
.header(CONTENT_TYPE, "0001")
.new_transform(srv),
.new_transform(ok_service()),
)
.unwrap();

View file

@ -3,6 +3,7 @@ mod compress;
#[cfg(any(feature = "brotli", feature = "flate2"))]
pub use self::compress::Compress;
pub mod cors;
mod defaultheaders;
mod errhandlers;
mod logger;

View file

@ -3,13 +3,13 @@ use std::cell::RefCell;
use std::rc::Rc;
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::{Extensions, PayloadStream, Request};
use actix_router::{Path, ResourceDef, Url};
use actix_rt::Runtime;
use actix_server_config::ServerConfig;
use actix_service::{IntoNewService, NewService, Service};
use actix_service::{FnService, IntoNewService, NewService, Service};
use bytes::Bytes;
#[cfg(feature = "cookies")]
use cookie::Cookie;
@ -17,9 +17,10 @@ use futures::future::{lazy, Future};
use crate::config::{AppConfig, AppConfigInner};
use crate::data::RouteData;
use crate::dev::Body;
use crate::rmap::ResourceMap;
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
use crate::{HttpRequest, HttpResponse};
use crate::{Error, HttpRequest, HttpResponse};
thread_local! {
static RT: RefCell<Runtime> = {
@ -55,6 +56,26 @@ where
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
/// service.
///