-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix empty request body issue #72
base: master
Are you sure you want to change the base?
Conversation
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.
- With this PR change
fdhttp.Request(ctx)
returnsnil
inStdHandler
-registered functions. - There is
fdhttp.RequestBody(ctx)
which works fine without this PR (on bothStdHandler
+Handler
registrations). - It shouldn't make a difference where exactly the request objects is saved into the context (as an
interface{}
, i.e. a reference), something else is going on. I'd rather we understand this before changing.
There are actually (at least) two
shows me
I fear the API of fdhttp is fundamentally broken, there should never have been |
I surprised that it is carrying all those request's data in a context even for those who use std handlers. |
@Drahflow goranger copies the request body into the context so middlewares. So we actually have 2 request bodies. One in the http.Request, and the other in the context. There is some good reason for this, being that middlewares can access this body multiple times. The request body is an io.Reader, so you can only read from it once. But from the context, you can read it multiple times. But even then, I think this is not a decision a router should be making for every application, since this basically means you have duplicate of the request body lying in memory, whether you use it or not. As a router, fdhttp is really doing too much in my opinion. This is where we inject the request body into the context: https://github.com/foodora/go-ranger/blob/master/fdhttp/router.go#L159 Actually, with the request being copied as well, we might have more than 2 copies of the request body |
You can't (or to be precise, it's going to be at EOF on later reads). The code you linked shows a single I tested this thus:
and I see the body printed once, and then two empty bodies being read. In theory, we could have a re-readable body in the context, but currently we don't. And we probably shouldn't, to avoid forcing body-buffering everywhere. |
My bad, you're right. For some reason, i assumed we were reading the raw bytes into the context and not the buffer. |
IMHO:
|
whats the verdict on this PR? |
Issue description:
Currently, to get the request from context, we do it like
fdhttp.Request(ctx)
but if you tried to access this request body or params, you'll find it empty.for example, if you try to do the following
you'll always receive an empty body
Issue details:
Currently, we set the request body in the HTTPServe() method
https://github.com/foodora/go-ranger/blob/2b6f1bd410f72b191bebe2163f5831ced6d9550e/fdhttp/router.go#L333
and at this point, the request object still has no request body (request body is being set in the request handler)