reinhardt-http 0.1.0-rc.29

HTTP primitives, request and response handling for Reinhardt
Documentation
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
421
422
423
use super::Request;
use hyper::Uri;
use percent_encoding::percent_decode_str;
use std::collections::HashMap;

impl Request {
	/// Parse query parameters from URI
	pub(super) fn parse_query_params(uri: &Uri) -> HashMap<String, String> {
		uri.query()
			.map(|q| {
				q.split('&')
					.filter_map(|pair| {
						// Split on first '=' only to preserve '=' in values (e.g., Base64)
						let mut parts = pair.splitn(2, '=');
						Some((
							parts.next()?.to_string(),
							parts.next().unwrap_or("").to_string(),
						))
					})
					.collect()
			})
			.unwrap_or_default()
	}

	/// Get the request path
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::Method;
	///
	/// let request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/api/users")
	///     .build()
	///     .unwrap();
	///
	/// assert_eq!(request.path(), "/api/users");
	/// ```
	pub fn path(&self) -> &str {
		self.uri.path()
	}

	/// Get URL-decoded query parameters
	///
	/// Returns a new HashMap with all query parameter keys and values URL-decoded.
	/// This is useful when query parameters contain special characters or Unicode.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::Method;
	///
	/// let request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/test?name=John%20Doe")
	///     .build()
	///     .unwrap();
	///
	/// let decoded = request.decoded_query_params();
	/// assert_eq!(decoded.get("name"), Some(&"John Doe".to_string()));
	/// ```
	pub fn decoded_query_params(&self) -> HashMap<String, String> {
		self.query_params
			.iter()
			.map(|(k, v)| {
				let decoded_key = percent_decode_str(k).decode_utf8_lossy().to_string();
				let decoded_value = percent_decode_str(v).decode_utf8_lossy().to_string();
				(decoded_key, decoded_value)
			})
			.collect()
	}

	/// Set a path parameter (used by routers for path variable extraction)
	///
	/// This method is typically called by routers when extracting path parameters
	/// from URL patterns like `/users/{id}/`.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::{Method, Uri, Version, HeaderMap};
	/// use bytes::Bytes;
	///
	/// let mut request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/users/123")
	///     .build()
	///     .unwrap();
	///
	/// request.set_path_param("id", "123");
	/// assert_eq!(request.path_params.get("id"), Some(&"123".to_string()));
	/// ```
	pub fn set_path_param(&mut self, key: impl Into<String>, value: impl Into<String>) {
		self.path_params.insert(key, value);
	}

	/// Parse Accept-Language header and return ordered list of language codes
	///
	/// Returns languages sorted by quality value (q parameter), highest first.
	/// Example: "en-US,en;q=0.9,ja;q=0.8" -> ["en-US", "en", "ja"]
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::{Method, Uri, Version, HeaderMap};
	/// use bytes::Bytes;
	///
	/// let mut headers = HeaderMap::new();
	/// headers.insert("accept-language", "en-US,en;q=0.9,ja;q=0.8".parse().unwrap());
	///
	/// let request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/")
	///     .headers(headers)
	///     .build()
	///     .unwrap();
	///
	/// let languages = request.get_accepted_languages();
	/// assert_eq!(languages[0].0, "en-US");
	/// assert_eq!(languages[0].1, 1.0);
	/// assert_eq!(languages[1].0, "en");
	/// assert_eq!(languages[1].1, 0.9);
	/// ```
	pub fn get_accepted_languages(&self) -> Vec<(String, f32)> {
		use hyper::header::ACCEPT_LANGUAGE;
		self.headers
			.get(ACCEPT_LANGUAGE)
			.and_then(|h| h.to_str().ok())
			.map(Self::parse_accept_language)
			.unwrap_or_default()
	}

	/// Get the most preferred language from Accept-Language header
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::{Method, Uri, Version, HeaderMap};
	/// use bytes::Bytes;
	///
	/// let mut headers = HeaderMap::new();
	/// headers.insert("accept-language", "ja;q=0.8,en-US,en;q=0.9".parse().unwrap());
	///
	/// let request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/")
	///     .headers(headers)
	///     .build()
	///     .unwrap();
	///
	/// assert_eq!(request.get_preferred_language(), Some("en-US".to_string()));
	/// ```
	pub fn get_preferred_language(&self) -> Option<String> {
		self.get_accepted_languages()
			.into_iter()
			.max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
			.map(|(lang, _)| lang)
	}

	/// Parse Accept-Language header value
	///
	/// Handles both weighted (q=) and unweighted language preferences.
	/// Example: "en-US,en;q=0.9,ja;q=0.8" -> [("en-US", 1.0), ("en", 0.9), ("ja", 0.8)]
	fn parse_accept_language(header: &str) -> Vec<(String, f32)> {
		let mut languages: Vec<(String, f32)> = header
			.split(',')
			.filter_map(|lang_part| {
				let lang_part = lang_part.trim();
				if lang_part.is_empty() {
					return None;
				}

				// Split on ';' to separate language from quality
				let parts: Vec<&str> = lang_part.split(';').collect();
				let language = parts[0].trim().to_string();

				// Parse quality value if present
				let quality = if parts.len() > 1 {
					parts[1]
						.trim()
						.strip_prefix("q=")
						.and_then(|q| q.parse::<f32>().ok())
						.unwrap_or(1.0)
				} else {
					1.0
				};

				// Validate language code
				if Self::is_valid_language_code(&language) {
					Some((language, quality))
				} else {
					None
				}
			})
			.collect();

		// Sort by quality (descending)
		languages.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
		languages
	}

