Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code cleanup #463

Merged
merged 9 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tower-http/src/compression/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ where
{
type Output = Result<Response<CompressionBody<B>>, E>;

#[allow(unreachable_code, unused_mut, unused_variables, unreachable_patterns)]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let res = ready!(self.as_mut().project().inner.poll(cx)?);

Expand All @@ -55,7 +54,7 @@ where
}

let body = match (should_compress, self.encoding) {
// if compression is _not_ support or the client doesn't accept it
// if compression is _not_ supported or the client doesn't accept it
(false, _) | (_, Encoding::Identity) => {
return Poll::Ready(Ok(Response::from_parts(
parts,
Expand All @@ -80,6 +79,7 @@ where
CompressionBody::new(BodyInner::zstd(WrapBody::new(body, self.quality)))
}
#[cfg(feature = "fs")]
#[allow(unreachable_patterns)]
(true, _) => {
// This should never happen because the `AcceptEncoding` struct which is used to determine
// `self.encoding` will only enable the different compression algorithms if the
Expand Down
87 changes: 36 additions & 51 deletions tower-http/src/compression_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,37 @@ impl SupportedEncodings for AcceptEncoding {
#[allow(dead_code)]
fn gzip(&self) -> bool {
#[cfg(any(feature = "decompression-gzip", feature = "compression-gzip"))]
{
self.gzip
}
return self.gzip;

#[cfg(not(any(feature = "decompression-gzip", feature = "compression-gzip")))]
{
false
}
return false;
}

#[allow(dead_code)]
fn deflate(&self) -> bool {
#[cfg(any(feature = "decompression-deflate", feature = "compression-deflate"))]
{
self.deflate
}
return self.deflate;

#[cfg(not(any(feature = "decompression-deflate", feature = "compression-deflate")))]
{
false
}
return false;
}

#[allow(dead_code)]
fn br(&self) -> bool {
#[cfg(any(feature = "decompression-br", feature = "compression-br"))]
{
self.br
}
return self.br;

#[cfg(not(any(feature = "decompression-br", feature = "compression-br")))]
{
false
}
return false;
}

#[allow(dead_code)]
fn zstd(&self) -> bool {
#[cfg(any(feature = "decompression-zstd", feature = "compression-zstd"))]
{
self.zstd
}
return self.zstd;

#[cfg(not(any(feature = "decompression-zstd", feature = "compression-zstd")))]
{
false
}
return false;
}
}

Expand Down Expand Up @@ -200,35 +188,32 @@ where
let mut this = self.project();
let mut buf = BytesMut::new();
if !*this.read_all_data {
match tokio_util::io::poll_read_buf(this.read.as_mut(), cx, &mut buf) {
Poll::Ready(result) => {
match result {
Ok(read) => {
if read == 0 {
*this.read_all_data = true;
} else {
return Poll::Ready(Some(Ok(Frame::data(buf.freeze()))));
}
}
Err(err) => {
let body_error: Option<B::Error> = M::get_pin_mut(this.read)
.get_pin_mut()
.project()
.error
.take();

if let Some(body_error) = body_error {
return Poll::Ready(Some(Err(body_error.into())));
} else if err.raw_os_error() == Some(SENTINEL_ERROR_CODE) {
// SENTINEL_ERROR_CODE only gets used when storing an underlying body error
unreachable!()
} else {
return Poll::Ready(Some(Err(err.into())));
}
}
let result = tokio_util::io::poll_read_buf(this.read.as_mut(), cx, &mut buf);

match ready!(result) {
Ok(0) => {
*this.read_all_data = true;
}
Ok(_) => {
return Poll::Ready(Some(Ok(Frame::data(buf.freeze()))));
}
Err(err) => {
let body_error: Option<B::Error> = M::get_pin_mut(this.read)
.get_pin_mut()
.project()
.error
.take();

if let Some(body_error) = body_error {
return Poll::Ready(Some(Err(body_error.into())));
} else if err.raw_os_error() == Some(SENTINEL_ERROR_CODE) {
// SENTINEL_ERROR_CODE only gets used when storing
// an underlying body error
unreachable!()
} else {
return Poll::Ready(Some(Err(err.into())));
}
}
Poll::Pending => return Poll::Pending,
}
}

Expand Down
11 changes: 7 additions & 4 deletions tower-http/src/cors/allow_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ impl AllowOrigin {
I: IntoIterator<Item = HeaderValue>,
{
let origins = origins.into_iter().collect::<Vec<_>>();
if origins.iter().any(|o| o == WILDCARD) {
panic!("Wildcard origin (`*`) cannot be passed to `AllowOrigin::list`. Use `AllowOrigin::any()` instead");
} else {
Self(OriginInner::List(origins))
if origins.contains(&WILDCARD) {
panic!(
"Wildcard origin (`*`) cannot be passed to `AllowOrigin::list`. \
Use `AllowOrigin::any()` instead"
);
}

Self(OriginInner::List(origins))
}

/// Set the allowed origins from a predicate
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/cors/vary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Vary {

pub(super) fn to_header(&self) -> Option<(HeaderName, HeaderValue)> {
let values = &self.0;
let mut res = values.get(0)?.as_bytes().to_owned();
let mut res = values.first()?.as_bytes().to_owned();
for val in &values[1..] {
res.extend_from_slice(b", ");
res.extend_from_slice(val.as_bytes());
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ mod tests {
tracing::info_span!("test-span", foo = tracing::field::Empty)
})
.on_request(|_req: &Request<Body>, span: &Span| {
span.record("foo", 42);
span.record("foo", &42);
ON_REQUEST_COUNT.fetch_add(1, Ordering::SeqCst);
})
.on_response(|_res: &Response<Body>, _latency: Duration, _span: &Span| {
Expand Down
Loading