Java 类org.apache.http.cookie.ClientCookie 实例源码

项目:lams    文件:RFC2965PortAttributeHandler.java   
/**
 * Validate cookie port attribute. If the Port attribute was specified
 * in header, the request port must be in cookie's port list.
 */
public void validate(final Cookie cookie, final CookieOrigin origin)
        throws MalformedCookieException {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (!portMatch(port, cookie.getPorts())) {
            throw new CookieRestrictionViolationException(
                    "Port attribute violates RFC 2965: "
                    + "Request port not found in cookie's port list.");
        }
    }
}
项目:lams    文件:RFC2965PortAttributeHandler.java   
/**
 * Match cookie port attribute. If the Port attribute is not specified
 * in header, the cookie can be sent to any port. Otherwise, the request port
 * must be in the cookie's port list.
 */
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (cookie.getPorts() == null) {
            // Invalid cookie state: port not specified
            return false;
        }
        if (!portMatch(port, cookie.getPorts())) {
            return false;
        }
    }
    return true;
}
项目:lams    文件:NetscapeDraftSpec.java   
/** Default constructor */
public NetscapeDraftSpec(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = new String[] { EXPIRES_PATTERN };
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:lams    文件:BrowserCompatSpec.java   
/** Default constructor */
public BrowserCompatSpec(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DEFAULT_DATE_PATTERNS;
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:lams    文件:RFC2109Spec.java   
/** Default constructor */
public RFC2109Spec(final String[] datepatterns, boolean oneHeader) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DATE_PATTERNS;
    }
    this.oneHeader = oneHeader;
    registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandler());
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:lams    文件:RFC2109Spec.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:remote-files-sync    文件:RFC2965PortAttributeHandlerHC4.java   
/**
 * Match cookie port attribute. If the Port attribute is not specified
 * in header, the cookie can be sent to any port. Otherwise, the request port
 * must be in the cookie's port list.
 */
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    Args.notNull(cookie, "Cookie");
    Args.notNull(origin, "Cookie origin");
    final int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (cookie.getPorts() == null) {
            // Invalid cookie state: port not specified
            return false;
        }
        if (!portMatch(port, cookie.getPorts())) {
            return false;
        }
    }
    return true;
}
项目:remote-files-sync    文件:NetscapeDraftSpecHC4.java   
/** Default constructor */
public NetscapeDraftSpecHC4(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = new String[] { EXPIRES_PATTERN };
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandlerHC4());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandlerHC4());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandlerHC4());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandlerHC4());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandlerHC4());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandlerHC4(
            this.datepatterns));
}
项目:remote-files-sync    文件:RFC2109SpecHC4.java   
/** Default constructor */
public RFC2109SpecHC4(final String[] datepatterns, final boolean oneHeader) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DATE_PATTERNS;
    }
    this.oneHeader = oneHeader;
    registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandlerHC4());
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandlerHC4());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandlerHC4());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandlerHC4());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandlerHC4());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandlerHC4());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandlerHC4(
            this.datepatterns));
}
项目:remote-files-sync    文件:RFC2109SpecHC4.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, final int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:cloud-meter    文件:HC4CookieHandler.java   
/**
 * Create an HttpClient cookie from a JMeter cookie
 */
private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) {
    long exp = jmc.getExpiresMillis();
    BasicClientCookie ret = new BasicClientCookie(jmc.getName(),
            jmc.getValue());
    ret.setDomain(jmc.getDomain());
    ret.setPath(jmc.getPath());
    ret.setExpiryDate(exp > 0 ? new Date(exp) : null); // use null for no expiry
    ret.setSecure(jmc.getSecure());
    ret.setVersion(jmc.getVersion());
    if(jmc.isDomainSpecified()) {
        ret.setAttribute(ClientCookie.DOMAIN_ATTR, jmc.getDomain());
    }
    if(jmc.isPathSpecified()) {
        ret.setAttribute(ClientCookie.PATH_ATTR, jmc.getPath());
    }
    return ret;
}
项目:purecloud-iot    文件:RFC2965PortAttributeHandler.java   
/**
 * Validate cookie port attribute. If the Port attribute was specified
 * in header, the request port must be in cookie's port list.
 */
