-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Server crashes when accessing body in middleware #362
Comments
Sorry my error message isn't clear enough. 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)
}
}
} |
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 |
I'm not sure what you mean about the async
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. |
Thanks Adam, appreciate it. Re: apply, I meant the async version of |
I am not sure if this issue is because I don't use Hummingbird correctly or not, but the server app crashes when accessing request.body.buffer in case the request is streamed (e.g. when a proxy server like Ngrok is in between the client and the server. This issue was brought to my attention by @upeugene, I'm tagging him here.
To replicate the issue, add a middleware to config, like MyAuthenticator in the example below.
Again, the issue might be a misunderstanding how to access the body of the request, but my assumption is the app shouldn't crash.
I will ask Eugene to copy and paste the call stack in a comment.
The text was updated successfully, but these errors were encountered: