Skip to content

Commit

Permalink
[RESTEASY-2771] Decode RequestURL
Browse files Browse the repository at this point in the history
Signed-off-by: Andy McCright <j.andrew.mccright@gmail.com>
  • Loading branch information
andymc12 authored and asoldano committed Mar 30, 2021
1 parent 07cd4d7 commit 71bbf49
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 8 deletions.
13 changes: 13 additions & 0 deletions resteasy-core/pom.xml
Expand Up @@ -61,6 +61,19 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down
Expand Up @@ -11,11 +11,16 @@
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
Expand All @@ -29,16 +34,35 @@ public static ResteasyUriInfo extractUriInfo(HttpServletRequest request, String
if (servletPrefix != null && servletPrefix.length() > 0 && !servletPrefix.equals("/"))
{
if (!contextPath.endsWith("/") && !servletPrefix.startsWith("/"))
contextPath += "/";
contextPath += "/";
contextPath += servletPrefix;
}
String queryString = request.getQueryString();
String absolute;
if (queryString != null && queryString.length() > 0) {
if (queryString != null && queryString.length() > 0)
{
absolute = request.getRequestURL().append('?').append(queryString).toString();
} else {
}
else
{
absolute = request.getRequestURL().toString();
}
if (!absolute.contains(contextPath))
{
String encodedContextPath = Arrays.stream(contextPath.substring(1).split("/"))
.map(s -> {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException ex) {
return s;
}
})
.collect(Collectors.joining("/", "/", ""));
if (absolute.contains(encodedContextPath))
{
absolute = absolute.replace(encodedContextPath, contextPath);
}
}
return new ResteasyUriInfo(absolute, contextPath);
}

Expand All @@ -49,7 +73,8 @@ public static ResteasyHttpHeaders extractHttpHeaders(HttpServletRequest request)
ResteasyHttpHeaders headers = new ResteasyHttpHeaders(requestHeaders);

String contentType = request.getContentType();
if (contentType != null) headers.getMutableHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
if (contentType != null)
headers.getMutableHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);

Map<String, Cookie> cookies = extractCookies(request);
headers.setCookies(cookies);
Expand All @@ -68,7 +93,8 @@ static Map<String, Cookie> extractCookies(HttpServletRequest request)
{
for (javax.servlet.http.Cookie cookie : request.getCookies())
{
cookies.put(cookie.getName(), new Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getVersion()));
cookies.put(cookie.getName(), new Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(),
cookie.getDomain(), cookie.getVersion()));

}
}
Expand All @@ -79,7 +105,8 @@ public static List<MediaType> extractAccepts(MultivaluedMap<String, String> requ
{
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
List<String> accepts = requestHeaders.get(HttpHeaderNames.ACCEPT);
if (accepts == null) return acceptableMediaTypes;
if (accepts == null)
return acceptableMediaTypes;

for (String accept : accepts)
{
Expand All @@ -92,12 +119,14 @@ public static List<String> extractLanguages(MultivaluedMap<String, String> reque
{
List<String> acceptable = new ArrayList<String>();
List<String> accepts = requestHeaders.get(HttpHeaderNames.ACCEPT_LANGUAGE);
if (accepts == null) return acceptable;
if (accepts == null)
return acceptable;

for (String accept : accepts)
{
String[] splits = accept.split(",");
for (String split : splits) acceptable.add(split.trim());
for (String split : splits)
acceptable.add(split.trim());
}
return acceptable;
}
Expand Down
@@ -0,0 +1,107 @@
package org.jboss.resteasy.plugins.server.servlet;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.servlet.http.HttpServletRequest;

import org.jboss.resteasy.specimpl.ResteasyUriInfo;
import org.junit.Test;

public class ServletUtilTest {

@Test
public void extractUriInfo_simple() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource"));
ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
assertEquals("", rui.getContextPath());
assertEquals("/app/resource", rui.getPath());
}

@Test
public void extractUriInfo_simpleWithContextRoot() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("/contextRoot");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/contextRoot/app/resource"));
ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
assertEquals("/contextRoot", rui.getContextPath());
assertEquals("/app/resource", rui.getPath());
}

@Test
public void extractUriInfo_encoded()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource!"));
ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
assertEquals("", rui.getContextPath());
assertEquals("/app/resource!", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource%21"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("", rui.getContextPath());
assertEquals("/app/resource!", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app!/resource"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("", rui.getContextPath());
assertEquals("/app!/resource", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app%21/resource"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("", rui.getContextPath());
assertEquals("/app!/resource", rui.getPath());
}

@Test
public void extractUriInfo_encodedWithContextRoot()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("/contextRoot");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/contextRoot/app/resource!"));
ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
assertEquals("/contextRoot", rui.getContextPath());
assertEquals("/app/resource!", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("/contextRoot");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/contextRoot/app/resource%21"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("/contextRoot", rui.getContextPath());
assertEquals("/app/resource!", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("/contextRoot");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/contextRoot/app!/resource"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("/contextRoot", rui.getContextPath());
assertEquals("/app!/resource", rui.getPath());

request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn("/contextRoot");
when(request.getQueryString()).thenReturn(null);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/contextRoot/app%21/resource"));
rui = ServletUtil.extractUriInfo(request, null);
assertEquals("/contextRoot", rui.getContextPath());
assertEquals("/app!/resource", rui.getPath());
}
}

0 comments on commit 71bbf49

Please sign in to comment.