@Override
public void validate(final Cookie cookie, final CookieOrigin origin)
        throws MalformedCookieException {
    Args.notNull(cookie, "Cookie");
    Args.notNull(origin, "Cookie origin");
    final int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (!portMatch(port, cookie.getPorts())) {
            throw new CookieRestrictionViolationException(
                    "Port attribute violates RFC 2965: "
                    + "Request port not found in cookie's port list.");
        }
    }
}
项目:purecloud-iot    文件:RFC2965PortAttributeHandler.java   
/**
 * Match cookie port attribute. If the Port attribute is not specified
 * in header, the cookie can be sent to any port. Otherwise, the request port
 * must be in the cookie's port list.
 */
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    Args.notNull(cookie, "Cookie");
    Args.notNull(origin, "Cookie origin");
    final int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (cookie.getPorts() == null) {
            // Invalid cookie state: port not specified
            return false;
        }
        if (!portMatch(port, cookie.getPorts())) {
            return false;
        }
    }
    return true;
}
项目:purecloud-iot    文件:RFC2109Spec.java   
/**
 * Return a string suitable for sending in a {@code "Cookie"} header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, final int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:purecloud-iot    文件:BasicDomainHandler.java   
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    Args.notNull(cookie, "Cookie");
    Args.notNull(origin, "Cookie origin");
    final String host = origin.getHost();
    String domain = cookie.getDomain();
    if (domain == null) {
        return false;
    }
    if (domain.startsWith(".")) {
        domain = domain.substring(1);
    }
    domain = domain.toLowerCase(Locale.ROOT);
    if (host.equals(domain)) {
        return true;
    }
    if (cookie instanceof ClientCookie) {
        if (((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            return domainMatch(domain, host);
        }
    }
    return false;
}
项目:purecloud-iot    文件:TestBasicCookieAttribHandlers.java   
@Test
public void testBasicDomainMatch2() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
    final CookieAttributeHandler h = new BasicDomainHandler();

    cookie.setDomain("somedomain.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "somedomain.com");
    Assert.assertTrue(h.match(cookie, origin));

    cookie.setDomain(".somedomain.com");
    Assert.assertTrue(h.match(cookie, origin));

    cookie.setDomain(null);
    Assert.assertFalse(h.match(cookie, origin));
}
项目:purecloud-iot    文件:TestCookieNetscapeDraft.java   
@Test
public void testFormatCookies() throws Exception {
    final BasicClientCookie c1 = new BasicClientCookie("name1", "value1");
    c1.setDomain(".whatever.com");
    c1.setAttribute(ClientCookie.DOMAIN_ATTR, c1.getDomain());
    c1.setPath("/");
    c1.setAttribute(ClientCookie.PATH_ATTR, c1.getPath());

    final Cookie c2 = new BasicClientCookie("name2", "value2");
    final Cookie c3 = new BasicClientCookie("name3", null);

    final CookieSpec cookiespec = new NetscapeDraftSpec();
    final List<Cookie> cookies = new ArrayList<Cookie>();
    cookies.add(c1);
    cookies.add(c2);
    cookies.add(c3);
    final List<Header> headers = cookiespec.formatCookies(cookies);
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals("name1=value1; name2=value2; name3", headers.get(0).getValue());
}
项目:purecloud-iot    文件:TestCookieRFC2965Spec.java   
/**
 * Test parsing cookie {@code "Port"} attribute.
 */
@Test
public void testParsePort() throws Exception {
    final CookieSpec cookiespec = new RFC2965Spec();
    final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
    final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"80,800,8000\";Version=1;Port=nonsense");
    final List<Cookie> cookies = cookiespec.parse(header, origin);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(1, cookies.size());
    // only the first occurrence of port attribute is considered, others ignored
    final ClientCookie cookie = (ClientCookie) cookies.get(0);
    final int[] ports = cookie.getPorts();
    Assert.assertNotNull(ports);
    Assert.assertEquals(3, ports.length);
    Assert.assertEquals(80, ports[0]);
    Assert.assertEquals(800, ports[1]);
    Assert.assertEquals(8000, ports[2]);
    Assert.assertTrue(cookie.containsAttribute(ClientCookie.PORT_ATTR));
}
项目:purecloud-iot    文件:TestCookieRFC2965Spec.java   
@Test
public void testParseNullPort() throws Exception {
    final CookieSpec cookiespec = new RFC2965Spec();
    final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
    // null port defaults to request port
    final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=;Version=1");
    final List<Cookie> cookies = cookiespec.parse(header, origin);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(1, cookies.size());
    final ClientCookie cookie = (ClientCookie) cookies.get(0);
    final int[] ports = cookie.getPorts();
    Assert.assertNotNull(ports);
    Assert.assertEquals(1, ports.length);
    Assert.assertEquals(80, ports[0]);
    Assert.assertEquals("", cookie.getAttribute(ClientCookie.PORT_ATTR));
}
项目:purecloud-iot    文件:TestCookieRFC2965Spec.java   
@Test
public void testParseBlankPort() throws Exception {
    final CookieSpec cookiespec = new RFC2965Spec();
    final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
    // blank port defaults to request port
    final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"  \";Version=1");
    final List<Cookie> cookies = cookiespec.parse(header, origin);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(1, cookies.size());
    final ClientCookie cookie = (ClientCookie) cookies.get(0);
    final int[] ports = cookie.getPorts();
    Assert.assertNotNull(ports);
    Assert.assertEquals(1, ports.length);
    Assert.assertEquals(80, ports[0]);
    Assert.assertEquals("  ", cookie.getAttribute(ClientCookie.PORT_ATTR));
}
项目:purecloud-iot    文件:TestCookieRFC2965Spec.java   
/**
 * Test {@code Domain} validation when domain is not specified
 * in {@code Set-Cookie2} header.
 */
