Skip to content

Commit 835ca28

Browse files
committed
refactor: remove internal from_shared functions
1 parent 88b7b54 commit 835ca28

File tree

4 files changed

+136
-152
lines changed

4 files changed

+136
-152
lines changed

src/header/value.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ impl HeaderValue {
234234
HeaderValue::from_shared_unchecked(src)
235235
}
236236

237-
fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValue> {
238-
HeaderValue::try_from_generic(src, std::convert::identity)
239-
}
240-
241237
fn try_from_generic<T: AsRef<[u8]>, F: FnOnce(T) -> Bytes>(
242238
src: T,
243239
into: F,
@@ -596,7 +592,7 @@ impl TryFrom<Bytes> for HeaderValue {
596592

597593
#[inline]
598594
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
599-
HeaderValue::from_shared(bytes)
595+
HeaderValue::try_from_generic(bytes, std::convert::identity)
600596
}
601597
}
602598

src/uri/authority.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ impl Authority {
2121
}
2222
}
2323

24-
fn from_shared(s: Bytes) -> Result<Self, InvalidUri> {
25-
// Precondition on create_authority: trivially satisfied by the
26-
// identity closure
27-
create_authority(s, |s| s)
28-
}
29-
3024
/// Attempt to convert an `Authority` from a static string.
3125
///
3226
/// This function will not perform any copying, and the string will be
@@ -282,7 +276,9 @@ impl TryFrom<Bytes> for Authority {
282276
/// # }
283277
/// ```
284278
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
285-
Authority::from_shared(bytes)
279+
// Precondition on create_authority: trivially satisfied by the
280+
// identity closure
281+
create_authority(bytes, |s| s)
286282
}
287283
}
288284

src/uri/mod.rs

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -286,56 +286,6 @@ impl Uri {
286286
Uri::try_from(src.as_ref())
287287
}
288288

289-
fn from_shared(s: Bytes) -> Result<Uri, InvalidUri> {
290-
use self::ErrorKind::*;
291-
292-
if s.len() > MAX_LEN {
293-
return Err(TooLong.into());
294-
}
295-
296-
match s.len() {
297-
0 => {
298-
return Err(Empty.into());
299-
}
300-
1 => match s[0] {
301-
b'/' => {
302-
return Ok(Uri {
303-
scheme: Scheme::empty(),
304-
authority: Authority::empty(),
305-
path_and_query: PathAndQuery::slash(),
306-
});
307-
}
308-
b'*' => {
309-
return Ok(Uri {
310-
scheme: Scheme::empty(),
311-
authority: Authority::empty(),
312-
path_and_query: PathAndQuery::star(),
313-
});
314-
}
315-
_ => {
316-
let authority = Authority::try_from(s)?;
317-
318-
return Ok(Uri {
319-
scheme: Scheme::empty(),
320-
authority,
321-
path_and_query: PathAndQuery::empty(),
322-
});
323-
}
324-
},
325-
_ => {}
326-
}
327-
328-
if s[0] == b'/' {
329-
return Ok(Uri {
330-
scheme: Scheme::empty(),
331-
authority: Authority::empty(),
332-
path_and_query: PathAndQuery::try_from(s)?,
333-
});
334-
}
335-
336-
parse_full(s)
337-
}
338-
339289
/// Convert a `Uri` from a static string.
340290
///
341291
/// This function will not perform any copying, however the string is
@@ -726,7 +676,53 @@ impl TryFrom<Bytes> for Uri {
726676
/// # }
727677
/// ```
728678
fn try_from(t: Bytes) -> Result<Uri, Self::Error> {
729-
Uri::from_shared(t)
679+
use self::ErrorKind::*;
680+
681+
if t.len() > MAX_LEN {
682+
return Err(TooLong.into());
683+
}
684+
685+
match t.len() {
686+
0 => {
687+
return Err(Empty.into());
688+
}
689+
1 => match t[0] {
690+
b'/' => {
691+
return Ok(Uri {
692+
scheme: Scheme::empty(),
693+
authority: Authority::empty(),
694+
path_and_query: PathAndQuery::slash(),
695+
});
696+
}
697+
b'*' => {
698+
return Ok(Uri {
699+
scheme: Scheme::empty(),
700+
authority: Authority::empty(),
701+
path_and_query: PathAndQuery::star(),
702+
});
703+
}
704+
_ => {
705+
let authority = Authority::try_from(t)?;
706+
707+
return Ok(Uri {
708+
scheme: Scheme::empty(),
709+
authority,
710+
path_and_query: PathAndQuery::empty(),
711+
});
712+
}
713+
},
714+
_ => {}
715+
}
716+
717+
if t[0] == b'/' {
718+
return Ok(Uri {
719+
scheme: Scheme::empty(),
720+
authority: Authority::empty(),
721+
path_and_query: PathAndQuery::try_from(t)?,
722+
});
723+
}
724+
725+
parse_full(t)
730726
}
731727
}
732728

src/uri/path.rs

