public final class Cookie
extends java.lang.Object
This class doesn't support additional attributes on cookies, like Chromium's Priority=HIGH extension.
Modifier and Type | Class and Description |
---|---|
static class |
Cookie.Builder
Builds a cookie.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.regex.Pattern |
DAY_OF_MONTH_PATTERN |
private java.lang.String |
domain |
private long |
expiresAt |
private boolean |
hostOnly |
private boolean |
httpOnly |
private static java.util.regex.Pattern |
MONTH_PATTERN |
private java.lang.String |
name |
private java.lang.String |
path |
private boolean |
persistent |
private boolean |
secure |
private static java.util.regex.Pattern |
TIME_PATTERN |
private java.lang.String |
value |
private static java.util.regex.Pattern |
YEAR_PATTERN |
Modifier | Constructor and Description |
---|---|
(package private) |
Cookie(Cookie.Builder builder) |
private |
Cookie(java.lang.String name,
java.lang.String value,
long expiresAt,
java.lang.String domain,
java.lang.String path,
boolean secure,
boolean httpOnly,
boolean hostOnly,
boolean persistent) |
Modifier and Type | Method and Description |
---|---|
private static int |
dateCharacterOffset(java.lang.String input,
int pos,
int limit,
boolean invert)
Returns the index of the next date character in
input , or if invert the index
of the next non-date character in input . |
java.lang.String |
domain()
Returns the cookie's domain.
|
private static boolean |
domainMatch(java.lang.String urlHost,
java.lang.String domain) |
boolean |
equals(java.lang.Object other) |
long |
expiresAt()
Returns the time that this cookie expires, in the same format as
System.currentTimeMillis() . |
int |
hashCode() |
boolean |
hostOnly()
Returns true if this cookie's domain should be interpreted as a single host name, or false if
it should be interpreted as a pattern.
|
boolean |
httpOnly()
Returns true if this cookie should be limited to only HTTP APIs.
|
boolean |
matches(HttpUrl url)
Returns true if this cookie should be included on a request to
url . |
java.lang.String |
name()
Returns a non-empty string with this cookie's name.
|
static Cookie |
parse(HttpUrl url,
java.lang.String setCookie)
Attempt to parse a
Set-Cookie HTTP header value setCookie as a cookie. |
(package private) static Cookie |
parse(long currentTimeMillis,
HttpUrl url,
java.lang.String setCookie) |
static java.util.List<Cookie> |
parseAll(HttpUrl url,
Headers headers)
Returns all of the cookies from a set of HTTP response headers.
|
private static java.lang.String |
parseDomain(java.lang.String s)
Returns a domain string like
example.com for an input domain like EXAMPLE.COM
or .example.com . |
private static long |
parseExpires(java.lang.String s,
int pos,
int limit)
Parse a date as specified in RFC 6265, section 5.1.1.
|
private static long |
parseMaxAge(java.lang.String s)
Returns the positive value if
attributeValue is positive, or Long.MIN_VALUE if
it is either 0 or negative. |
java.lang.String |
path()
Returns this cookie's path.
|
private static boolean |
pathMatch(HttpUrl url,
java.lang.String path) |
boolean |
persistent()
Returns true if this cookie does not expire at the end of the current session.
|
boolean |
secure()
Returns true if this cookie should be limited to only HTTPS requests.
|
java.lang.String |
toString() |
(package private) java.lang.String |
toString(boolean forObsoleteRfc2965) |
java.lang.String |
value()
Returns a possibly-empty string with this cookie's value.
|
private static final java.util.regex.Pattern YEAR_PATTERN
private static final java.util.regex.Pattern MONTH_PATTERN
private static final java.util.regex.Pattern DAY_OF_MONTH_PATTERN
private static final java.util.regex.Pattern TIME_PATTERN
private final java.lang.String name
private final java.lang.String value
private final long expiresAt
private final java.lang.String domain
private final java.lang.String path
private final boolean secure
private final boolean httpOnly
private final boolean persistent
private final boolean hostOnly
private Cookie(java.lang.String name, java.lang.String value, long expiresAt, java.lang.String domain, java.lang.String path, boolean secure, boolean httpOnly, boolean hostOnly, boolean persistent)
Cookie(Cookie.Builder builder)
public java.lang.String name()
public java.lang.String value()
public boolean persistent()
public long expiresAt()
System.currentTimeMillis()
. This is December 31, 9999 if the cookie is not persistent, in which case it will expire at the end of the current session.
This may return a value less than the current time, in which case the cookie is already expired. Webservers may return expired cookies as a mechanism to delete previously set cookies that may or may not themselves be expired.
public boolean hostOnly()
Set-Cookie
header
included a domain
attribute.
For example, suppose the cookie's domain is example.com
. If this flag is true it
matches only example.com
. If this flag is false it matches example.com
and all subdomains including api.example.com
, www.example.com
, and
beta.api.example.com
.
public java.lang.String domain()
hostOnly()
returns true this is the only domain that
matches this cookie; otherwise it matches this domain and all subdomains.public java.lang.String path()
/foo
this cookie matches requests to
/foo
and /foo/bar
, but not /
or /football
.public boolean httpOnly()
public boolean secure()
public boolean matches(HttpUrl url)
url
. In addition to this
check callers should also confirm that this cookie has not expired.private static boolean domainMatch(java.lang.String urlHost, java.lang.String domain)
private static boolean pathMatch(HttpUrl url, java.lang.String path)
@Nullable public static Cookie parse(HttpUrl url, java.lang.String setCookie)
Set-Cookie
HTTP header value setCookie
as a cookie. Returns
null if setCookie
is not a well-formed cookie.@Nullable static Cookie parse(long currentTimeMillis, HttpUrl url, java.lang.String setCookie)
private static long parseExpires(java.lang.String s, int pos, int limit)
private static int dateCharacterOffset(java.lang.String input, int pos, int limit, boolean invert)
input
, or if invert
the index
of the next non-date character in input
.private static long parseMaxAge(java.lang.String s)
attributeValue
is positive, or Long.MIN_VALUE
if
it is either 0 or negative. If the value is positive but out of range, this returns Long.MAX_VALUE
.java.lang.NumberFormatException
- if s
is not an integer of any precision.private static java.lang.String parseDomain(java.lang.String s)
example.com
for an input domain like EXAMPLE.COM
or .example.com
.public static java.util.List<Cookie> parseAll(HttpUrl url, Headers headers)
public java.lang.String toString()
toString
in class java.lang.Object
java.lang.String toString(boolean forObsoleteRfc2965)
forObsoleteRfc2965
- true to include a leading .
on the domain pattern. This is
necessary for example.com
to match www.example.com
under RFC 2965. This
extra dot is ignored by more recent specifications.public boolean equals(@Nullable java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object