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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! # Utility Functions and Helpers
//!
//! This module provides a collection of utility functions and helpers that support
//! various operations throughout the Ignitia web framework. These utilities handle
//! common tasks such as URL parsing, query string manipulation, content type parsing,
//! and other HTTP-related operations.
//!
//! ## Features
//!
//! - **Query String Parsing**: Efficient parsing of URL query parameters
//! - **URL Encoding/Decoding**: Safe URL encoding and decoding operations
//! - **Content Type Parsing**: MIME type and parameter extraction from headers
//! - **Path Normalization**: URL path cleaning and normalization
//! - **Header Utilities**: Common HTTP header parsing and manipulation
//! - **Validation Helpers**: Input validation and sanitization functions
//!
//! ## Usage Examples
//!
//! ### Query String Parsing
//! ```
//! use ignitia::utils::parse_query_string;
//!
//! let query = "name=John&age=30&city=New%20York";
//! let params = parse_query_string(query);
//!
//! assert_eq!(params.get("name"), Some(&"John".to_string()));
//! assert_eq!(params.get("age"), Some(&"30".to_string()));
//! assert_eq!(params.get("city"), Some(&"New York".to_string()));
//! ```
//!
//! ### Content Type Parsing
//! ```
//! use ignitia::utils::parse_content_type;
//!
//! let content_type = "application/json; charset=utf-8; boundary=something";
//! let (media_type, params) = parse_content_type(content_type);
//!
//! assert_eq!(media_type, "application/json");
//! assert_eq!(params.get("charset"), Some(&"utf-8".to_string()));
//! assert_eq!(params.get("boundary"), Some(&"something".to_string()));
//! ```
//!
//! ### URL Encoding
//! ```
//! use ignitia::utils::{url_encode, url_decode};
//!
//! let original = "Hello World & Special Characters!";
//! let encoded = url_encode(original);
//! let decoded = url_decode(&encoded);
//!
//! assert_eq!(decoded, original);
//! ```
use HashMap;
use form_urlencoded;
/// Parses a query string into a HashMap of key-value pairs.
///
/// This function efficiently parses URL query parameters, handling URL decoding
/// and multiple values for the same parameter. It's designed to work with
/// standard web query string formats.
///
/// # Parameters
/// - `query`: The query string to parse (without the leading '?')
///
/// # Returns
/// A `HashMap<String, String>` containing the parsed key-value pairs
///
/// # URL Decoding
/// All keys and values are automatically URL-decoded using percent-encoding rules.
/// Special characters like spaces (%20), ampersands (%26), and equals signs (%3D)
/// are properly decoded.
///
/// # Duplicate Keys
/// If the same key appears multiple times in the query string, only the last
/// value will be retained. For applications that need to handle multiple values
/// for the same key, use `parse_query_string_multi` instead.
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// use ignitia::utils::parse_query_string;
///
/// let query = "name=John&age=30&city=Seattle";
/// let params = parse_query_string(query);
///
/// assert_eq!(params.get("name"), Some(&"John".to_string()));
/// assert_eq!(params.get("age"), Some(&"30".to_string()));
/// assert_eq!(params.get("city"), Some(&"Seattle".to_string()));
/// assert_eq!(params.len(), 3);
/// ```
///
/// ## URL Decoding
/// ```
/// use ignitia::utils::parse_query_string;
///
/// let query = "name=John%20Doe&message=Hello%20World%21&special=%26%3D%25";
/// let params = parse_query_string(query);
///
/// assert_eq!(params.get("name"), Some(&"John Doe".to_string()));
/// assert_eq!(params.get("message"), Some(&"Hello World!".to_string()));
/// assert_eq!(params.get("special"), Some(&"&=%".to_string()));
/// ```
///
/// ## Empty and Missing Values
/// ```
/// use ignitia::utils::parse_query_string;
///
/// let query = "empty=&missing&present=value";
/// let params = parse_query_string(query);
///
/// assert_eq!(params.get("empty"), Some(&"".to_string()));
/// assert_eq!(params.get("missing"), Some(&"".to_string()));
/// assert_eq!(params.get("present"), Some(&"value".to_string()));
/// ```
///
/// ## Complex Query Strings
/// ```
/// use ignitia::utils::parse_query_string;
///
/// let query = "search=rust%20web%20framework&category=programming&tags=rust&tags=web&sort=relevance";
/// let params = parse_query_string(query);
///
/// assert_eq!(params.get("search"), Some(&"rust web framework".to_string()));
/// assert_eq!(params.get("category"), Some(&"programming".to_string()));
/// // Note: Only the last 'tags' value is kept
/// assert_eq!(params.get("tags"), Some(&"web".to_string()));
/// assert_eq!(params.get("sort"), Some(&"relevance".to_string()));
/// ```
///
/// # Performance Notes
/// This function is optimized for typical web usage patterns and should perform
/// well with query strings containing dozens of parameters. For very large query
/// strings (hundreds of parameters), consider streaming parsing approaches.
/// Parses a query string into a HashMap with support for multiple values per key.
///
/// Unlike `parse_query_string`, this function preserves all values when a key
/// appears multiple times in the query string, storing them as a comma-separated
/// string.
///
/// # Parameters
/// - `query`: The query string to parse (without the leading '?')
///
/// # Returns
/// A `HashMap<String, String>` where multiple values are joined with commas
///
/// # Examples
///
/// ```
/// use ignitia::utils::parse_query_string_multi;
///
/// let query = "tags=rust&tags=web&tags=framework&category=programming";
/// let params = parse_query_string_multi(query);
///
/// assert_eq!(params.get("tags"), Some(&"rust,web,framework".to_string()));
/// assert_eq!(params.get("category"), Some(&"programming".to_string()));
/// ```
/// URL-encodes a string using percent-encoding.
///
/// This function encodes special characters in a string to make it safe for use
/// in URLs. It follows the percent-encoding scheme defined in RFC 3986.
///
/// # Parameters
/// - `input`: The string to encode
///
/// # Returns
/// A URL-encoded string where special characters are replaced with %XX sequences
///
/// # Examples
///
/// ```
/// use ignitia::utils::url_encode;
///
/// assert_eq!(url_encode("Hello World"), "Hello%20World");
/// assert_eq!(url_encode("user@example.com"), "user%40example.com");
/// assert_eq!(url_encode("price: $10.99"), "price%3A%20%2410.99");
/// ```
/// URL-decodes a percent-encoded string.
///
/// This function decodes a URL-encoded string, converting %XX sequences back to
/// their original characters. This is the inverse operation of `url_encode`.
///
/// # Parameters
/// - `input`: The URL-encoded string to decode
///
/// # Returns
/// The decoded string with percent-encoded sequences converted back to original characters
///
/// # Error Handling
/// Invalid percent-encoding sequences are passed through unchanged rather than
/// causing an error, making this function robust for real-world usage.
///
/// # Examples
///
/// ```
/// use ignitia::utils::url_decode;
///
/// assert_eq!(url_decode("Hello%20World"), "Hello World");
/// assert_eq!(url_decode("user%40example.com"), "user@example.com");
/// assert_eq!(url_decode("price%3A%20%2410.99"), "price: $10.99");
/// ```
///
/// ## Handling Invalid Encoding
/// ```
/// use ignitia::utils::url_decode;
///
/// // Invalid sequences are preserved
/// assert_eq!(url_decode("Hello%World"), "Hello%World");
/// assert_eq!(url_decode("test%2"), "test%2");
/// ```
/// Parses a Content-Type header value into media type and parameters.
///
/// This function parses HTTP Content-Type headers, extracting the main media type
/// and any additional parameters (like charset, boundary, etc.). It handles the
/// full Content-Type syntax as defined in RFC 2045 and RFC 7231.
///
/// # Parameters
/// - `content_type`: The Content-Type header value to parse
///
/// # Returns
/// A tuple containing:
/// - `String`: The main media type (lowercase)
/// - `HashMap<String, String>`: Parameters as key-value pairs (lowercase keys)
///
/// # Content-Type Format
/// Content-Type headers have the format: `type/subtype; param1=value1; param2=value2`
///
/// # Parameter Handling
/// - Parameter names are converted to lowercase for consistent access
/// - Parameter values have surrounding quotes removed if present
/// - Whitespace around parameters is automatically trimmed
///
/// # Examples
///
/// ## Basic Media Types
/// ```
/// use ignitia::utils::parse_content_type;
///
/// let (media_type, params) = parse_content_type("text/html");
/// assert_eq!(media_type, "text/html");
/// assert!(params.is_empty());
///
/// let (media_type, params) = parse_content_type("application/json");
/// assert_eq!(media_type, "application/json");
/// assert!(params.is_empty());
/// ```
///
/// ## Media Types with Parameters
/// ```
/// use ignitia::utils::parse_content_type;
///
/// let (media_type, params) = parse_content_type("text/html; charset=utf-8");
/// assert_eq!(media_type, "text/html");
/// assert_eq!(params.get("charset"), Some(&"utf-8".to_string()));
///
/// let (media_type, params) = parse_content_type("application/json; charset=utf-8; boundary=something");
/// assert_eq!(media_type, "application/json");
/// assert_eq!(params.get("charset"), Some(&"utf-8".to_string()));
/// assert_eq!(params.get("boundary"), Some(&"something".to_string()));
/// ```
///
/// ## Quoted Parameter Values
/// ```
/// use ignitia::utils::parse_content_type;
///
/// let (media_type, params) = parse_content_type(r#"text/html; charset="utf-8"; title="My Page""#);
/// assert_eq!(media_type, "text/html");
/// assert_eq!(params.get("charset"), Some(&"utf-8".to_string()));
/// assert_eq!(params.get("title"), Some(&"My Page".to_string()));
/// ```
///
/// ## Multipart Content Types
/// ```
/// use ignitia::utils::parse_content_type;
///
/// let content_type = "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
/// let (media_type, params) = parse_content_type(content_type);
///
/// assert_eq!(media_type, "multipart/form-data");
/// assert_eq!(params.get("boundary"), Some(&"----WebKitFormBoundary7MA4YWxkTrZu0gW".to_string()));
/// ```
///
/// ## Case Insensitive Handling
/// ```
/// use ignitia::utils::parse_content_type;
///
/// let (media_type, params) = parse_content_type("TEXT/HTML; CHARSET=UTF-8");
/// assert_eq!(media_type, "text/html"); // Lowercase
/// assert_eq!(params.get("charset"), Some(&"UTF-8".to_string())); // Key lowercase, value preserved
/// ```
/// Normalizes a URL path by removing redundant elements and ensuring consistent format.
///
/// This function cleans up URL paths by removing double slashes, resolving relative
/// path components (. and ..), and ensuring the path starts with a forward slash.
///
/// # Parameters
/// - `path`: The URL path to normalize
///
/// # Returns
/// A normalized path string
///
/// # Normalization Rules
/// - Removes leading and trailing whitespace
/// - Ensures path starts with '/'
/// - Removes duplicate consecutive slashes
/// - Resolves '.' (current directory) components
/// - Resolves '..' (parent directory) components
/// - Preserves trailing slash if originally present
///
/// # Examples
///
/// ```
/// use ignitia::utils::normalize_path;
///
/// assert_eq!(normalize_path("/api/users"), "/api/users");
/// assert_eq!(normalize_path("api/users"), "/api/users");
/// assert_eq!(normalize_path("/api//users"), "/api/users");
/// assert_eq!(normalize_path("/api/./users"), "/api/users");
/// assert_eq!(normalize_path("/api/v1/../users"), "/api/users");
/// assert_eq!(normalize_path("/api/users/"), "/api/users/");
/// ```
/// Validates if a string is a valid HTTP method.
///
/// # Parameters
/// - `method`: The method string to validate
///
/// # Returns
/// `true` if the method is valid, `false` otherwise
///
/// # Examples
///
/// ```
/// use ignitia::utils::is_valid_http_method;
///
/// assert!(is_valid_http_method("GET"));
/// assert!(is_valid_http_method("POST"));
/// assert!(is_valid_http_method("PATCH"));
/// assert!(!is_valid_http_method("INVALID"));
/// assert!(!is_valid_http_method("get")); // Case sensitive
/// ```
/// Extracts the file extension from a path or filename.
///
/// # Parameters
/// - `path`: The file path or filename
///
/// # Returns
/// The file extension (without the dot) or an empty string if no extension
///
/// # Examples
///
/// ```
/// use ignitia::utils::get_file_extension;
///
/// assert_eq!(get_file_extension("index.html"), "html");
/// assert_eq!(get_file_extension("style.css"), "css");
/// assert_eq!(get_file_extension("script.min.js"), "js");
/// assert_eq!(get_file_extension("README"), "");
/// assert_eq!(get_file_extension("/path/to/file.txt"), "txt");
/// ```
/// Generates a simple ETag for content based on length and a hash.
///
/// # Parameters
/// - `content`: The content to generate an ETag for
///
/// # Returns
/// An ETag string suitable for HTTP caching
///
/// # Examples
///
/// ```
/// use ignitia::utils::generate_etag;
///
/// let content = "Hello, World!";
/// let etag = generate_etag(content.as_bytes());
///
/// // ETags should be consistent for the same content
/// assert_eq!(etag, generate_etag(content.as_bytes()));
/// ```
/// Sanitizes a string for safe use in HTML contexts.
///
/// This function escapes HTML special characters to prevent XSS attacks
/// when user input is displayed in HTML.
///
/// # Parameters
/// - `input`: The string to sanitize
///
/// # Returns
/// A sanitized string with HTML entities escaped
///
/// # Examples
///
/// ```
/// use ignitia::utils::html_escape;
///
/// assert_eq!(html_escape("Hello <world>"), "Hello <world>");
/// assert_eq!(html_escape("AT&T"), "AT&T");
/// assert_eq!(html_escape(r#"Say "hello""#), "Say "hello"");
/// ```
/// Checks if a string contains only ASCII alphanumeric characters and common safe symbols.
///
/// This is useful for validating user input that should only contain safe characters.
///
/// # Parameters
/// - `input`: The string to validate
///
/// # Returns
/// `true` if the string contains only safe characters, `false` otherwise
///
/// # Safe Characters
/// - ASCII letters (a-z, A-Z)
/// - ASCII digits (0-9)
/// - Common symbols: - _ . @ + =
/// - Space character
///
/// # Examples
///
/// ```
/// use ignitia::utils::is_safe_string;
///
/// assert!(is_safe_string("hello_world-123"));
/// assert!(is_safe_string("user@example.com"));
/// assert!(is_safe_string("My Name"));
/// assert!(!is_safe_string("hello<script>"));
/// assert!(!is_safe_string("test\n\r"));
/// ```
/// Truncates a string to a maximum length, adding an ellipsis if truncated.
///
/// # Parameters
/// - `input`: The string to potentially truncate
/// - `max_len`: Maximum length (including ellipsis if added)
///
/// # Returns
/// The original string if it fits, or a truncated version with "..." appended
///
/// # Examples
///
/// ```
/// use ignitia::utils::truncate_string;
///
/// assert_eq!(truncate_string("Hello", 10), "Hello");
/// assert_eq!(truncate_string("Hello, World!", 8), "Hello...");
/// assert_eq!(truncate_string("Hi", 5), "Hi");
/// ```
/// Utility functions for working with HTTP headers.
/// Utility functions for working with MIME types.
/// Utility functions for validation and sanitization.