Tomcat 7 by Dissection: How CGI File Upload Works

NOTE: “Tomcat 7 by Dissection” is a collection of blogs pinpointing, and detailing some Tomcat 7  functionalities on code level . The blogs are meant as code “footprints” for the author.

I wonder which ancient Tomcat version our application based its CGIServlet class on. CGI HTTP POST was not working and recently another problem has cropped up – CGI File Upload does not work at all. What gives? I can only speculate.

  • The original codes may have been taken from a development branch,
  • Tomcat CGI was, by then, an afterthought, incomplete, and kept dormant as a module,
  • Or the functions were omitted in the early derivatives, early versions of our application.

Whatever. The fact of the matter is that there is a ticket and it is assigned to me. So, how should the codes be modified to make the file upload work? The solution is to write out the bytes from the HttpServletRequest InputStream object onto the Process object’s OutputStream when the content length is greater than zero. It figures, right?

In Tomcat 7, the functionality is coded in CGIServlet class as follows:

 Here is a sample CGI script to play around with TC7’s CGI File Upload function:

 TC7 Rev 1097024