simplification after review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2019-05-02 10:39:26 +02:00
parent 1a76d487cc
commit aafb0fcec1
5 changed files with 32 additions and 130 deletions

View File

@ -10,6 +10,9 @@ handler
[depend]
server
[files]
basehome:modules/inetaccess/jetty-inetaccess.xml|etc/jetty-inetaccess.xml
[xml]
etc/jetty-inetaccess.xml
@ -22,8 +25,8 @@ etc/jetty-inetaccess.xml
#jetty.inetaccess.exclude=127.0.0.1,127.0.0.2
## List of Connector names to include
#jetty.inetaccess.includeConnectorNames=http
#jetty.inetaccess.includeConnectors=http
## List of Connector names to exclude
#jetty.inetaccess.excludeConnectorNames=tls
#jetty.inetaccess.excludeConnectors=tls

View File

@ -5,7 +5,6 @@
<Call name="insertHandler">
<Arg>
<New id="InetAccessHandler" class="org.eclipse.jetty.server.handler.InetAccessHandler">
<Call name="include">
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
@ -20,17 +19,17 @@
</Call>
</Arg>
</Call>
<Call name="includeConnectorNames">
<Call name="includeConnectors">
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.inetaccess.includeConnectorNames" default=""/></Arg>
<Arg><Property name="jetty.inetaccess.includeConnectors" default=""/></Arg>
</Call>
</Arg>
</Call>
<Call name="excludeConnectorNames">
<Call name="excludeConnectors">
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.inetaccess.excludeConnectorNames" default=""/></Arg>
<Arg><Property name="jetty.inetaccess.excludeConnectors" default=""/></Arg>
</Call>
</Arg>
</Call>

View File

@ -116,7 +116,7 @@ public class InetAccessHandler extends HandlerWrapper
*
* @param name Connector name to include in this handler.
*/
public void includeConnectorName(String name)
public void includeConnector(String name)
{
_names.include(name);
}
@ -126,7 +126,7 @@ public class InetAccessHandler extends HandlerWrapper
*
* @param name Connector name to exclude in this handler.
*/
public void excludeConnectorName(String name)
public void excludeConnector(String name)
{
_names.exclude(name);
}
@ -136,7 +136,7 @@ public class InetAccessHandler extends HandlerWrapper
*
* @param names Connector names to include in this handler.
*/
public void includeConnectorNames(String... names)
public void includeConnectors(String... names)
{
_names.include(names);
}
@ -146,7 +146,7 @@ public class InetAccessHandler extends HandlerWrapper
*
* @param names Connector names to exclude in this handler.
*/
public void excludeConnectorNames(String... names)
public void excludeConnectors(String... names)
{
_names.exclude(names);
}
@ -195,7 +195,7 @@ public class InetAccessHandler extends HandlerWrapper
Boolean allowedByAddr = _addrs.isIncludedAndNotExcluded(addr);
LOG.debug("{} allowedByName={} allowedByAddr={} for {}/{}", this, allowedByName, allowedByAddr, addr, request);
}
return IncludeExclude.and(_addrs, addr, _names, baseRequest.getHttpChannel().getConnector()::getName);
return _names.test(name) && _addrs.test(addr);
}
@Override

View File

@ -18,19 +18,11 @@
package org.eclipse.jetty.server.handler;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.servlet.ServletException;
@ -38,6 +30,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
@ -49,7 +43,6 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class InetAccessHandlerTest
{
@ -81,14 +74,12 @@ public class InetAccessHandlerTest
_server.start();
}
/* ------------------------------------------------------------ */
@AfterAll
public static void tearDown() throws Exception
{
_server.stop();
}
/* ------------------------------------------------------------ */
@ParameterizedTest
@MethodSource("data")
public void testHandler(String include, String exclude, String includeConnectors, String excludeConnectors, String code)
@ -113,104 +104,43 @@ public class InetAccessHandlerTest
{
if (inc.length() > 0)
{
_handler.includeConnectorName(inc);
_handler.includeConnector(inc);
}
}
for (String exc : excludeConnectors.split(";", -1))
{
if (exc.length() > 0)
{
_handler.excludeConnectorName(exc);
_handler.excludeConnector(exc);
}
}
String request = "GET /path HTTP/1.1\n" + "Host: 127.0.0.1\n\n";
Socket socket = new Socket("127.0.0.1", _connector.getLocalPort());
try (Socket socket = new Socket("127.0.0.1", _connector.getLocalPort());)
{
socket.setSoTimeout(5000);
try
{
OutputStream output = socket.getOutputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
HttpTester.Request request = HttpTester.newRequest();
request.setMethod("GET");
request.setURI("/path");
request.setHeader("Host","127.0.0.1");
request.setVersion(HttpVersion.HTTP_1_0);
Response response = readResponse(input);
ByteBuffer output = request.generate();
socket.getOutputStream().write(output.array(),output.arrayOffset()+output.position(),output.remaining());
HttpTester.Input input = HttpTester.from(socket.getInputStream());
HttpTester.Response response = HttpTester.parseResponse(input);
Object[] params = new Object[]
{ "Request WBHUC", include, exclude, includeConnectors, excludeConnectors, code, "Response", response.getCode() };
assertEquals(code, response.getCode(), Arrays.deepToString(params));
} finally
{
socket.close();
{ "Request WBHUC", include, exclude, includeConnectors, excludeConnectors, code, "Response", response.getStatus() };
assertEquals(Integer.parseInt(code), response.getStatus(), Arrays.deepToString(params));
}
}
/* ------------------------------------------------------------ */
protected Response readResponse(BufferedReader reader) throws IOException
{
// Simplified parser for HTTP responses
String line = reader.readLine();
if (line == null)
throw new EOFException();
Matcher responseLine = Pattern.compile("HTTP/1\\.1\\s+(\\d+)").matcher(line);
assertTrue(responseLine.lookingAt());
String code = responseLine.group(1);
Map<String, String> headers = new LinkedHashMap<>();
while ((line = reader.readLine()) != null)
{
if (line.trim().length() == 0)
break;
Matcher header = Pattern.compile("([^:]+):\\s*(.*)").matcher(line);
assertTrue(header.lookingAt());
String headerName = header.group(1);
String headerValue = header.group(2);
headers.put(headerName.toLowerCase(Locale.ENGLISH), headerValue.toLowerCase(Locale.ENGLISH));
}
StringBuilder body = new StringBuilder();
if (headers.containsKey("content-length"))
{
int length = Integer.parseInt(headers.get("content-length"));
for (int i = 0; i < length; ++i)
{
char c = (char) reader.read();
body.append(c);
}
} else if ("chunked".equals(headers.get("transfer-encoding")))
{
while ((line = reader.readLine()) != null)
{
if ("0".equals(line))
{
line = reader.readLine();
assertEquals("", line);
break;
}
int length = Integer.parseInt(line, 16);
for (int i = 0; i < length; ++i)
{
char c = (char) reader.read();
body.append(c);
}
line = reader.readLine();
assertEquals("", line);
}
}
return new Response(code, headers, body.toString().trim());
}
/* ------------------------------------------------------------ */
protected class Response
{
private final String code;
private final Map<String, String> headers;
private final String body;
/* ------------------------------------------------------------ */
private Response(String code, Map<String, String> headers, String body)
{
this.code = code;
@ -218,25 +148,21 @@ public class InetAccessHandlerTest
this.body = body;
}
/* ------------------------------------------------------------ */
public String getCode()
{
return code;
}
/* ------------------------------------------------------------ */
public Map<String, String> getHeaders()
{
return headers;
}
/* ------------------------------------------------------------ */
public String getBody()
{
return body;
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
@ -250,7 +176,6 @@ public class InetAccessHandlerTest
}
}
/* ------------------------------------------------------------ */
public static Stream<Arguments> data()
{
Object[][] data = new Object[][]

View File

@ -223,31 +223,6 @@ public class IncludeExcludeSet<T,P> implements Predicate<P>
return _includes.isEmpty() && _excludes.isEmpty();
}
/**
* Combine the results of two {@link IncludeExcludeSet}s with an "and"-like operation.
* Item x must be included and not excluded by xSet AND item y must be included and not excluded by ySet.
* If a sets inclusions are empty, then all items are considered to be included.
* @param xSet The set that param x is tested against
* @param x An item to test against xSet
* @param ySet The set that param y is tested against
* @param y A supplier of an item to test against ySet, that is executed only of x is not excluded from xSet
* @param <XS> The type of xSet items
* @param <XP> The type of xSet predicates
* @param <YS> The type of ySet items
* @param <YP> The type of ySet predicates
* @return True only if the items are included and not excluded from their respected sets
*/
public static <XS, XP, YS, YP> boolean and(IncludeExcludeSet<XS, XP> xSet, XP x, IncludeExcludeSet<YS,YP> ySet, Supplier<YP> y)
{
Boolean xb = xSet.isIncludedAndNotExcluded(x);
if (Boolean.FALSE==xb || (xb==null && xSet.hasIncludes()))
return false;
Boolean yb = ySet.isIncludedAndNotExcluded(y.get());
if (Boolean.FALSE==yb || (yb==null && ySet.hasIncludes()))
return false;
return true;
}
/**
* Combine the results of two {@link IncludeExcludeSet}s with an "or"-like operation.