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
//! Headers class implementation

use std::str::FromStr;

use js::{
	class::Trace,
	prelude::{Coerced, List},
	Array, Ctx, Exception, Result, Value,
};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

#[derive(Clone, Trace)]
#[js::class]
pub struct Headers {
	#[qjs(skip_trace)]
	pub(crate) inner: HeaderMap,
}

#[js::methods]
impl Headers {
	// ------------------------------
	// Constructor
	// ------------------------------

	#[qjs(constructor)]
	pub fn new<'js>(ctx: Ctx<'js>, init: Value<'js>) -> Result<Self> {
		Headers::new_inner(&ctx, init)
	}

	// ------------------------------
	// Instance methods
	// ------------------------------

	// Convert the object to a string
	#[qjs(rename = "toString")]
	pub fn js_to_string(&self) -> String {
		String::from("[object Header]")
	}

	// Adds or appends a new value to a header
	pub fn append(&mut self, ctx: Ctx<'_>, key: String, val: String) -> Result<()> {
		self.append_inner(&ctx, &key, &val)
	}

	// Deletes a header from the header set
	pub fn delete(&mut self, ctx: Ctx<'_>, key: String) -> Result<()> {
		// Process and check the header name is valid
		let key =
			HeaderName::from_str(&key).map_err(|e| Exception::throw_type(&ctx, &format!("{e}")))?;
		// Remove the header entry from the map
		self.inner.remove(&key);
		// Everything ok
		Ok(())
	}

	// Returns all header entries in the header set
	pub fn entries(&self) -> Vec<List<(String, String)>> {
		let mut res = Vec::<List<(String, String)>>::with_capacity(self.inner.len());

		for (k, v) in self.inner.iter() {
			let k = k.as_str();
			if Some(k) == res.last().map(|x| x.0 .0.as_str()) {
				let ent = res.last_mut().unwrap();
				ent.0 .1.push_str(", ");
				// Header value came from a string, so it should also be able to be cast back
				// to a string
				ent.0 .1.push_str(v.to_str().unwrap());
			} else {
				res.push(List((k.to_owned(), v.to_str().unwrap().to_owned())));
			}
		}

		res
	}

	// Returns all values of a header in the header set
	pub fn get(&self, ctx: Ctx<'_>, key: String) -> Result<Option<String>> {
		// Process and check the header name is valid
		let key =
			HeaderName::from_str(&key).map_err(|e| Exception::throw_type(&ctx, &format!("{e}")))?;
		// Convert the header values to strings
		let all = self.inner.get_all(&key);

		// Header value came from a string, so it should also be able to be cast back
		// to a string
		let mut res = String::new();
		for (idx, v) in all.iter().enumerate() {
			if idx != 0 {
				res.push_str(", ");
			}
			res.push_str(v.to_str().unwrap());
		}

		if res.is_empty() {
			return Ok(None);
		}
		Ok(Some(res))
	}

	// Returns all values for the `Set-Cookie` header.
	#[qjs(rename = "getSetCookie")]
	pub fn get_set_cookie(&self) -> Vec<String> {
		// This should always be a correct cookie;
		let key = HeaderName::from_str("set-cookie").unwrap();
		self.inner.get_all(key).iter().map(|x| x.to_str().unwrap().to_owned()).collect()
	}

	// Checks to see if the header set contains a header
	pub fn has(&self, ctx: Ctx<'_>, key: String) -> Result<bool> {
		// Process and check the header name is valid
		let key =
			HeaderName::from_str(&key).map_err(|e| Exception::throw_type(&ctx, &format!("{e}")))?;
		// Check if the header entry exists
		Ok(self.inner.contains_key(&key))
	}

	// Returns all header keys contained in the header set
	pub fn keys(&self) -> Vec<String> {
		// TODO: Incorrect, should return an iterator but iterators are not supported yet by quickjs
		self.inner.keys().map(|v| v.as_str().to_owned()).collect::<Vec<String>>()
	}

	// Sets a new value or adds a header to the header set
	pub fn set(&mut self, ctx: Ctx<'_>, key: String, val: String) -> Result<()> {
		// Process and check the header name is valid
		let key = HeaderName::from_str(&key)
			.map_err(|e| Exception::throw_type(&ctx, &format!("Invalid header name: {e}")))?;
		// Process and check the header name is valid
		let val = HeaderValue::from_str(&val)
			.map_err(|e| Exception::throw_type(&ctx, &format!("Invalid header value: {e}")))?;
		// Insert and overwrite the header entry
		self.inner.insert(key, val);
		// Everything ok
		Ok(())
	}

	// Returns all header values contained in the header set
	pub fn values(&self) -> Vec<String> {
		let mut res = Vec::<String>::with_capacity(self.inner.len());

		let mut pref = None;
		for (k, v) in self.inner.iter() {
			if Some(k) == pref {
				let ent = res.last_mut().unwrap();
				ent.push_str(", ");
				ent.push_str(v.to_str().unwrap())
			} else {
				pref = Some(k);
				res.push(v.to_str().unwrap().to_owned());
			}
		}

		res
	}
}

