rama-net 0.3.0

rama network types and utilities
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
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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
use core::{
    fmt,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
};

use crate::std::{
    self as std,
    borrow::{Cow, ToOwned},
    string::String,
    vec::Vec,
};

use super::{Domain, DomainAddress, Host, SocketAddress};
use crate::address::{HostRef, HostWithOptPort, HostWithPort, OptPort, UserInfo, UserInfoRef};

use rama_core::error::{BoxError, BoxErrorExt as _, ErrorContext};
use rama_utils::macros::generate_set_and_with;

/// A [`Host`] with optionally a port and/or user-info ([`UserInfo`]).
///
/// `user_info` is the raw RFC 3986 §3.2.1 view (opaque, possibly
/// percent-encoded bytes — the *wire* layer). For an HTTP Basic-Auth
/// credential (the *decoded, logical* layer) call [`UserInfo::to_basic`],
/// which percent-decodes and validates; [`From<Basic>`](UserInfo) is the
/// percent-encoding reverse. The two types are kept distinct on purpose.
///
/// ## Examples
///
/// - example.com
/// - 127.0.0.1
/// - example.com:80
/// - 127.0.0.1:80
/// - joe@example.com:80
/// - joe:secret@example.com
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Authority {
    pub user_info: Option<UserInfo>,
    pub address: HostWithOptPort,
}

impl Authority {
    /// Creates a new [`Authority`] from a [`HostWithOptPort`].
    #[must_use]
    #[inline(always)]
    pub const fn new(addr: HostWithOptPort) -> Self {
        Self {
            address: addr,
            user_info: None,
        }
    }

    /// Creates a new [`Authority`] from a [`HostWithOptPort`] and user-info
    /// ([`UserInfo`]).
    ///
    /// Not `const fn` — `UserInfo` wraps `Bytes` which has no const
    /// constructor; use the builder ([`Self::with_user_info`]) plus
    /// [`UserInfo::from_static`] for the const-friendly path.
    #[must_use]
    #[inline(always)]
    pub fn new_with_user_info(addr: HostWithOptPort, user_info: UserInfo) -> Self {
        Self {
            address: addr,
            user_info: Some(user_info),
        }
    }

    /// Compile-time constructor for a domain-only [`Authority`]
    /// (no user-info, no explicit port). Panics at compile time when
    /// `s` isn't a valid domain.
    #[must_use]
    pub const fn from_static(s: &'static str) -> Self {
        Self::new(HostWithOptPort::new(Host::from_static(s)))
    }

