Issue #3012 - Adding Cookie Compliance Listener support.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-03-13 12:35:02 -05:00
parent 07d62bb45e
commit 012bfddddc
5 changed files with 41 additions and 25 deletions

View File

@ -34,10 +34,12 @@ public abstract class CookieCutter
protected static final Logger LOG = Log.getLogger(CookieCutter.class); protected static final Logger LOG = Log.getLogger(CookieCutter.class);
protected final CookieCompliance _complianceMode; protected final CookieCompliance _complianceMode;
private final ComplianceViolation.Listener _complianceListener;
protected CookieCutter(CookieCompliance compliance) protected CookieCutter(CookieCompliance compliance, ComplianceViolation.Listener complianceListener)
{ {
_complianceMode = compliance; _complianceMode = compliance;
_complianceListener = complianceListener;
} }
protected void parseFields(List<String> rawFields) protected void parseFields(List<String> rawFields)
@ -285,9 +287,11 @@ public abstract class CookieCutter
protected void reportComplianceViolation(CookieCompliance.Violation violation, String reason) protected void reportComplianceViolation(CookieCompliance.Violation violation, String reason)
{ {
if (_complianceListener != null)
{
_complianceListener.onComplianceViolation(_complianceMode, violation, reason);
}
} }
protected abstract void addCookie(String cookieName, String cookieValue, String cookieDomain, String cookiePath, int cookieVersion, String cookieComment); protected abstract void addCookie(String cookieName, String cookieValue, String cookieDomain, String cookiePath, int cookieVersion, String cookieComment);
} }

View File

@ -18,15 +18,15 @@
package org.eclipse.jetty.http; package org.eclipse.jetty.http;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -179,7 +179,7 @@ public class CookieCutterLenientTest
protected TestCutter() protected TestCutter()
{ {
super(CookieCompliance.RFC6265); super(CookieCompliance.RFC6265, null);
} }
@Override @Override

View File

@ -18,13 +18,12 @@
package org.eclipse.jetty.http; package org.eclipse.jetty.http;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -32,7 +31,7 @@ public class CookieCutterTest
{ {
private Cookie[] parseCookieHeaders(CookieCompliance compliance,String... headers) private Cookie[] parseCookieHeaders(CookieCompliance compliance,String... headers)
{ {
TestCutter cutter = new TestCutter(compliance); TestCutter cutter = new TestCutter(compliance, null);
for (String header : headers) for (String header : headers)
{ {
cutter.parseFields(header); cutter.parseFields(header);
@ -269,14 +268,9 @@ public class CookieCutterTest
{ {
List<Cookie> cookies = new ArrayList<>(); List<Cookie> cookies = new ArrayList<>();
protected TestCutter() public TestCutter(CookieCompliance compliance, ComplianceViolation.Listener complianceListener)
{ {
this(CookieCompliance.RFC6265); super(compliance, complianceListener);
}
public TestCutter(CookieCompliance compliance)
{
super(compliance);
} }
@Override @Override

View File

@ -17,11 +17,12 @@
// //
package org.eclipse.jetty.server; package org.eclipse.jetty.server;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import org.eclipse.jetty.http.ComplianceViolation;
import org.eclipse.jetty.http.CookieCompliance; import org.eclipse.jetty.http.CookieCompliance;
import org.eclipse.jetty.http.CookieCutter; import org.eclipse.jetty.http.CookieCutter;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
@ -49,12 +50,12 @@ public class Cookies extends CookieCutter
public Cookies() public Cookies()
{ {
this(CookieCompliance.RFC6265); this(CookieCompliance.RFC6265, null);
} }
public Cookies(CookieCompliance compliance) public Cookies(CookieCompliance compliance, ComplianceViolation.Listener complianceListener)
{ {
super(compliance); super(compliance, complianceListener);
} }
public void addCookieField(String rawField) public void addCookieField(String rawField)

View File

@ -66,6 +66,7 @@ import javax.servlet.http.Part;
import javax.servlet.http.PushBuilder; import javax.servlet.http.PushBuilder;
import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.ComplianceViolation;
import org.eclipse.jetty.http.HostPortHttpField; import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpField;
@ -566,6 +567,22 @@ public class Request implements HttpServletRequest
return _channel.getState(); return _channel.getState();
} }
/* ------------------------------------------------------------ */
public ComplianceViolation.Listener getComplianceViolationListener()
{
if (_channel instanceof ComplianceViolation.Listener)
{
return (ComplianceViolation.Listener) _channel;
}
ComplianceViolation.Listener listener = _channel.getConnector().getBean(ComplianceViolation.Listener.class);
if (listener == null)
{
listener = _channel.getServer().getBean(ComplianceViolation.Listener.class);
}
return listener;
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* Get Request Attribute. * Get Request Attribute.
@ -763,7 +780,7 @@ public class Request implements HttpServletRequest
if (field.getHeader()==HttpHeader.COOKIE) if (field.getHeader()==HttpHeader.COOKIE)
{ {
if (_cookies==null) if (_cookies==null)
_cookies = new Cookies(getHttpChannel().getHttpConfiguration().getRequestCookieCompliance()); _cookies = new Cookies(getHttpChannel().getHttpConfiguration().getRequestCookieCompliance(), getComplianceViolationListener());
_cookies.addCookieField(field.getValue()); _cookies.addCookieField(field.getValue());
} }
} }
@ -2038,7 +2055,7 @@ public class Request implements HttpServletRequest
public void setCookies(Cookie[] cookies) public void setCookies(Cookie[] cookies)
{ {
if (_cookies == null) if (_cookies == null)
_cookies = new Cookies(getHttpChannel().getHttpConfiguration().getRequestCookieCompliance()); _cookies = new Cookies(getHttpChannel().getHttpConfiguration().getRequestCookieCompliance(), getComplianceViolationListener());
_cookies.setCookies(cookies); _cookies.setCookies(cookies);
} }