I have a servlet (it's really a SpringBoot controller but it shouldn't
matter?) in Tomcat 9.0.46 that streams responses of unknown size to
the browser.

I've discovered that if the streaming process fails in the middle of
the write, the client will simply get an incomplete file and think it
was successful.
Indeed there is no way to tell from the browser side there was an error.

  void doGet(HttpServletResponse resp) {
    resp.setStatus(200);
    resp.setHeader("Content-Disposition", "...");
    resp.setContentType("application/octet-stream");
    var out = resp.getOutputStream();
    out.flush();
    if (true) throw new RuntimeException("simulated error");
    // status is already sent here, can't change it!
  }

As far as I can tell a web server should close connection with TCP RST
or write some garbage that's not a valid chunked response.
This is what some other servers do such as express.js.

Unfortunately Tomcat does neither and also there seems to be no way to
trigger that using either Servlet or Tomcat api.

I've tested it by setting "Connection: close" then writing garbage and
it seems browser recognized download error.
But then I lose chunking in the output stream and can't actually write
a response.

How do people solve it?

PS. Please CC me, I'm only subscribed to digest.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to