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
//! Module for dealing with cookies.

use std::{borrow::Cow, marker::PhantomData, ops::*, time::SystemTime};

use futures_channel::oneshot;

use crate::core::cookie::*;

pub struct Cookie(CookieImpl);

pub struct CookieJar(CookieJarImpl);

pub struct CookieIterator<'a> {
	inner: CookieIteratorImpl,
	_phantom: PhantomData<&'a u8>,
}

impl Cookie {
	pub fn new(name: &str, value: &str) -> Self { Self(CookieImpl::new(name, value)) }

	pub fn creation_time(&self) -> SystemTime { self.0.creation_time() }

	pub fn expires(&self) -> Option<SystemTime> { self.0.expires() }

	pub fn domain(&self) -> Cow<'_, str> { self.0.domain() }

	pub fn is_http_only(&self) -> bool { self.0.is_http_only() }

	pub fn name(&self) -> Cow<'_, str> { self.0.name() }

	pub fn path(&self) -> Cow<'_, str> { self.0.path() }

	pub fn is_secure(&self) -> bool { self.0.is_secure() }

	pub fn value(&self) -> Cow<'_, str> { self.0.value() }

	pub fn make_http_only(&mut self) -> &mut Self {
		self.0.make_http_only();
		self
	}

	pub fn make_secure(&mut self) -> &mut Self {
		self.0.make_secure();
		self
	}

	pub fn set_creation_time(&mut self, time: &SystemTime) -> &mut Self {
		self.0.set_creation_time(time);
		self
	}

	pub fn set_expires(&mut self, time: &SystemTime) -> &mut Self {
		self.0.set_expires(time);
		self
	}

	pub fn set_domain(&mut self, domain: &str) -> &mut Self {
		self.0.set_domain(domain);
		self
	}

	pub fn set_name(&mut self, name: &str) -> &mut Self {
		self.0.set_name(name);
		self
	}

	pub fn set_path(&mut self, path: &str) -> &mut Self {
		self.0.set_path(path);
		self
	}

	pub fn set_value(&mut self, value: &str) -> &mut Self {
		self.0.set_value(value);
		self
	}
}

impl Drop for Cookie {
	fn drop(&mut self) { self.0.free(); }
}

impl<'a> CookieIterator<'a> {
	pub async fn next(&mut self) -> Option<Cookie> {
		let (tx, rx) = oneshot::channel::<Option<Cookie>>();

		let more = self._next(|result| {
			if let Err(_) = tx.send(result) {
				panic!("unable to send cookie iterator next result back");
			}
		});

		if !more {
			return None;
		}

		rx.await.unwrap()
	}

	fn _next<H>(&mut self, on_next: H) -> bool
	where
		H: FnOnce(Option<Cookie>),
	{
		let data = Box::into_raw(Box::new(on_next));

		let called_closure = self
			.inner
			.next(cookie_iterator_next_handler::<H>, data as _);

		if !called_closure {
			unsafe {
				let _ = Box::from_raw(data);
			}
		}

		called_closure
	}
}

impl<'a> Drop for CookieIterator<'a> {
	fn drop(&mut self) { self.inner.free(); }
}

impl CookieJar {
	/// Deletes all cookies.
	/// If `url` is not an empty string, only the cookies of the given url will
	/// be deleted.
	pub async fn clear(&mut self, url: &str) -> usize { self.delete(url, "").await }

	/// Like `clear`, but with `url` set empty.
	pub async fn clear_all(&mut self) -> usize { self.clear("").await }

	fn _delete<H>(&mut self, url: &str, name: &str, on_complete: H)
	where
		H: FnOnce(usize),
	{
		let data = Box::into_raw(Box::new(on_complete));

		self.0
			.delete(url, name, cookie_delete_callback::<H>, data as _);
	}

	/// Deletes all cookies with the given `name`.
	/// If `url` is not empty, only the cookie with the given `name` at that
	/// `url` will be deleted. If `name` is empty, all cookies at the given
	/// `url` will be deleted. If both `url` and `name` are empty, all cookies
	/// will be deleted.
	pub async fn delete(&mut self, url: &str, name: &str) -> usize {
		let (tx, rx) = oneshot::channel::<usize>();

		self._delete(url, name, |result| {
			tx.send(result)
				.expect("unable to send back cookie delete count");
		});

		rx.await.unwrap()
	}