@Test
public void testValidateNoDomain() throws Exception {
    final CookieSpec cookiespec = new RFC2965Spec();
    final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
    final Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
    final List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
        cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertNotNull(cookies);
    Assert.assertEquals(1, cookies.size());
    final ClientCookie cookie = (ClientCookie) cookies.get(0);
    // cookie domain must string match request host
    Assert.assertEquals("www.domain.com", cookie.getDomain());
}
项目:purecloud-iot    文件:TestCookieRFC2965Spec.java   
/**
 * test cookie {@code Port} matching.
 */
@Test
public void testMatchPort() throws Exception {
    // cookie can be sent to any port if port attribute not specified
    BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
    cookie.setDomain(".domain.com");
    cookie.setPath("/");
    cookie.setPorts(null);

    final CookieSpec cookiespec = new RFC2965Spec();
    final CookieOrigin origin1 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false);
    Assert.assertTrue(cookiespec.match(cookie, origin1));
    final CookieOrigin origin2 = new CookieOrigin("www.domain.com", 323 /* request port */, "/", false);
    Assert.assertTrue(cookiespec.match(cookie, origin2));

    // otherwise, request port must be in cookie's port list
    cookie = new BasicClientCookie2("name", "value");
    cookie.setDomain(".domain.com");
    cookie.setPath("/");
    cookie.setPorts(new int[] {80, 8080});
    cookie.setAttribute(ClientCookie.PORT_ATTR, "80, 8080");
    final CookieOrigin origin3 = new CookieOrigin("www.domain.com", 434 /* request port */, "/", false);
    Assert.assertFalse(cookiespec.match(cookie, origin3));
    final CookieOrigin origin4 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false);
    Assert.assertTrue(cookiespec.match(cookie, origin4));
}
项目:purecloud-iot    文件:TestDefaultCookieSpec.java   
@Test
public void testCookieStandardCompliantMatch() throws Exception {
    final CookieSpec cookiespec = new DefaultCookieSpec();
    final CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);

    // Make sure the strict (RFC2965) cookie matching
    // is used for version 1 cookies
    final BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
    cookie.setVersion(1);
    cookie.setDomain(".domain.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
    cookie.setPath("/");
    cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());

    Assert.assertFalse(cookiespec.match(cookie, origin));

    cookie.setDomain(".b.domain.com");

    Assert.assertTrue(cookiespec.match(cookie, origin));
}
项目:purecloud-iot    文件:TestRFC6265CookieSpec.java   
@Test
public void testParseCookieQuotedValue() throws Exception {
    final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();

    final Header header = new BasicHeader("Set-Cookie", "name = \" one, two, three; four \" ; this = stuff;");
    final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
    final List<Cookie> cookies = cookiespec.parse(header, origin);

    Assert.assertEquals(1, cookies.size());
    final Cookie cookie = cookies.get(0);
    Assert.assertEquals("name", cookie.getName());
    Assert.assertEquals(" one, two, three; four ", cookie.getValue());
    Assert.assertTrue(cookie instanceof ClientCookie);
    final ClientCookie clientCookie = (ClientCookie) cookie;
    Assert.assertEquals("stuff", clientCookie.getAttribute("this"));
}
项目:purecloud-iot    文件:TestRFC6265CookieSpec.java   
@Test
public void testParseCookieWithAttributes() throws Exception {
    final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h1.getAttributeName()).thenReturn("this");
    final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h2.getAttributeName()).thenReturn("that");

    final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);

    final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 = v ; p2 = v,0; p3 ; p4");
    final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
    final List<Cookie> cookies = cookiespec.parse(header, origin);

    Assert.assertEquals(1, cookies.size());
    final Cookie cookie = cookies.get(0);
    Assert.assertEquals("name", cookie.getName());
    Assert.assertEquals("value", cookie.getValue());
    Assert.assertTrue(cookie instanceof ClientCookie);
    final ClientCookie clientCookie = (ClientCookie) cookie;
    Assert.assertEquals("v", clientCookie.getAttribute("p1"));
    Assert.assertEquals("v,0", clientCookie.getAttribute("p2"));
    Assert.assertTrue(clientCookie.containsAttribute("p3"));
    Assert.assertTrue(clientCookie.containsAttribute("p4"));
    Assert.assertFalse(clientCookie.containsAttribute("p5"));
}
项目:purecloud-iot    文件:TestRFC6265CookieSpec.java   
@Test
public void testParseCookieWithAttributes2() throws Exception {
    final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h1.getAttributeName()).thenReturn("this");
    final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h2.getAttributeName()).thenReturn("that");

    final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);

    final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 = v");
    final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
    final List<Cookie> cookies = cookiespec.parse(header, origin);

    Assert.assertEquals(1, cookies.size());
    final Cookie cookie = cookies.get(0);
    Assert.assertEquals("name", cookie.getName());
    Assert.assertEquals("value", cookie.getValue());
    Assert.assertTrue(cookie instanceof ClientCookie);
    final ClientCookie clientCookie = (ClientCookie) cookie;
    Assert.assertEquals("v", clientCookie.getAttribute("p1"));
}
项目:purecloud-iot    文件:TestRFC6265CookieSpec.java   
@Test
public void testParseCookieWithAttributes3() throws Exception {
    final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h1.getAttributeName()).thenReturn("this");
    final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
    Mockito.when(h2.getAttributeName()).thenReturn("that");

    final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);

    final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 =");
    final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
    final List<Cookie> cookies = cookiespec.parse(header, origin);

    Assert.assertEquals(1, cookies.size());
    final Cookie cookie = cookies.get(0);
    Assert.assertEquals("name", cookie.getName());
    Assert.assertEquals("value", cookie.getValue());
    Assert.assertTrue(cookie instanceof ClientCookie);
    final ClientCookie clientCookie = (ClientCookie) cookie;
    Assert.assertEquals("", clientCookie.getAttribute("p1"));
}
项目:purecloud-iot    文件:TestCookieRFC2109Spec.java   
/**
 * Tests if that invalid second domain level cookie gets
 * rejected in the strict mode, but gets accepted in the
 * browser compatibility mode.
 */