    /// creates a new local ipv4 [`Authority`] without a port.
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::local_ipv4();
    /// assert_eq!("127.0.0.1", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn local_ipv4() -> Self {
        Self::new(HostWithOptPort::local_ipv4())
    }

    /// creates a new local ipv4 [`Authority`] with the given port
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::local_ipv4_with_port(8080);
    /// assert_eq!("127.0.0.1:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn local_ipv4_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::local_ipv4_with_port(port))
    }

    /// creates a new local ipv6 [`Authority`] without a port.
    ///
    /// # Example
    ///
    /// IPv6 addresses always render with `[…]` brackets even with no
    /// port — see [`HostWithOptPort`]'s `Display` impl for the
    /// rationale.
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::local_ipv6();
    /// assert_eq!("[::1]", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn local_ipv6() -> Self {
        Self::new(HostWithOptPort::local_ipv6())
    }

    /// creates a new local ipv6 [`Authority`] with the given port.
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::local_ipv6_with_port(8080);
    /// assert_eq!("[::1]:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn local_ipv6_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::local_ipv6_with_port(port))
    }

    /// creates a default ipv4 [`Authority`] without a port
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::default_ipv4_with_port(8080);
    /// assert_eq!("0.0.0.0:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn default_ipv4() -> Self {
        Self::new(HostWithOptPort::default_ipv4())
    }

    /// creates a default ipv4 [`Authority`] with the given port
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::default_ipv4_with_port(8080);
    /// assert_eq!("0.0.0.0:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn default_ipv4_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::default_ipv4_with_port(port))
    }

    /// creates a new default ipv6 [`Authority`] without a port.
    ///
    /// IPv6 addresses always render with `[…]` brackets even with no
    /// port — see [`HostWithOptPort`]'s `Display` impl for the
    /// rationale.
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::default_ipv6();
    /// assert_eq!("[::]", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn default_ipv6() -> Self {
        Self::new(HostWithOptPort::default_ipv6())
    }

    /// creates a new default ipv6 [`Authority`] with the given port.
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::default_ipv6_with_port(8080);
    /// assert_eq!("[::]:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn default_ipv6_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::default_ipv6_with_port(port))
    }

    /// creates a new broadcast ipv4 [`Authority`] without a port
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::broadcast_ipv4();
    /// assert_eq!("255.255.255.255", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn broadcast_ipv4() -> Self {
        Self::new(HostWithOptPort::broadcast_ipv4())
    }

    /// creates a new broadcast ipv4 [`Authority`] with the given port
    ///
    /// # Example
    ///
    /// ```
    /// use rama_net::address::Authority;
    ///
    /// let addr = Authority::broadcast_ipv4_with_port(8080);
    /// assert_eq!("255.255.255.255:8080", addr.to_string());
    /// ```
    #[must_use]
    #[inline(always)]
    pub const fn broadcast_ipv4_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::broadcast_ipv4_with_port(port))
    }

    /// Creates a new example domain [`Authority`] without a port.
    #[must_use]
    #[inline(always)]
    pub const fn example_domain() -> Self {
        Self::new(HostWithOptPort::example_domain())
    }

    /// Creates a new example domain [`HostWithOptPort`] for the `http` default port.
    #[must_use]
    #[inline(always)]
    pub const fn example_domain_http() -> Self {
        Self::new(HostWithOptPort::example_domain_http())
    }

    /// Creates a new example domain [`HostWithOptPort`] for the `https` default port.
    #[must_use]
    #[inline(always)]
    pub const fn example_domain_https() -> Self {
        Self::new(HostWithOptPort::example_domain_https())
    }

    /// Creates a new example domain [`HostWithOptPort`] for the given port.
    #[must_use]
    #[inline(always)]
    pub const fn example_domain_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::example_domain_with_port(port))
    }

    /// Creates a new localhost domain [`HostWithOptPort`] without a port.
    #[must_use]
    #[inline(always)]
    pub const fn localhost_domain() -> Self {
        Self::new(HostWithOptPort::localhost_domain())
    }

    /// Creates a new localhost domain [`HostWithOptPort`] for the `http` default port.
    #[must_use]
    #[inline(always)]
    pub const fn localhost_domain_http() -> Self {
        Self::new(HostWithOptPort::localhost_domain_http())
    }

    /// Creates a new localhost domain [`HostWithOptPort`] for the `https` default port.
    #[must_use]
    #[inline(always)]
    pub const fn localhost_domain_https() -> Self {
        Self::new(HostWithOptPort::localhost_domain_https())
    }

    /// Creates a new localhost domain [`HostWithOptPort`] for the given port.
    #[must_use]
    #[inline(always)]
    pub const fn localhost_domain_with_port(port: u16) -> Self {
        Self::new(HostWithOptPort::localhost_domain_with_port(port))
    }

    generate_set_and_with! {
        /// Set [`Host`] of [`Authority`]. Accepts any [`Into<Host>`] —
        /// [`Domain`], [`IpAddr`](core::net::IpAddr), and so on.
        pub fn host(mut self, host: impl Into<Host>) -> Self {
            self.address.set_host(host.into());
            self
        }
    }

    generate_set_and_with! {
        /// Set the port of [`Authority`]. Accepts `u16`, `OptPort`, or
        /// `Option<u16>` via [`Into<OptPort>`]. Pass `OptPort::Unset`
        /// to clear.
        pub fn port(mut self, port: impl Into<OptPort>) -> Self {
            self.address.port = port.into();
            self
        }
    }

    /// Relaxed view of the port — `Set(n) → Some(n)`, everything else
    /// `None`. Use when the `Unset` vs `Empty` distinction doesn't matter.
    #[must_use]
    #[inline]
    pub const fn port_u16(&self) -> Option<u16> {
        self.address.port.as_u16()
    }

    generate_set_and_with! {
        /// (un)set user-info ([`UserInfo`]) of [`Authority`]
        pub fn user_info(mut self, user_info: Option<UserInfo>) -> Self {
            self.user_info = user_info;
            self
        }
    }

    /// Borrowed view.
    #[must_use]
    #[inline]
    pub fn view(&self) -> AuthorityRef<'_> {
        AuthorityRef::from(self)
    }
}

impl<'a> From<&'a Authority> for AuthorityRef<'a> {
    fn from(a: &'a Authority) -> Self {
        Self::new(
            a.user_info.as_ref().map(UserInfoRef::from),
            HostRef::from(&a.address.host),
            a.address.port,
        )
    }
}

impl From<(Domain, u16)> for Authority {
    #[inline(always)]
    fn from((domain, port): (Domain, u16)) -> Self {
        (Host::Name(domain), port).into()
    }
}

impl From<(IpAddr, u16)> for Authority {
    #[inline(always)]
    fn from((ip, port): (IpAddr, u16)) -> Self {
        (Host::Address(ip), port).into()
    }
}

impl From<(Ipv4Addr, u16)> for Authority {
    #[inline(always)]
    fn from((ip, port): (Ipv4Addr, u16)) -> Self {
        (Host::Address(IpAddr::V4(ip)), port).into()
    }
}

