View | Details | Raw Unified | Return to bug 57741
Collapse All | Expand All

(-)java/org/apache/catalina/servlets/CGIServlet.java (-3 / +42 lines)
Lines 239-244 Link Here
239
239
240
    private static final long serialVersionUID = 1L;
240
    private static final long serialVersionUID = 1L;
241
241
242
    /**
243
     * Flag for specifying whether the servlet should trigger the container's
244
     * error page mechanism. For error codes, true to trigger the container's
245
     * error page mechanism, false to handle the error internally.
246
     */
247
    private boolean shouldTriggerErrorPage = false;
248
242
    /** the debugging detail level for this servlet. */
249
    /** the debugging detail level for this servlet. */
243
    private int debug = 0;
250
    private int debug = 0;
244
251
Lines 329-334 Link Here
329
                    "stderrTimeout"));
336
                    "stderrTimeout"));
330
        }
337
        }
331
338
339
        if (getServletConfig().getInitParameter("shouldTriggerErrorPage") != null) {
340
            shouldTriggerErrorPage = Boolean.parseBoolean(getServletConfig().getInitParameter(
341
                    "shouldTriggerErrorPage"));
342
        }
343
332
    }
344
    }
333
345
334
346
Lines 589-595 Link Here
589
        }
601
        }
590
602
591
        if (!cgiEnv.isValid()) {
603
        if (!cgiEnv.isValid()) {
592
            res.setStatus(404);
604
            handleHttpStatusCode(res, 404, shouldTriggerErrorPage);
605
606
            // no need to write to the response below since the error page mechanism will handle that
607
            if (shouldTriggerErrorPage) {
608
                return;
609
            }
593
        }
610
        }
594
611
595
        if (debug >= 10) {
612
        if (debug >= 10) {
Lines 631-636 Link Here
631
648
632
    } //doGet
649
    } //doGet
633
650
651
    /**
652
     * Either calls HttpServletResponse.setStatus(int) or HttpServletResponse.sendError(int)
653
     * based on the status code and flag provided.
654
     *
655
     * @param  res HttpServletResponse passed in by servlet container
656
     * @param  statusCode HTTP status code to set for the response
657
     * @param  shouldTriggerErrorPage true to trigger the container's error page mechanism
658
               for errors, false otherwise
659
     * @throws IOException If an input or output exception occurs
660
     * @throws IllegalStateException If the response was committed before this method call
661
     */
662
    private static void handleHttpStatusCode(HttpServletResponse res, int statusCode, boolean shouldTriggerErrorPage)
663
            throws IOException {
664
        // indicate to the container that the error-page mechanism should be triggered for this error
665
        if (statusCode >= 400 && shouldTriggerErrorPage) {
666
            res.sendError(statusCode);
667
        // otherwise, simply set the status (preventing the error-page mechanism for errors)
668
        } else {
669
            res.setStatus(statusCode);
670
        }
671
    }
634
672
635
    /**
673
    /**
636
     * Encapsulates the CGI environment and rules to derive
674
     * Encapsulates the CGI environment and rules to derive
Lines 1654-1660 Link Here
1654
                                log("runCGI: addHeader(\"" + line + "\")");
1692
                                log("runCGI: addHeader(\"" + line + "\")");
1655
                            }
1693
                            }
1656
                            if (line.startsWith("HTTP")) {
1694
                            if (line.startsWith("HTTP")) {
1657
                                response.setStatus(getSCFromHttpStatusLine(line));
1695
                                handleHttpStatusCode(response, getSCFromHttpStatusLine(line), shouldTriggerErrorPage);
1658
                            } else if (line.indexOf(":") >= 0) {
1696
                            } else if (line.indexOf(":") >= 0) {
1659
                                String header =
1697
                                String header =
1660
                                    line.substring(0, line.indexOf(":")).trim();
1698
                                    line.substring(0, line.indexOf(":")).trim();
Lines 1661-1667 Link Here
1661
                                String value =
1699
                                String value =
1662
                                    line.substring(line.indexOf(":") + 1).trim();
1700
                                    line.substring(line.indexOf(":") + 1).trim();
1663
                                if (header.equalsIgnoreCase("status")) {
1701
                                if (header.equalsIgnoreCase("status")) {
1664
                                    response.setStatus(getSCFromCGIStatusHeader(value));
1702
                                    handleHttpStatusCode(response, getSCFromCGIStatusHeader(value),
1703
                                            shouldTriggerErrorPage);
1665
                                } else {
1704
                                } else {
1666
                                    response.addHeader(header , value);
1705
                                    response.addHeader(header , value);
1667
                                }
1706
                                }

Return to bug 57741