Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.8k stars 75 forks source link

Add gzip compression middleware #325

Closed tyzhnenko closed 1 year ago

tyzhnenko commented 1 year ago

Add GzipMiddleware class that compresses response body if request Accept-Encoding header contains 'gzip'

Add GzipMiddleware for app_1, app_2, app_3, app_4 integration tests servers

Add unit tests for GzipMiddleware

RobertoPrevato commented 1 year ago

Hi @tyzhnenko Thank You for your contribution, I appreciate this very much! I thought many times to adding something similar but always postponed that.

I have two recommendations:

Something like:

handled_content_types = {
    b"json",
    b"xml",
    b"yaml",
    b"html",
    b"text/plain",
    b"application/javascript",
    b"text/css",
    b"text/csv",
}

def handle_content_type(self, content_type: bytes) -> bool:
    lower_content_type = content_type.lower()
    return any(fragment in lower_content_type for fragment in self.handled_content_types)

# handle request only if the content type should be handled

If you don´t have more time to spare, we can merge sooner and I can add these small changes before releasing to PyPi. 😄

tyzhnenko commented 1 year ago

@RobertoPrevato take a look, please

I've addressed all suggestions. If you have any other ideas or suggestions please share them with me.

RobertoPrevato commented 1 year ago

@tyzhnenko it looks great! We can merge it soon, I only have a few final remarks about a typo and a small change on the executor side.

tyzhnenko commented 1 year ago

@tyzhnenko it looks great! We can merge it soon, I only have a few final remarks about a typo and a small change on the executor side.

@RobertoPrevato Sure. Just put your remarks somewhere and I gonna handle them

RobertoPrevato commented 1 year ago

@tyzhnenko it looks great! We can merge it soon, I only have a few final remarks about a typo and a small change on the executor side.

@RobertoPrevato Sure. Just put your remarks somewhere and I gonna handle them

Thanks - I forgot to submit the review.

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 94.11% and project coverage change: -0.04 :warning:

Comparison is base (1de19c6) 97.91% compared to head (0283443) 97.88%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #325 +/- ## ========================================== - Coverage 97.91% 97.88% -0.04% ========================================== Files 64 65 +1 Lines 6053 6104 +51 ========================================== + Hits 5927 5975 +48 - Misses 126 129 +3 ``` | [Impacted Files](https://codecov.io/gh/Neoteroi/BlackSheep/pull/325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Neoteroi) | Coverage Δ | | |---|---|---| | [blacksheep/server/gzip.py](https://codecov.io/gh/Neoteroi/BlackSheep/pull/325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Neoteroi#diff-YmxhY2tzaGVlcC9zZXJ2ZXIvZ3ppcC5weQ==) | `94.11% <94.11%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Neoteroi). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Neoteroi)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

RobertoPrevato commented 1 year ago

@tyzhnenko Thank You for your contribution - I merge this and release it to PyPi as soon as possible. In the next days I'll focus on updating the documentation for version 2. I'm also looking into Copier to add a CLI to blacksheep to scaffold project templates.