下面列出了怎么用org.apache.http.cookie.MalformedCookieException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void parse(final SetCookie cookie, final String value)
throws MalformedCookieException {
Args.notNull(cookie, HttpHeader.COOKIE);
if (TextUtils.isBlank(value)) {
throw new MalformedCookieException("Blank or null value for domain attribute");
}
// Ignore domain attributes ending with '.' per RFC 6265, 4.1.2.3
if (value.endsWith(".")) {
return;
}
String domain = value;
domain = domain.toLowerCase(Locale.ROOT);
final int dotIndex = domain.indexOf('.');
if (browserVersion_.hasFeature(HTTP_COOKIE_REMOVE_DOT_FROM_ROOT_DOMAINS)
&& dotIndex == 0 && domain.length() > 1 && domain.indexOf('.', 1) == -1) {
domain = domain.toLowerCase(Locale.ROOT);
domain = domain.substring(1);
}
if (dotIndex > 0) {
domain = '.' + domain;
}
cookie.setDomain(domain);
}
/**
* Parse cookie version attribute.
*/
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
if (value == null) {
throw new MalformedCookieException("Missing value for version attribute");
}
int version = 0;
try {
version = Integer.parseInt(value);
}
catch (final NumberFormatException e) {
// ignore invalid versions
}
cookie.setVersion(version);
}
@Override
public void parse(final SetCookie cookie, String value) throws MalformedCookieException {
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
value = value.replaceAll("[ ,:-]+", " ");
Date startDate = null;
String[] datePatterns = DEFAULT_DATE_PATTERNS;
if (null != browserVersion_) {
if (browserVersion_.hasFeature(HTTP_COOKIE_START_DATE_1970)) {
startDate = HtmlUnitBrowserCompatCookieSpec.DATE_1_1_1970;
}
if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1)) {
datePatterns = EXTENDED_DATE_PATTERNS_1;
}
if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2)) {
final Calendar calendar = Calendar.getInstance(Locale.ROOT);
calendar.setTimeZone(DateUtils.GMT);
calendar.set(1969, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
startDate = calendar.getTime();
datePatterns = EXTENDED_DATE_PATTERNS_2;
}
}
final Date expiry = DateUtils.parseDate(value, datePatterns, startDate);
cookie.setExpiryDate(expiry);
}
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
if (origin != null && origin.getHost() != null && cookie != null) {
String hostPort = origin.getHost() + ":" + origin.getPort();
String domain = cookie.getDomain();
if (hostPort.equals(domain)) {
return;
}
}
super.validate(cookie, origin);
}
@Test
public void testDomainHostPortValidate() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
cookie.setDomain("somehost:80");
h.validate(cookie, origin);
cookie.setDomain("somehost:1234");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
}
@Test
public void testDomainValidate1() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
cookie.setDomain("somehost");
h.validate(cookie, origin);
cookie.setDomain("otherhost");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
}
@Test
public void testDomainValidate2() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
cookie.setDomain(".somedomain.com");
h.validate(cookie, origin);
cookie.setDomain(".otherdomain.com");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
cookie.setDomain("www.otherdomain.com");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
}
@Test
public void testDomainValidate3() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);
final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
cookie.setDomain(".a.com");
h.validate(cookie, origin);
cookie.setDomain(".com");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
}
@Test
public void testDomainValidate4() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);
final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
cookie.setDomain(".a.b.c");
h.validate(cookie, origin);
cookie.setDomain(".b.c");
SolrTestCaseJ4.expectThrows(MalformedCookieException.class, () -> h.validate(cookie, origin));
}
/**
* Removes ";jsessionid=<id>" from the url, if the session id is also set in "httpResponse".
* <p>
* This methods first looks for the following header :
*
* <pre>
* Set-Cookie: JSESSIONID=
* </pre>
*
* If found and perfectly matches the jsessionid value in url, the complete jsessionid definition is removed from
* the url.
*
* @param uri
* original uri, may contains a jsessionid.
* @param httpResponse
* the response which set the jsessionId
* @return uri, without jsession
*/
public static String removeSessionId(String uri, HttpResponse httpResponse) {
CookieSpec cookieSpec = new DefaultCookieSpec();
// Dummy origin, used only by CookieSpec for setting the domain for the
// cookie but we don't need it
CookieOrigin cookieOrigin = new CookieOrigin("dummy", Http.DEFAULT_HTTP_PORT, "/", false);
Header[] responseHeaders = httpResponse.getHeaders("Set-cookie");
String jsessionid = null;
for (Header header : responseHeaders) {
try {
List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
for (Cookie cookie : cookies) {
if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
jsessionid = cookie.getValue();
}
break;
}
} catch (MalformedCookieException ex) {
LOG.warn("Malformed header: " + header.getName() + ": " + header.getValue());
}
if (jsessionid != null) {
break;
}
}
if (jsessionid == null) {
return uri;
}
return UriUtils.removeSessionId(jsessionid, uri);
}
@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
// nothing, browsers seem not to perform any validation
}
@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
// nothing
}
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true");
}
@Override
public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
// do nothing
}
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
((BasicClientCookie) cookie).setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");
}