Skip to content

Commit

Permalink
Fixed duplicate content-type for json & form (#195)
Browse files Browse the repository at this point in the history
* Fixed duplicate content-type for json & form

* Bump revision

* Clippy fixes

* clippy & fmt
  • Loading branch information
Samuel-B-D authored Apr 25, 2022
1 parent e403596 commit e996fff
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions saphir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "saphir"
version = "3.0.0"
version = "3.0.1"
edition = "2018"
authors = ["Richer Archambault <[email protected]>"]
description = "Fully async-await http server framework"
Expand Down Expand Up @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions saphir/src/file/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions saphir/src/multipart/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ pub async fn parse_field(mut stream: FieldStream, boundary: &str) -> Result<Fiel
}
}

#[allow(clippy::iter_with_drain)]
pub async fn parse_next_field_chunk(stream: &mut FieldStream, boundary: &str) -> Result<Vec<u8>, MultipartError> {
let data;
let parse_ctx = stream.stream();
Expand All @@ -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();
Expand Down
10 changes: 4 additions & 6 deletions saphir/src/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,10 @@ mod json {

impl<T: Serialize> Responder for Json<T> {
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")
}
}
}
}
Expand All @@ -163,11 +162,10 @@ mod form {

impl<T: Serialize> Responder for Form<T> {
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")
}
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions saphir/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<(), Error>> {
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),
}
}
}
Expand Down Expand Up @@ -941,5 +935,5 @@ pub async fn inject_raw_with_peer_addr(req: RawRequest<RawBody>, 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()))
}
2 changes: 1 addition & 1 deletion saphir_cli/src/openapi/generate/controller_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion saphir_cli/src/openapi/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit e996fff

Please sign in to comment.