Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest variable with slash vs %2f

Here is my controller:

@RequestMapping(method = RequestMethod.GET, value="/test/**", headers="Accept=*/*")
public @ResponseBody ResponseEntity<byte[]> getRequest(HttpServletRequest request) 
{
        System.out.println((String) request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE ));
}

Whenever I want to create GETrequest looking like this: localhost:8080/test/some/request/given/in My system out write in console:

some/request/given/in

As I want it to. Problem comes when I have instead of slash - / symbol %2F or %2f. When I have those symbols in my path request is not handled by controller at all.

Is there any way to fix this?

like image 762
Mithrand1r Avatar asked Jun 08 '26 21:06

Mithrand1r


2 Answers

Typically most of the application server will not not treat %2F as / if %2F is found in URL. But as the specs declare %2F is one of the ways a path delimiter can be specified, for handling this scenario, tomcat provides system level property

For tomcat, if

org.apache.tomcat.util.buf. UDecoder.ALLOW_ENCODED_SLASH = true

then %2F and %5C will be permitted as path delimiters. Refer to the documentation. This can be set as JAVA_OPTS in tomcat batch files as follows

set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%   -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

This is specifically for Tomcat. I am sure there would be similar arrangement for other application servers as well.

like image 50
Santosh Avatar answered Jun 11 '26 10:06

Santosh


You need to use URL encoder and decoder to send the path params with special chars such as / and -. In your server side code, you just need to put:

String result = URLDecoder.decode(url, "UTF-8");

and then fetch the params.

like image 36
Juned Ahsan Avatar answered Jun 11 '26 10:06

Juned Ahsan