use of org.apache.http.cookie.MalformedCookieException in project jmeter by apache.
the class HC4CookieHandler method addCookieFromHeader.
@Override
public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader, URL url) {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm());
}
String protocol = url.getProtocol();
String host = url.getHost();
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
String path = url.getPath();
boolean isSecure = HTTPSamplerBase.isSecure(protocol);
List<org.apache.http.cookie.Cookie> cookies = null;
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure);
BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader);
try {
cookies = cookieSpec.parse(basicHeader, cookieOrigin);
} catch (MalformedCookieException e) {
log.error("Unable to add the cookie", e);
}
if (cookies == null) {
return;
}
for (org.apache.http.cookie.Cookie cookie : cookies) {
try {
if (checkCookies) {
cookieSpec.validate(cookie, cookieOrigin);
}
Date expiryDate = cookie.getExpiryDate();
long exp = 0;
if (expiryDate != null) {
exp = expiryDate.getTime();
}
Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), exp / 1000, ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR), ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR), cookie.getVersion());
// Store session cookies as well as unexpired ones
if (exp == 0 || exp >= System.currentTimeMillis()) {
// Has its own debug log; removes matching cookies
cookieManager.add(newCookie);
} else {
cookieManager.removeMatchingCookies(newCookie);
if (debugEnabled) {
log.info("Dropping expired Cookie: " + newCookie.toString());
}
}
} catch (MalformedCookieException e) {
// This means the cookie was wrong for the URL
log.warn("Not storing invalid cookie: <" + cookieHeader + "> for URL " + url + " (" + e.getLocalizedMessage() + ")");
} catch (IllegalArgumentException e) {
log.warn(cookieHeader + e.getLocalizedMessage());
}
}
}
use of org.apache.http.cookie.MalformedCookieException in project lucene-solr by apache.
the class SolrPortAwareCookieSpecTest method testDomainValidate1.
@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");
try {
h.validate(cookie, origin);
Assert.fail("MalformedCookieException should have been thrown");
} catch (final MalformedCookieException ex) {
// expected
}
}
use of org.apache.http.cookie.MalformedCookieException in project lucene-solr by apache.
the class SolrPortAwareCookieSpecTest method testDomainHostPortValidate.
@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");
try {
h.validate(cookie, origin);
Assert.fail("MalformedCookieException should have been thrown");
} catch (final MalformedCookieException ex) {
// expected
}
}
use of org.apache.http.cookie.MalformedCookieException in project lucene-solr by apache.
the class SolrPortAwareCookieSpecTest method testDomainValidate2.
@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");
try {
h.validate(cookie, origin);
Assert.fail("MalformedCookieException should have been thrown");
} catch (final MalformedCookieException ex) {
// expected
}
cookie.setDomain("www.otherdomain.com");
try {
h.validate(cookie, origin);
Assert.fail("MalformedCookieException should have been thrown");
} catch (final MalformedCookieException ex) {
// expected
}
}
use of org.apache.http.cookie.MalformedCookieException in project lucene-solr by apache.
the class SolrPortAwareCookieSpecTest method testDomainValidate3.
@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");
try {
h.validate(cookie, origin);
Assert.fail("MalformedCookieException should have been thrown");
} catch (final MalformedCookieException ex) {
// expected
}
}
Aggregations