From bf48798bcec35580c408d190b50cc2e42526f1a2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 13 Jun 2019 15:27:21 +0600 Subject: [PATCH 1/5] Content-Length is 0 for NamedFile HEAD request #914 --- actix-files/CHANGES.md | 6 ++++-- actix-files/src/lib.rs | 23 +++++++++++++++++++++++ actix-files/src/named.rs | 26 +++++++++++--------------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 862a59442..e79d80967 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -1,8 +1,10 @@ # Changes -## [0.1.2] - 2019-06-06 +## [0.1.2] - 2019-06-13 -* Fix ring dependency from actix-web default features for #741. +* Content-Length is 0 for NamedFile HEAD request #914 + +* Fix ring dependency from actix-web default features for #741 ## [0.1.1] - 2019-06-01 diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 301d9d81d..9f526f3f0 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -926,6 +926,29 @@ mod tests { assert_eq!(bytes.freeze(), data); } + #[test] + fn test_head_content_length_headers() { + let mut srv = test::init_service( + App::new().service(Files::new("test", ".").index_file("tests/test.binary")), + ); + + // Valid range header + let request = TestRequest::default() + .method(Method::HEAD) + .uri("/t%65st/tests/test.binary") + .to_request(); + let response = test::call_service(&mut srv, request); + + let contentlength = response + .headers() + .get(header::CONTENT_LENGTH) + .unwrap() + .to_str() + .unwrap(); + + assert_eq!(contentlength, "100"); + } + #[test] fn test_static_files_with_spaces() { let mut srv = test::init_service( diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 29e9eee41..3ece7c212 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -422,20 +422,16 @@ impl Responder for NamedFile { return Ok(resp.status(StatusCode::NOT_MODIFIED).finish()); } - if *req.method() == Method::HEAD { - Ok(resp.finish()) - } else { - let reader = ChunkedReadFile { - offset, - size: length, - file: Some(self.file), - fut: None, - counter: 0, - }; - if offset != 0 || length != self.md.len() { - return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader)); - }; - Ok(resp.body(SizedStream::new(length, reader))) - } + let reader = ChunkedReadFile { + offset, + size: length, + file: Some(self.file), + fut: None, + counter: 0, + }; + if offset != 0 || length != self.md.len() { + return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader)); + }; + Ok(resp.body(SizedStream::new(length, reader))) } } From cd323f2ff1da9129e0d0ba63131f85f6e433cbb2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 15 Jun 2019 09:34:16 +0600 Subject: [PATCH 2/5] Move cors middleware to actix-cors crate --- CHANGES.md | 4 ++- Cargo.toml | 3 ++ actix-cors/CHANGES.md | 5 ++++ actix-cors/Cargo.toml | 28 +++++++++++++++++++ actix-cors/LICENSE-APACHE | 1 + actix-cors/LICENSE-MIT | 1 + actix-cors/README.md | 9 ++++++ .../cors.rs => actix-cors/src/lib.rs | 20 ++++++------- src/middleware/mod.rs | 4 ++- 9 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 actix-cors/CHANGES.md create mode 100644 actix-cors/Cargo.toml create mode 120000 actix-cors/LICENSE-APACHE create mode 120000 actix-cors/LICENSE-MIT create mode 100644 actix-cors/README.md rename src/middleware/cors.rs => actix-cors/src/lib.rs (98%) diff --git a/CHANGES.md b/CHANGES.md index 5f3d519a7..1f34b7977 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,10 +10,12 @@ ### Changes -* Disable default feature `secure-cookies`. +* Move cors middleware to `actix-cors` crate. * Move identity middleware to `actix-identity` crate. +* Disable default feature `secure-cookies`. + * Allow to test an app that uses async actors #897 * Re-apply patch from #637 #894 diff --git a/Cargo.toml b/Cargo.toml index 871ed7451..56a42bf97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ members = [ ".", "awc", "actix-http", + "actix-cors", "actix-files", "actix-framed", "actix-session", @@ -80,6 +81,8 @@ actix-server-config = "0.1.1" actix-threadpool = "0.1.1" awc = { version = "0.2.1", optional = true } +# actix-cors = "0.1."{ path="./actix-cors" } + bytes = "0.4" derive_more = "0.14" encoding = "0.2" diff --git a/actix-cors/CHANGES.md b/actix-cors/CHANGES.md new file mode 100644 index 000000000..1e842f37e --- /dev/null +++ b/actix-cors/CHANGES.md @@ -0,0 +1,5 @@ +# Changes + +## [0.1.0] - 2019-06-15 + +* Move cors middleware to separate crate diff --git a/actix-cors/Cargo.toml b/actix-cors/Cargo.toml new file mode 100644 index 000000000..a62cc664d --- /dev/null +++ b/actix-cors/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "actix-cors" +version = "0.1.0" +authors = ["Nikolay Kim "] +description = "Cross-origin resource sharing (CORS) for Actix applications." +readme = "README.md" +keywords = ["http", "web", "framework", "async", "futures"] +homepage = "https://actix.rs" +repository = "https://github.com/actix/actix-web.git" +documentation = "https://docs.rs/actix-cors/" +license = "MIT/Apache-2.0" +edition = "2018" +workspace = ".." + +[lib] +name = "actix_cors" +path = "src/lib.rs" + +[dependencies] +actix-web = "1.0.0" +actix-service = "0.4.0" +derive_more = "0.14.1" +futures = "0.1.25" + +[dev-dependencies] +actix-rt = "0.2.2" +#actix-http = "0.2.3" +#bytes = "0.4" \ No newline at end of file diff --git a/actix-cors/LICENSE-APACHE b/actix-cors/LICENSE-APACHE new file mode 120000 index 000000000..965b606f3 --- /dev/null +++ b/actix-cors/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE \ No newline at end of file diff --git a/actix-cors/LICENSE-MIT b/actix-cors/LICENSE-MIT new file mode 120000 index 000000000..76219eb72 --- /dev/null +++ b/actix-cors/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT \ No newline at end of file diff --git a/actix-cors/README.md b/actix-cors/README.md new file mode 100644 index 000000000..60b615c76 --- /dev/null +++ b/actix-cors/README.md @@ -0,0 +1,9 @@ +# Identity service for actix web framework [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-identity)](https://crates.io/crates/actix-identity) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +## Documentation & community resources + +* [User Guide](https://actix.rs/docs/) +* [API Documentation](https://docs.rs/actix-identity/) +* [Chat on gitter](https://gitter.im/actix/actix) +* Cargo package: [actix-session](https://crates.io/crates/actix-identity) +* Minimum supported Rust version: 1.34 or later diff --git a/src/middleware/cors.rs b/actix-cors/src/lib.rs similarity index 98% rename from src/middleware/cors.rs rename to actix-cors/src/lib.rs index f731f49bd..5d0d013e3 100644 --- a/src/middleware/cors.rs +++ b/actix-cors/src/lib.rs @@ -7,7 +7,7 @@ //! # Example //! //! ```rust -//! use actix_web::middleware::cors::Cors; +//! use actix_cors::Cors; //! use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer}; //! //! fn index(req: HttpRequest) -> &'static str { @@ -42,17 +42,15 @@ use std::iter::FromIterator; use std::rc::Rc; use actix_service::{IntoTransform, Service, Transform}; +use actix_web::dev::{RequestHead, ServiceRequest, ServiceResponse}; +use actix_web::error::{Error, ResponseError, Result}; +use actix_web::http::header::{self, HeaderName, HeaderValue}; +use actix_web::http::{self, HttpTryFrom, Method, StatusCode, Uri}; +use actix_web::HttpResponse; use derive_more::Display; use futures::future::{ok, Either, Future, FutureResult}; use futures::Poll; -use crate::dev::RequestHead; -use crate::error::{Error, ResponseError, Result}; -use crate::http::header::{self, HeaderName, HeaderValue}; -use crate::http::{self, HttpTryFrom, Method, StatusCode, Uri}; -use crate::service::{ServiceRequest, ServiceResponse}; -use crate::HttpResponse; - /// A set of errors that can occur during processing CORS #[derive(Debug, Display)] pub enum CorsError { @@ -152,11 +150,11 @@ impl AllOrSome { /// # Example /// /// ```rust +/// use actix_cors::Cors; /// use actix_web::http::header; -/// use actix_web::middleware::cors; /// /// # fn main() { -/// let cors = cors::Cors::new() +/// let cors = Cors::new() /// .allowed_origin("https://www.rust-lang.org/") /// .allowed_methods(vec!["GET", "POST"]) /// .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) @@ -806,9 +804,9 @@ where #[cfg(test)] mod tests { use actix_service::{IntoService, Transform}; + use actix_web::test::{self, block_on, TestRequest}; use super::*; - use crate::test::{self, block_on, TestRequest}; impl Cors { fn finish(self, srv: F) -> CorsMiddleware diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 99c6cb457..f0b90e773 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -2,7 +2,6 @@ mod compress; pub use self::compress::{BodyEncoding, Compress}; -pub mod cors; mod defaultheaders; pub mod errhandlers; mod logger; @@ -11,3 +10,6 @@ mod normalize; pub use self::defaultheaders::DefaultHeaders; pub use self::logger::Logger; pub use self::normalize::NormalizePath; + +// +// use actix_cors as cors; From d7ec241fd081c625551ef07a1039b322935b5821 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 15 Jun 2019 21:47:06 +0600 Subject: [PATCH 3/5] re-export identity and cors middleware --- Cargo.toml | 16 ++++++++++------ actix-cors/Cargo.toml | 9 ++------- src/app.rs | 3 +-- src/middleware/mod.rs | 15 +++++++++++++-- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 56a42bf97..8531a93ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ members = [ ] [features] -default = ["brotli", "flate2-zlib", "client", "fail"] +default = ["brotli", "flate2-zlib", "client", "fail", "depracated"] # http client client = ["awc"] @@ -68,6 +68,9 @@ ssl = ["openssl", "actix-server/ssl", "awc/ssl"] # rustls rust-tls = ["rustls", "actix-server/rust-tls"] +# deprecated middlewares +depracated = ["actix-cors", "actix-identity"] + [dependencies] actix-codec = "0.1.2" actix-service = "0.4.0" @@ -81,13 +84,15 @@ actix-server-config = "0.1.1" actix-threadpool = "0.1.1" awc = { version = "0.2.1", optional = true } -# actix-cors = "0.1."{ path="./actix-cors" } +# deprecated middlewares +actix-cors = { version = "0.1.0", optional = true } +actix-identity = { version = "0.1.0", optional = true } bytes = "0.4" derive_more = "0.14" encoding = "0.2" futures = "0.1.25" -hashbrown = "0.3.0" +hashbrown = "0.3.1" log = "0.4" mime = "0.3" net2 = "0.2.33" @@ -104,10 +109,9 @@ openssl = { version="0.10", optional = true } rustls = { version = "0.15", optional = true } [dev-dependencies] +actix = { version = "0.8.3" } actix-http = { version = "0.2.3", features=["ssl", "brotli", "flate2-zlib"] } actix-http-test = { version = "0.2.0", features=["ssl"] } -actix-files = "0.1.1" -actix = { version = "0.8.3" } rand = "0.6" env_logger = "0.6" serde_derive = "1.0" @@ -121,7 +125,7 @@ opt-level = 3 codegen-units = 1 [patch.crates-io] -actix-web = { path = "." } +# actix-web = { path = "." } actix-http = { path = "actix-http" } actix-http-test = { path = "test-server" } actix-web-codegen = { path = "actix-web-codegen" } diff --git a/actix-cors/Cargo.toml b/actix-cors/Cargo.toml index a62cc664d..98ed67a2a 100644 --- a/actix-cors/Cargo.toml +++ b/actix-cors/Cargo.toml @@ -4,13 +4,13 @@ version = "0.1.0" authors = ["Nikolay Kim "] description = "Cross-origin resource sharing (CORS) for Actix applications." readme = "README.md" -keywords = ["http", "web", "framework", "async", "futures"] +keywords = ["web", "framework"] homepage = "https://actix.rs" repository = "https://github.com/actix/actix-web.git" documentation = "https://docs.rs/actix-cors/" license = "MIT/Apache-2.0" edition = "2018" -workspace = ".." +#workspace = ".." [lib] name = "actix_cors" @@ -21,8 +21,3 @@ actix-web = "1.0.0" actix-service = "0.4.0" derive_more = "0.14.1" futures = "0.1.25" - -[dev-dependencies] -actix-rt = "0.2.2" -#actix-http = "0.2.3" -#bytes = "0.4" \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 4f8b283e1..897b36459 100644 --- a/src/app.rs +++ b/src/app.rs @@ -225,7 +225,6 @@ where /// It is also possible to use static files as default service. /// /// ```rust - /// use actix_files::Files; /// use actix_web::{web, App, HttpResponse}; /// /// fn main() { @@ -233,7 +232,7 @@ where /// .service( /// web::resource("/index.html").to(|| HttpResponse::Ok())) /// .default_service( - /// Files::new("", "./static") + /// web::to(|| HttpResponse::NotFound()) /// ); /// } /// ``` diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index f0b90e773..c2001e00f 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -11,5 +11,16 @@ pub use self::defaultheaders::DefaultHeaders; pub use self::logger::Logger; pub use self::normalize::NormalizePath; -// -// use actix_cors as cors; +#[cfg(feature = "deprecated")] +#[deprecated( + since = "1.0.1", + note = "please use `actix_cors` instead. support will be removed in actix-web 1.0.2" +)] +pub use actix_cors as cors; + +#[cfg(feature = "deprecated")] +#[deprecated( + since = "1.0.1", + note = "please use `actix_identity` instead. support will be removed in actix-web 1.0.2" +)] +pub use actix_identity as identity; From d293ae2a691fb6f9b9981346643e56932114d2da Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 15 Jun 2019 22:12:20 +0600 Subject: [PATCH 4/5] fix nested resource map registration #915 --- CHANGES.md | 4 ++++ src/rmap.rs | 3 ++- src/scope.rs | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 1f34b7977..87729eb6a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,10 @@ * Re-apply patch from #637 #894 +### Fixed + +* HttpRequest::url_for is broken with nested scopes #915 + ## [1.0.0] - 2019-06-05 diff --git a/src/rmap.rs b/src/rmap.rs index 6a543b75c..cad62dca0 100644 --- a/src/rmap.rs +++ b/src/rmap.rs @@ -38,7 +38,8 @@ impl ResourceMap { pub(crate) fn finish(&self, current: Rc) { for (_, nested) in &self.patterns { if let Some(ref nested) = nested { - *nested.parent.borrow_mut() = Some(current.clone()) + *nested.parent.borrow_mut() = Some(current.clone()); + nested.finish(nested.clone()); } } } diff --git a/src/scope.rs b/src/scope.rs index ad97fcb62..400da668d 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1135,4 +1135,24 @@ mod tests { let body = read_body(resp); assert_eq!(body, &b"https://youtube.com/watch/xxxxxx"[..]); } + + #[test] + fn test_url_for_nested() { + let mut srv = init_service(App::new().service(web::scope("/a").service( + web::scope("/b").service(web::resource("/c/{stuff}").name("c").route( + web::get().to(|req: HttpRequest| { + HttpResponse::Ok() + .body(format!("{}", req.url_for("c", &["12345"]).unwrap())) + }), + )), + ))); + let req = TestRequest::with_uri("/a/b/c/test").to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + let body = read_body(resp); + assert_eq!( + body, + Bytes::from_static(b"http://localhost:8080/a/b/c/12345") + ); + } } From eaa371db8b6a2d1637c4cc31855fe029b576b6a6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 15 Jun 2019 22:20:46 +0600 Subject: [PATCH 5/5] update migration --- MIGRATION.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/MIGRATION.md b/MIGRATION.md index a2591a1d5..5273a0135 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -1,5 +1,19 @@ ## 1.0.1 +* Cors middleware has been moved to `actix-cors` crate + + instead of + + ```rust + use actix_web::middleware::cors::Cors; + ``` + + use + + ```rust + use actix_cors::Cors; + ``` + * Identity middleware has been moved to `actix-identity` crate instead of