	/// Like `delete`, but with `url` set empty.
	pub async fn delete_all(&mut self, name: &str) -> usize { self.delete("", name).await }

	/// Finds the first cookie that has the given `name` in the given `url`.
	/// If `include_http_only` is set to `false`, a `HttpOnly` cookie will not
	/// be found.
	pub async fn find(&self, url: &str, name: &str, include_http_only: bool) -> Option<Cookie> {
		let mut iter = self.iter(url, include_http_only);

		while let Some(cookie) = iter.next().await {
			if cookie.name() == name {
				return Some(cookie);
			}
		}

		None
	}

	/// Finds the first cookie that has the given `name`.
	pub async fn find_from_all(&self, name: &str) -> Option<Cookie> {
		let mut iter = self.iter_all();

		while let Some(cookie) = iter.next().await {
			if cookie.name() == name {
				return Some(cookie);
			}
		}

		None
	}

	pub(crate) fn global() -> Option<Self> { CookieJarImpl::global().map(|i| Self(i)) }

	/// Returns a `CookieIterator` that iterates over cookies asynchronously.
	/// The `CookieIterator` has an async `next` function that you can use.
	///
	/// # Example
	/// ```ignore
	/// if let Some(cookie_jar) = app.cookie_jar() {
	/// 	let mut iterator = cookie_jar.iter("http://localhost/", true);
	///
	/// 	while let Some(cookie) = iterator.next().await {
	/// 		// ... do something with `cookie`
	/// 	}
	/// }
	/// ```
	pub fn iter<'a>(&'a self, url: &str, include_http_only: bool) -> CookieIterator<'a> {
		let inner = self.0.iterator(url, include_http_only);

		CookieIterator {
			inner,
			_phantom: PhantomData,
		}
	}

	/// Returns a `CookieIterator` that iterators over cookies asynchronously.
	/// Like `iter`, but iterates over all cookies from any url.
	pub fn iter_all<'a>(&'a self) -> CookieIterator<'a> {
		let inner = self.0.iterator_all();

		CookieIterator {
			inner,
			_phantom: PhantomData,
		}
	}

	fn _store<'a, H>(&mut self, url: &str, cookie: &Cookie, on_complete: H)
	where
		H: FnOnce(Result<(), CookieStorageError>) + 'a,
	{
		let data = Box::into_raw(Box::new(on_complete));

		self.0.store(
			url.into(),
			&cookie.0,
			Some(cookie_store_callback::<'a, H>),
			data as _,
		);
	}

	/// Stores the given `cookie` for the given `url`.
	pub async fn store(&mut self, url: &str, cookie: &Cookie) -> Result<(), CookieStorageError> {
		let (tx, rx) = oneshot::channel::<Result<(), CookieStorageError>>();

		self._store(url, cookie, |result| {
			tx.send(result)
				.expect("unable to retrieve cookie storage error");
		});

		rx.await.unwrap()
	}
}

impl Drop for CookieJar {
	fn drop(&mut self) { self.0.free(); }
}

unsafe fn cookie_delete_callback<'a, H>(_handle: CookieJarImpl, cb_data: *mut (), deleted: usize)
where
	H: FnOnce(usize) + 'a,
{
	let data_ptr = cb_data as *mut H;
	let data: Box<H> = Box::from_raw(data_ptr);

	(*data)(deleted);
}

unsafe fn cookie_store_callback<'a, H>(
	_handle: CookieJarImpl, cb_data: *mut (), result: Result<(), CookieStorageError>,
) where
	H: FnOnce(Result<(), CookieStorageError>) + 'a,
{
	let data_ptr = cb_data as *mut H;
	let data: Box<H> = Box::from_raw(data_ptr);

	(*data)(result);
}

unsafe fn cookie_iterator_next_handler<H>(
	_handle: CookieIteratorImpl, cb_data: *mut (), cookie: Option<CookieImpl>,
) where
	H: FnOnce(Option<Cookie>),
{
	let data_ptr = cb_data as *mut H;
	let data: Box<H> = Box::from_raw(data_ptr);

	(*data)(cookie.map(|c| Cookie(c)));
}