impl Headers {
	pub fn from_map(map: HeaderMap) -> Self {
		Self {
			inner: map,
		}
	}

	pub fn new_empty() -> Self {
		Self::from_map(HeaderMap::new())
	}

	pub fn new_inner<'js>(ctx: &Ctx<'js>, val: Value<'js>) -> Result<Self> {
		static INVALID_ERROR: &str = "Headers constructor: init was neither sequence<sequence<ByteString>> or record<ByteString, ByteString>";
		let mut res = Self::new_empty();

		// TODO Set and Map,
		if let Some(array) = val.as_array() {
			// a sequence<sequence<String>>;
			for v in array.iter::<Array>() {
				let v = match v {
					Ok(x) => x,
					Err(e) => {
						if e.is_from_js() {
							return Err(Exception::throw_type(ctx, INVALID_ERROR));
						}
						return Err(e);
					}
				};
				let key = match v.get::<Coerced<String>>(0) {
					Ok(x) => x,
					Err(e) => {
						if e.is_from_js() {
							return Err(Exception::throw_type(ctx, INVALID_ERROR));
						}
						return Err(e);
					}
				};
				let value = match v.get::<Coerced<String>>(1) {
					Ok(x) => x,
					Err(e) => {
						if e.is_from_js() {
							return Err(Exception::throw_type(ctx, INVALID_ERROR));
						}
						return Err(e);
					}
				};
				res.append_inner(ctx, &key, &value)?;
			}
		} else if let Some(obj) = val.as_object() {
			// a record<String,String>;
			for prop in obj.props::<String, Coerced<String>>() {
				let (key, value) = match prop {
					Ok(x) => x,
					Err(e) => {
						if e.is_from_js() {
							return Err(Exception::throw_type(ctx, INVALID_ERROR));
						}
						return Err(e);
					}
				};
				res.append_inner(ctx, &key, &value.0)?;
			}
		} else {
			return Err(Exception::throw_type(ctx, INVALID_ERROR));
		}

		Ok(res)
	}

	fn append_inner(&mut self, ctx: &Ctx<'_>, key: &str, val: &str) -> Result<()> {
		// Unsure what to do exactly here.
		// Spec dictates normalizing string before adding it as a header value, i.e. removing
		// any leading and trailing whitespace:
		// [`https://fetch.spec.whatwg.org/#concept-header-value-normalize`]
		// But non of the platforms I tested, normalize, instead they throw an error
		// with `Invalid header value`. I'll chose to just do what the platforms do.

		let key = match HeaderName::from_bytes(key.as_bytes()) {
			Ok(x) => x,
			Err(e) => {
				return Err(Exception::throw_type(
					ctx,
					&format!("invalid header name `{key}`: {e}"),
				))
			}
		};
		let val = match HeaderValue::from_bytes(val.as_bytes()) {
			Ok(x) => x,
			Err(e) => {
				return Err(Exception::throw_type(
					ctx,
					&format!("invalid header value `{val}`: {e}"),
				))
			}
		};

		self.inner.append(key, val);

		Ok(())
	}
}

#[cfg(test)]
mod test {
	use crate::fnc::script::fetch::test::create_test_context;
	use js::CatchResultExt;

	#[tokio::test]
	async fn basic_headers_use() {
		create_test_context!(ctx => {
			ctx.eval::<(),_>(r#"
				let headers = new Headers([
					["a","b"],
					["a","c"],
					["d","e"],
				]);
				assert(headers.has("a"));
				assert(headers.has("d"));
				assert(headers.has("d"));

				let keys = [];
				for(const key of headers.keys()){
					keys.push(key);
				}
				assert.seq(keys[0], "a");
				assert.seq(keys[1], "d");
				assert.seq(headers.get("a"), "b, c");

				let values = [];
				for(const v of headers.values()){
					values.push(v);
				}
				assert.seq(values[0], "b, c");
				assert.seq(values[1], "e");

				headers.set("a","f");
				assert.seq(headers.get("a"), "f");
				assert.seq(headers.get("A"), "f");
				headers.append("a","g");
				assert.seq(headers.get("a"), "f, g");
				headers.delete("a");
				assert(!headers.has("a"));

				headers.set("Set-Cookie","somecookie");
				let cookies = headers.getSetCookie();
				assert.seq(cookies.length,1);
				assert.seq(cookies[0],"somecookie");
				headers.append("sEt-cOoKiE","memecookie");
				cookies = headers.getSetCookie();
				assert.seq(cookies.length,2);
				assert.seq(cookies[0],"somecookie");
				assert.seq(cookies[1],"memecookie");

				headers = new Headers({
					"f": "g",
					"h": "j",
				});
				assert.seq(headers.get("f"), "g");
				assert.seq(headers.get("h"), "j");
			"#).catch(&ctx).unwrap();
		})
		.await
	}
}