perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Static files aren't sending the file size, causing the browser to not display the file download progress

MrPowerGamerBR opened this issue · comments

http://i.imgur.com/Zlx35tA.png

This is what happens when trying to download a static file hosted on Sparkjava (by using Spark.externalStaticFileLocation(rootFolder + "static");)

I'm not a 100% Java/HTML/header magic expert, but I think it doesn't show the file progress because the file size header isn't sent, right?

Took a quick look. This isn't quite as trivial as it first seemed, due to the possibility of gzipped content. Will look more closely into this.

I think a workable solution would be something to the effect of this: wrap the response stream with a wrapper which keeps track of written bytes correctly whether or not there is a gzipped stream in between or not, and add the content length header after done.

Did some experimentation. The approach does somewhat work, but there's a big problem with setting the length header and flushing the stream - if the "final flush" is left last, content length doesn't reflect possible unflushed bytes, but if flushing is done first, it commits the response and the header will not be set.

Catching the flush and setting the header before propagating the flush to the underlying servlet output stream should work.