perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

NotFound body not present in afterAfter filter #1098

Open msrd0 opened 5 years ago

msrd0 commented 5 years ago

Today I found my notFound route to be not working, but unlike #1051 I was able to use a debugger and detected the following:

Due to a broken python client library that needs to be able to connect to my server, I count the bytes of the body in an afterAfter routine and set them as the Content-Length header. However, this doesn't work with the notFound filter, because the body of the response passed to the afterAfter routine is not populated. I tried to call response.body(...) inside the notFound method instead of returning the website's content, but that also doesn't work because the body that's being returned by spark is then being overwritten with the return value, kotlin.Unit (equivalent to Java's void).

Trying to find a workaround I wrote throw NotFoundException() into the notFound routine, hoping to trigger the exception handler that does correctly populate the afterAfter routine's body. However, spark just decided to use the default 404 page instead of calling the exception handler, which also didn't populate the body of the afterAfter routine's response.

Hence all scenarios resulted in a Content-Length: 0 header and caused the client to throw away all content of the response.

To resolve this issue, please make sure that the afterAfter routine's response always contains the current body that's supposed to be returned to the client.