@Test
public void testSecondDomainLevelCookie() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", null);
    cookie.setDomain(".sourceforge.net");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
    cookie.setPath("/");
    cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());

    final CookieSpec cookiespec = new RFC2109Spec();
    final CookieOrigin origin = new CookieOrigin("sourceforge.net", 80, "/", false);
    try {
        cookiespec.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException e) {
        // Expected
    }
}
项目:purecloud-iot    文件:TestCookieRFC2109Spec.java   
@Test
public void testCookieNullDomainNullPathFormatting() {
    final BasicClientCookie cookie = new BasicClientCookie("name", null);
    cookie.setPath("/");
    cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());

    final CookieSpec cookiespec = new RFC2109Spec();
    List<Cookie> cookies = new ArrayList<Cookie>();
    cookies.add(cookie);
    List<Header> headers = cookiespec.formatCookies(cookies);
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals("$Version=0; name=; $Path=/", headers.get(0).getValue());

    cookie.removeAttribute(ClientCookie.DOMAIN_ATTR);
    cookie.removeAttribute(ClientCookie.PATH_ATTR);
    cookies = new ArrayList<Cookie>();
    cookies.add(cookie);
    headers = cookiespec.formatCookies(cookies);
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals("$Version=0; name=", headers.get(0).getValue());
}
项目:Visit    文件:RFC2965PortAttributeHandlerHC4.java   
/**
 * Match cookie port attribute. If the Port attribute is not specified
 * in header, the cookie can be sent to any port. Otherwise, the request port
 * must be in the cookie's port list.
 */
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    Args.notNull(cookie, "Cookie");
    Args.notNull(origin, "Cookie origin");
    final int port = origin.getPort();
    if (cookie instanceof ClientCookie
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (cookie.getPorts() == null) {
            // Invalid cookie state: port not specified
            return false;
        }
        if (!portMatch(port, cookie.getPorts())) {
            return false;
        }
    }
    return true;
}
项目:Visit    文件:NetscapeDraftSpecHC4.java   
/** Default constructor */
public NetscapeDraftSpecHC4(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = new String[] { EXPIRES_PATTERN };
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandlerHC4());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandlerHC4());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandlerHC4());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandlerHC4());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandlerHC4());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandlerHC4(
            this.datepatterns));
}
项目:Visit    文件:RFC2109SpecHC4.java   
/** Default constructor */
public RFC2109SpecHC4(final String[] datepatterns, final boolean oneHeader) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DATE_PATTERNS;
    }
    this.oneHeader = oneHeader;
    registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandlerHC4());
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandlerHC4());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandlerHC4());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandlerHC4());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandlerHC4());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandlerHC4());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandlerHC4(
            this.datepatterns));
}
项目:Visit    文件:RFC2109SpecHC4.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, final int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:cJUnit-mc626    文件:RFC2965PortAttributeHandler.java   
/**
 * Validate cookie port attribute. If the Port attribute was specified
 * in header, the request port must be in cookie's port list.
 */
