From e996fff063f1991b4a090201e532025dbc43b8db Mon Sep 17 00:00:00 2001 From: Samuel Bergeron-Drouin Date: Mon, 25 Apr 2022 08:16:44 -0400 Subject: [PATCH] Fixed duplicate content-type for json & form (#195) * Fixed duplicate content-type for json & form * Bump revision * Clippy fixes * clippy & fmt --- saphir/Cargo.toml | 4 ++-- saphir/src/file/middleware.rs | 3 +-- saphir/src/multipart/parser.rs | 3 +++ saphir/src/responder.rs | 10 ++++------ saphir/src/server.rs | 12 +++--------- saphir_cli/src/openapi/generate/controller_info.rs | 2 +- saphir_cli/src/openapi/generate/mod.rs | 2 +- 7 files changed, 15 insertions(+), 21 deletions(-) diff --git a/saphir/Cargo.toml b/saphir/Cargo.toml index 5c407a9..477f6c0 100644 --- a/saphir/Cargo.toml +++ b/saphir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "saphir" -version = "3.0.0" +version = "3.0.1" edition = "2018" authors = ["Richer Archambault "] description = "Fully async-await http server framework" @@ -47,7 +47,7 @@ futures-util = "0.3" saphir-cookie = "0.13.2" http = "0.2" http-body = "0.4" -parking_lot = "0.11" +parking_lot = "0.12" regex = "1.4" uuid = { version = "0.8", features = ["serde", "v4"], optional = true } rustls = { version = "0.20.2", optional = true } diff --git a/saphir/src/file/middleware.rs b/saphir/src/file/middleware.rs index a6df24d..61339fe 100644 --- a/saphir/src/file/middleware.rs +++ b/saphir/src/file/middleware.rs @@ -131,8 +131,7 @@ impl FileMiddleware { .headers() .get(header::ACCEPT_ENCODING) .and_then(|header| header.to_str().ok()) - .map(|str| str.split(',').map(|encoding| Compression::from_str(encoding.trim()).unwrap_or_default()).max()) - .flatten() + .and_then(|str| str.split(',').map(|encoding| Compression::from_str(encoding.trim()).unwrap_or_default()).max()) .unwrap_or_default(); if let Some(range) = req diff --git a/saphir/src/multipart/parser.rs b/saphir/src/multipart/parser.rs index ae7c6f1..7bc6ece 100644 --- a/saphir/src/multipart/parser.rs +++ b/saphir/src/multipart/parser.rs @@ -236,6 +236,7 @@ pub async fn parse_field(mut stream: FieldStream, boundary: &str) -> Result Result, MultipartError> { let data; let parse_ctx = stream.stream(); @@ -256,6 +257,8 @@ pub async fn parse_next_field_chunk(stream: &mut FieldStream, boundary: &str) -> } Err(_) => { if parse_ctx.exhausted { + // FIXME: False-positive clippy; cannot into_iter() on a &mut ref. + // Remove #[allow(clippy::iter_with_drain)] once fixed data = buf.drain(0..buf.len()).collect(); } else { data = buf[0..(buf.len() - boundary_len)].to_vec(); diff --git a/saphir/src/responder.rs b/saphir/src/responder.rs index afbab78..2ec2376 100644 --- a/saphir/src/responder.rs +++ b/saphir/src/responder.rs @@ -145,11 +145,10 @@ mod json { impl Responder for Json { fn respond_with_builder(self, builder: Builder, _ctx: &HttpContext) -> Builder { - let b = match builder.json(&self.0) { + match builder.json(&self.0) { Ok(b) => b, Err((b, _e)) => b.status(500).body("Unable to serialize json data"), - }; - b.header(http::header::CONTENT_TYPE, "application/json") + } } } } @@ -163,11 +162,10 @@ mod form { impl Responder for Form { fn respond_with_builder(self, builder: Builder, _ctx: &HttpContext) -> Builder { - let b = match builder.form(&self.0) { + match builder.form(&self.0) { Ok(b) => b, Err((b, _e)) => b.status(500).body("Unable to serialize form data"), - }; - b.header(http::header::CONTENT_TYPE, "application/x-www-form-urlencoded") + } } } } diff --git a/saphir/src/server.rs b/saphir/src/server.rs index 0a6acb4..1c2a1bd 100644 --- a/saphir/src/server.rs +++ b/saphir/src/server.rs @@ -790,14 +790,8 @@ mod ssl_loading_utils { impl AsyncRead for MaybeTlsStream { fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf) -> Poll> { match self.get_mut() { - MaybeTlsStream::Tls(t) => match t.as_mut().poll_read(cx, buf) { - Poll::Ready(r) => Poll::Ready(r), - Poll::Pending => Poll::Pending, - }, - MaybeTlsStream::Plain(p) => match p.as_mut().poll_read(cx, buf) { - Poll::Ready(r) => Poll::Ready(r), - Poll::Pending => Poll::Pending, - }, + MaybeTlsStream::Tls(t) => t.as_mut().poll_read(cx, buf), + MaybeTlsStream::Plain(p) => p.as_mut().poll_read(cx, buf), } } } @@ -941,5 +935,5 @@ pub async fn inject_raw_with_peer_addr(req: RawRequest, peer_addr: Opti let saphir_req = Request::new(req.map(Body::from_raw), peer_addr); REQUEST_FUTURE_COUNT.fetch_add(1, Ordering::SeqCst); let saphir_res = stack.invoke(saphir_req).await?; - Ok(saphir_res.into_raw().map(|r| r.map(|b| b.into_raw()))?) + saphir_res.into_raw().map(|r| r.map(|b| b.into_raw())) } diff --git a/saphir_cli/src/openapi/generate/controller_info.rs b/saphir_cli/src/openapi/generate/controller_info.rs index dc26ffe..413e349 100644 --- a/saphir_cli/src/openapi/generate/controller_info.rs +++ b/saphir_cli/src/openapi/generate/controller_info.rs @@ -40,7 +40,7 @@ impl Gen { if first_seg.ident.eq("controller") { let controller_name = struct_first_seg.ident.to_string(); let name = controller_name.to_ascii_lowercase(); - let name = &name[0..name.rfind("controller").unwrap_or_else(|| name.len())]; + let name = &name[0..name.rfind("controller").unwrap_or(name.len())]; let mut name = name.to_string(); let mut prefix = None; let mut version = None; diff --git a/saphir_cli/src/openapi/generate/mod.rs b/saphir_cli/src/openapi/generate/mod.rs index 450ae64..54738fd 100644 --- a/saphir_cli/src/openapi/generate/mod.rs +++ b/saphir_cli/src/openapi/generate/mod.rs @@ -497,7 +497,7 @@ by using the --package flag." fn get_open_api_body_param<'b>(&mut self, entrypoint: &'b Module<'b>, body_info: &mut BodyParamInfo) -> OpenApiRequestBody { let schema = if body_info.type_info.is_type_deserializable { let ty = &mut body_info.type_info; - let name = ty.rename.as_deref().unwrap_or_else(|| ty.name.as_str()); + let name = ty.rename.as_deref().unwrap_or(ty.name.as_str()); let as_ref = self.args.schema_granularity != SchemaGranularity::None; self.get_open_api_schema_from_type_info(entrypoint, ty, as_ref) .unwrap_or_else(|| self.get_schema(name, None, OpenApiType::anonymous_input_object(), as_ref))