From bca31eb7adbc9010291e530c42c5e2f99921715a Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 2 Apr 2019 13:35:01 -0700 Subject: [PATCH] remove Deref --- CHANGES.md | 2 + Cargo.toml | 3 ++ actix-http/tests/test_client.rs | 10 +++-- actix-http/tests/test_server.rs | 48 +++++++++++----------- awc/src/response.rs | 2 +- awc/tests/test_client.rs | 32 +++++++-------- src/data.rs | 2 +- src/guard.rs | 63 ++++++++++++++++------------- src/middleware/compress.rs | 4 +- src/middleware/cors.rs | 28 ++++++------- src/middleware/decompress.rs | 1 - src/middleware/identity.rs | 4 +- src/middleware/logger.rs | 4 +- src/request.rs | 31 +++++++++----- src/responder.rs | 2 +- src/scope.rs | 6 +-- src/service.rs | 72 ++++++++++++++++----------------- src/types/form.rs | 2 +- src/types/json.rs | 2 +- src/types/path.rs | 2 +- src/types/query.rs | 2 +- tests/test_server.rs | 42 +++++++++---------- 22 files changed, 192 insertions(+), 172 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 39975fb46..655c23cef 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ * Renamed `TestRequest::to_response()` to `TestRequest::to_srv_response()` +* Removed `Deref` impls + ### Removed diff --git a/Cargo.toml b/Cargo.toml index b5d0e8769..b8bd6efcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -116,4 +116,7 @@ codegen-units = 1 [patch.crates-io] actix = { git = "https://github.com/actix/actix.git" } +actix-web = { path = "." } actix-http = { path = "actix-http" } +actix-http-test = { path = "test-server" } +awc = { path = "awc" } diff --git a/actix-http/tests/test_client.rs b/actix-http/tests/test_client.rs index ea0c5eb9a..109a3e4c4 100644 --- a/actix-http/tests/test_client.rs +++ b/actix-http/tests/test_client.rs @@ -35,10 +35,10 @@ fn test_h1_v2() { .finish(|_| future::ok::<_, ()>(Response::Ok().body(STR))) .map(|_| ()) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); - let request = srv.get().header("x-test", "111").send(); + let request = srv.get("/").header("x-test", "111").send(); let response = srv.block_on(request).unwrap(); assert!(response.status().is_success()); @@ -46,7 +46,7 @@ fn test_h1_v2() { let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); - let response = srv.block_on(srv.post().send()).unwrap(); + let response = srv.block_on(srv.post("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -61,7 +61,9 @@ fn test_connection_close() { .finish(|_| ok::<_, ()>(Response::Ok().body(STR))) .map(|_| ()) }); - let response = srv.block_on(srv.get().close_connection().send()).unwrap(); + let response = srv + .block_on(srv.get("/").close_connection().send()) + .unwrap(); assert!(response.status().is_success()); } diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index a18d19626..85cab929c 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -37,7 +37,7 @@ fn test_h1() { .h1(|_| future::ok::<_, ()>(Response::Ok().finish())) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); } @@ -55,7 +55,7 @@ fn test_h1_2() { .map(|_| ()) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); } @@ -98,7 +98,7 @@ fn test_h2() -> std::io::Result<()> { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); Ok(()) } @@ -121,7 +121,7 @@ fn test_h2_1() -> std::io::Result<()> { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); Ok(()) } @@ -145,7 +145,7 @@ fn test_h2_body() -> std::io::Result<()> { ) }); - let response = srv.block_on(srv.sget().send_body(data.clone())).unwrap(); + let response = srv.block_on(srv.sget("/").send_body(data.clone())).unwrap(); assert!(response.status().is_success()); let body = srv.load_body(response).unwrap(); @@ -437,7 +437,7 @@ fn test_h1_headers() { }) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -482,7 +482,7 @@ fn test_h2_headers() { }).map_err(|_| ())) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -518,7 +518,7 @@ fn test_h1_body() { HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().body(STR))) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -541,7 +541,7 @@ fn test_h2_body2() { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -555,7 +555,7 @@ fn test_h1_head_empty() { HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) }); - let response = srv.block_on(srv.head().send()).unwrap(); + let response = srv.block_on(srv.head("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -586,7 +586,7 @@ fn test_h2_head_empty() { ) }); - let response = srv.block_on(srv.shead().send()).unwrap(); + let response = srv.block_on(srv.shead("/").send()).unwrap(); assert!(response.status().is_success()); assert_eq!(response.version(), http::Version::HTTP_2); @@ -611,7 +611,7 @@ fn test_h1_head_binary() { }) }); - let response = srv.block_on(srv.head().send()).unwrap(); + let response = srv.block_on(srv.head("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -646,7 +646,7 @@ fn test_h2_head_binary() { ) }); - let response = srv.block_on(srv.shead().send()).unwrap(); + let response = srv.block_on(srv.shead("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -668,7 +668,7 @@ fn test_h1_head_binary2() { HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) }); - let response = srv.block_on(srv.head().send()).unwrap(); + let response = srv.block_on(srv.head("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -695,7 +695,7 @@ fn test_h2_head_binary2() { ) }); - let response = srv.block_on(srv.shead().send()).unwrap(); + let response = srv.block_on(srv.shead("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -719,7 +719,7 @@ fn test_h1_body_length() { }) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -747,7 +747,7 @@ fn test_h2_body_length() { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -768,7 +768,7 @@ fn test_h1_body_chunked_explicit() { }) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); assert_eq!( response @@ -810,7 +810,7 @@ fn test_h2_body_chunked_explicit() { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert!(response.status().is_success()); assert!(!response.headers().contains_key(header::TRANSFER_ENCODING)); @@ -830,7 +830,7 @@ fn test_h1_body_chunked_implicit() { }) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); assert_eq!( response @@ -862,7 +862,7 @@ fn test_h1_response_http_error_handling() { })) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response @@ -895,7 +895,7 @@ fn test_h2_response_http_error_handling() { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response @@ -910,7 +910,7 @@ fn test_h1_service_error() { .h1(|_| Err::(error::ErrorBadRequest("error"))) }); - let response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get("/").send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response @@ -934,7 +934,7 @@ fn test_h2_service_error() { ) }); - let response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget("/").send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response diff --git a/awc/src/response.rs b/awc/src/response.rs index a4719a9a4..73194d673 100644 --- a/awc/src/response.rs +++ b/awc/src/response.rs @@ -83,7 +83,7 @@ impl ClientResponse { } #[inline] - /// Returns Request's headers. + /// Returns request's headers. pub fn headers(&self) -> &HeaderMap { &self.head().headers } diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 51791d67a..6aed72e41 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -44,7 +44,7 @@ fn test_simple() { )) }); - let request = srv.get().header("x-test", "111").send(); + let request = srv.get("/").header("x-test", "111").send(); let response = srv.block_on(request).unwrap(); assert!(response.status().is_success()); @@ -52,7 +52,7 @@ fn test_simple() { let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); - let response = srv.block_on(srv.post().send()).unwrap(); + let response = srv.block_on(srv.post("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -114,7 +114,7 @@ fn test_timeout_override() { // let mut srv = // test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR))); -// let request = srv.get().header("Connection", "close").finish().unwrap(); +// let request = srv.get("/").header("Connection", "close").finish().unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success()); // } @@ -128,7 +128,7 @@ fn test_timeout_override() { // }) // }); -// let request = srv.get().uri(srv.url("/?qp=5").as_str()).finish().unwrap(); +// let request = srv.get("/").uri(srv.url("/?qp=5").as_str()).finish().unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success()); @@ -139,7 +139,7 @@ fn test_timeout_override() { // let mut srv = // test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR))); -// let request = srv.get().disable_decompress().finish().unwrap(); +// let request = srv.get("/").disable_decompress().finish().unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success()); @@ -177,7 +177,7 @@ fn test_client_gzip_encoding() { }); // client request - let response = srv.block_on(srv.post().send()).unwrap(); + let response = srv.block_on(srv.post("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -200,7 +200,7 @@ fn test_client_gzip_encoding_large() { }); // client request - let response = srv.block_on(srv.post().send()).unwrap(); + let response = srv.block_on(srv.post("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -229,7 +229,7 @@ fn test_client_gzip_encoding_large_random() { }); // client request - let response = srv.block_on(srv.post().send_body(data.clone())).unwrap(); + let response = srv.block_on(srv.post("/").send_body(data.clone())).unwrap(); assert!(response.status().is_success()); // read response @@ -253,7 +253,7 @@ fn test_client_brotli_encoding() { }); // client request - let response = srv.block_on(srv.post().send_body(STR)).unwrap(); + let response = srv.block_on(srv.post("/").send_body(STR)).unwrap(); assert!(response.status().is_success()); // read response @@ -375,7 +375,7 @@ fn test_client_brotli_encoding() { // let body = once(Ok(Bytes::from_static(STR.as_ref()))); -// let request = srv.get().body(Body::Streaming(Box::new(body))).unwrap(); +// let request = srv.get("/").body(Body::Streaming(Box::new(body))).unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success()); @@ -395,7 +395,7 @@ fn test_client_brotli_encoding() { // }) // }); -// let request = srv.get().finish().unwrap(); +// let request = srv.get("/").finish().unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success()); @@ -459,7 +459,7 @@ fn test_client_cookie_handling() { )) }); - let request = srv.get().cookie(cookie1.clone()).cookie(cookie2.clone()); + let request = srv.get("/").cookie(cookie1.clone()).cookie(cookie2.clone()); let response = srv.block_on(request.send()).unwrap(); assert!(response.status().is_success()); let c1 = response.cookie("cookie1").expect("Missing cookie1"); @@ -472,7 +472,7 @@ fn test_client_cookie_handling() { // fn test_default_headers() { // let srv = test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR))); -// let request = srv.get().finish().unwrap(); +// let request = srv.get("/").finish().unwrap(); // let repr = format!("{:?}", request); // assert!(repr.contains("\"accept-encoding\": \"gzip, deflate\"")); // assert!(repr.contains(concat!( @@ -482,7 +482,7 @@ fn test_client_cookie_handling() { // ))); // let request_override = srv -// .get() +// .get("/") // .header("User-Agent", "test") // .header("Accept-Encoding", "over_test") // .finish() @@ -551,7 +551,7 @@ fn client_basic_auth() { }); // set authorization header to Basic - let request = srv.get().basic_auth("username", Some("password")); + let request = srv.get("/").basic_auth("username", Some("password")); let response = srv.block_on(request.send()).unwrap(); assert!(response.status().is_success()); } @@ -579,7 +579,7 @@ fn client_bearer_auth() { }); // set authorization header to Bearer - let request = srv.get().bearer_auth("someS3cr3tAutht0k3n"); + let request = srv.get("/").bearer_auth("someS3cr3tAutht0k3n"); let response = srv.block_on(request.send()).unwrap(); assert!(response.status().is_success()); } diff --git a/src/data.rs b/src/data.rs index a53015c23..a79a303bc 100644 --- a/src/data.rs +++ b/src/data.rs @@ -92,7 +92,7 @@ impl FromRequest