impl From<([u8; 4], u16)> for Authority {
    #[inline(always)]
    fn from((ip, port): ([u8; 4], u16)) -> Self {
        (Host::Address(IpAddr::V4(ip.into())), port).into()
    }
}

impl From<(Ipv6Addr, u16)> for Authority {
    #[inline(always)]
    fn from((ip, port): (Ipv6Addr, u16)) -> Self {
        (Host::Address(IpAddr::V6(ip)), port).into()
    }
}

impl From<([u8; 16], u16)> for Authority {
    #[inline(always)]
    fn from((ip, port): ([u8; 16], u16)) -> Self {
        (Host::Address(IpAddr::V6(ip.into())), port).into()
    }
}

impl From<Host> for Authority {
    #[inline(always)]
    fn from(host: Host) -> Self {
        Self::new(HostWithOptPort::new(host))
    }
}

impl From<Domain> for Authority {
    #[inline(always)]
    fn from(domain: Domain) -> Self {
        Host::Name(domain).into()
    }
}

impl From<IpAddr> for Authority {
    #[inline(always)]
    fn from(ip: IpAddr) -> Self {
        Host::Address(ip).into()
    }
}

impl From<Ipv4Addr> for Authority {
    #[inline(always)]
    fn from(ip: Ipv4Addr) -> Self {
        Host::Address(IpAddr::V4(ip)).into()
    }
}

impl From<Ipv6Addr> for Authority {
    #[inline(always)]
    fn from(ip: Ipv6Addr) -> Self {
        Host::Address(IpAddr::V6(ip)).into()
    }
}

impl From<(Host, u16)> for Authority {
    #[inline(always)]
    fn from((host, port): (Host, u16)) -> Self {
        Self::new(HostWithOptPort::new_with_port(host, port))
    }
}

impl From<Authority> for Host {
    #[inline(always)]
    fn from(authority: Authority) -> Self {
        authority.address.host
    }
}

impl From<SocketAddr> for Authority {
    #[inline(always)]
    fn from(addr: SocketAddr) -> Self {
        Self::new(HostWithOptPort::new_with_port(
            Host::Address(addr.ip()),
            addr.port(),
        ))
    }
}

impl From<&SocketAddr> for Authority {
    #[inline(always)]
    fn from(addr: &SocketAddr) -> Self {
        Self::new(HostWithOptPort::new_with_port(
            Host::Address(addr.ip()),
            addr.port(),
        ))
    }
}

impl From<HostWithOptPort> for Authority {
    #[inline(always)]
    fn from(addr: HostWithOptPort) -> Self {
        Self {
            user_info: None,
            address: addr,
        }
    }
}

impl From<Authority> for HostWithOptPort {
    #[inline(always)]
    fn from(addr: Authority) -> Self {
        addr.address
    }
}

impl From<HostWithPort> for Authority {
    #[inline(always)]
    fn from(addr: HostWithPort) -> Self {
        Self {
            user_info: None,
            address: addr.into(),
        }
    }
}

impl From<SocketAddress> for Authority {
    #[inline(always)]
    fn from(addr: SocketAddress) -> Self {
        let SocketAddress { ip_addr, port } = addr;
        Self::new(HostWithOptPort::new_with_port(Host::Address(ip_addr), port))
    }
}

impl From<&SocketAddress> for Authority {
    #[inline(always)]
    fn from(addr: &SocketAddress) -> Self {
        Self::new(HostWithOptPort::new_with_port(
            Host::Address(addr.ip_addr),
            addr.port,
        ))
    }
}

impl From<DomainAddress> for Authority {
    #[inline(always)]
    fn from(addr: DomainAddress) -> Self {
        let DomainAddress { domain, port } = addr;
        Self::from((domain, port))
    }
}

impl fmt::Display for Authority {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.view(), f)
    }
}

impl core::str::FromStr for Authority {
    type Err = BoxError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from(s)
    }
}

impl TryFrom<String> for Authority {
    type Error = BoxError;

    #[inline(always)]
    fn try_from(s: String) -> Result<Self, Self::Error> {
        try_from_maybe_borrowed_str(s.into())
    }
}

impl TryFrom<&str> for Authority {
    type Error = BoxError;

    #[inline(always)]
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        try_from_maybe_borrowed_str(s.into())
    }
}

/// Reg-name fallback for inputs `Domain::try_from` rejects but the
/// URI reg-name grammar accepts (pct-encoded / sub-delim / raw UTF-8).
/// Validates against the same byte set as the URI parser without
/// constructing a `Uri`.
fn try_as_uninterpreted_host(host_str: &str) -> Result<Host, BoxError> {
    let host = super::UninterpretedHost::try_from_reg_name_str(host_str)
        .context("parse authority host as reg-name")?;
    Ok(Host::Uninterpreted(host))
}