	/// Validate language code format
	///
	/// Accepts formats like:
	/// - "en"
	/// - "en-US"
	/// - "zh-Hans"
	/// - "sr-Latn-RS"
	/// - "nl-nl-x-informal" (with private use subtag)
	///
	/// Rejects:
	/// - Too long (>255 chars)
	/// - Invalid characters
	/// - Starting/ending with hyphen
	fn is_valid_language_code(code: &str) -> bool {
		if code.is_empty() || code.len() > 255 {
			return false;
		}

		// Must not start or end with hyphen
		if code.starts_with('-') || code.ends_with('-') {
			return false;
		}

		// Check for valid characters (alphanumeric and hyphen)
		code.chars().all(|c| c.is_alphanumeric() || c == '-')
	}

	/// Get language from cookie
	///
	/// Looks for a language cookie (typically named "reinhardt_language" or similar)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_http::Request;
	/// use hyper::{Method, Uri, Version, HeaderMap};
	/// use bytes::Bytes;
	///
	/// let mut headers = HeaderMap::new();
	/// headers.insert("cookie", "session_id=abc123; language=ja; theme=dark".parse().unwrap());
	///
	/// let request = Request::builder()
	///     .method(Method::GET)
	///     .uri("/")
	///     .headers(headers)
	///     .build()
	///     .unwrap();
	///
	/// assert_eq!(request.get_language_from_cookie("language"), Some("ja".to_string()));
	/// assert_eq!(request.get_language_from_cookie("nonexistent"), None);
	/// ```
	pub fn get_language_from_cookie(&self, cookie_name: &str) -> Option<String> {
		use hyper::header::COOKIE;

		self.headers
			.get(COOKIE)
			.and_then(|h| h.to_str().ok())
			.and_then(Self::parse_cookies)
			.and_then(|parsed| {
				parsed.into_iter().find_map(|(name, value)| {
					if name == cookie_name {
						Some(value)
					} else {
						None
					}
				})
			})
			.filter(|lang| Self::is_valid_language_code(lang))
	}

	/// Parse cookie header with strict validation.
	///
	/// Rejects malformed cookies:
	/// - Missing `=` separator
	/// - Cookie name containing separators (`;`, `=`, whitespace, control chars)
	/// - Empty cookie name
	fn parse_cookies(header: &str) -> Option<Vec<(String, String)>> {
		let mut cookies = Vec::new();
		for cookie in header.split(';') {
			let cookie = cookie.trim();
			if cookie.is_empty() {
				continue;
			}
			// Cookie must contain '=' separator
			let mut parts = cookie.splitn(2, '=');
			let name = parts.next()?.trim();
			let value = match parts.next() {
				Some(v) => v.trim(),
				// Missing '=' means malformed cookie - skip it
				None => continue,
			};
			// Validate cookie name: must not be empty or contain separators/control chars
			if name.is_empty() || !Self::is_valid_cookie_name(name) {
				continue;
			}
			cookies.push((name.to_string(), value.to_string()));
		}
		Some(cookies)
	}

	/// Validate cookie name per RFC 6265.
	///
	/// Cookie name must not contain separators, whitespace, or control characters.
	fn is_valid_cookie_name(name: &str) -> bool {
		name.chars().all(|c| {
			// Must be a visible ASCII character (0x21-0x7E) excluding separators
			let code = c as u32;
			(0x21..=0x7E).contains(&code)
				&& !matches!(
					c,
					'(' | ')'
						| '<' | '>' | '@' | ','
						| ';' | ':' | '\\' | '"'
						| '/' | '[' | ']' | '?'
						| '=' | '{' | '}' | ' '
						| '\t'
				)
		})
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::rstest;

	// =================================================================
	// Query parameter '=' preservation tests (Issue #362)
	// =================================================================

	#[rstest]
	fn test_parse_query_params_preserves_equals_in_value() {
		// Arrange
		let uri: hyper::Uri = "/test?token=abc==".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("token"), Some(&"abc==".to_string()));
	}

	#[rstest]
	fn test_parse_query_params_base64_encoded_value() {
		// Arrange
		let uri: hyper::Uri = "/test?data=dGVzdA==".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("data"), Some(&"dGVzdA==".to_string()));
	}

	#[rstest]
	fn test_parse_query_params_multiple_equals_in_value() {
		// Arrange
		let uri: hyper::Uri = "/test?formula=a=b=c".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("formula"), Some(&"a=b=c".to_string()));
	}

	#[rstest]
	fn test_parse_query_params_simple_key_value() {
		// Arrange
		let uri: hyper::Uri = "/test?key=value".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("key"), Some(&"value".to_string()));
	}

	#[rstest]
	fn test_parse_query_params_key_without_value() {
		// Arrange
		let uri: hyper::Uri = "/test?key=".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("key"), Some(&"".to_string()));
	}

	#[rstest]
	fn test_parse_query_params_no_query_string() {
		// Arrange
		let uri: hyper::Uri = "/test".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert!(params.is_empty());
	}

	#[rstest]
	fn test_parse_query_params_multiple_params_with_equals() {
		// Arrange
		let uri: hyper::Uri = "/test?a=1&b=x=y=z&c=3".parse().unwrap();

		// Act
		let params = Request::parse_query_params(&uri);

		// Assert
		assert_eq!(params.get("a"), Some(&"1".to_string()));
		assert_eq!(params.get("b"), Some(&"x=y=z".to_string()));
		assert_eq!(params.get("c"), Some(&"3".to_string()));
	}
}