Search in sources :

Example 11 with MalformedCookieException

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());
        }
    }
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) Date(java.util.Date) CookieOrigin(org.apache.http.cookie.CookieOrigin) BasicHeader(org.apache.http.message.BasicHeader)

Example 12 with MalformedCookieException

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
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 13 with MalformedCookieException

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
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 14 with MalformedCookieException

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
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 15 with MalformedCookieException

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
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Aggregations

MalformedCookieException (org.apache.http.cookie.MalformedCookieException)30 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)11 HeaderElement (org.apache.http.HeaderElement)9 Cookie (org.apache.http.cookie.Cookie)9 ArrayList (java.util.ArrayList)6 StringTokenizer (java.util.StringTokenizer)6 FormattedHeader (org.apache.http.FormattedHeader)6 NameValuePair (org.apache.http.NameValuePair)6 CookieOrigin (org.apache.http.cookie.CookieOrigin)6 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)6 ParserCursor (org.apache.http.message.ParserCursor)6 CharArrayBuffer (org.apache.http.util.CharArrayBuffer)6 Test (org.junit.Test)5 Date (java.util.Date)4 ClientCookie (org.apache.http.cookie.ClientCookie)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Header (org.apache.http.Header)3 BasicHeader (org.apache.http.message.BasicHeader)1