Class Cookie
- All Implemented Interfaces:
Proxy
To have a Session
handle cookies for your appliction
automatically, use a CookieJar
.
name
and value
will be set for all cookies. If the cookie is
generated from a string that appears to have no name, then name
will be the empty string.
domain
and path
give the host or domain, and path within that
host/domain, to restrict this cookie to. If domain
starts with
".", that indicates a domain (which matches the string after the
".", or any hostname that has domain
as a suffix). Otherwise, it
is a hostname and must match exactly.
expires
will be non-null
if the cookie uses either the original
"expires" attribute, or the newer "max-age" attribute. If expires
is null
, it indicates that neither "expires" nor "max-age" was
specified, and the cookie expires at the end of the session.
If httpOnly
is set, the cookie should not be exposed to untrusted
code (eg, javascript), so as to minimize the danger posed by
cross-site scripting attacks.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
appliesToUri
(Uri uri) Tests if this Cookie should be sent touri
.copy()
Copies this Cookie.boolean
domainMatches
(String host) Checks if the this Cookie's domain andhost
match.boolean
Tests if this Cookie andcookie2
are equal.void
free()
Frees this Cookie.Gets this Cookie's domain.Gets this Cookie's expiration time.boolean
Gets this Cookie's HttpOnly attribute.getName()
Gets this Cookie's name.getPath()
Gets this Cookie's path.Returns the same-site policy for this cookie.boolean
Gets this Cookie's secure attribute.static Type
getType()
Get the GType of the Cookie classgetValue()
Gets this Cookie's value.static Cookie
Parsesheader
and returns aSoupCookie
.void
Sets this Cookie's domain todomain
.void
setExpires
(DateTime expires) Sets this Cookie's expiration time toexpires
.void
setHttpOnly
(boolean httpOnly) Sets this Cookie's HttpOnly attribute tohttpOnly
.void
setMaxAge
(int maxAge) Sets this Cookie's max age tomaxAge
.void
Sets this Cookie's name toname
.void
Sets this Cookie's path topath
.void
setSameSitePolicy
(SameSitePolicy policy) When used in conjunction withCookieJar.getCookieListWithSameSiteInfo(org.gnome.glib.Uri, org.gnome.glib.Uri, org.gnome.glib.Uri, boolean, boolean, boolean)
this sets the policy of when this cookie should be exposed.void
setSecure
(boolean secure) Sets this Cookie's secure attribute tosecure
.void
Sets this Cookie's value tovalue
.Serializes this Cookie in the format used by the Cookie header (ie, for returning a cookie from aSession
to a server).Serializes this Cookie in the format used by the Set-Cookie header.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Cookie
Create a Cookie proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Cookie
public Cookie(String name, String value, String domain, String path, int maxAge) throws UnsupportedPlatformException Creates a newSoupCookie
with the given attributes.Use
setSecure(boolean)
andsetHttpOnly(boolean)
if you need to set those attributes on the returned cookie.If
domain
starts with ".", that indicates a domain (which matches the string after the ".", or any hostname that hasdomain
as a suffix). Otherwise, it is a hostname and must match exactly.maxAge
is used to set the "expires" attribute on the cookie; pass -1 to not include the attribute (indicating that the cookie expires with the current session), 0 for an already-expired cookie, or a lifetime in seconds. You can use the constantsSOUP_COOKIE_MAX_AGE_ONE_HOUR
,SOUP_COOKIE_MAX_AGE_ONE_DAY
,SOUP_COOKIE_MAX_AGE_ONE_WEEK
andSOUP_COOKIE_MAX_AGE_ONE_YEAR
(or multiples thereof) to calculate this value. (If you really care about setting the exact time that the cookie will expire, usesetExpires(org.gnome.glib.DateTime)
.)As of version 3.4.0 the default value of a cookie's same-site-policy is
SameSitePolicy.LAX
.- Parameters:
name
- cookie namevalue
- cookie valuedomain
- cookie domain or hostnamepath
- cookie path, ornull
maxAge
- max age of the cookie, or -1 for a session cookie- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
parse
public static Cookie parse(String header, @Nullable @Nullable Uri origin) throws UnsupportedPlatformException Parsesheader
and returns aSoupCookie
.If
header
contains multiple cookies, only the first one will be parsed.If
header
does not have "path" or "domain" attributes, they will be defaulted fromorigin
. Iforigin
isnull
, path will default to "/", but domain will be left asnull
. Note that this is not a valid state for aSoupCookie
, and you will need to fill in some appropriate string for the domain if you want to actually make use of the cookie.As of version 3.4.0 the default value of a cookie's same-site-policy is
SameSitePolicy.LAX
.- Parameters:
header
- a cookie string (eg, the value of a Set-Cookie header)origin
- origin of the cookie- Returns:
- a new
SoupCookie
, ornull
if it could not be parsed, or contained an illegal "domain" attribute for a cookie originating fromorigin
. - Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
appliesToUri
Tests if this Cookie should be sent touri
.(At the moment, this does not check that this Cookie's domain matches
uri
, because it assumes that the caller has already done that. But don't rely on that; it may change in the future.)- Parameters:
uri
- aGUri
- Returns:
true
if this Cookie should be sent touri
,false
if not
-
copy
-
domainMatches
Checks if the this Cookie's domain andhost
match.The domains match if this Cookie should be sent when making a request to
host
, or that this Cookie should be accepted when receiving a response fromhost
.- Parameters:
host
- a URI- Returns:
true
if the domains match,false
otherwise
-
equal
Tests if this Cookie andcookie2
are equal.Note that currently, this does not check that the cookie domains match. This may change in the future.
- Parameters:
cookie2
- aSoupCookie
- Returns:
- whether the cookies are equal.
-
free
public void free()Frees this Cookie. -
getDomain
-
getExpires
Gets this Cookie's expiration time.- Returns:
- this Cookie's expiration time, which is owned by this Cookie and should not be modified or freed.
-
getHttpOnly
public boolean getHttpOnly()Gets this Cookie's HttpOnly attribute.- Returns:
- this Cookie's HttpOnly attribute
-
getName
-
getPath
-
getSameSitePolicy
Returns the same-site policy for this cookie.- Returns:
- a
SoupSameSitePolicy
-
getSecure
public boolean getSecure()Gets this Cookie's secure attribute.- Returns:
- this Cookie's secure attribute
-
getValue
-
setDomain
Sets this Cookie's domain todomain
.- Parameters:
domain
- the new domain
-
setExpires
Sets this Cookie's expiration time toexpires
.If
expires
isnull
, this Cookie will be a session cookie and will expire at the end of the client's session.(This sets the same property as
setMaxAge(int)
.)- Parameters:
expires
- the new expiration time, ornull
-
setHttpOnly
public void setHttpOnly(boolean httpOnly) Sets this Cookie's HttpOnly attribute tohttpOnly
.If
true
, this Cookie will be marked as "http only", meaning it should not be exposed to web page scripts or other untrusted code.- Parameters:
httpOnly
- the new value for the HttpOnly attribute
-
setMaxAge
public void setMaxAge(int maxAge) Sets this Cookie's max age tomaxAge
.If
maxAge
is -1, the cookie is a session cookie, and will expire at the end of the client's session. Otherwise, it is the number of seconds until the cookie expires. You can use the constantsSOUP_COOKIE_MAX_AGE_ONE_HOUR
,SOUP_COOKIE_MAX_AGE_ONE_DAY
,SOUP_COOKIE_MAX_AGE_ONE_WEEK
andSOUP_COOKIE_MAX_AGE_ONE_YEAR
(or multiples thereof) to calculate this value. (A value of 0 indicates that the cookie should be considered already-expired.)This sets the same property as
setExpires(org.gnome.glib.DateTime)
.- Parameters:
maxAge
- the new max age
-
setName
-
setPath
-
setSameSitePolicy
When used in conjunction withCookieJar.getCookieListWithSameSiteInfo(org.gnome.glib.Uri, org.gnome.glib.Uri, org.gnome.glib.Uri, boolean, boolean, boolean)
this sets the policy of when this cookie should be exposed.- Parameters:
policy
- aSoupSameSitePolicy
-
setSecure
public void setSecure(boolean secure) Sets this Cookie's secure attribute tosecure
.If
true
, this Cookie will only be transmitted from the client to the server over secure (https) connections.- Parameters:
secure
- the new value for the secure attribute
-
setValue
Sets this Cookie's value tovalue
.- Parameters:
value
- the new value
-
toCookieHeader
-
toSetCookieHeader
-