Skip to content

Commit

Permalink
Merge pull request #4337 from eclipse/jetty-9.4.x-4329-rewrite-rules-…
Browse files Browse the repository at this point in the history
…demobase

Issue #4329 - Fixing RuleContainer to handle path parameters
  • Loading branch information
joakime committed Nov 27, 2019
2 parents de3ff47 + 15c9f0b commit 380cebc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
9 changes: 9 additions & 0 deletions jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
Expand Up @@ -573,6 +573,15 @@ public String getParam()
return _param;
}

public void setParam(String param)
{
_param = param;
if (_path != null && !_path.contains(_param))
{
_path += ";" + _param;
}
}

public String getQuery()
{
return _query;
Expand Down
Expand Up @@ -22,8 +22,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
Expand Down Expand Up @@ -185,7 +187,18 @@ protected String apply(String target, HttpServletRequest request, HttpServletRes
if (rule instanceof Rule.ApplyURI)
((Rule.ApplyURI)rule).applyURI(baseRequest, baseRequest.getRequestURI(), encoded);
else
baseRequest.setURIPathQuery(encoded);
{
String uriPathQuery = encoded;
HttpURI baseUri = baseRequest.getHttpURI();
// Copy path params from original URI if present
if ((baseUri != null) && StringUtil.isNotBlank(baseUri.getParam()))
{
HttpURI uri = new HttpURI(uriPathQuery);
uri.setParam(baseUri.getParam());
uriPathQuery = uri.toString();
}
baseRequest.setURIPathQuery(uriPathQuery);
}
}

if (_rewritePathInfo)
Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.jetty.server.handler.HandlerList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -48,7 +47,6 @@

public class CookiePatternRuleTest
{

private Server server;
private LocalConnector localConnector;

Expand Down Expand Up @@ -150,7 +148,6 @@ public void testSetAlready() throws Exception
}

@Test
@Disabled("See #2675 for details") // TODO: needs to be fixed in RuleContainer
public void testUrlParameter() throws Exception
{
CookiePatternRule rule = new CookiePatternRule();
Expand All @@ -170,7 +167,6 @@ public void testUrlParameter() throws Exception
HttpTester.Response response = HttpTester.parseResponse(rawResponse);

String responseContent = response.getContent();
System.out.println(responseContent);
assertResponseContentLine(responseContent, "baseRequest.requestUri=", "/other;fruit=apple");

// verify
Expand Down
Expand Up @@ -18,11 +18,14 @@

package org.eclipse.jetty.tests.distribution;

import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.FormContentProvider;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.Fields;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
Expand Down Expand Up @@ -173,4 +176,58 @@ public void testJPMS() throws Exception
assertEquals(HttpStatus.OK_200, response.getStatus());
}
}

@Test
public void testSessionDump() throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
DistributionTester distribution = DistributionTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.jettyBase(Paths.get("demo-base"))
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();

int httpPort = distribution.freePort();
int httpsPort = distribution.freePort();
String[] args = {
"jetty.http.port=" + httpPort,
"jetty.httpConfig.port=" + httpsPort,
"jetty.ssl.port=" + httpsPort
};
try (DistributionTester.Run run = distribution.start(args))
{
assertTrue(run.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS));

startHttpClient();
client.setFollowRedirects(true);
ContentResponse response = client.GET("http://localhost:" + httpPort + "/test/session/");
assertEquals(HttpStatus.OK_200, response.getStatus());

// Submit "New Session"
Fields form = new Fields();
form.add("Action", "New Session");
response = client.POST("http://localhost:" + httpPort + "/test/session/")
.content(new FormContentProvider(form))
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
String content = response.getContentAsString();
assertThat("Content", content, containsString("<b>test:</b> value<br/>"));
assertThat("Content", content, containsString("<b>WEBCL:</b> {}<br/>"));

// Last Location
URI location = response.getRequest().getURI();

// Submit a "Set" for a new entry in the cookie
form = new Fields();
form.add("Action", "Set");
form.add("Name", "Zed");
form.add("Value", "[alpha]");
response = client.POST(location)
.content(new FormContentProvider(form))
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
content = response.getContentAsString();
assertThat("Content", content, containsString("<b>Zed:</b> [alpha]<br/>"));
}
}
}
Expand Up @@ -41,7 +41,7 @@ public class SessionDump extends HttpServlet
/**
* Simple object attribute to test serialization
*/
public class ObjectAttributeValue implements java.io.Serializable
public static class ObjectAttributeValue implements java.io.Serializable
{
long l;

Expand All @@ -58,17 +58,14 @@ public long getValue()

int redirectCount = 0;

String pageType;

@Override
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}

protected void handleForm(HttpServletRequest request,
HttpServletResponse response)
protected void handleForm(HttpServletRequest request)
{
HttpSession session = request.getSession(false);
String action = request.getParameter("Action");
Expand Down Expand Up @@ -99,9 +96,9 @@ else if (action.equals("Remove"))
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
throws IOException
{
handleForm(request, response);
handleForm(request);
String nextUrl = getURI(request) + "?R=" + redirectCount++;
String encodedUrl = response.encodeRedirectURL(nextUrl);
response.sendRedirect(encodedUrl);
Expand All @@ -110,9 +107,9 @@ public void doPost(HttpServletRequest request,
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
throws IOException
{
handleForm(request, response);
handleForm(request);

response.setContentType("text/html");

Expand All @@ -124,6 +121,7 @@ public void doGet(HttpServletRequest request,
}
catch (IllegalStateException e)
{
log("Session already invalidated", e);
session = null;
}

Expand Down

0 comments on commit 380cebc

Please sign in to comment.