@@ -36,16 +36,13 @@ use std::uint;
36
36
/// # Example
37
37
///
38
38
/// ```rust
39
- /// use url::{ Url, UserInfo} ;
39
+ /// use url::Url;
40
40
///
41
- /// let url = Url { scheme: "https".to_string(),
42
- /// user: Some(UserInfo { user: "username".to_string(), pass: None }),
43
- /// host: "example.com".to_string(),
44
- /// port: Some("8080".to_string()),
45
- /// path: "/foo/bar".to_string(),
46
- /// query: vec!(("baz".to_string(), "qux".to_string())),
47
- /// fragment: Some("quz".to_string()) };
48
- /// // https://username@example.com:8080/foo/bar?baz=qux#quz
41
+ /// let raw = "https://username@example.com:8080/foo/bar?baz=qux#quz";
42
+ /// match Url::parse(raw) {
43
+ /// Ok(u) => println!("Parsed '{}'", u),
44
+ /// Err(e) => println!("Couldn't parse '{}': {}", raw, e),
45
+ /// }
49
46
/// ```
50
47
#[ deriving( Clone , PartialEq , Eq ) ]
51
48
pub struct Url {
@@ -110,6 +107,38 @@ impl Url {
110
107
fragment : fragment,
111
108
}
112
109
}
110
+
111
+ /// Parses a URL, converting it from a string to a `Url` representation.
112
+ ///
113
+ /// # Arguments
114
+ /// * rawurl - a string representing the full URL, including scheme.
115
+ ///
116
+ /// # Return value
117
+ ///
118
+ /// `Err(e)` if the string did not represent a valid URL, where `e` is a
119
+ /// `String` error message. Otherwise, `Ok(u)` where `u` is a `Url` struct
120
+ /// representing the URL.
121
+ pub fn parse ( rawurl : & str ) -> Result < Url , String > {
122
+ // scheme
123
+ let ( scheme, rest) = try!( get_scheme ( rawurl) ) ;
124
+
125
+ // authority
126
+ let ( userinfo, host, port, rest) = try!( get_authority ( rest. as_slice ( ) ) ) ;
127
+
128
+ // path
129
+ let has_authority = host. len ( ) > 0 ;
130
+ let ( path, rest) = try!( get_path ( rest. as_slice ( ) , has_authority) ) ;
131
+
132
+ // query and fragment
133
+ let ( query, fragment) = try!( get_query_fragment ( rest. as_slice ( ) ) ) ;
134
+
135
+ Ok ( Url :: new ( scheme, userinfo, host, port, path, query, fragment) )
136
+ }
137
+ }
138
+
139
+ #[ deprecated="use `Url::parse`" ]
140
+ pub fn from_str ( s : & str ) -> Result < Url , String > {
141
+ Url :: parse ( s)
113
142
}
114
143
115
144
impl Path {
@@ -734,46 +763,6 @@ fn get_query_fragment(rawurl: &str) ->
734
763
return Ok ( ( query_from_str ( q. as_slice ( ) ) , f) ) ;
735
764
}
736
765
737
- /**
738
- * Parses a URL, converting it from a string to `Url` representation.
739
- *
740
- * # Arguments
741
- *
742
- * `rawurl` - a string representing the full URL, including scheme.
743
- *
744
- * # Returns
745
- *
746
- * A `Url` struct type representing the URL.
747
- */
748
- pub fn from_str ( rawurl : & str ) -> Result < Url , String > {
749
- // scheme
750
- let ( scheme, rest) = match get_scheme ( rawurl) {
751
- Ok ( val) => val,
752
- Err ( e) => return Err ( e) ,
753
- } ;
754
-
755
- // authority
756
- let ( userinfo, host, port, rest) = match get_authority ( rest. as_slice ( ) ) {
757
- Ok ( val) => val,
758
- Err ( e) => return Err ( e) ,
759
- } ;
760
-
761
- // path
762
- let has_authority = host. len ( ) > 0 ;
763
- let ( path, rest) = match get_path ( rest. as_slice ( ) , has_authority) {
764
- Ok ( val) => val,
765
- Err ( e) => return Err ( e) ,
766
- } ;
767
-
768
- // query and fragment
769
- let ( query, fragment) = match get_query_fragment ( rest. as_slice ( ) ) {
770
- Ok ( val) => val,
771
- Err ( e) => return Err ( e) ,
772
- } ;
773
-
774
- Ok ( Url :: new ( scheme, userinfo, host, port, path, query, fragment) )
775
- }
776
-
777
766
pub fn path_from_str ( rawpath : & str ) -> Result < Path , String > {
778
767
let ( path, rest) = match get_path ( rawpath, false ) {
779
768
Ok ( val) => val,
@@ -791,7 +780,7 @@ pub fn path_from_str(rawpath: &str) -> Result<Path, String> {
791
780
792
781
impl FromStr for Url {
793
782
fn from_str ( s : & str ) -> Option < Url > {
794
- match from_str ( s) {
783
+ match Url :: parse ( s) {
795
784
Ok ( url) => Some ( url) ,
796
785
Err ( _) => None
797
786
}
@@ -969,16 +958,16 @@ fn test_get_path() {
969
958
#[ cfg( test) ]
970
959
mod tests {
971
960
use { encode_form_urlencoded, decode_form_urlencoded,
972
- decode, encode, from_str , encode_component, decode_component,
973
- path_from_str, UserInfo , get_scheme} ;
961
+ decode, encode, encode_component, decode_component,
962
+ path_from_str, UserInfo , get_scheme, Url } ;
974
963
975
964
use std:: collections:: HashMap ;
976
965
977
966
#[ test]
978
967
fn test_url_parse ( ) {
979
968
let url = "http://user:pass@rust-lang.org:8080/doc/~u?s=v#something" ;
980
969
981
- let up = from_str ( url) ;
970
+ let up = from_str :: < Url > ( url) ;
982
971
let u = up. unwrap ( ) ;
983
972
assert_eq ! ( & u. scheme, & "http" . to_string( ) ) ;
984
973
assert_eq ! ( & u. user, & Some ( UserInfo :: new( "user" . to_string( ) , Some ( "pass" . to_string( ) ) ) ) ) ;
@@ -1003,7 +992,7 @@ mod tests {
1003
992
#[ test]
1004
993
fn test_url_parse_host_slash ( ) {
1005
994
let urlstr = "http://0.42.42.42/" ;
1006
- let url = from_str ( urlstr) . unwrap ( ) ;
995
+ let url = from_str :: < Url > ( urlstr) . unwrap ( ) ;
1007
996
assert ! ( url. host == "0.42.42.42" . to_string( ) ) ;
1008
997
assert ! ( url. path == "/" . to_string( ) ) ;
1009
998
}
@@ -1018,14 +1007,14 @@ mod tests {
1018
1007
#[ test]
1019
1008
fn test_url_host_with_port ( ) {
1020
1009
let urlstr = "scheme://host:1234" ;
1021
- let url = from_str ( urlstr) . unwrap ( ) ;
1010
+ let url = from_str :: < Url > ( urlstr) . unwrap ( ) ;
1022
1011
assert_eq ! ( & url. scheme, & "scheme" . to_string( ) ) ;
1023
1012
assert_eq ! ( & url. host, & "host" . to_string( ) ) ;
1024
1013
assert_eq ! ( & url. port, & Some ( "1234" . to_string( ) ) ) ;
1025
1014
// is empty path really correct? Other tests think so
1026
1015
assert_eq ! ( & url. path, & "" . to_string( ) ) ;
1027
1016
let urlstr = "scheme://host:1234/" ;
1028
- let url = from_str ( urlstr) . unwrap ( ) ;
1017
+ let url = from_str :: < Url > ( urlstr) . unwrap ( ) ;
1029
1018
assert_eq ! ( & url. scheme, & "scheme" . to_string( ) ) ;
1030
1019
assert_eq ! ( & url. host, & "host" . to_string( ) ) ;
1031
1020
assert_eq ! ( & url. port, & Some ( "1234" . to_string( ) ) ) ;
@@ -1035,7 +1024,7 @@ mod tests {
1035
1024
#[ test]
1036
1025
fn test_url_with_underscores ( ) {
1037
1026
let urlstr = "http://dotcom.com/file_name.html" ;
1038
- let url = from_str ( urlstr) . unwrap ( ) ;
1027
+ let url = from_str :: < Url > ( urlstr) . unwrap ( ) ;
1039
1028
assert ! ( url. path == "/file_name.html" . to_string( ) ) ;
1040
1029
}
1041
1030
@@ -1049,7 +1038,7 @@ mod tests {
1049
1038
#[ test]
1050
1039
fn test_url_with_dashes ( ) {
1051
1040
let urlstr = "http://dotcom.com/file-name.html" ;
1052
- let url = from_str ( urlstr) . unwrap ( ) ;
1041
+ let url = from_str :: < Url > ( urlstr) . unwrap ( ) ;
1053
1042
assert ! ( url. path == "/file-name.html" . to_string( ) ) ;
1054
1043
}
1055
1044
@@ -1067,75 +1056,75 @@ mod tests {
1067
1056
1068
1057
#[ test]
1069
1058
fn test_invalid_scheme_errors ( ) {
1070
- assert ! ( from_str ( "99://something" ) . is_err( ) ) ;
1071
- assert ! ( from_str ( "://something" ) . is_err( ) ) ;
1059
+ assert ! ( Url :: parse ( "99://something" ) . is_err( ) ) ;
1060
+ assert ! ( Url :: parse ( "://something" ) . is_err( ) ) ;
1072
1061
}
1073
1062
1074
1063
#[ test]
1075
1064
fn test_full_url_parse_and_format ( ) {
1076
1065
let url = "http://user:pass@rust-lang.org/doc?s=v#something" ;
1077
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1066
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1078
1067
}
1079
1068
1080
1069
#[ test]
1081
1070
fn test_userless_url_parse_and_format ( ) {
1082
1071
let url = "http://rust-lang.org/doc?s=v#something" ;
1083
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1072
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1084
1073
}
1085
1074
1086
1075
#[ test]
1087
1076
fn test_queryless_url_parse_and_format ( ) {
1088
1077
let url = "http://user:pass@rust-lang.org/doc#something" ;
1089
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1078
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1090
1079
}
1091
1080
1092
1081
#[ test]
1093
1082
fn test_empty_query_url_parse_and_format ( ) {
1094
1083
let url = "http://user:pass@rust-lang.org/doc?#something" ;
1095
1084
let should_be = "http://user:pass@rust-lang.org/doc#something" ;
1096
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , should_be) ;
1085
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , should_be) ;
1097
1086
}
1098
1087
1099
1088
#[ test]
1100
1089
fn test_fragmentless_url_parse_and_format ( ) {
1101
1090
let url = "http://user:pass@rust-lang.org/doc?q=v" ;
1102
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1091
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1103
1092
}
1104
1093
1105
1094
#[ test]
1106
1095
fn test_minimal_url_parse_and_format ( ) {
1107
1096
let url = "http://rust-lang.org/doc" ;
1108
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1097
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1109
1098
}
1110
1099
1111
1100
#[ test]
1112
1101
fn test_url_with_port_parse_and_format ( ) {
1113
1102
let url = "http://rust-lang.org:80/doc" ;
1114
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1103
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1115
1104
}
1116
1105
1117
1106
#[ test]
1118
1107
fn test_scheme_host_only_url_parse_and_format ( ) {
1119
1108
let url = "http://rust-lang.org" ;
1120
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1109
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1121
1110
}
1122
1111
1123
1112
#[ test]
1124
1113
fn test_pathless_url_parse_and_format ( ) {
1125
1114
let url = "http://user:pass@rust-lang.org?q=v#something" ;
1126
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1115
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1127
1116
}
1128
1117
1129
1118
#[ test]
1130
1119
fn test_scheme_host_fragment_only_url_parse_and_format ( ) {
1131
1120
let url = "http://rust-lang.org#something" ;
1132
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1121
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1133
1122
}
1134
1123
1135
1124
#[ test]
1136
1125
fn test_url_component_encoding ( ) {
1137
1126
let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B" ;
1138
- let u = from_str ( url) . unwrap ( ) ;
1127
+ let u = from_str :: < Url > ( url) . unwrap ( ) ;
1139
1128
assert ! ( u. path == "/doc uments" . to_string( ) ) ;
1140
1129
assert ! ( u. query == vec!( ( "ba%d " . to_string( ) , "#&+" . to_string( ) ) ) ) ;
1141
1130
}
@@ -1151,7 +1140,7 @@ mod tests {
1151
1140
#[ test]
1152
1141
fn test_url_without_authority ( ) {
1153
1142
let url = "mailto:test@email.com" ;
1154
- assert_eq ! ( from_str( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1143
+ assert_eq ! ( from_str:: < Url > ( url) . unwrap( ) . to_str( ) . as_slice( ) , url) ;
1155
1144
}
1156
1145
1157
1146
#[ test]
0 commit comments