org.apache.http.cookie.MalformedCookieException Java Examples

The following examples show how to use org.apache.http.cookie.MalformedCookieException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlUnitDomainHandler.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: HtmlUnitVersionAttributeHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: HtmlUnitExpiresHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: SolrPortAwareCookieSpecFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #5
Source File: SolrPortAwareCookieSpecTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #6
Source File: SolrPortAwareCookieSpecTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #7
Source File: SolrPortAwareCookieSpecTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #8
Source File: SolrPortAwareCookieSpecTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #9
Source File: SolrPortAwareCookieSpecTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #10
Source File: HttpResponseUtils.java    From esigate with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

}
 
Example #11
Source File: HtmlUnitPathHandler.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
    // nothing, browsers seem not to perform any validation
}
 
Example #12
Source File: HtmlUnitHttpOnlyHandler.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
    // nothing
}
 
Example #13
Source File: LenientCookieSpec.java    From karate with MIT License 4 votes vote down vote up
@Override
public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
    // do nothing
}
 
Example #14
Source File: HtmlUnitHttpOnlyHandler.java    From htmlunit with Apache License 2.0 3 votes vote down vote up
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
    ((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true");
}
 
Example #15
Source File: HttpOnlyHandler.java    From esigate with Apache License 2.0 2 votes vote down vote up
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {

        Args.notNull(cookie, "Cookie");
        ((BasicClientCookie) cookie).setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");

    }