fn try_from_maybe_borrowed_str(maybe_borrowed: Cow<'_, str>) -> Result<Authority, BoxError> {
    let mut s = maybe_borrowed.as_ref();

    if s.is_empty() {
        return Err(BoxError::from_static_str(
            "empty string is invalid authority",
        ));
    }

    // Split on the *last* `@`. See [`super::parse_utils::find_userinfo_split`]
    // for the rationale (curl / browsers / `url` crate parity, plus the
    // observation that `@` is not in the strict RFC 3986 userinfo grammar
    // and so a permissive consumer that wanted to use it MUST place it
    // before the boundary, not after).
    let mut user_info = None;
    if let Some(idx) = crate::address::parse_utils::find_userinfo_split(s.as_bytes()) {
        // Graceful path: the last-`@` split deliberately leaves
        // earlier `@`s inside the userinfo region (curl / browser /
        // url-crate parity). `UserInfo::try_from(&str)` is strict and
        // would reject those, so we do explicit control-byte
        // screening here and bypass via `from_bytes_unchecked` —
        // mirroring the URI parser's authority handler.
        let ui_bytes = &s.as_bytes()[..idx];
        if ui_bytes.iter().any(|&b| b < 0x20 || b == 0x7F) {
            return Err(BoxError::from_static_str(
                "userinfo contains control character",
            ));
        }
        user_info = Some(UserInfo::from_bytes_unchecked(
            rama_core::bytes::Bytes::copy_from_slice(ui_bytes),
        ));
        s = &s[idx + 1..];
    }

    let host;
    let mut port = OptPort::Unset;

    // Standalone bracketed IP-literal (no trailing port): `[::1]` or
    // `[v1.fe80::a]`. Without this fast-path the colon-split below
    // treats the final `:` inside the address as a port separator.
    if s.starts_with('[') && s.ends_with(']') {
        let inside = &s[1..s.len() - 1];
        if inside.is_empty() {
            return Err(BoxError::from_static_str("empty bracketed IP-literal"));
        }
        // IPvFuture: `[v1.xxx]` — stored as `Uninterpreted(bracketed=true)`
        // verbatim, matching the URI authority parser's shape.
        if matches!(inside.as_bytes().first(), Some(b'v' | b'V')) {
            crate::uri::parser::authority::validate_ipvfuture(inside.as_bytes())
                .map_err(BoxError::from)
                .context("parse bracketed IPvFuture")?;
            return Ok(Authority {
                user_info,
                address: HostWithOptPort {
                    host: Host::Uninterpreted(super::UninterpretedHost::from_validated_bytes(
                        rama_core::bytes::Bytes::copy_from_slice(inside.as_bytes()),
                        true,
                    )),
                    port: OptPort::Unset,
                },
            });
        }
        if crate::address::parse_utils::ipv6_bracket_has_zone(inside.as_bytes()) {
            return Err(BoxError::from_static_str(
                "ipv6 zone identifiers (RFC 6874) are not supported",
            ));
        }
        let addr = inside
            .parse::<Ipv6Addr>()
            .context("parse bracketed ipv6 authority host without port")?;
        return Ok(Authority {
            user_info,
            address: HostWithOptPort {
                host: Host::Address(IpAddr::V6(addr)),
                port: OptPort::Unset,
            },
        });
    }

    if let Some(last_colon) = s.as_bytes().iter().rposition(|c| *c == b':') {
        let first_part = &s[..last_colon];
        if first_part.contains(':') {
            // ipv6 (bare or bracketed, possibly with trailing port)
            let (addr, parsed_port) =
                crate::address::parse_utils::parse_bracketed_ipv6_with_port(s, last_colon)
                    .context("authority: parse ipv6 host")?;
            host = Host::Address(IpAddr::V6(addr));
            port = parsed_port;
        } else {
            // Reject `:port` (empty host before colon). The URI authority
            // parser rejects the same shape — keep the eager paths
            // symmetric.
            if first_part.is_empty() {
                return Err(BoxError::from_static_str(
                    "empty host before ':port' is invalid",
                ));
            }
            let port_bytes = &s.as_bytes()[last_colon + 1..];
            port = if port_bytes.is_empty() {
                OptPort::Empty
            } else {
                OptPort::Set(
                    crate::address::parse_utils::parse_port_bytes(port_bytes)
                        .context("parse authority port string as u16")?,
                )
            };

            // try ipv4 first, domain afterwards, then Uninterpreted fallback
            host = if let Ok(ipv4) = first_part.parse::<Ipv4Addr>() {
                Host::Address(IpAddr::V4(ipv4))
            } else {
                let mut owned_vec = if user_info.is_some() {
                    s.as_bytes().to_vec()
                } else {
                    maybe_borrowed.into_owned().into_bytes()
                };
                owned_vec.truncate(last_colon);
                let owned_str = String::from_utf8(owned_vec)
                    .context("interpret authority host as utf-8 str")?;
                match Domain::try_from(owned_str.as_str()) {
                    Ok(domain) => Host::Name(domain),
                    // Pct-encoded reg-name, sub-delim reg-name, etc. — the
                    // URI parser accepts these and the `Host` enum can
                    // represent them. Route through the URI authority-
                    // form parser so the byte-set validation matches.
                    Err(_) => try_as_uninterpreted_host(&owned_str)?,
                }
            };
        };
    } else {
        // no port, so either IpAddr, Domain, or Uninterpreted fallback
        host = if let Ok(ip) = s.parse::<IpAddr>() {
            Host::Address(ip)
        } else {
            let owned_str = if user_info.is_some() {
                s.to_owned()
            } else {
                maybe_borrowed.into_owned()
            };
            match Domain::try_from(owned_str.as_str()) {
                Ok(domain) => Host::Name(domain),
                Err(_) => try_as_uninterpreted_host(&owned_str)?,
            }
        };
    }

    Ok(Authority {
        user_info,
        address: HostWithOptPort { host, port },
    })
}

