1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::SameSitePolicy;
use glib::translate::*;

glib::wrapper! {
    /// Implements HTTP cookies, as described by
    /// [RFC 6265](http://tools.ietf.org/html/rfc6265.txt).
    ///
    /// To have a [`Session`][crate::Session] handle cookies for your appliction
    /// automatically, use a [`CookieJar`][crate::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-[`None`] if the cookie uses either the original
    /// "expires" attribute, or the newer "max-age" attribute. If @expires
    /// is [`None`], it indicates that neither "expires" nor "max-age" was
    /// specified, and the cookie expires at the end of the session.
    ///
    /// If @http_only 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.
    #[derive(Debug, PartialOrd, Ord, Hash)]
    pub struct Cookie(Boxed<ffi::SoupCookie>);

    match fn {
        copy => |ptr| ffi::soup_cookie_copy(mut_override(ptr)),
        free => |ptr| ffi::soup_cookie_free(ptr),
        type_ => || ffi::soup_cookie_get_type(),
    }
}

impl Cookie {
    /// Creates a new #SoupCookie with the given attributes.
    ///
    /// Use [`set_secure()`][Self::set_secure()] and [`set_http_only()`][Self::set_http_only()] 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 has @domain as a
    /// suffix). Otherwise, it is a hostname and must match exactly.
    ///
    /// @max_age 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 constants
    /// `SOUP_COOKIE_MAX_AGE_ONE_HOUR`, `SOUP_COOKIE_MAX_AGE_ONE_DAY`,
    /// `SOUP_COOKIE_MAX_AGE_ONE_WEEK` and `SOUP_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, use
    /// [`set_expires()`][Self::set_expires()].)
    ///
    /// As of version 3.4.0 the default value of a cookie's same-site-policy
    /// is [`SameSitePolicy::Lax`][crate::SameSitePolicy::Lax].
    /// ## `name`
    /// cookie name
    /// ## `value`
    /// cookie value
    /// ## `domain`
    /// cookie domain or hostname
    /// ## `path`
    /// cookie path, or [`None`]
    /// ## `max_age`
    /// max age of the cookie, or -1 for a session cookie
    ///
    /// # Returns
    ///
    /// a new #SoupCookie.
    #[doc(alias = "soup_cookie_new")]
    pub fn new(name: &str, value: &str, domain: &str, path: &str, max_age: i32) -> Cookie {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::soup_cookie_new(
                name.to_glib_none().0,
                value.to_glib_none().0,
                domain.to_glib_none().0,
                path.to_glib_none().0,
                max_age,
            ))
        }
    }

    /// Tests if @self should be sent to @uri.
    ///
    /// (At the moment, this does not check that @self'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.)
    /// ## `uri`
    /// a #GUri
    ///
    /// # Returns
    ///
    /// [`true`] if @self should be sent to @uri, [`false`] if not
    #[doc(alias = "soup_cookie_applies_to_uri")]
    pub fn applies_to_uri(&mut self, uri: &glib::Uri) -> bool {
        unsafe {
            from_glib(ffi::soup_cookie_applies_to_uri(
                self.to_glib_none_mut().0,
                uri.to_glib_none().0,
            ))
        }
    }

    /// Checks if the @self's domain and @host match.
    ///
    /// The domains match if @self should be sent when making a request to @host,
    /// or that @self should be accepted when receiving a response from @host.
    /// ## `host`
    /// a URI
    ///
    /// # Returns
    ///
    /// [`true`] if the domains match, [`false`] otherwise
    #[doc(alias = "soup_cookie_domain_matches")]
    pub fn domain_matches(&mut self, host: &str) -> bool {
        unsafe {
            from_glib(ffi::soup_cookie_domain_matches(
                self.to_glib_none_mut().0,
                host.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "soup_cookie_equal")]
    fn equal(&self, cookie2: &Cookie) -> bool {
        unsafe {
            from_glib(ffi::soup_cookie_equal(
                mut_override(self.to_glib_none().0),
                mut_override(cookie2.to_glib_none().0),
            ))
        }
    }

    /// Gets @self's domain.
    ///
    /// # Returns
    ///
    /// @self's domain
    #[doc(alias = "soup_cookie_get_domain")]
    #[doc(alias = "get_domain")]
    pub fn domain(&mut self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::soup_cookie_get_domain(self.to_glib_none_mut().0)) }
    }

    /// Gets @self's expiration time.
    ///
    /// # Returns
    ///
    /// @self's expiration time, which is
    ///   owned by @self and should not be modified or freed.
    #[doc(alias = "soup_cookie_get_expires")]
    #[doc(alias = "get_expires")]
    pub fn expires(&mut self) -> Option<glib::DateTime> {
        unsafe { from_glib_none(ffi::soup_cookie_get_expires(self.to_glib_none_mut().0)) }
    }

    /// Gets @self's HttpOnly attribute.
    ///
    /// # Returns
    ///
    /// @self's HttpOnly attribute
    #[doc(alias = "soup_cookie_get_http_only")]
    #[doc(alias = "get_http_only")]
    pub fn is_http_only(&mut self) -> bool {
        unsafe { from_glib(ffi::soup_cookie_get_http_only(self.to_glib_none_mut().0)) }
    }

    /// Gets @self's name.
    ///
    /// # Returns
    ///
    /// @self's name
    #[doc(alias = "soup_cookie_get_name")]
    #[doc(alias = "get_name")]
    pub fn name(&mut self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::soup_cookie_get_name(self.to_glib_none_mut().0)) }
    }

    /// Gets @self's path.
    ///
    /// # Returns
    ///
    /// @self's path
    #[doc(alias = "soup_cookie_get_path")]
    #[doc(alias = "get_path")]
    pub fn path(&mut self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::soup_cookie_get_path(self.to_glib_none_mut().0)) }
    }

    /// Returns the same-site policy for this cookie.
    ///
    /// # Returns
    ///
    /// a #SoupSameSitePolicy
    #[doc(alias = "soup_cookie_get_same_site_policy")]
    #[doc(alias = "get_same_site_policy")]
    pub fn same_site_policy(&mut self) -> SameSitePolicy {
        unsafe {
            from_glib(ffi::soup_cookie_get_same_site_policy(
                self.to_glib_none_mut().0,
            ))
        }
    }

    /// Gets @self's secure attribute.
    ///
    /// # Returns
    ///
    /// @self's secure attribute
    #[doc(alias = "soup_cookie_get_secure")]
    #[doc(alias = "get_secure")]
    pub fn is_secure(&mut self) -> bool {
        unsafe { from_glib(ffi::soup_cookie_get_secure(self.to_glib_none_mut().0)) }
    }

    /// Gets @self's value.
    ///
    /// # Returns
    ///
    /// @self's value
    #[doc(alias = "soup_cookie_get_value")]
    #[doc(alias = "get_value")]
    pub fn value(&mut self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::soup_cookie_get_value(self.to_glib_none_mut().0)) }
    }

    /// Sets @self's domain to @domain.
    /// ## `domain`
    /// the new domain
    #[doc(alias = "soup_cookie_set_domain")]
    pub fn set_domain(&mut self, domain: &str) {
        unsafe {
            ffi::soup_cookie_set_domain(self.to_glib_none_mut().0, domain.to_glib_none().0);
        }
    }

    /// Sets @self's expiration time to @expires.
    ///
    /// If @expires is [`None`], @self will be a session cookie and will expire at the
    /// end of the client's session.
    ///
    /// (This sets the same property as [`set_max_age()`][Self::set_max_age()].)
    /// ## `expires`
    /// the new expiration time, or [`None`]
    #[doc(alias = "soup_cookie_set_expires")]
    pub fn set_expires(&mut self, expires: &glib::DateTime) {
        unsafe {
            ffi::soup_cookie_set_expires(self.to_glib_none_mut().0, expires.to_glib_none().0);
        }
    }

    /// Sets @self's HttpOnly attribute to @http_only.
    ///
    /// If [`true`], @self will be marked as "http only", meaning it should not be
    /// exposed to web page scripts or other untrusted code.
    /// ## `http_only`
    /// the new value for the HttpOnly attribute
    #[doc(alias = "soup_cookie_set_http_only")]
    pub fn set_http_only(&mut self, http_only: bool) {
        unsafe {
            ffi::soup_cookie_set_http_only(self.to_glib_none_mut().0, http_only.into_glib());
        }
    }

    /// Sets @self's max age to @max_age.
    ///
    /// If @max_age 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 constants `SOUP_COOKIE_MAX_AGE_ONE_HOUR`,
    /// `SOUP_COOKIE_MAX_AGE_ONE_DAY`, `SOUP_COOKIE_MAX_AGE_ONE_WEEK` and
    /// `SOUP_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 [`set_expires()`][Self::set_expires()].
    /// ## `max_age`
    /// the new max age
    #[doc(alias = "soup_cookie_set_max_age")]
    pub fn set_max_age(&mut self, max_age: i32) {
        unsafe {
            ffi::soup_cookie_set_max_age(self.to_glib_none_mut().0, max_age);
        }
    }

    /// Sets @self's name to @name.
    /// ## `name`
    /// the new name
    #[doc(alias = "soup_cookie_set_name")]
    pub fn set_name(&mut self, name: &str) {
        unsafe {
            ffi::soup_cookie_set_name(self.to_glib_none_mut().0, name.to_glib_none().0);
        }
    }

    /// Sets @self's path to @path.
    /// ## `path`
    /// the new path
    #[doc(alias = "soup_cookie_set_path")]
    pub fn set_path(&mut self, path: &str) {
        unsafe {
            ffi::soup_cookie_set_path(self.to_glib_none_mut().0, path.to_glib_none().0);
        }
    }

    /// When used in conjunction with
    /// [`CookieJarExt::cookie_list_with_same_site_info()`][crate::prelude::CookieJarExt::cookie_list_with_same_site_info()] this sets the policy
    /// of when this cookie should be exposed.
    /// ## `policy`
    /// a #SoupSameSitePolicy
    #[doc(alias = "soup_cookie_set_same_site_policy")]
    pub fn set_same_site_policy(&mut self, policy: SameSitePolicy) {
        unsafe {
            ffi::soup_cookie_set_same_site_policy(self.to_glib_none_mut().0, policy.into_glib());
        }
    }

    /// Sets @self's secure attribute to @secure.
    ///
    /// If [`true`], @self will only be transmitted from the client to the server over
    /// secure (https) connections.
    /// ## `secure`
    /// the new value for the secure attribute
    #[doc(alias = "soup_cookie_set_secure")]
    pub fn set_secure(&mut self, secure: bool) {
        unsafe {
            ffi::soup_cookie_set_secure(self.to_glib_none_mut().0, secure.into_glib());
        }
    }

    /// Sets @self's value to @value.
    /// ## `value`
    /// the new value
    #[doc(alias = "soup_cookie_set_value")]
    pub fn set_value(&mut self, value: &str) {
        unsafe {
            ffi::soup_cookie_set_value(self.to_glib_none_mut().0, value.to_glib_none().0);
        }
    }

    /// Serializes @self in the format used by the Cookie header (ie, for
    /// returning a cookie from a [`Session`][crate::Session] to a server).
    ///
    /// # Returns
    ///
    /// the header
    #[doc(alias = "soup_cookie_to_cookie_header")]
    pub fn to_cookie_header(&mut self) -> Option<glib::GString> {
        unsafe { from_glib_full(ffi::soup_cookie_to_cookie_header(self.to_glib_none_mut().0)) }
    }

    /// Serializes @self in the format used by the Set-Cookie header.
    ///
    /// i.e. for sending a cookie from a [`Server`][crate::Server] to a client.
    ///
    /// # Returns
    ///
    /// the header
    #[doc(alias = "soup_cookie_to_set_cookie_header")]
    pub fn to_set_cookie_header(&mut self) -> Option<glib::GString> {
        unsafe {
            from_glib_full(ffi::soup_cookie_to_set_cookie_header(
                self.to_glib_none_mut().0,
            ))
        }
    }

    /// Parses @header and returns a #SoupCookie.
    ///
    /// 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 from @origin. If @origin is [`None`], path will default
    /// to "/", but domain will be left as [`None`]. Note that this is not a
    /// valid state for a #SoupCookie, 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`][crate::SameSitePolicy::Lax].
    /// ## `header`
    /// a cookie string (eg, the value of a Set-Cookie header)
    /// ## `origin`
    /// origin of the cookie
    ///
    /// # Returns
    ///
    /// a new #SoupCookie, or [`None`] if it could
    ///   not be parsed, or contained an illegal "domain" attribute for a
    ///   cookie originating from @origin.
    #[doc(alias = "soup_cookie_parse")]
    pub fn parse(header: &str, origin: Option<&glib::Uri>) -> Option<Cookie> {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::soup_cookie_parse(
                header.to_glib_none().0,
                origin.to_glib_none().0,
            ))
        }
    }
}

impl PartialEq for Cookie {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.equal(other)
    }
}

impl Eq for Cookie {}