13
13
//! Converts between a string (such as an URL’s query string)
14
14
//! and a sequence of (name, value) pairs.
15
15
16
- use encoding:: EncodingOverride ;
16
+ use encoding:: { decode_utf8_lossy , EncodingOverride } ;
17
17
use percent_encoding:: { percent_decode, percent_encode_byte} ;
18
18
use std:: borrow:: { Borrow , Cow } ;
19
19
use std:: fmt;
@@ -28,61 +28,12 @@ use std::str;
28
28
/// converted to `[("#first", "%try%")]`.
29
29
#[ inline]
30
30
pub fn parse ( input : & [ u8 ] ) -> Parse {
31
- Parse {
32
- input : input,
33
- encoding : EncodingOverride :: utf8 ( ) ,
34
- }
31
+ Parse { input : input }
35
32
}
36
-
37
- /// Convert a byte string in the `application/x-www-form-urlencoded` syntax
38
- /// into a iterator of (name, value) pairs.
39
- ///
40
- /// Use `parse(input.as_bytes())` to parse a `&str` string.
41
- ///
42
- /// This function is only available if the `query_encoding`
43
- /// [feature](http://doc.crates.io/manifest.html#the-features-section]) is enabled.
44
- ///
45
- /// Arguments:
46
- ///
47
- /// * `encoding_override`: The character encoding each name and values is decoded as
48
- /// after percent-decoding. Defaults to UTF-8.
49
- /// `EncodingRef` is defined in [rust-encoding](https://github.com/lifthrasiir/rust-encoding).
50
- /// * `use_charset`: The *use _charset_ flag*. If in doubt, set to `false`.
51
- #[ cfg( feature = "query_encoding" ) ]
52
- pub fn parse_with_encoding < ' a > (
53
- input : & ' a [ u8 ] ,
54
- encoding_override : Option < :: encoding:: EncodingRef > ,
55
- use_charset : bool ,
56
- ) -> Result < Parse < ' a > , ( ) > {
57
- let mut encoding = EncodingOverride :: from_opt_encoding ( encoding_override) ;
58
- if !( encoding. is_utf8 ( ) || input. is_ascii ( ) ) {
59
- return Err ( ( ) ) ;
60
- }
61
- if use_charset {
62
- for sequence in input. split ( |& b| b == b'&' ) {
63
- // No '+' in "_charset_" to replace with ' '.
64
- if sequence. starts_with ( b"_charset_=" ) {
65
- let value = & sequence[ b"_charset_=" . len ( ) ..] ;
66
- // Skip replacing '+' with ' ' in value since no encoding label contains either:
67
- // https://encoding.spec.whatwg.org/#names-and-labels
68
- if let Some ( e) = EncodingOverride :: lookup ( value) {
69
- encoding = e;
70
- break ;
71
- }
72
- }
73
- }
74
- }
75
- Ok ( Parse {
76
- input : input,
77
- encoding : encoding,
78
- } )
79
- }
80
-
81
33
/// The return type of `parse()`.
82
34
#[ derive( Copy , Clone , Debug ) ]
83
35
pub struct Parse < ' a > {
84
36
input : & ' a [ u8 ] ,
85
- encoding : EncodingOverride ,
86
37
}
87
38
88
39
impl < ' a > Iterator for Parse < ' a > {
@@ -102,14 +53,14 @@ impl<'a> Iterator for Parse<'a> {
102
53
let mut split2 = sequence. splitn ( 2 , |& b| b == b'=' ) ;
103
54
let name = split2. next ( ) . unwrap ( ) ;
104
55
let value = split2. next ( ) . unwrap_or ( & [ ] [ ..] ) ;
105
- return Some ( ( decode ( name, self . encoding ) , decode ( value, self . encoding ) ) ) ;
56
+ return Some ( ( decode ( name) , decode ( value) ) ) ;
106
57
}
107
58
}
108
59
}
109
60
110
- fn decode ( input : & [ u8 ] , encoding : EncodingOverride ) -> Cow < str > {
61
+ fn decode ( input : & [ u8 ] ) -> Cow < str > {
111
62
let replaced = replace_plus ( input) ;
112
- encoding . decode ( match percent_decode ( & replaced) . if_any ( ) {
63
+ decode_utf8_lossy ( match percent_decode ( & replaced) . if_any ( ) {
113
64
Some ( vec) => Cow :: Owned ( vec) ,
114
65
None => replaced,
115
66
} )
0 commit comments