impl TryFrom<Vec<u8>> for Authority {
    type Error = BoxError;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        let s = String::from_utf8(bytes).context("parse authority from bytes")?;
        s.try_into()
    }
}

impl TryFrom<&[u8]> for Authority {
    type Error = BoxError;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        let s = core::str::from_utf8(bytes).context("parse authority from bytes")?;
        s.try_into()
    }
}

/// Borrowed view of an [`Authority`] — userinfo + host + port, each
/// borrowing into the underlying buffer. Mirrors the [`HostRef`] /
/// [`DomainRef`](crate::address::DomainRef) /
/// [`UserInfoRef`](super::UserInfoRef) pattern for the rest of the
/// address types.
///
/// Constructed by [`Uri::authority`](crate::uri::Uri::authority) and
/// — eventually — by [`Authority`]'s own borrow accessor.
///
/// `PartialEq` / `Eq` / `Hash` follow the same component-wise rules as
/// the owned [`Authority`] (case-insensitive host via `HostRef`'s impl,
/// strict equality on userinfo / port), so the two types are
/// interchangeable as collection keys.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AuthorityRef<'a> {
    pub(crate) userinfo: Option<UserInfoRef<'a>>,
    pub(crate) host: HostRef<'a>,
    pub(crate) port: OptPort,
}

impl<'a> AuthorityRef<'a> {
    /// `pub(crate)` constructor — only [`Uri::authority`] and
    /// internal helpers should build one.
    #[must_use]
    #[inline]
    pub(crate) const fn new(
        userinfo: Option<UserInfoRef<'a>>,
        host: HostRef<'a>,
        port: OptPort,
    ) -> Self {
        Self {
            userinfo,
            host,
            port,
        }
    }

    /// Userinfo component, or `None` if the authority has no `@`
    /// (RFC 3986 §3.2.1 userinfo is optional).
    ///
    /// `Some("")` (an empty userinfo before the `@`) is distinct from
    /// `None` — preserved for wire fidelity.
    #[must_use]
    pub fn userinfo(&self) -> Option<UserInfoRef<'a>> {
        self.userinfo
    }

    /// The host component. Always present — every well-formed
    /// authority has a host.
    #[must_use]
    pub fn host(&self) -> HostRef<'a> {
        self.host
    }

    /// The port marker. Distinguishes wire-level `Unset` / `Empty` /
    /// `Set(u16)`. Most callers want [`port_u16`](Self::port_u16) which
    /// collapses to `Option<u16>`.
    #[must_use]
    pub const fn port(&self) -> OptPort {
        self.port
    }

    /// Relaxed view of the port — `Set(n) → Some(n)`, everything else
    /// `None`. Use when the `Unset` vs `Empty` distinction doesn't matter.
    #[must_use]
    #[inline]
    pub const fn port_u16(&self) -> Option<u16> {
        self.port.as_u16()
    }

    /// Promote this borrowed view to an owned [`Authority`] by copying
    /// the underlying bytes. Mirrors the `into_owned` family on the
    /// other borrowed views.
    #[must_use]
    pub fn into_owned(self) -> Authority {
        Authority {
            user_info: self.userinfo.map(|u| u.into_owned()),
            address: HostWithOptPort {
                host: self.host.into_owned(),
                port: self.port,
            },
        }
    }
}

