Skip to content

Commit a9413d7

Browse files
committed
fix(uri): make absolute-form uris always have a path
1 parent 8e57338 commit a9413d7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/common/str.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ impl ByteStr {
3030
unsafe { str::from_utf8_unchecked(self.0.as_ref()) }
3131
}
3232

33+
pub fn insert(&mut self, idx: usize, ch: char) {
34+
let mut s = self.as_str().to_owned();
35+
s.insert(idx, ch);
36+
let bytes = Bytes::from(s);
37+
self.0 = bytes;
38+
}
39+
3340
#[cfg(feature = "compat")]
3441
pub fn into_bytes(self) -> Bytes {
3542
self.0

src/uri.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Uri {
4242

4343
impl Uri {
4444
/// Parse a string into a `Uri`.
45-
fn new(s: ByteStr) -> Result<Uri, UriError> {
45+
fn new(mut s: ByteStr) -> Result<Uri, UriError> {
4646
if s.len() == 0 {
4747
Err(UriError(ErrorKind::Empty))
4848
} else if s.as_bytes() == b"*" {
@@ -81,8 +81,19 @@ impl Uri {
8181
return Err(UriError(ErrorKind::Malformed));
8282
}
8383
}
84+
85+
// absolute-form must always have a path
86+
// if there isn't a '/', for consistency, add one.
87+
let slash = auth_end;
88+
if s.len() == slash {
89+
s.insert(slash, '/');
90+
} else if s.as_bytes()[slash] != b'/' {
91+
s.insert(slash, '/');
92+
}
93+
8494
let query = parse_query(&s);
8595
let fragment = parse_fragment(&s);
96+
8697
Ok(Uri {
8798
source: s,
8899
scheme_end: scheme,
@@ -443,7 +454,6 @@ macro_rules! test_parse {
443454
#[test]
444455
fn $test_name() {
445456
let uri = Uri::from_str($str).unwrap();
446-
println!("{:?} = {:#?}", $str, uri);
447457
$(
448458
assert_eq!(uri.$method(), $value, stringify!($method));
449459
)+
@@ -486,6 +496,8 @@ test_parse! {
486496
query = None,
487497
fragment = None,
488498
port = Some(61761),
499+
500+
to_string = "https://127.0.0.1:61761/",
489501
}
490502

491503
test_parse! {
@@ -497,6 +509,8 @@ test_parse! {
497509
path = "*",
498510
query = None,
499511
fragment = None,
512+
513+
to_string = "*",
500514
}
501515

502516
test_parse! {
@@ -510,6 +524,8 @@ test_parse! {
510524
query = None,
511525
fragment = None,
512526
port = None,
527+
528+
to_string = "localhost",
513529
}
514530

515531
test_parse! {
@@ -627,6 +643,8 @@ test_parse! {
627643
query = Some("foo=bar"),
628644
fragment = None,
629645
port = None,
646+
647+
to_string = "http://127.0.0.1/?foo=bar",
630648
}
631649

632650
test_parse! {
@@ -651,6 +669,8 @@ test_parse! {
651669
query = None,
652670
fragment = Some("foo?bar"),
653671
port = None,
672+
673+
to_string = "http://127.0.0.1/#foo?bar",
654674
}
655675

656676
#[test]

0 commit comments

Comments
 (0)