Merge branch 'jetty-9.3.x' into issue-472

This commit is contained in:
Greg Wilkins 2016-04-05 12:24:25 +10:00
commit bbb7f2188c
24 changed files with 367 additions and 54 deletions

View File

@ -40,6 +40,11 @@
<artifactId>jetty-deploy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-rewrite</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>

View File

@ -0,0 +1,52 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.embedded;
import org.eclipse.jetty.rewrite.RewriteCustomizer;
import org.eclipse.jetty.rewrite.handler.CompactPathRule;
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class RewriteServer
{
public static void main( String[] args ) throws Exception
{
Server server = new Server(8080);
HttpConfiguration config=server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
RewriteCustomizer rewrite = new RewriteCustomizer();
config.addCustomizer(rewrite);
rewrite.addRule(new CompactPathRule());
rewrite.addRule(new RewriteRegexRule("(.*)foo(.*)","$1FOO$2"));
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(DumpServlet.class, "/*");
server.start();
server.join();
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<!-- =========================================================== -->
<!-- configure rewrite rule container as a customizer -->
<!-- =========================================================== -->
<Call name="addCustomizer">
<Arg>
<New id="Rewrite" class="org.eclipse.jetty.rewrite.RewriteCustomizer">
<Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" default="true"/></Set>
<Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" default="requestedPath"/></Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -8,7 +8,7 @@
<!-- =========================================================== -->
<Call name="insertHandler">
<Arg>
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<New class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set>
<Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set>
@ -21,19 +21,9 @@
</Array>
</Set>
<!-- example rule -->
<!--
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
<Set name="pattern">/favicon.ico</Set>
<Set name="name">Cache-Control</Set>
<Set name="value">Max-Age=3600,public</Set>
<Set name="terminating">true</Set>
</New>
</Arg>
</Call>
-->
<Get id="Rewrite" name="ruleContainer"/>
<!-- see rewrite-compactpath.xml for example how to add a rule -->
</New>
</Arg>

View File

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RuleContainer">
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.CompactPathRule"/>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,10 @@
#
# Jetty Rewrite CompactPath module
#
[xml]
etc/rewrite-compactpath.xml
[ini-template]
## Requires either rewrite or rewrite-customizer module
## with rewritePathInfo==true
jetty.rewrite.rewritePathInfo=true

View File

@ -0,0 +1,24 @@
#
# Jetty Rewrite Customizer module
#
# Apply rewrite rules as a request customizer applied to all
# connectors sharing a HttpConfiguration
#
[depend]
server
[lib]
lib/jetty-rewrite-${jetty.version}.jar
[xml]
etc/jetty-rewrite-customizer.xml
[ini-template]
## Whether to rewrite the request URI
# jetty.rewrite.rewriteRequestURI=true
## Whether to rewrite the path info
# jetty.rewrite.rewritePathInfo=true
## Request attribute key under with the original path is stored
# jetty.rewrite.originalPathAttribute=requestedPath

View File

@ -1,7 +1,8 @@
#
# Jetty Rewrite module
#
# Install rewrite rules as a handler applied to all requests on a server
#
[depend]
server

View File

@ -0,0 +1,44 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.rewrite;
import java.io.IOException;
import org.eclipse.jetty.io.RuntimeIOException;
import org.eclipse.jetty.rewrite.handler.RuleContainer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConfiguration.Customizer;
import org.eclipse.jetty.server.Request;
public class RewriteCustomizer extends RuleContainer implements Customizer
{
@Override
public void customize(Connector connector, HttpConfiguration channelConfig, Request request)
{
try
{
matchAndApply(request.getPathInfo(), request, request.getResponse());
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
}

View File

@ -24,6 +24,8 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.annotation.Name;
/**
* Sets the cookie in the response whenever the rule finds a match.
@ -38,8 +40,17 @@ public class CookiePatternRule extends PatternRule
/* ------------------------------------------------------------ */
public CookiePatternRule()
{
this(null,null,null);
}
/* ------------------------------------------------------------ */
public CookiePatternRule(@Name("pattern") String pattern, @Name("name") String name, @Name("value") String value)
{
super(pattern);
_handling = false;
_terminating = false;
setName(name);
setValue(value);
}
/* ------------------------------------------------------------ */

View File

@ -23,6 +23,8 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.annotation.Name;
/**
* Sets the header in the response whenever the rule finds a match.
@ -31,13 +33,23 @@ public class HeaderPatternRule extends PatternRule
{
private String _name;
private String _value;
private boolean _add=false;
private boolean _add;
/* ------------------------------------------------------------ */
public HeaderPatternRule()
{
this(null,null,null);
}
/* ------------------------------------------------------------ */
public HeaderPatternRule(@Name("pattern") String pattern, @Name("name") String name, @Name("value") String value)
{
super(pattern);
_handling = false;
_terminating = false;
_add=false;
setName(name);
setValue(value);
}
/* ------------------------------------------------------------ */

View File

@ -24,13 +24,14 @@ import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.annotation.Name;
/* ------------------------------------------------------------ */
/** Rule to add a header based on a Regex match
*/
public class HeaderRegexRule extends RegexRule
{
private String _name;
private String _value;
private boolean _add=false;
@ -38,10 +39,19 @@ public class HeaderRegexRule extends RegexRule
/* ------------------------------------------------------------ */
public HeaderRegexRule()
{
_handling = false;
_terminating = false;
this(null,null,null);
}
/* ------------------------------------------------------------ */
public HeaderRegexRule(@Name("regex") String regex, @Name("name") String name, @Name("value") String value)
{
super(regex);
setHandling(false);
setTerminating(false);
setName(name);
setValue(value);
}
/* ------------------------------------------------------------ */
/**
* Sets the header name.

View File

@ -33,7 +33,18 @@ public abstract class PatternRule extends Rule
{
protected String _pattern;
/* ------------------------------------------------------------ */
protected PatternRule()
{
}
/* ------------------------------------------------------------ */
protected PatternRule(String pattern)
{
this();
setPattern(pattern);
}
/* ------------------------------------------------------------ */
public String getPattern()
{

View File

@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.annotation.Name;
/**
* Issues a (3xx) Redirect response whenever the rule finds a match.
@ -39,10 +40,17 @@ public class RedirectPatternRule extends PatternRule
public RedirectPatternRule()
{
_handling = true;
_terminating = true;
this(null,null);
}
public RedirectPatternRule(@Name("pattern") String pattern, @Name("location") String location)
{
super(pattern);
_handling = true;
_terminating = true;
_location=location;
}
/**
* Sets the redirect location.
*

View File

@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.annotation.Name;
/**
* Issues a (3xx) Redirect response whenever the rule finds a match via regular expression.
@ -37,23 +38,31 @@ import org.eclipse.jetty.http.HttpStatus;
*/
public class RedirectRegexRule extends RegexRule
{
protected String _replacement;
protected String _location;
private int _statusCode = HttpStatus.FOUND_302;
public RedirectRegexRule()
{
_handling = true;
_terminating = true;
this(null,null);
}
public RedirectRegexRule(@Name("regex") String regex, @Name("location") String location)
{
super(regex);
setHandling(true);
setTerminating(true);
setLocation(location);
}
/**
* Whenever a match is found, it replaces with this value.
*
* @param replacement the replacement string.
*/
@Deprecated
public void setReplacement(String replacement)
{
_replacement = replacement;
_location = replacement;
}
public void setLocation(String location)
{
_location = location;
}
/**
@ -77,7 +86,7 @@ public class RedirectRegexRule extends RegexRule
protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
throws IOException
{
target=_replacement;
target=_location;
for (int g=1;g<=matcher.groupCount();g++)
{
String group = matcher.group(g);
@ -101,7 +110,7 @@ public class RedirectRegexRule extends RegexRule
StringBuilder str = new StringBuilder();
str.append(super.toString());
str.append('[').append(_statusCode);
str.append('>').append(_replacement);
str.append('>').append(_location);
str.append(']');
return str.toString();
}

View File

@ -32,7 +32,18 @@ import javax.servlet.http.HttpServletResponse;
public abstract class RegexRule extends Rule
{
protected Pattern _regex;
/* ------------------------------------------------------------ */
protected RegexRule()
{
}
/* ------------------------------------------------------------ */
protected RegexRule(String pattern)
{
setRegex(pattern);
}
/* ------------------------------------------------------------ */
/**
* Sets the regular expression string used to match with string URI.
@ -41,7 +52,7 @@ public abstract class RegexRule extends Rule
*/
public void setRegex(String regex)
{
_regex=Pattern.compile(regex);
_regex=regex==null?null:Pattern.compile(regex);
}
/* ------------------------------------------------------------ */

View File

@ -23,19 +23,29 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.annotation.Name;
/**
* Sends the response code whenever the rule finds a match.
*/
public class ResponsePatternRule extends PatternRule
{
private String _code;
private String _reason = "";
private String _reason;
/* ------------------------------------------------------------ */
public ResponsePatternRule()
{
this(null,null,"");
}
/* ------------------------------------------------------------ */
public ResponsePatternRule(@Name("pattern") String pattern, @Name("code") String code, @Name("reason") String reason)
{
_handling = true;
_terminating = true;
setCode(code);
setReason(reason);
}
/* ------------------------------------------------------------ */

View File

@ -206,11 +206,28 @@ public class RewriteHandler extends HandlerWrapper
* Assigns the rules to process.
* @param rules a {@link RuleContainer} containing other rules to process
*/
@Deprecated
public void setRules(RuleContainer rules)
{
_rules = rules;
}
/*------------------------------------------------------------ */
/**
* Assigns the rules to process.
* @param rules a {@link RuleContainer} containing other rules to process
*/
public void setRuleContainer(RuleContainer rules)
{
_rules = rules;
}
/*------------------------------------------------------------ */
public RuleContainer getRuleContainer()
{
return _rules;
}
/* ------------------------------------------------------------ */
/**
* Add a Rule

View File

@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.Name;
/**
* Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string.
@ -37,11 +38,19 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
/* ------------------------------------------------------------ */
public RewritePatternRule()
{
this(null,null);
}
/* ------------------------------------------------------------ */
public RewritePatternRule(@Name("pattern") String pattern, @Name("replacement") String replacement)
{
_handling = false;
_terminating = false;
setReplacement(replacement);
}
/* ------------------------------------------------------------ */
/**
* Whenever a match is found, it replaces with this value.
@ -50,9 +59,17 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
*/
public void setReplacement(String replacement)
{
String[] split = replacement.split("\\?", 2);
_replacement = split[0];
_query = split.length == 2 ? split[1] : null;
if (replacement==null)
{
_replacement=null;
_query=null;
}
else
{
String[] split = replacement.split("\\?", 2);
_replacement = split[0];
_query = split.length == 2 ? split[1] : null;
}
}
/* ------------------------------------------------------------ */

View File

@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.annotation.Name;
/**
* Rewrite the URI by matching with a regular expression.
@ -43,8 +44,16 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
/* ------------------------------------------------------------ */
public RewriteRegexRule()
{
_handling = false;
_terminating = false;
this(null,null);
}
/* ------------------------------------------------------------ */
public RewriteRegexRule(@Name("regex") String regex, @Name("replacement") String replacement)
{
setHandling(false);
setTerminating(false);
setRegex(regex);
setReplacement(replacement);
}
/* ------------------------------------------------------------ */
@ -55,10 +64,19 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
*/
public void setReplacement(String replacement)
{
String[] split=replacement.split("\\?",2);
_replacement = split[0];
_query=split.length==2?split[1]:null;
_queryGroup=_query!=null && _query.contains("$Q");
if (replacement==null)
{
_replacement=null;
_query=null;
_queryGroup=false;
}
else
{
String[] split=replacement.split("\\?",2);
_replacement = split[0];
_query=split.length==2?split[1]:null;
_queryGroup=_query!=null && _query.contains("$Q");
}
}

View File

@ -161,13 +161,18 @@ public class RuleContainer extends Rule
{
boolean original_set=_originalPathAttribute==null;
// TODO why is this always done?
target = URIUtil.compactPath(target);
if (_rules==null)
return target;
for (Rule rule : _rules)
{
String applied=rule.matchAndApply(target,request, response);
if (applied!=null)
{
// TODO why is this always done?
applied = URIUtil.compactPath(applied);
LOG.debug("applied {}",rule);

View File

@ -31,6 +31,12 @@ public class TerminatingPatternRule extends PatternRule
{
public TerminatingPatternRule()
{
this(null);
}
public TerminatingPatternRule(String pattern)
{
super(pattern);
super.setTerminating(true);
}

View File

@ -24,6 +24,8 @@ import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.annotation.Name;
/**
* If this rule matches, terminate the processing of other rules.
* Allowing the request to be processed by the handlers after the rewrite rules.
@ -32,6 +34,12 @@ public class TerminatingRegexRule extends RegexRule
{
public TerminatingRegexRule()
{
this(null);
}
public TerminatingRegexRule(@Name("regex") String regex)
{
super(regex);
super.setTerminating(true);
}

View File

@ -295,16 +295,23 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
_request.setHandled(false);
_response.getHttpOutput().reopen();
List<HttpConfiguration.Customizer> customizers = _configuration.getCustomizers();
if (!customizers.isEmpty())
{
for (HttpConfiguration.Customizer customizer : customizers)
customizer.customize(getConnector(), _configuration, _request);
}
try
{
_request.setDispatcherType(DispatcherType.REQUEST);
getServer().handle(this);
List<HttpConfiguration.Customizer> customizers = _configuration.getCustomizers();
if (!customizers.isEmpty())
{
for (HttpConfiguration.Customizer customizer : customizers)
{
customizer.customize(getConnector(), _configuration, _request);
if (_request.isHandled())
break;
}
}
if (!_request.isHandled())
getServer().handle(this);
}
finally
{