/* Copyright 2008 Ben Gunter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sourceforge.stripes.util;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.stripes.controller.StripesConstants;

/**
 * Provides helper methods for working with HTTP requests and responses.
 * 
 * @author Ben Gunter
 * @since Stripes 1.5.1
 */
public class HttpUtil {
    /**
     * <p>
     * Get the path from the given request. This method is different from
     * {@link HttpServletRequest#getRequestURI()} in that it concatenates and returns the servlet
     * path plus the path info from the request. These are usually the same, but in some cases they
     * are not.
     * </p>
     * <p>
     * One case where they are known to differ is when a request for a directory is forwarded by the
     * servlet container to a welcome file. In that case, {@link HttpServletRequest#getRequestURI()}
     * returns the path that was actually requested (e.g., {@code "/"}), whereas the servlet path
     * plus path info is the path to the welcome file (e.g. {@code "/index.jsp"}).
     * </p>
     */
    public static String getRequestedPath(HttpServletRequest request) {
        String servletPath, pathInfo;

        // Check to see if the request is processing an include, and pull the path
        // information from the appropriate source.
        servletPath = (String) request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH);
        if (servletPath != null) {
            pathInfo = (String) request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH_INFO);
        }
        else {
            servletPath = request.getServletPath();
            pathInfo = request.getPathInfo();
        }

        if (servletPath == null)
            return pathInfo == null ? "" : StringUtil.urlDecode(pathInfo);
        else if (pathInfo == null)
            return StringUtil.urlDecode(servletPath);
        else
            return StringUtil.urlDecode(servletPath + pathInfo);
    }

    /**
     * Get the servlet path of the current request. The value returned by this method may differ
     * from {@link HttpServletRequest#getServletPath()}. If the given request is an include, then
     * the servlet path of the included resource is returned.
     */
    public static String getRequestedServletPath(HttpServletRequest request) {
        // Check to see if the request is processing an include, and pull the path
        // information from the appropriate source.
        String path = (String) request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH);
        if (path == null) {
            path = request.getServletPath();
        }
        return path == null ? "" : path;
    }

    /** No instances */
    private HttpUtil() {
    }
}

@@ -1873,30 +1873,195 @@
         
-servletPath = 
+// only request attributes need decoding, not servletPath and pathInfo%0A        // see http://www.stripesframework.org/jira/browse/STS-899%0A%0A        servletPath = urlDecodeNullSafe(
 (String)
@@ -2113,32 +2113,33 @@
 TR_INCLUDE_PATH)
+)
 ;%0A        if (se
@@ -2182,16 +2182,34 @@
 hInfo = 
+urlDecodeNullSafe(
 (String)
@@ -2274,16 +2274,17 @@
 TH_INFO)
+)
 ;%0A      
@@ -2491,37 +2491,16 @@
 %22 : 
-StringUtil.urlDecode(
 pathInfo
 );%0A 
@@ -2491,25 +2491,24 @@
 %22 : pathInfo
-)
 ;%0A        el
@@ -2551,37 +2551,16 @@
  return 
-StringUtil.urlDecode(
 servletP
@@ -2562,17 +2562,16 @@
 vletPath
-)
 ;%0A      
@@ -2600,29 +2600,8 @@
 urn 
-StringUtil.urlDecode(
 serv
@@ -2618,17 +2618,16 @@
 pathInfo
-)
 ;%0A    %7D%0A
@@ -3392,11 +3392,178 @@
  %7B%0A    %7D
+%0A%0A    private static String urlDecodeNullSafe(String url) %7B%0A        if (url==null) %7B%0A            return null;%0A        %7D%0A        return StringUtil.urlDecode(url);%0A    %7D
 %0A%7D%0A

fix STS-899, STS-894