public void validate(final Cookie cookie, final CookieOrigin origin)
        throws MalformedCookieException {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    int port = origin.getPort();
    if (cookie instanceof ClientCookie 
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (!portMatch(port, cookie.getPorts())) {
            throw new MalformedCookieException(
                    "Port attribute violates RFC 2965: "
                    + "Request port not found in cookie's port list.");
        }
    }
}
项目:cJUnit-mc626    文件:RFC2965PortAttributeHandler.java   
/**
 * Match cookie port attribute. If the Port attribute is not specified
 * in header, the cookie can be sent to any port. Otherwise, the request port
 * must be in the cookie's port list.
 */
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    int port = origin.getPort();
    if (cookie instanceof ClientCookie 
            && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
        if (cookie.getPorts() == null) {
            // Invalid cookie state: port not specified
            return false;
        }
        if (!portMatch(port, cookie.getPorts())) {
            return false;
        }
    }
    return true;
}
项目:cJUnit-mc626    文件:NetscapeDraftSpec.java   
/** Default constructor */
public NetscapeDraftSpec(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = new String[] { EXPIRES_PATTERN };
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:cJUnit-mc626    文件:BrowserCompatSpec.java   
/** Default constructor */
public BrowserCompatSpec(final String[] datepatterns) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DEFAULT_DATE_PATTERNS;
    }
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:cJUnit-mc626    文件:RFC2109Spec.java   
/** Default constructor */
public RFC2109Spec(final String[] datepatterns, boolean oneHeader) {
    super();
    if (datepatterns != null) {
        this.datepatterns = datepatterns.clone();
    } else {
        this.datepatterns = DATE_PATTERNS;
    }
    this.oneHeader = oneHeader;
    registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandler());
    registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandler());
    registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
            this.datepatterns));
}
项目:cJUnit-mc626    文件:RFC2109Spec.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header 
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer, 
        final Cookie cookie, int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie 
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie 
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:hsac-fitnesse-fixtures    文件:CookieConverter.java   
/**
 * Converts Selenium cookie to Apache http client.
 * @param browserCookie selenium cookie.
 * @return http client format.
 */
protected ClientCookie convertCookie(Cookie browserCookie) {
    BasicClientCookie cookie = new BasicClientCookie(browserCookie.getName(), browserCookie.getValue());
    String domain = browserCookie.getDomain();
    if (domain != null && domain.startsWith(".")) {
        // http client does not like domains starting with '.', it always removes it when it receives them
        domain = domain.substring(1);
    }
    cookie.setDomain(domain);
    cookie.setPath(browserCookie.getPath());
    cookie.setExpiryDate(browserCookie.getExpiry());
    cookie.setSecure(browserCookie.isSecure());
    if (browserCookie.isHttpOnly()) {
        cookie.setAttribute("httponly", "");
    }
    return cookie;
}