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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanappliesToUri(Uri uri) Tests if this Cookie should be sent touri.copy()Copiescookie.booleandomainMatches(String host) Checks if thecookie'sdomain andhostmatch.booleanTests if this Cookie andcookie2are equal.voidfree()Freescookie.Getscookie'sdomain.@Nullable DateTimeGetscookie'sexpiration time.booleanGetscookie'sHttpOnly attribute.getName()Getscookie'sname.getPath()Getscookie'spath.Returns the same-site policy for this cookie.booleanGetscookie'ssecure attribute.static @Nullable TypegetType()Get the GType of the Cookie classgetValue()Getscookie'svalue.static @Nullable CookieParsesheaderand returns aSoupCookie.voidSetscookie'sdomain todomain.voidsetExpires(DateTime expires) Setscookie'sexpiration time toexpires.voidsetHttpOnly(boolean httpOnly) Setscookie'sHttpOnly attribute tohttpOnly.voidsetMaxAge(int maxAge) Setscookie'smax age tomaxAge.voidSetscookie'sname toname.voidSetscookie'spath topath.voidsetSameSitePolicy(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.voidsetSecure(boolean secure) Setscookie'ssecure attribute tosecure.voidSetscookie'svalue tovalue.Serializes this Cookie in the format used by the Cookie header (ie, for returning a cookie from aSessionto a server).Serializes this Cookie in the format used by the Set-Cookie header.Methods inherited from class org.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
Creates a newSoupCookiewith the given attributes.Use
setSecure(boolean)andsetHttpOnly(boolean)if you need to set those attributes on the returned cookie.If
domainstarts with ".", that indicates a domain (which matches the string after the ".", or any hostname that hasdomainas a suffix). Otherwise, it is a hostname and must match exactly.maxAgeis 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_WEEKandSOUP_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, ornullmaxAge- max age of the cookie, or -1 for a session cookie
-
-
Method Details
-
getType
-
parse
Parsesheaderand returns aSoupCookie.If
headercontains multiple cookies, only the first one will be parsed.If
headerdoes not have "path" or "domain" attributes, they will be defaulted fromorigin.Iforiginisnull, 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, ornullif it could not be parsed, or contained an illegal "domain" attribute for a cookie originating fromorigin.
-
appliesToUri
Tests if this Cookie should be sent touri.(At the moment, this does not check that
cookie'sdomain matchesuri,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:
trueif this Cookie should be sent touri,falseif not
-
copy
-
domainMatches
Checks if thecookie'sdomain andhostmatch.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:
trueif the domains match,falseotherwise
-
equal
Tests if this Cookie andcookie2are 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()Freescookie. -
getDomain
-
getExpires
Getscookie'sexpiration time.- Returns:
cookie'sexpiration time, which is owned by this Cookie and should not be modified or freed.
-
getHttpOnly
public boolean getHttpOnly()Getscookie'sHttpOnly attribute.- Returns:
cookie'sHttpOnly attribute
-
getName
-
getPath
-
getSameSitePolicy
Returns the same-site policy for this cookie.- Returns:
- a
SoupSameSitePolicy
-
getSecure
public boolean getSecure()Getscookie'ssecure attribute.- Returns:
cookie'ssecure attribute
-
getValue
-
setDomain
Setscookie'sdomain todomain.- Parameters:
domain- the new domain
-
setExpires
Setscookie'sexpiration time toexpires.If
expiresisnull, 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) Setscookie'sHttpOnly 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) Setscookie'smax age tomaxAge.If
maxAgeis -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_WEEKandSOUP_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) Setscookie'ssecure 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
-
toCookieHeader
-
toSetCookieHeader
-