Lines changed: 85 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,93 +17,6 @@ pub struct PathAndQuery {
1717
const NONE: u16 = u16::MAX;
1818

1919
impl PathAndQuery {
20-
fn from_shared(mut src: Bytes) -> Result<Self, InvalidUri> {
21-
let mut query = NONE;
22-
let mut fragment = None;
23-
24-
// block for iterator borrow
25-
{
26-
let mut iter = src.as_ref().iter().enumerate();
27-
28-
// path ...
29-
for (i, &b) in &mut iter {
30-
// See https://url.spec.whatwg.org/#path-state
31-
match b {
32-
b'?' => {
33-
debug_assert_eq!(query, NONE);
34-
query = i as u16;
35-
break;
36-
}
37-
b'#' => {
38-
fragment = Some(i);
39-
break;
40-
}
41-
42-
// This is the range of bytes that don't need to be
43-
// percent-encoded in the path. If it should have been
44-
// percent-encoded, then error.
45-
#[rustfmt::skip]
46-
0x21 |
47-
0x24..=0x3B |
48-
0x3D |
49-
0x40..=0x5F |
50-
0x61..=0x7A |
51-
0x7C |
52-
0x7E => {}
53-
54-
// These are code points that are supposed to be
55-
// percent-encoded in the path but there are clients
56-
// out there sending them as is and httparse accepts
57-
// to parse those requests, so they are allowed here
58-
// for parity.
59-
//
60-
// For reference, those are code points that are used
61-
// to send requests with JSON directly embedded in
62-
// the URI path. Yes, those things happen for real.
63-
#[rustfmt::skip]
64-
b'"' |
65-
b'{' | b'}' => {}
66-
67-
_ => return Err(ErrorKind::InvalidUriChar.into()),
68-
}
69-
}
70-
71-
// query ...
72-
if query != NONE {
73-
for (i, &b) in iter {
74-
match b {
75-
// While queries *should* be percent-encoded, most
76-
// bytes are actually allowed...
77-
// See https://url.spec.whatwg.org/#query-state
78-
//
79-
// Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E
80-
#[rustfmt::skip]
81-
0x21 |
82-
0x24..=0x3B |
83-
0x3D |
84-
0x3F..=0x7E => {}
85-
86-
b'#' => {
87-
fragment = Some(i);
88-
break;
89-
}
90-
91-
_ => return Err(ErrorKind::InvalidUriChar.into()),
92-
}
93-
}
94-
}
95-
}
96-
97-
if let Some(i) = fragment {
98-
src.truncate(i);
99-
}
100-
101-
Ok(PathAndQuery {
102-
data: unsafe { ByteStr::from_utf8_unchecked(src) },
103-
query,
104-
})
105-
}
106-
10720
/// Convert a `PathAndQuery` from a static string.
10821
///
10922
/// This function will not perform any copying, however the string is
@@ -278,8 +191,91 @@ impl PathAndQuery {
278191
impl TryFrom<Bytes> for PathAndQuery {
279192
type Error = InvalidUri;
280193
#[inline]
281-
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
282-
PathAndQuery::from_shared(bytes)
194+
fn try_from(mut bytes: Bytes) -> Result<Self, Self::Error> {
195+
let mut query = NONE;
196+
let mut fragment = None;
197+
198+
// block for iterator borrow
199+
{
200+
let mut iter = bytes.as_ref().iter().enumerate();
201+
202+
// path ...
203+
for (i, &b) in &mut iter {
204+
// See https://url.spec.whatwg.org/#path-state
205+
match b {
206+
b'?' => {
207+
debug_assert_eq!(query, NONE);
208+
query = i as u16;
209+
break;
210+
}
211+
b'#' => {
212+
fragment = Some(i);
213+
break;
214+
}
215+
216+
// This is the range of bytes that don't need to be
217+
// percent-encoded in the path. If it should have been
218+
// percent-encoded, then error.
219+
#[rustfmt::skip]
220+
0x21 |
221+
0x24..=0x3B |
222+
0x3D |
223+
0x40..=0x5F |
224+
0x61..=0x7A |
225+
0x7C |
226+
0x7E => {}
227+
228+
// These are code points that are supposed to be
229+
// percent-encoded in the path but there are clients
230+
// out there sending them as is and httparse accepts
231+
// to parse those requests, so they are allowed here
232+
// for parity.
233+
//
234+
// For reference, those are code points that are used
235+
// to send requests with JSON directly embedded in
236+
// the URI path. Yes, those things happen for real.
237+
#[rustfmt::skip]
238+
b'"' |
239+
b'{' | b'}' => {}
240+
241+
_ => return Err(ErrorKind::InvalidUriChar.into()),
242+
}
243+
}
244+
245+
// query ...
246+
if query != NONE {
247+
for (i, &b) in iter {
248+
match b {
249+
// While queries *should* be percent-encoded, most
250+
// bytes are actually allowed...
251+
// See https://url.spec.whatwg.org/#query-state
252+
//
253+
// Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E
254+
#[rustfmt::skip]
255+
0x21 |
256+
0x24..=0x3B |
257+
0x3D |
258+
0x3F..=0x7E => {}
259+
260+
b'#' => {
261+
fragment = Some(i);
262+
break;
263+
}
264+
265+
_ => return Err(ErrorKind::InvalidUriChar.into()),
266+
}
267+
}
268+
}
269+
}
270+
271+
if let Some(i) = fragment {
272+
bytes.truncate(i);
273+
}
274+
275+
Ok(PathAndQuery {
276+
data: unsafe { ByteStr::from_utf8_unchecked(bytes) },
277+
query,
278+
})
283279
}
284280
}
285281

0 commit comments

Comments
 (0)