From: <jbo...@li...> - 2005-12-31 17:00:42
|
Author: rem...@jb... Date: 2005-12-31 12:00:32 -0500 (Sat, 31 Dec 2005) New Revision: 1967 Modified: trunk/labs/jbossweb/src/share/classes/org/apache/catalina/servlets/CGIServlet.java Log: - Patch for 38012. Modified: trunk/labs/jbossweb/src/share/classes/org/apache/catalina/servlets/CGIServlet.java =================================================================== --- trunk/labs/jbossweb/src/share/classes/org/apache/catalina/servlets/CGIServlet.java 2005-12-31 01:42:28 UTC (rev 1966) +++ trunk/labs/jbossweb/src/share/classes/org/apache/catalina/servlets/CGIServlet.java 2005-12-31 17:00:32 UTC (rev 1967) @@ -236,7 +236,7 @@ * * @author Martin T Dengler [ro...@ma...] * @author Amy Roh - * @version $Revision: 359798 $, $Date: 2005-12-29 15:06:58 +0100 (jeu., 29 déc. 2005) $ + * @version $Revision: 359869 $, $Date: 2005-12-29 19:42:08 +0100 (jeu., 29 déc. 2005) $ * @since Tomcat 4.0 * */ @@ -709,7 +709,7 @@ * <p> * </p> * - * @version $Revision: 359798 $, $Date: 2005-12-29 15:06:58 +0100 (jeu., 29 déc. 2005) $ + * @version $Revision: 359869 $, $Date: 2005-12-29 19:42:08 +0100 (jeu., 29 déc. 2005) $ * @since Tomcat 4.0 * */ @@ -1423,7 +1423,7 @@ * and <code>setResponse</code> methods, respectively. * </p> * - * @version $Revision: 359798 $, $Date: 2005-12-29 15:06:58 +0100 (jeu., 29 déc. 2005) $ + * @version $Revision: 359869 $, $Date: 2005-12-29 19:42:08 +0100 (jeu., 29 déc. 2005) $ */ protected class CGIRunner { @@ -1772,13 +1772,17 @@ log("runCGI: addHeader(\"" + line + "\")"); } if (line.startsWith("HTTP")) { - response.setStatus(getStatus(line)); + response.setStatus(getSCFromHttpStatusLine(line)); } else if (line.indexOf(":") >= 0) { String header = line.substring(0, line.indexOf(":")).trim(); String value = line.substring(line.indexOf(":") + 1).trim(); - response.addHeader(header , value); + if (header.equalsIgnoreCase("status")) { + response.setStatus(getSCFromCGIStatusHeader(value)); + } else { + response.addHeader(header , value); + } if ((header.toLowerCase().equals("content-type")) && (!value.toLowerCase().startsWith("text"))) { isBinaryContent = true; @@ -1849,22 +1853,22 @@ } /** - * Parses the status header and extracts the status code. + * Parses the Status-Line and extracts the status code. * * @param line The HTTP Status-Line (RFC2616, section 6.1) * @return The extracted status code or the code representing an * internal error if a valid status code cannot be extracted. */ - private int getStatus(String line) { - int statusStart = line.indexOf(' '); + private int getSCFromHttpStatusLine(String line) { + int statusStart = line.indexOf(' ') + 1; - if (statusStart < 0 || line.length() < statusStart + 4) { - // Not a valid status line - log ("runCGI: invalid status line:" + line); + if (statusStart < 1 || line.length() < statusStart + 3) { + // Not a valid HTTP Status-Line + log ("runCGI: invalid HTTP Status-Line:" + line); return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } - String status = line.substring(statusStart + 1, statusStart + 4); + String status = line.substring(statusStart, statusStart + 3); int statusCode; try { @@ -1877,6 +1881,35 @@ return statusCode; } + + /** + * Parses the CGI Status Header value and extracts the status code. + * + * @param value The CGI Status value of the form <code> + * digit digit digit SP reason-phrase</code> + * @return The extracted status code or the code representing an + * internal error if a valid status code cannot be extracted. + */ + private int getSCFromCGIStatusHeader(String value) { + if (value.length() < 3) { + // Not a valid status value + log ("runCGI: invalid status value:" + value); + return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; + } + + String status = value.substring(0, 3); + + int statusCode; + try { + statusCode = Integer.parseInt(status); + } catch (NumberFormatException nfe) { + // Not a valid status code + log ("runCGI: invalid status code:" + status); + return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; + } + + return statusCode; + } private void sendToLog(BufferedReader rdr) { String line = null; |