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

DMGzipMiddleware: emit warning when middleware receives an unknown X-Compression-Safe value #538

Merged
merged 2 commits into from
Sep 27, 2019

Conversation

risicle
Copy link
Contributor

@risicle risicle commented Sep 27, 2019

@lfdebrux had a point in #537 that the choice of 0, 1 as X-Compression-Safe's values may seem funny to some - I still think it's the best choice but in case anyone misses the plot let's at least have a warning so developers know when an attempted use isn't having the effect they expect it to.

…Compression-Safe value

this should catch programming errors where people try returning "true"/"y"
etc.
warnings.warn(
f"{self.__class__.__name__} received unknown X-Compression-Safe header value {x_compression_safe!r} - "
"falling back to default"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use the built in warnings anywhere else? How do these look when fed through an app logger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they come out at all in logs - I'm expecting people to notice/get annoyed by them being shown after a pytest run.

known_values = {"0": False, "1": True}
compress = known_values.get(x_compression_safe, self.compress_by_default)

if x_compression_safe not in known_values and x_compression_safe != "" and x_compression_safe is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional could be simplified to

Suggested change
if x_compression_safe not in known_values and x_compression_safe != "" and x_compression_safe is not None:
if x_compression_safe and x_compression_safe not in known_values:

Copy link
Contributor

@lfdebrux lfdebrux Sep 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, I didn't think not in worked in Python? 🤔 no it does, I'm think of something else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional could be simplified to

Nearly. Your line would not warn on actual python False, and I didn't go and check whether flask always stringifies header values between view and middleware and don't really want to make the reader have to go & check up on that.

@risicle risicle merged commit 158a742 into master Sep 27, 2019
@risicle risicle deleted the ris-dm-gzip-middleware-warning branch September 27, 2019 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants