templates icon indicating copy to clipboard operation
templates copied to clipboard

Fixes issue #255: Can't access HTTP request body in java11-vert-x template.

Open p-fortier opened this issue 4 years ago • 10 comments

Signed-off-by: Patrik Fortier [email protected]

Description

Added a io.vertx.ext.web.handler.BodyHandler instance to the handlers to populate routingContext with the HTTP request body.

Motivation and Context

  • [x] I have raised an issue to propose this change (required)

Which issue(s) this PR fixes

Fixes #255

How Has This Been Tested?

I deployed a function reading the http request body using the modified

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Version change (see: Impact to existing users)

Impact to existing users

Adds one handler to the router. According to vertx documentation, the overhead is negligible.

Checklist:

  • [x] My code follows the code style of this project.
    • Maybe a separation is required between my modification and the static handler bloc.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I've read the CONTRIBUTION guide
  • [x] I have signed-off my commits with git commit -s
  • [ ] I have added tests to cover my changes.
  • [ ] All new and existing tests passed.
    • I did not run the verify.sh script

p-fortier avatar Apr 03 '21 13:04 p-fortier

How Has This Been Tested? I deployed a function reading the http request body using the modified

Could you share testing instructions please? I'd like to try it.

Before:

faas-cli template pull ...
faas-cli new --...
faas-cli up ...
faas-cli invoke

After:

faas-cli template pull ...
faas-cli new --...
faas-cli up ...
faas-cli invoke

alexellis avatar Apr 06 '21 09:04 alexellis

Tried this change. And it works.

akshatdev0 avatar Apr 19 '21 08:04 akshatdev0

Sorry I wasn't clear.

How Has This Been Tested? <- I'd like the output from the above.

Thanks

alexellis avatar Apr 20 '21 07:04 alexellis

Hi, sorry I didn't follow this right away. I took time to make a small example. Source code is available at https://github.com/p-fortier/try-fix but the idea is described below:

Before:

faas-cli template pull 
faas-cli new --lang java11-vert-x tryout

The handler for the project should look something like this. This simply returns the request body sent:

  public void handle(RoutingContext routingContext) {
    routingContext.response()
      .putHeader("content-type", "application/json;charset=UTF-8")
      .end(
              routingContext.getBodyAsString()
      );
  }

Calling the function

faas-cli up -f tryout.yml -g $OPENFAAS_URL
echo "hello" | faas-cli invoke tryout -g $OPENFAAS_URL
->  Server returned unexpected status code: 500 - Internal Server Error

Logs show something like this:

tryout-5f76f98558-5hkhn:2021/05/02 14:51:53 stderr: SEVERE: Unexpected exception in route                                                                  
tryout-5f76f98558-5hkhn:2021/05/02 14:51:53 stderr: java.lang.NullPointerException
...
tryout-5f76f98558-5hkhn:2021/05/02 14:51:53 POST / - 500 Internal Server Error - ContentLength: 21

Applying fix

rm -r template/java11-vert-x
faas template pull https://github.com/p-fortier/templates#f255 #This should take the fixed template 
faas-cli up -f tryout.yml -g $OPENFAAS_URL
echo "hello" | faas-cli invoke tryout -g $OPENFAAS_URL
--> hello

p-fortier avatar May 02 '21 14:05 p-fortier

Hi @p-fortier this dropped off my radar.

It seems important to be able to access the request body and I wasn't aware that this was not possible with the template!

@pmlopes pinged me about this, so I think we should consider revisiting this.

Thanks for adding context.

alexellis avatar Feb 18 '22 10:02 alexellis

Please see also #282 - let me know your thoughts on that one.

alexellis avatar Feb 18 '22 10:02 alexellis

@pmlopes can this be merged in your opinion?

alexellis avatar Feb 18 '22 10:02 alexellis

Yes, this will always parse the request body. For security or latency reasons not everyone might want/need it, but is a quick solution. In the refered issue about upgrading the template we shall address this in a user defined way.

pmlopes avatar Feb 18 '22 10:02 pmlopes

What do you mean "parse"?

Is there a way to get the RAW body without this change?

alexellis avatar Feb 18 '22 10:02 alexellis

Yes you can get the raw body, by replicating what the BodyHandler is doing, on the request object, add a handler that will receive the chunks of the HTTP body and either assemble a full buffer or process it as a stream.

The body handler does a bit more, it handles, http body, form uploads, ensures that files are discarded, limits the size to avoid DoS, etc...

pmlopes avatar Feb 18 '22 11:02 pmlopes