From 5c20355f0e7a62b6d6b6d670bc75e6633c6f5cac Mon Sep 17 00:00:00 2001 From: quininer Date: Mon, 19 Oct 2020 00:48:59 +0800 Subject: [PATCH 1/8] allow custom status code --- src/status.rs | 35 ++++++++++++++++++++--------------- tests/status_code.rs | 4 ++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/status.rs b/src/status.rs index 903bd82c..52a1d1d9 100644 --- a/src/status.rs +++ b/src/status.rs @@ -15,6 +15,7 @@ //! ``` use std::convert::TryFrom; +use std::num::NonZeroU16; use std::error::Error; use std::fmt; use std::str::FromStr; @@ -39,7 +40,7 @@ use std::str::FromStr; /// assert!(StatusCode::OK.is_success()); /// ``` #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct StatusCode(u16); +pub struct StatusCode(NonZeroU16); /// A possible error value when converting a `StatusCode` from a `u16` or `&str` /// @@ -53,7 +54,7 @@ impl StatusCode { /// Converts a u16 to a status code. /// /// The function validates the correctness of the supplied u16. It must be - /// greater or equal to 100 but less than 600. + /// greater or equal to 100 but less than 1000. /// /// # Example /// @@ -68,11 +69,13 @@ impl StatusCode { /// ``` #[inline] pub fn from_u16(src: u16) -> Result { - if src < 100 || src >= 600 { + if src < 100 || src >= 1000 { return Err(InvalidStatusCode::new()); } - Ok(StatusCode(src)) + NonZeroU16::new(src) + .map(StatusCode) + .ok_or_else(InvalidStatusCode::new) } /// Converts a &[u8] to a status code @@ -85,12 +88,14 @@ impl StatusCode { let b = src[1].wrapping_sub(b'0') as u16; let c = src[2].wrapping_sub(b'0') as u16; - if a == 0 || a > 5 || b > 9 || c > 9 { + if a == 0 || a > 9 || b > 9 || c > 9 { return Err(InvalidStatusCode::new()); } let status = (a * 100) + (b * 10) + c; - Ok(StatusCode(status)) + NonZeroU16::new(status) + .map(StatusCode) + .ok_or_else(InvalidStatusCode::new) } /// Returns the `u16` corresponding to this `StatusCode`. @@ -126,7 +131,7 @@ impl StatusCode { /// ``` #[inline] pub fn as_str(&self) -> &str { - CODES_AS_STR[(self.0 - 100) as usize] + CODES_AS_STR[(self.0.get() - 100) as usize] } /// Get the standardised `reason-phrase` for this status code. @@ -148,37 +153,37 @@ impl StatusCode { /// assert_eq!(status.canonical_reason(), Some("OK")); /// ``` pub fn canonical_reason(&self) -> Option<&'static str> { - canonical_reason(self.0) + canonical_reason(self.0.get()) } /// Check if status is within 100-199. #[inline] pub fn is_informational(&self) -> bool { - 200 > self.0 && self.0 >= 100 + 200 > self.0.get() && self.0.get() >= 100 } /// Check if status is within 200-299. #[inline] pub fn is_success(&self) -> bool { - 300 > self.0 && self.0 >= 200 + 300 > self.0.get() && self.0.get() >= 200 } /// Check if status is within 300-399. #[inline] pub fn is_redirection(&self) -> bool { - 400 > self.0 && self.0 >= 300 + 400 > self.0.get() && self.0.get() >= 300 } /// Check if status is within 400-499. #[inline] pub fn is_client_error(&self) -> bool { - 500 > self.0 && self.0 >= 400 + 500 > self.0.get() && self.0.get() >= 400 } /// Check if status is within 500-599. #[inline] pub fn is_server_error(&self) -> bool { - 600 > self.0 && self.0 >= 500 + 600 > self.0.get() && self.0.get() >= 500 } } @@ -231,7 +236,7 @@ impl PartialEq for u16 { impl From for u16 { #[inline] fn from(status: StatusCode) -> u16 { - status.0 + status.0.get() } } @@ -287,7 +292,7 @@ macro_rules! status_codes { impl StatusCode { $( $(#[$docs])* - pub const $konst: StatusCode = StatusCode($num); + pub const $konst: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked($num) }); )+ } diff --git a/tests/status_code.rs b/tests/status_code.rs index 80df748e..478bf140 100644 --- a/tests/status_code.rs +++ b/tests/status_code.rs @@ -3,13 +3,13 @@ use http::*; #[test] fn from_bytes() { for ok in &[ - "100", "101", "199", "200", "250", "299", "321", "399", "499", "599", + "100", "101", "199", "200", "250", "299", "321", "399", "499", "599", "600", "999" ] { assert!(StatusCode::from_bytes(ok.as_bytes()).is_ok()); } for not_ok in &[ - "0", "00", "10", "40", "99", "000", "010", "099", "600", "610", "999", + "0", "00", "10", "40", "99", "000", "010", "099", "1000", "1999", ] { assert!(StatusCode::from_bytes(not_ok.as_bytes()).is_err()); } From 5ba3fb2dc18740e65037ea563355f761555bcf27 Mon Sep 17 00:00:00 2001 From: quininer Date: Mon, 19 Oct 2020 00:56:56 +0800 Subject: [PATCH 2/8] Fix as_str --- src/status.rs | 26 +++++++++++++++++++++++++- tests/status_code.rs | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/status.rs b/src/status.rs index 52a1d1d9..74121f97 100644 --- a/src/status.rs +++ b/src/status.rs @@ -528,7 +528,7 @@ impl Error for InvalidStatusCode {} macro_rules! status_code_strs { ($($num:expr,)+) => { - const CODES_AS_STR: [&'static str; 500] = [ $( stringify!($num), )+ ]; + const CODES_AS_STR: [&'static str; 900] = [ $( stringify!($num), )+ ]; } } @@ -562,4 +562,28 @@ status_code_strs!( 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, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, + 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, + 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, + + 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, + 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, + 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, + 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, + 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, ); diff --git a/tests/status_code.rs b/tests/status_code.rs index 478bf140..9dda9bd1 100644 --- a/tests/status_code.rs +++ b/tests/status_code.rs @@ -66,4 +66,28 @@ test_round_trip!( 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, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, + 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, + 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, + + 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, + 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, + 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, + 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, + 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, ); From 1d9ddb1258e4fd48fd3b4c44a93699b161a5fe7a Mon Sep 17 00:00:00 2001 From: quininer Date: Tue, 20 Oct 2020 00:58:49 +0800 Subject: [PATCH 3/8] Fix doc --- src/status.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/status.rs b/src/status.rs index 74121f97..28d0c63f 100644 --- a/src/status.rs +++ b/src/status.rs @@ -23,7 +23,7 @@ use std::str::FromStr; /// An HTTP status code (`status-code` in RFC 7230 et al.). /// /// This type contains constants for all common status codes. -/// It allows status codes in the range [100, 599]. +/// It allows status codes in the range [100, 999]. /// /// IANA maintain the [Hypertext Transfer Protocol (HTTP) Status Code /// Registry](http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) which is @@ -45,7 +45,7 @@ pub struct StatusCode(NonZeroU16); /// A possible error value when converting a `StatusCode` from a `u16` or `&str` /// /// This error indicates that the supplied input was not a valid number, was less -/// than 100, or was greater than 599. +/// than 100, or was greater than 999. pub struct InvalidStatusCode { _priv: (), } From 5ba5778e5c7f60f2732e80a2078bb515b49ca198 Mon Sep 17 00:00:00 2001 From: quininer Date: Tue, 20 Oct 2020 01:56:48 +0800 Subject: [PATCH 4/8] improve StatusCode as_str --- src/status.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/status.rs b/src/status.rs index 28d0c63f..53de2d1a 100644 --- a/src/status.rs +++ b/src/status.rs @@ -131,7 +131,14 @@ impl StatusCode { /// ``` #[inline] pub fn as_str(&self) -> &str { - CODES_AS_STR[(self.0.get() - 100) as usize] + let offset = (self.0.get() - 100) as usize; + let offset = offset * 3; + + unsafe { + CODES_AS_STR + .get_unchecked(offset..) + .get_unchecked(..3) + } } /// Get the standardised `reason-phrase` for this status code. @@ -528,7 +535,11 @@ impl Error for InvalidStatusCode {} macro_rules! status_code_strs { ($($num:expr,)+) => { - const CODES_AS_STR: [&'static str; 900] = [ $( stringify!($num), )+ ]; + const CODES_AS_STR: &'static str = concat!( + $( + stringify!($num) , + )+ + ); } } From 8166eabb188941db6c3c443d57a3322fe24eb90e Mon Sep 17 00:00:00 2001 From: David Kellum Date: Thu, 12 Nov 2020 12:04:10 -0800 Subject: [PATCH 5/8] status code roundtrip test with as_str --- tests/status_code.rs | 74 +++++--------------------------------------- 1 file changed, 7 insertions(+), 67 deletions(-) diff --git a/tests/status_code.rs b/tests/status_code.rs index 9dda9bd1..45875450 100644 --- a/tests/status_code.rs +++ b/tests/status_code.rs @@ -22,72 +22,12 @@ fn equates_with_u16() { assert_eq!(status, 200u16); } -macro_rules! test_round_trip { - ($($num:expr,)+) => { - #[test] - fn roundtrip() { - $( - let status = StatusCode::from_bytes(stringify!($num).as_bytes()).unwrap(); - let expect = $num; - - assert_eq!(u16::from(status), expect); - )+ - } +#[test] +fn roundtrip() { + for s in 100..1000 { + let sstr = s.to_string(); + let status = StatusCode::from_bytes(sstr.as_bytes()).unwrap(); + assert_eq!(s, u16::from(status)); + assert_eq!(sstr, status.as_str()); } } - -test_round_trip!( - 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, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, - 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, - 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, - - 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, - 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, - 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, - 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, - 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, - ); From b9efebf77f115384a66c61124f0b4b2388778d15 Mon Sep 17 00:00:00 2001 From: David Kellum Date: Fri, 13 Nov 2020 10:55:07 -0800 Subject: [PATCH 6/8] inline the status CODE_DIGITS --- src/status.rs | 119 +++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 70 deletions(-) diff --git a/src/status.rs b/src/status.rs index 53de2d1a..5ff9b93e 100644 --- a/src/status.rs +++ b/src/status.rs @@ -134,11 +134,7 @@ impl StatusCode { let offset = (self.0.get() - 100) as usize; let offset = offset * 3; - unsafe { - CODES_AS_STR - .get_unchecked(offset..) - .get_unchecked(..3) - } + unsafe { CODE_DIGITS.get_unchecked(offset..offset+3) } } /// Get the standardised `reason-phrase` for this status code. @@ -533,68 +529,51 @@ impl fmt::Display for InvalidStatusCode { impl Error for InvalidStatusCode {} -macro_rules! status_code_strs { - ($($num:expr,)+) => { - const CODES_AS_STR: &'static str = concat!( - $( - stringify!($num) , - )+ - ); - } -} - -status_code_strs!( - 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, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, - 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, - 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, - - 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, - 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, - 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, - 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, - 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, - ); +// A string of packed 3-ASCII-digit status code values for the supported range +// of (100..=999) +const CODE_DIGITS: &'static str = "\ +100101102103104105106107108109110111112113114115116117118119\ +120121122123124125126127128129130131132133134135136137138139\ +140141142143144145146147148149150151152153154155156157158159\ +160161162163164165166167168169170171172173174175176177178179\ +180181182183184185186187188189190191192193194195196197198199\ +200201202203204205206207208209210211212213214215216217218219\ +220221222223224225226227228229230231232233234235236237238239\ +240241242243244245246247248249250251252253254255256257258259\ +260261262263264265266267268269270271272273274275276277278279\ +280281282283284285286287288289290291292293294295296297298299\ +300301302303304305306307308309310311312313314315316317318319\ +320321322323324325326327328329330331332333334335336337338339\ +340341342343344345346347348349350351352353354355356357358359\ +360361362363364365366367368369370371372373374375376377378379\ +380381382383384385386387388389390391392393394395396397398399\ +400401402403404405406407408409410411412413414415416417418419\ +420421422423424425426427428429430431432433434435436437438439\ +440441442443444445446447448449450451452453454455456457458459\ +460461462463464465466467468469470471472473474475476477478479\ +480481482483484485486487488489490491492493494495496497498499\ +500501502503504505506507508509510511512513514515516517518519\ +520521522523524525526527528529530531532533534535536537538539\ +540541542543544545546547548549550551552553554555556557558559\ +560561562563564565566567568569570571572573574575576577578579\ +580581582583584585586587588589590591592593594595596597598599\ +600601602603604605606607608609610611612613614615616617618619\ +620621622623624625626627628629630631632633634635636637638639\ +640641642643644645646647648649650651652653654655656657658659\ +660661662663664665666667668669670671672673674675676677678679\ +680681682683684685686687688689690691692693694695696697698699\ +700701702703704705706707708709710711712713714715716717718719\ +720721722723724725726727728729730731732733734735736737738739\ +740741742743744745746747748749750751752753754755756757758759\ +760761762763764765766767768769770771772773774775776777778779\ +780781782783784785786787788789790791792793794795796797798799\ +800801802803804805806807808809810811812813814815816817818819\ +820821822823824825826827828829830831832833834835836837838839\ +840841842843844845846847848849850851852853854855856857858859\ +860861862863864865866867868869870871872873874875876877878879\ +880881882883884885886887888889890891892893894895896897898899\ +900901902903904905906907908909910911912913914915916917918919\ +920921922923924925926927928929930931932933934935936937938939\ +940941942943944945946947948949950951952953954955956957958959\ +960961962963964965966967968969970971972973974975976977978979\ +980981982983984985986987988989990991992993994995996997998999"; From 9c772d7410a04ea6d373b0cf3b4b458f64e1656e Mon Sep 17 00:00:00 2001 From: David Kellum Date: Tue, 8 Dec 2020 10:18:08 -0800 Subject: [PATCH 7/8] improve StatusCode docs for supported range --- src/status.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/status.rs b/src/status.rs index 5ff9b93e..5be28e02 100644 --- a/src/status.rs +++ b/src/status.rs @@ -22,13 +22,15 @@ use std::str::FromStr; /// An HTTP status code (`status-code` in RFC 7230 et al.). /// -/// This type contains constants for all common status codes. -/// It allows status codes in the range [100, 999]. +/// Constants are provided for known status codes, including those in the IANA +/// [HTTP Status Code Registry]( +/// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml). /// -/// IANA maintain the [Hypertext Transfer Protocol (HTTP) Status Code -/// Registry](http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) which is -/// the source for this enum (with one exception, 418 I'm a teapot, which is -/// inexplicably not in the register). +/// Status code values in the range 100-999 (inclusive) are supported by this +/// type. Values in the range 100-599 are semantically classified by the most +/// significant digit. See [`StatusCode::is_success`], etc. Values above 599 +/// are unclassified but allowed for legacy compatibility, though their use is +/// discouraged. Applications may interpret such values as protocol errors. /// /// # Examples /// @@ -54,7 +56,7 @@ impl StatusCode { /// Converts a u16 to a status code. /// /// The function validates the correctness of the supplied u16. It must be - /// greater or equal to 100 but less than 1000. + /// greater or equal to 100 and less than 1000. /// /// # Example /// @@ -530,7 +532,7 @@ impl fmt::Display for InvalidStatusCode { impl Error for InvalidStatusCode {} // A string of packed 3-ASCII-digit status code values for the supported range -// of (100..=999) +// of [100, 999] (900 codes, 2700 bytes). const CODE_DIGITS: &'static str = "\ 100101102103104105106107108109110111112113114115116117118119\ 120121122123124125126127128129130131132133134135136137138139\ From e93c972d6065a05c87dc044b31ca84bb505e2126 Mon Sep 17 00:00:00 2001 From: David Kellum Date: Tue, 8 Dec 2020 10:20:14 -0800 Subject: [PATCH 8/8] indexing CODE_DIGITS debug assertions and doc --- src/status.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/status.rs b/src/status.rs index 5be28e02..d98d24c3 100644 --- a/src/status.rs +++ b/src/status.rs @@ -136,6 +136,13 @@ impl StatusCode { let offset = (self.0.get() - 100) as usize; let offset = offset * 3; + // Invariant: self has checked range [100, 999] and CODE_DIGITS is + // ASCII-only, of length 900 * 3 = 2700 bytes + + #[cfg(debug_assertions)] + { &CODE_DIGITS[offset..offset+3] } + + #[cfg(not(debug_assertions))] unsafe { CODE_DIGITS.get_unchecked(offset..offset+3) } }