You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/03/01 15:08:37 UTC

[tomcat] branch 8.5.x updated: Fix Response#sendRedirect() if no request context exists.

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 1871833  Fix Response#sendRedirect() if no request context exists.
1871833 is described below

commit 18718332d8c15b72e02f7495ec2521e2419f8170
Author: Knut Sander <kn...@mgm-sp.com>
AuthorDate: Thu Feb 24 18:40:16 2022 +0100

    Fix Response#sendRedirect() if no request context exists.
    
    If no ROOT context is defined, the context may be null in special cases, e.g. RewriteValve may use Response#sendRedirect() without any application context associated.
    In this case, the Tomcat behaviors for the context attributes useRelativeRedirects and sendRedirectBody are assumed, but without considering org.apache.catalina.STRICT_SERVLET_COMPLIANCE.
---
 java/org/apache/catalina/connector/Response.java | 9 +++++++--
 webapps/docs/changelog.xml                       | 5 +++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/catalina/connector/Response.java b/java/org/apache/catalina/connector/Response.java
index 8b09315..d0010bf 100644
--- a/java/org/apache/catalina/connector/Response.java
+++ b/java/org/apache/catalina/connector/Response.java
@@ -1358,17 +1358,22 @@ public class Response implements HttpServletResponse {
 
         // Generate a temporary redirect to the specified location
         try {
+            Context context = getContext();
+            // If no ROOT context is defined, the context can be null.
+            // In this case, the default Tomcat values are assumed, but without
+            // reference to org.apache.catalina.STRICT_SERVLET_COMPLIANCE.
+            boolean reqHasContext = context == null;
             String locationUri;
             // Relative redirects require HTTP/1.1
             if (getRequest().getCoyoteRequest().getSupportsRelativeRedirects() &&
-                    getContext().getUseRelativeRedirects()) {
+                    (!reqHasContext || context.getUseRelativeRedirects())) {
                 locationUri = location;
             } else {
                 locationUri = toAbsolute(location);
             }
             setStatus(status);
             setHeader("Location", locationUri);
-            if (getContext().getSendRedirectBody()) {
+            if (reqHasContext && context.getSendRedirectBody()) {
                 PrintWriter writer = getWriter();
                 writer.print(sm.getString("coyoteResponse.sendRedirect.note",
                         Escape.htmlElementContent(locationUri)));
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 877cbec..4e0c0b5 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -112,6 +112,11 @@
         rewrite valve should set the content type for the response, not the
         request. (markt)
       </fix>
+      <fix>
+        <pr>479</pr>: Enable the rewrite valve to redirect requests when the
+        original request cannot be mapped to a context. This typically happens
+        when no ROOT context is defined. Pull request by elkman. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org