-
Notifications
You must be signed in to change notification settings - Fork 121
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
[WIP] Add compression middleware #65
Conversation
src/http_client/mod.rs
Outdated
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so we should probably use the AsyncRead
variant in async-compression
. This might be related to #28 so the body can be read without an extra memcpy
, but I definitely do not want to expose tokio-rs/bytes
in any public API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is, I don't think we will need bytes if AsyncBufRead
is used.
@@ -20,6 +20,9 @@ wasm-client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures"] | |||
middleware-logger = [] | |||
|
|||
[dependencies] | |||
async-compression = {version = "0.1.0-alpha.1"} | |||
bytes = "0.4.12" | |||
accept-encoding = "0.2.0-alpha.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably should make the dependencies part of a middleware-compression
feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work, seems like you've been making a lot of progress on this!
I guess the biggest point is we should be using the AsyncRead
variant of async-compression
so we can keep a consistent end-user interface; but overall super excited about this!
Thank you! I presume you mean the |
@Nehliin haa, yes I did! |
I'm not sure if I am misreading the documentation for async_compression but it seems like only the Deflate and Zlib encoders are available to use with What would you suggest the next steps to be? Should I wait for more work on |
Hmm, yeah I'd really like us to not rely on To be fair though, if there's a problem with encoding, I think it's fine to only do decoding. Encoding is a lot harder for clients, because unless you control both client + server you can't really know ahead of time which compression algorithms are supported, so there needs to be a bit of back-and-forth to figure it out, which is not standardized in any HTTP protocol, and thus is hard to write generic middleware for. Do you think this is this something you can work with? Also cc/ @fairingrey @Nemo157, this might be interesting to read along on. |
Yeah, the selection outside of
The easiest way to do that might be to wrap the body in a If there's a usecase for the |
Yes I also only want to decode but the problem was that there were only encoders while I was looking for a decoder :)
I could try and make that work! I'm a bit busy the up coming week but hopefully I could get something started. Thanks for the help! |
@Nehliin just published |
Great thanks! Hopefully I can have some new commits done this weekend :) |
@Nemo157 I can't use the latest version since that depends on |
As a workaround until chttp/isahc can be updated you could use this as your dependency, it's 0.1.0-alpha.5 but with the dependency changed back to futures alpha.17: async-compression = { git = "https://github.com/Nemo157/async-compression-rs", branch = "0.17-override" } |
That's great! Thanks for the quick reply :) |
…d added feature flag
I updated the code so it now uses the |
I have now updated documentation a bit and added errors, the integration tests pass but the doc tests that tests a request to https://httpbin.org/gzip fails. with the error I've been stuck on this for awhile do you have any suggestions on things I could try? |
When you say it decoded ok using EDIT: Either way it's probably an async-compression bug. If it parses ok in a test then it could be related to the exact sequence of pending/partial-buffers being read from the network, it would be nice to be able to instrument the |
I used the |
So, throwing some debugging in I found this:
that's before the data is passed into the |
Oh, well that explains it. It turns out that isahc already decodes automatically. Looks like hyper doesn't do any compression reading through this discussion I wanted to try it myself but couldn't because of a compiler error with the latest surf release which isn't that nice:
And if I use |
Maybe this PR isn't necessary now that isach already does this under the hood? Should I close the PR? I learned a lot either way so I'm happy anyways :) |
Apologies for the slow reply. I do think this would be fantastic to have, even if Isahc already does this. Not all client backends support compression, and having an easy way to enable it is probably a win for everyone involved! I'm not sure if now is the right time to pick this back up, since #131 will likely change the signature of our middleware, but overall this would be fantastic to have! |
Hi @Nehliin, if you still want to persue this, could you open a new PR with something that is more like https://github.com/Fishrock123/tide-compress? Client decompression is likely a necessary feature for Surf to switch to using async-h1 as the default backend: #217 |
@Fishrock123 I won't have time to work on this for a while :/ |
Description
The middleware inserts an
Accepted_Encoding
header with"gzip, br, deflate, zstd"
as value and then decodes the response from the server depending on theContent-Encoding
. The implementation is heavily inspired of theDecompression
implemented in tide. To implement a similar approach for surf I wrapped theBody
used in requests and responses in aBufReader
and used the differentAsyncBufRead
decoders fromasync_compression
.Motivation and Context
Fixes #24
How Has This Been Tested?
I have added a StubClient so the middleware can be tested with integration tests. More tested are needed though, for example responses with multiple layers of compression.
Types of changes
Checklist: