Jetty and Spark
I am using Spark Java for writing the REST API servies. Spark is a neat web microframework which kinda reminds me of good-old Python Flask.
Problem⌗
Some of my HTTP requests I have to send are humongous. Sometimes in megabytes. The underlying Jetty server was throwing "Form too large" exception
because, POST
requests’ body size exceeds what is supported. The default maximum POST
request size is 2 MiB
.
Now, Spark does not offer any direct ways to configure the settings of the underlying Jetty server. Here is the open issue on that.
Fix⌗
The framework authors were busy with discussing the design on how to expose the Jetty object to configure from Spark. But, our requirement is get the service working immediately. So, I was looking for a temporary fix, in the meantime the authors were trying to come up with the best method to do this.
Thanks to some kind people in the internet, the best way was to modify the hardcoded default value in the Jetty server directly.
Obtained the exact version of Jetty, Spark was using from its Maven dependencies as - 9.3.6.v20151106
. Finding out the source code for this particular release was harder than I anticipated.
Finally obtained the source from Eclipse’s Jetty repo.
From the issues page, we can observe that the value is hardcoded at maxFormContentSize
. A recursive search for string maxFormContentSize
grep -r maxFormContentSize
shows that the largest number of occurrences is in src/main/java/org/eclipse/jetty/server/Request.java
. Searching for maxFormContentSize and replacing the hardcoded value to 100MiB
.
Build the project with
mvn package
and we get jetty-server-9.3.6.v20151106.jar
Replacing the jetty server jar file obtained from the maven repos, with our custom-built one fixes the issue.
Sometimes duct tapes make the greatest weapons.