Comments (5)
I'm not sure what you mean about the async apply
method being unavailable in HBAsyncMiddleware
.
collateBody
needs to run on the EventLoop
the request was created on but that is all managed internally. I thought I added an async version of it, but if not you can call
let request = try await request.collateBody().get()
V2 of Hummingbird simplifies this by removing all the EventLoop internals. It is a pure Swift Concurrency based solution.
from hummingbird.
from hummingbird.
Sorry my error message isn't clear enough.
Before you access the body in middleware you need to collate the stream of buffers into one buffer. The most likely reason you are seeing this when running behind a proxy is because the proxy is breaking up the body into a lot smaller sections.
var request = request
request.collateBody().flatMap {
/// You can now access
authenticate(request)
}
You also you have the issue where you are processing a stream of buffers in two places (the middleware and the route). If you collate the request body stream you need to edit the request to give it the buffer back, otherwise you are sending a request with an empty body to you route. Unfortunately an authenticator doesn't give you a chance to pass the edited request on. So either you'll have to write a Middleware that acts like an Authenticator but passes on the collated request body, or you could stick another middleware in front of your authenticator that does the request body collation
struct CollateBodyMiddleware: HBMiddleware {
public func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
return request.collateBody().flatMap { request in
return next.respond(to: request)
}
}
}
from hummingbird.
Thanks the quick reply and examples. This is a clearly an implementation issue on my side and not a bug so I'll close the this issue.
I do have a question regarding some potential complexities related to asynchronous operations and event loops. For instance, I noticed that collateBody is based on the event loop, and the method apply(to request: HBRequest, next: HBResponder) async throws
is not accessible within HBAsyncMiddleware
because HBAsyncMiddleware
extends HBMiddleware
rather than HBAsyncMiddleware
. I'm wondering if the upcoming HummingBird 2.0 release might address these issues and simplify the implementation. If that's the case, I might wait for the 2.0 release to resolve this
from hummingbird.
Thanks Adam, appreciate it.
Re: apply, I meant the async version of apply
isn't available in HBAsyncAuthenticator
since HBAsyncAuthenticator
extends HBAuthenticator
and HBAuthenticator
extends HBMiddleware
, which means HBAsyncAuthenticator
does not extend HBAsyncMiddleware
where the async version of apply
is declared. It would indeed be great to have a single concurrency based version on HB, it will make it a lot easier to develop.
from hummingbird.
Related Issues (20)
- 2.x.x - Make XCT test setups more configurable
- Can `hb_id` always be unique? HOT 7
- 2.x.x - How to disallow `HBResponseBodyWriter` from being used outside `HBResponseBody.write`.
- 2.x.x - Bikeshedding HOT 2
- 2.x.x - Add timeout support to HTTP2 channel handler
- 2.x.x - Alpha release todo list
- HBAsyncMiddleware precondition fail HOT 7
- 2.x.x - Investigate performance of nonisolated HBServer.serve function HOT 1
- Allow disabling `hb_id` HOT 6
- Consideration: Use `AsyncSequence<ByteBuffer>` to represent a request body HOT 1
- Prevent users from iterating a request's body after having responded to that request HOT 3
- Fix Tests when running CI in environment with locked KeyChain HOT 1
- 1.12.0 Does not contain all commits from branch 1.1.x HOT 2
- Easier way to add groups of methods HOT 5
- Hook to customize creation of Request Context HOT 2
- Investigate HTTP2 connection manager from gRPC
- Investigate confusing router API HOT 1
- Streaming issues HOT 4
- Missing CORS headers when request fails HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hummingbird.