impl fmt::Display for AuthorityRef<'_> {
    /// Renders `[userinfo@]host[:port]`. Matches [`Authority`]'s
    /// `Display` byte-for-byte.
    ///
    /// IPv6 hosts are **always** bracketed (`[ip]`), regardless of
    /// whether a port follows — same rule [`HostWithOptPort`]'s
    /// `Display` uses. Without brackets, `::1:8080` would be ambiguous between
    /// "address `::1` + port `8080`" and "address `::1:8080`, no
    /// port". We bracket inline here rather than delegating to
    /// `HostRef`'s `Display` because that formatter is a standalone-
    /// host renderer that doesn't compose with `:port`.
    ///
    /// Note: userinfo emission is the *Display* contract — wire writers
    /// for HTTP request-targets strip userinfo separately
    /// (`write_http_authority_form` / `write_h2_authority` on
    /// [`crate::uri::Uri`]).
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ui) = self.userinfo {
            write!(f, "{ui}@")?;
        }
        match self.host {
            HostRef::Address(IpAddr::V6(ip)) => write!(f, "[{ip}]")?,
            _ => self.host.fmt(f)?,
        }
        self.port.fmt(f)
    }
}

use rama_utils::macros::serde_str::impl_serde_str;

impl_serde_str!(display Authority);

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

    #[expect(clippy::needless_pass_by_value)]
    fn assert_eq(
        s: &str,
        authority: Authority,
        user_info: Option<UserInfo>,
        host: &str,
        port: Option<u16>,
    ) {
        assert_eq!(authority.user_info, user_info, "parsing: {s}");
        assert_eq!(authority.address.host, host, "parsing: {s}");
        assert_eq!(authority.address.port.as_u16(), port, "parsing: {s}");
    }

    #[test]
    fn test_parse_valid() {
        for (s, (expected_user_info, expected_host, expected_port)) in [
            ("example.com", (None, "example.com", None)),
            (
                "user@example.com",
                (Some(UserInfo::from_static("user")), "example.com", None),
            ),
            (
                "user:password@example.com",
                (
                    Some(UserInfo::from_static("user:password")),
                    "example.com",
                    None,
                ),
            ),
            ("example.com:80", (None, "example.com", Some(80))),
            // Empty port (`host:`) — `OptPort::Empty`, surfaces as
            // `None` in the relaxed `.as_u16()` view. See the dedicated
            // round-trip test below for the Display check.
            ("example.com:", (None, "example.com", None)),
            (
                "user@example.com:80",
                (Some(UserInfo::from_static("user")), "example.com", Some(80)),
            ),
            (
                "user:secret@example.com:80",
                (
                    Some(UserInfo::from_static("user:secret")),
                    "example.com",
                    Some(80),
                ),
            ),
            (
                "user@::1",
                (Some(UserInfo::from_static("user")), "::1", None),
            ),
            (
                "user:password@::1",
                (Some(UserInfo::from_static("user:password")), "::1", None),
            ),
            ("::1", (None, "::1", None)),
            ("[::1]:80", (None, "::1", Some(80))),
            (
                "user@[::1]:80",
                (Some(UserInfo::from_static("user")), "::1", Some(80)),
            ),
            (
                "user:password@[::1]:80",
                (
                    Some(UserInfo::from_static("user:password")),
                    "::1",
                    Some(80),
                ),
            ),
            ("127.0.0.1", (None, "127.0.0.1", None)),
            (
                "user@127.0.0.1",
                (Some(UserInfo::from_static("user")), "127.0.0.1", None),
            ),
            (
                "user:password@127.0.0.1",
                (
                    Some(UserInfo::from_static("user:password")),
                    "127.0.0.1",
                    None,
                ),
            ),
            ("127.0.0.1:80", (None, "127.0.0.1", Some(80))),
            (
                "user@127.0.0.1:80",
                (Some(UserInfo::from_static("user")), "127.0.0.1", Some(80)),
            ),
            (
                "user:secret@127.0.0.1:80",
                (
                    Some(UserInfo::from_static("user:secret")),
                    "127.0.0.1",
                    Some(80),
                ),
            ),
            (
                "2001:db8:3333:4444:5555:6666:7777:8888",
                (None, "2001:db8:3333:4444:5555:6666:7777:8888", None),
            ),
            (
                "user@2001:db8:3333:4444:5555:6666:7777:8888",
                (
                    Some(UserInfo::from_static("user")),
                    "2001:db8:3333:4444:5555:6666:7777:8888",
                    None,
                ),
            ),
            (
                "user:secret@2001:db8:3333:4444:5555:6666:7777:8888",
                (
                    Some(UserInfo::from_static("user:secret")),
                    "2001:db8:3333:4444:5555:6666:7777:8888",
                    None,
                ),
            ),
            (
                "[2001:db8:3333:4444:5555:6666:7777:8888]:80",
                (None, "2001:db8:3333:4444:5555:6666:7777:8888", Some(80)),
            ),
            (
                "user@[2001:db8:3333:4444:5555:6666:7777:8888]:80",
                (
                    Some(UserInfo::from_static("user")),
                    "2001:db8:3333:4444:5555:6666:7777:8888",
                    Some(80),
                ),
            ),
            (
                "user:secret@[2001:db8:3333:4444:5555:6666:7777:8888]:80",
                (
                    Some(UserInfo::from_static("user:secret")),
                    "2001:db8:3333:4444:5555:6666:7777:8888",
                    Some(80),
                ),
            ),
        ] {
            let msg = format!("parsing '{s}'");

            assert_eq(
                s,
                s.parse().expect(&msg),
                expected_user_info.clone(),
                expected_host,
                expected_port,
            );
            assert_eq(
                s,
                s.try_into().expect(&msg),
                expected_user_info.clone(),
                expected_host,
                expected_port,
            );
            assert_eq(
                s,
                s.to_owned().try_into().expect(&msg),
                expected_user_info.clone(),
                expected_host,
                expected_port,
            );
            assert_eq(
                s,
                s.as_bytes().try_into().expect(&msg),
                expected_user_info.clone(),
                expected_host,
                expected_port,
            );
            assert_eq(
                s,
                s.as_bytes().to_vec().try_into().expect(&msg),
                expected_user_info.clone(),
                expected_host,
                expected_port,
            );
        }
    }

    #[test]
    fn test_parse_invalid() {
        for s in [
            "",
            // Empty host with port — eager and lazy paths agree on
            // rejection.
            ":80",
            ":foo@:80",
            // Empty bracketed IP-literal.
            "[]",
            "[2001:db8:3333:4444:5555:6666:7777:8888",
            "2001:db8:3333:4444:5555:6666:7777:8888]",
            "example.com:-1",
            "example.com:999999",
            "[127.0.0.1]:80",
            "2001:db8:3333:4444:5555:6666:7777:8888:80",
            // IPv6 zone identifiers (RFC 9844 `%25en0` wire form) — rejected
            // by both eager and lazy paths with the same `parse_utils`
            // helper.
            "[fe80::1%25en0]",
            "[fe80::1%25en0]:8080",
            "user@[fe80::1%25en0]:8080",
        ] {
            let msg = format!("parsing '{s}'");
            assert!(s.parse::<Authority>().is_err(), "{msg}");
            assert!(Authority::try_from(s).is_err(), "{msg}");
            assert!(Authority::try_from(s.to_owned()).is_err(), "{msg}");
            assert!(Authority::try_from(s.as_bytes()).is_err(), "{msg}");
            assert!(Authority::try_from(s.as_bytes().to_vec()).is_err(), "{msg}");
        }
    }

    #[test]
    fn ipv6_zone_rejection_has_clear_message() {
        // The eager path now surfaces a specific message instead of letting
        // `Ipv6Addr::parse` fail opaquely on `%25`. Consumers can match on
        // the substring "zone identifiers" for diagnostics.
        let err = Authority::try_from("[fe80::1%25en0]:8080").unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("zone identifier") || msg.contains("zone identifiers"),
            "expected zone-identifier message, got: {msg}"
        );
    }

    #[test]
    fn test_parse_display() {
        for (s, expected) in [
            ("example.com", "example.com"),
            ("user@example.com", "user@example.com"),
            ("user:secret@example.com", "user:secret@example.com"),
            ("example.com:80", "example.com:80"),
            ("user@example.com:80", "user@example.com:80"),
            ("user:secret@example.com:80", "user:secret@example.com:80"),
            ("[::1]:80", "[::1]:80"),
            ("user@[::1]:80", "user@[::1]:80"),
            ("secret:user@[::1]:80", "secret:user@[::1]:80"),
            // IPv6 hosts ALWAYS render with `[…]` brackets — even
            // when no port is present — to avoid the `::1:8080`
            // ambiguity. See `HostWithOptPort::Display` for the
            // single-source-of-truth rationale.
            ("::1", "[::1]"),
            ("user@::1", "user@[::1]"),
            ("user:secret@::1", "user:secret@[::1]"),
            ("127.0.0.1:80", "127.0.0.1:80"),
            ("user@127.0.0.1:80", "user@127.0.0.1:80"),
            ("user:secret@127.0.0.1:80", "user:secret@127.0.0.1:80"),
            ("127.0.0.1", "127.0.0.1"),
            ("user@127.0.0.1", "user@127.0.0.1"),
            ("user:secret@127.0.0.1", "user:secret@127.0.0.1"),
        ] {
            let msg = format!("parsing '{s}'");
            let authority: Authority = s.parse().expect(&msg);
            assert_eq!(authority.to_string(), expected, "{msg}");
        }
    }

    /// Regression: when an authority contains multiple `@`, the userinfo
    /// section runs up to the *last* `@` (RFC 3986 §3.2 / §3.2.1).
    /// Splitting on the first `@` mis-parsed `user@name:pass@host:80` as
    /// userinfo=`user`, host=`name:pass@host` and rejected it.
    #[test]
    fn regression_authority_userinfo_splits_on_last_at() {
        let auth = Authority::try_from("user@name:pass@example.com:80").unwrap();
        assert_eq!(auth.address.host, "example.com");
        assert_eq!(auth.address.port, OptPort::Set(80));
        let ui = auth.user_info.as_ref().expect("userinfo present");
        let (user, pass) = ui.split_user_password();
        assert_eq!(user, b"user@name");
        assert_eq!(pass, Some(&b"pass"[..]));
    }

    /// Regression: pct-encoded reg-names are accepted by both the URI
    /// parser and the eager `Authority::try_from` path — uniform host
    /// shape across the public API.
    #[test]
    fn authority_try_from_accepts_pct_encoded_reg_name() {
        let from_uri = crate::uri::Uri::parse_authority_form("exa%6Dple.com")
            .unwrap()
            .authority()
            .unwrap()
            .into_owned();
        let direct = Authority::try_from("exa%6Dple.com").unwrap();
        assert_eq!(direct, from_uri);
        // ...and with a port.
        let from_uri_p = crate::uri::Uri::parse_authority_form("exa%6Dple.com:443")
            .unwrap()
            .authority()
            .unwrap()
            .into_owned();
        let direct_p = Authority::try_from("exa%6Dple.com:443").unwrap();
        assert_eq!(direct_p, from_uri_p);
    }

    /// `:port` with empty host is rejected at the eager parser, matching
    /// the URI authority-form parser's behavior. Without this the eager
    /// and lazy paths disagree.
    #[test]
    fn authority_try_from_rejects_empty_host_with_port() {
        Authority::try_from(":80").unwrap_err();
        Authority::try_from(":foo@:80").unwrap_err();
        // Confirm the URI parser agrees.
        crate::uri::Uri::parse_authority_form(":80").unwrap_err();
    }

    /// Standalone bracketed IPvFuture (`[vN.X]`) parses as
    /// `Host::Uninterpreted(bracketed=true)`, mirroring the URI parser.
    #[test]
    fn authority_try_from_bracketed_ipvfuture() {
        let direct = Authority::try_from("[v1.fe80::a]").unwrap();
        let from_uri = crate::uri::Uri::parse_authority_form("[v1.fe80::a]")
            .unwrap()
            .authority()
            .unwrap()
            .into_owned();
        assert_eq!(direct, from_uri);
        assert!(matches!(direct.address.host, Host::Uninterpreted(_)));
    }

    /// Standalone bracketed IPv6 (no trailing port) parses as a typed
    /// `Host::Address`, not as `Host::Uninterpreted`.
    #[test]
    fn authority_try_from_bracketed_ipv6_no_port_is_typed_address() {
        let auth = Authority::try_from("[::1]").unwrap();
        assert!(
            matches!(auth.address.host, Host::Address(IpAddr::V6(_))),
            "expected typed IPv6 Address, got {:?}",
            auth.address.host
        );
        assert_eq!(auth.address.port, OptPort::Unset);
        // Display round-trips with brackets.
        assert_eq!(auth.to_string(), "[::1]");
    }

    /// Regression: RFC 6874 IPv6 zone-ids must never be accepted in an
    /// authority position. See `host::tests::regression_host_rejects_ipv6_zone_id`
    /// for the rationale; this guards the higher-level `Authority` entry
    /// points so a future change to lower-level parsing can't silently
    /// re-allow them.
    #[test]
    fn regression_authority_rejects_ipv6_zone_id() {
        for input in [
            "[fe80::1%eth0]:80",
            "[fe80::1%25eth0]:80",
            "user@[fe80::1%eth0]:80",
        ] {
            assert!(
                Authority::try_from(input).is_err(),
                "authority should reject zone-id input {input:?}",
            );
        }
    }

    // ---- AuthorityRef::Display parity -----------------------

    #[test]
    fn authority_ref_display_matches_owned() {
        for input in [
            "example.com",
            "example.com:443",
            "user@example.com:80",
            "user:secret@example.com:8080",
            "127.0.0.1:8080",
            "[2001:db8::1]:443",
        ] {
            let owned: Authority = input.parse().unwrap();
            let ref_view = AuthorityRef::new(
                owned.user_info.as_ref().map(UserInfoRef::from),
                HostRef::from(&owned.address.host),
                owned.address.port,
            );
            assert_eq!(
                ref_view.to_string(),
                owned.to_string(),
                "AuthorityRef Display must match Authority for {input:?}"
            );
        }
    }
}