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

Add encoding primitives and a compression middleware #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tmattio
Copy link
Contributor

@tmattio tmattio commented May 13, 2023

This essentially upstreams dream-encoding.

It adds a new dependency on decompress.

Two new middleware are added:

  • Dream.compress
  • Dream.decompress

And we add a new Encoding section to the API with the following functions:

  • Dream.with_encoded_body
  • Dream.accepted_encodings
  • Dream.accepted_encodings_with_weights
  • Dream.content_encodings
  • Dream.preferred_content_encoding

I'm happy to keep this in a separate package, I'm opening a PR in case you're interested in having this upstream, but don't hesitate to close the PR if that's outside of the scope of first-class features you see.

Copy link
Owner

@aantron aantron left a comment

Choose a reason for hiding this comment

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

Thanks! I see an argument for not having this in Dream, which is this, or variants of this, are also needed on the client side (i.e., in Hyper).

@@ -0,0 +1,201 @@
(* This file is part of Dream, released under the MIT license. See LICENSE.md
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this file in src/unix/? Can't we do compression/decompression "purely," i.e. in memory?

log.info (fun log ->
log ~request:req "Compressing request with algorithm: %s"
(algorithm_to_string algorithm));
let%lwt body = Message.body response in
Copy link
Owner

Choose a reason for hiding this comment

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

Note: it should be possible to have a streaming compressor/decompressor with the current Dream API. I will probably write an example that does a streaming transformation of a body stream in some way, and a compressor/decompressor would be built analogously.

match algorithms with
| None -> handler req
| Some algorithms -> (
let%lwt body = Message.body req in
Copy link
Owner

Choose a reason for hiding this comment

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

(Same note here about streaming)

@aantron
Copy link
Owner

aantron commented May 22, 2023

Thanks! I see an argument for not having this in Dream, which is this, or variants of this, are also needed on the client side (i.e., in Hyper).

There are dream-pure and dream-httpaf, which are used by both Dream and Hyper, even though they are in this repo, so maybe the core machinery in this PR can still be upstreamed to this repo into a similar shared package, but the functionality probably needs to be exposed in both Dream and Hyper.

OTOH, how much common functionality is there? For example, as I understand it, handling Accept-Encoding headers is only needed on the server. We probably need to sketch out the client versions of these middlewares, to see if there are any substantial common helpers, beyond just using common third-party compression libraries.

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.

None yet

2 participants