for Data { #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - if let Some(st) = req.config().extensions().get::>() { + if let Some(st) = req.request().config().extensions().get::>() { Ok(st.clone()) } else { Err(ErrorInternalServerError( diff --git a/src/guard.rs b/src/guard.rs index 44e4891e6..0990e876a 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -73,6 +73,15 @@ where } } +impl Guard for F +where + F: Fn(&RequestHead) -> bool, +{ + fn check(&self, head: &RequestHead) -> bool { + (self)(head) + } +} + /// Return guard that matches if any of supplied guards. /// /// ```rust @@ -300,13 +309,13 @@ mod tests { .to_http_request(); let pred = Header("transfer-encoding", "chunked"); - assert!(pred.check(&req)); + assert!(pred.check(req.head())); let pred = Header("transfer-encoding", "other"); - assert!(!pred.check(&req)); + assert!(!pred.check(req.head())); let pred = Header("content-type", "other"); - assert!(!pred.check(&req)); + assert!(!pred.check(req.head())); } // #[test] @@ -332,50 +341,50 @@ mod tests { .method(Method::POST) .to_http_request(); - assert!(Get().check(&req)); - assert!(!Get().check(&req2)); - assert!(Post().check(&req2)); - assert!(!Post().check(&req)); + assert!(Get().check(req.head())); + assert!(!Get().check(req2.head())); + assert!(Post().check(req2.head())); + assert!(!Post().check(req.head())); let r = TestRequest::default().method(Method::PUT).to_http_request(); - assert!(Put().check(&r)); - assert!(!Put().check(&req)); + assert!(Put().check(r.head())); + assert!(!Put().check(req.head())); let r = TestRequest::default() .method(Method::DELETE) .to_http_request(); - assert!(Delete().check(&r)); - assert!(!Delete().check(&req)); + assert!(Delete().check(r.head())); + assert!(!Delete().check(req.head())); let r = TestRequest::default() .method(Method::HEAD) .to_http_request(); - assert!(Head().check(&r)); - assert!(!Head().check(&req)); + assert!(Head().check(r.head())); + assert!(!Head().check(req.head())); let r = TestRequest::default() .method(Method::OPTIONS) .to_http_request(); - assert!(Options().check(&r)); - assert!(!Options().check(&req)); + assert!(Options().check(r.head())); + assert!(!Options().check(req.head())); let r = TestRequest::default() .method(Method::CONNECT) .to_http_request(); - assert!(Connect().check(&r)); - assert!(!Connect().check(&req)); + assert!(Connect().check(r.head())); + assert!(!Connect().check(req.head())); let r = TestRequest::default() .method(Method::PATCH) .to_http_request(); - assert!(Patch().check(&r)); - assert!(!Patch().check(&req)); + assert!(Patch().check(r.head())); + assert!(!Patch().check(req.head())); let r = TestRequest::default() .method(Method::TRACE) .to_http_request(); - assert!(Trace().check(&r)); - assert!(!Trace().check(&req)); + assert!(Trace().check(r.head())); + assert!(!Trace().check(req.head())); } #[test] @@ -384,13 +393,13 @@ mod tests { .method(Method::TRACE) .to_http_request(); - assert!(Not(Get()).check(&r)); - assert!(!Not(Trace()).check(&r)); + assert!(Not(Get()).check(r.head())); + assert!(!Not(Trace()).check(r.head())); - assert!(All(Trace()).and(Trace()).check(&r)); - assert!(!All(Get()).and(Trace()).check(&r)); + assert!(All(Trace()).and(Trace()).check(r.head())); + assert!(!All(Get()).and(Trace()).check(r.head())); - assert!(Any(Get()).or(Trace()).check(&r)); - assert!(!Any(Get()).or(Get()).check(&r)); + assert!(Any(Get()).or(Trace()).check(r.head())); + assert!(!Any(Get()).or(Get()).check(r.head())); } } diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index d797e1250..f74754402 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -113,7 +113,7 @@ where fn call(&mut self, req: ServiceRequest

) -> Self::Future { // negotiate content-encoding - let encoding = if let Some(val) = req.headers.get(ACCEPT_ENCODING) { + let encoding = if let Some(val) = req.headers().get(ACCEPT_ENCODING) { if let Ok(enc) = val.to_str() { AcceptEncoding::parse(enc, self.encoding) } else { @@ -157,7 +157,7 @@ where fn poll(&mut self) -> Poll { let resp = futures::try_ready!(self.fut.poll()); - let enc = if let Some(enc) = resp.head().extensions().get::() { + let enc = if let Some(enc) = resp.response().extensions().get::() { enc.0 } else { self.encoding diff --git a/src/middleware/cors.rs b/src/middleware/cors.rs index 8924eb0ab..920b480bb 100644 --- a/src/middleware/cors.rs +++ b/src/middleware/cors.rs @@ -51,7 +51,7 @@ use crate::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::{HttpMessage, HttpResponse}; +use crate::HttpResponse; /// A set of errors that can occur during processing CORS #[derive(Debug, Display)] @@ -702,9 +702,9 @@ where if self.inner.preflight && Method::OPTIONS == *req.method() { if let Err(e) = self .inner - .validate_origin(&req) - .and_then(|_| self.inner.validate_allowed_method(&req)) - .and_then(|_| self.inner.validate_allowed_headers(&req)) + .validate_origin(req.head()) + .and_then(|_| self.inner.validate_allowed_method(req.head())) + .and_then(|_| self.inner.validate_allowed_headers(req.head())) { return Either::A(ok(req.error_response(e))); } @@ -739,7 +739,7 @@ where let _ = resp.header(header::ACCESS_CONTROL_ALLOW_HEADERS, headers); }) .if_some( - self.inner.access_control_allow_origin(&req), + self.inner.access_control_allow_origin(req.head()), |origin, resp| { let _ = resp.header(header::ACCESS_CONTROL_ALLOW_ORIGIN, origin); }, @@ -762,7 +762,7 @@ where Either::A(ok(req.into_response(res))) } else if req.headers().contains_key(header::ORIGIN) { // Only check requests with a origin header. - if let Err(e) = self.inner.validate_origin(&req) { + if let Err(e) = self.inner.validate_origin(req.head()) { return Either::A(ok(req.error_response(e))); } @@ -771,7 +771,7 @@ where Either::B(Either::B(Box::new(self.service.call(req).and_then( move |mut res| { if let Some(origin) = - inner.access_control_allow_origin(&res.request()) + inner.access_control_allow_origin(res.request().head()) { res.headers_mut() .insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, origin.clone()); @@ -869,8 +869,8 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - assert!(cors.inner.validate_allowed_method(&req).is_err()); - assert!(cors.inner.validate_allowed_headers(&req).is_err()); + assert!(cors.inner.validate_allowed_method(req.head()).is_err()); + assert!(cors.inner.validate_allowed_headers(req.head()).is_err()); let resp = test::call_success(&mut cors, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); @@ -879,8 +879,8 @@ mod tests { .method(Method::OPTIONS) .to_srv_request(); - assert!(cors.inner.validate_allowed_method(&req).is_err()); - assert!(cors.inner.validate_allowed_headers(&req).is_err()); + assert!(cors.inner.validate_allowed_method(req.head()).is_err()); + assert!(cors.inner.validate_allowed_headers(req.head()).is_err()); let req = TestRequest::with_header("Origin", "https://www.example.com") .header(header::ACCESS_CONTROL_REQUEST_METHOD, "POST") @@ -961,9 +961,9 @@ mod tests { let req = TestRequest::with_header("Origin", "https://www.unknown.com") .method(Method::GET) .to_srv_request(); - cors.inner.validate_origin(&req).unwrap(); - cors.inner.validate_allowed_method(&req).unwrap(); - cors.inner.validate_allowed_headers(&req).unwrap(); + cors.inner.validate_origin(req.head()).unwrap(); + cors.inner.validate_allowed_method(req.head()).unwrap(); + cors.inner.validate_allowed_headers(req.head()).unwrap(); } #[test] diff --git a/src/middleware/decompress.rs b/src/middleware/decompress.rs index 84d357375..13735143a 100644 --- a/src/middleware/decompress.rs +++ b/src/middleware/decompress.rs @@ -10,7 +10,6 @@ use futures::{Async, Poll, Stream}; use crate::dev::Payload; use crate::error::{Error, PayloadError}; use crate::service::ServiceRequest; -use crate::HttpMessage; /// `Middleware` for decompressing request's payload. /// `Decompress` middleware must be added with `App::chain()` method. diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index 34979e167..7a2c9f376 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -148,7 +148,7 @@ impl

FromRequest

for Identity { #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - Ok(Identity(req.clone())) + Ok(Identity(req.request().clone())) } } @@ -507,7 +507,7 @@ mod tests { let resp = test::call_success(&mut srv, TestRequest::with_uri("/login").to_request()); assert_eq!(resp.status(), StatusCode::OK); - let c = resp.cookies().next().unwrap().to_owned(); + let c = resp.response().cookies().next().unwrap().to_owned(); let resp = test::call_success( &mut srv, diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index d9c9b138a..bdcc00f28 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -15,7 +15,7 @@ use time; use crate::dev::{BodySize, MessageBody, ResponseBody}; use crate::error::{Error, Result}; use crate::service::{ServiceRequest, ServiceResponse}; -use crate::{HttpMessage, HttpResponse}; +use crate::HttpResponse; /// `Middleware` for logging request and response info to the terminal. /// @@ -201,7 +201,7 @@ where if let Some(ref mut format) = self.format { for unit in &mut format.0 { - unit.render_response(&res); + unit.render_response(res.response()); } } diff --git a/src/request.rs b/src/request.rs index c524d4978..b5ba74122 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,6 +1,5 @@ use std::cell::{Ref, RefMut}; use std::fmt; -use std::ops::Deref; use std::rc::Rc; use actix_http::http::{HeaderMap, Method, Uri, Version}; @@ -66,6 +65,12 @@ impl HttpRequest { self.head().version } + #[inline] + /// Returns request's headers. + pub fn headers(&self) -> &HeaderMap { + &self.head().headers + } + /// The target path of this Request. #[inline] pub fn path(&self) -> &str { @@ -111,6 +116,18 @@ impl HttpRequest { } } + /// Request extensions + #[inline] + pub fn extensions(&self) -> Ref { + self.head().extensions() + } + + /// Mutable reference to a the request's extensions + #[inline] + pub fn extensions_mut(&self) -> RefMut { + self.head().extensions_mut() + } + /// Generate url for named resource /// /// ```rust @@ -154,15 +171,7 @@ impl HttpRequest { /// Get *ConnectionInfo* for the current request. #[inline] pub fn connection_info(&self) -> Ref { - ConnectionInfo::get(&*self, &*self.config()) - } -} - -impl Deref for HttpRequest { - type Target = RequestHead; - - fn deref(&self) -> &RequestHead { - self.head() + ConnectionInfo::get(self.head(), &*self.config()) } } @@ -219,7 +228,7 @@ impl

FromRequest

for HttpRequest { #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - Ok(req.clone()) + Ok(req.request().clone()) } } diff --git a/src/responder.rs b/src/responder.rs index 50467883c..3e0676289 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -313,7 +313,7 @@ pub(crate) mod tests { let req = TestRequest::with_uri("/some").to_request(); let resp = TestRequest::block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - match resp.body() { + match resp.response().body() { ResponseBody::Body(Body::Bytes(ref b)) => { let bytes: Bytes = b.clone().into(); assert_eq!(bytes, Bytes::from_static(b"some")); diff --git a/src/scope.rs b/src/scope.rs index 874240e73..7ad2d95eb 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -693,7 +693,7 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - match resp.body() { + match resp.response().body() { ResponseBody::Body(Body::Bytes(ref b)) => { let bytes: Bytes = b.clone().into(); assert_eq!(bytes, Bytes::from_static(b"project: project1")); @@ -799,7 +799,7 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::CREATED); - match resp.body() { + match resp.response().body() { ResponseBody::Body(Body::Bytes(ref b)) => { let bytes: Bytes = b.clone().into(); assert_eq!(bytes, Bytes::from_static(b"project: project_1")); @@ -826,7 +826,7 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::CREATED); - match resp.body() { + match resp.response().body() { ResponseBody::Body(Body::Bytes(ref b)) => { let bytes: Bytes = b.clone().into(); assert_eq!(bytes, Bytes::from_static(b"project: test - 1")); diff --git a/src/service.rs b/src/service.rs index c260f25b2..13aae8692 100644 --- a/src/service.rs +++ b/src/service.rs @@ -4,7 +4,7 @@ use std::marker::PhantomData; use std::rc::Rc; use actix_http::body::{Body, MessageBody, ResponseBody}; -use actix_http::http::{HeaderMap, Method, Uri, Version}; +use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version}; use actix_http::{ Error, Extensions, HttpMessage, Payload, PayloadStream, Request, RequestHead, Response, ResponseHead, @@ -123,7 +123,13 @@ impl

ServiceRequest

{ } #[inline] - /// Returns mutable Request's headers. + /// Returns request's headers. + pub fn headers(&self) -> &HeaderMap { + &self.head().headers + } + + #[inline] + /// Returns mutable request's headers. pub fn headers_mut(&mut self) -> &mut HeaderMap { &mut self.head_mut().headers } @@ -202,20 +208,6 @@ impl

HttpMessage for ServiceRequest

{ } } -impl

std::ops::Deref for ServiceRequest

{ - type Target = RequestHead; - - fn deref(&self) -> &RequestHead { - self.req.head() - } -} - -impl

std::ops::DerefMut for ServiceRequest

{ - fn deref_mut(&mut self) -> &mut RequestHead { - self.head_mut() - } -} - impl

fmt::Debug for ServiceRequest

{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!( @@ -255,11 +247,19 @@ impl

ServiceFromRequest

{ } #[inline] + /// Get reference to inner HttpRequest + pub fn request(&self) -> &HttpRequest { + &self.req + } + + #[inline] + /// Convert this request into a HttpRequest pub fn into_request(self) -> HttpRequest { self.req } #[inline] + /// Get match information for this request pub fn match_info_mut(&mut self) -> &mut Path { &mut self.req.path } @@ -281,14 +281,6 @@ impl

ServiceFromRequest

{ } } -impl

std::ops::Deref for ServiceFromRequest

{ - type Target = HttpRequest; - - fn deref(&self) -> &HttpRequest { - &self.req - } -} - impl

HttpMessage for ServiceFromRequest

{ type Stream = P; @@ -366,6 +358,24 @@ impl ServiceResponse { &mut self.response } + /// Get the response status code + #[inline] + pub fn status(&self) -> StatusCode { + self.response.status() + } + + #[inline] + /// Returns response's headers. + pub fn headers(&self) -> &HeaderMap { + self.response.headers() + } + + #[inline] + /// Returns mutable response's headers. + pub fn headers_mut(&mut self) -> &mut HeaderMap { + self.response.headers_mut() + } + /// Execute closure and in case of error convert it to response. pub fn checked_expr(mut self, f: F) -> Self where @@ -402,20 +412,6 @@ impl ServiceResponse { } } -impl std::ops::Deref for ServiceResponse { - type Target = Response; - - fn deref(&self) -> &Response { - self.response() - } -} - -impl std::ops::DerefMut for ServiceResponse { - fn deref_mut(&mut self) -> &mut Response { - self.response_mut() - } -} - impl Into> for ServiceResponse { fn into(self) -> Response { self.response diff --git a/src/types/form.rs b/src/types/form.rs index cd4d09bb5..812a08e52 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -80,7 +80,7 @@ where #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - let req2 = req.clone(); + let req2 = req.request().clone(); let (limit, err) = req .route_data::() .map(|c| (c.limit, c.ehandler.clone())) diff --git a/src/types/json.rs b/src/types/json.rs index 9e13d994e..c8ed5afd3 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -174,7 +174,7 @@ where #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - let req2 = req.clone(); + let req2 = req.request().clone(); let (limit, err) = req .route_data::() .map(|c| (c.limit, c.ehandler.clone())) diff --git a/src/types/path.rs b/src/types/path.rs index 4e6784794..fbd106630 100644 --- a/src/types/path.rs +++ b/src/types/path.rs @@ -170,7 +170,7 @@ where #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - Self::extract(req).map_err(ErrorNotFound) + Self::extract(req.request()).map_err(ErrorNotFound) } } diff --git a/src/types/query.rs b/src/types/query.rs index f0eb6a7ae..85dab0610 100644 --- a/src/types/query.rs +++ b/src/types/query.rs @@ -119,7 +119,7 @@ where #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - serde_urlencoded::from_str::(req.query_string()) + serde_urlencoded::from_str::(req.request().query_string()) .map(|val| Ok(Query(val))) .unwrap_or_else(|e| Err(e.into())) } diff --git a/tests/test_server.rs b/tests/test_server.rs index fc590ff0b..3c5d09066 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -54,7 +54,7 @@ fn test_body() { ) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); // read response @@ -73,7 +73,7 @@ fn test_body_gzip() { ) }); - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); // read response @@ -111,7 +111,7 @@ fn test_body_encoding_override() { }); // Builder - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); // read response @@ -161,7 +161,7 @@ fn test_body_gzip_large() { ) }); - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); // read response @@ -195,7 +195,7 @@ fn test_body_gzip_large_random() { ) }); - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); // read response @@ -224,7 +224,7 @@ fn test_body_chunked_implicit() { ) }); - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); assert_eq!( response.headers().get(TRANSFER_ENCODING).unwrap(), @@ -258,7 +258,7 @@ fn test_body_br_streaming() { let mut response = srv .block_on( - srv.get() + srv.get("/") .header(ACCEPT_ENCODING, "br") .no_decompress() .send(), @@ -284,7 +284,7 @@ fn test_head_binary() { ))) }); - let mut response = srv.block_on(srv.head().send()).unwrap(); + let mut response = srv.block_on(srv.head("/").send()).unwrap(); assert!(response.status().is_success()); { @@ -310,7 +310,7 @@ fn test_no_chunking() { )))) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").send()).unwrap(); assert!(response.status().is_success()); assert!(!response.headers().contains_key(TRANSFER_ENCODING)); @@ -333,7 +333,7 @@ fn test_body_deflate() { }); // client request - let mut response = srv.block_on(srv.get().no_decompress().send()).unwrap(); + let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap(); assert!(response.status().is_success()); // read response @@ -362,7 +362,7 @@ fn test_body_brotli() { // client request let mut response = srv .block_on( - srv.get() + srv.get("/") .header(ACCEPT_ENCODING, "br") .no_decompress() .send(), @@ -398,7 +398,7 @@ fn test_encoding() { let enc = e.finish().unwrap(); let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "gzip") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -427,7 +427,7 @@ fn test_gzip_encoding() { let enc = e.finish().unwrap(); let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "gzip") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -457,7 +457,7 @@ fn test_gzip_encoding_large() { let enc = e.finish().unwrap(); let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "gzip") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -491,7 +491,7 @@ fn test_reading_gzip_encoding_large_random() { let enc = e.finish().unwrap(); let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "gzip") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -521,7 +521,7 @@ fn test_reading_deflate_encoding() { // client request let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "deflate") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -551,7 +551,7 @@ fn test_reading_deflate_encoding_large() { // client request let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "deflate") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -585,7 +585,7 @@ fn test_reading_deflate_encoding_large_random() { // client request let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "deflate") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -615,7 +615,7 @@ fn test_brotli_encoding() { // client request let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "br") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -645,7 +645,7 @@ fn test_brotli_encoding_large() { // client request let request = srv - .post() + .post("/") .header(CONTENT_ENCODING, "br") .send_body(enc.clone()); let mut response = srv.block_on(request).unwrap(); @@ -912,7 +912,7 @@ fn test_reading_deflate_encoding_large_random_ssl() { // .finish(); // let second_cookie = http::Cookie::new("second", "second_value"); -// let request = srv.get().finish().unwrap(); +// let request = srv.get("/").finish().unwrap(); // let response = srv.execute(request.send()).unwrap(); // assert!(response.status().is_success());