@@ -30,8 +30,9 @@ type userinfo = {
30
30
31
31
type query = ~[ ( ~str , ~str ) ] ;
32
32
33
- fn url ( -scheme : ~str , -user : option < userinfo > , -host : ~str , -port : option < ~str > ,
34
- -path : ~str , -query : query , -fragment : option < ~str > ) -> url {
33
+ fn url ( -scheme : ~str , -user : option < userinfo > , -host : ~str ,
34
+ -port : option < ~str > , -path : ~str , -query : query ,
35
+ -fragment : option < ~str > ) -> url {
35
36
{ scheme: scheme, user: user, host: host, port: port,
36
37
path: path, query: query, fragment: fragment }
37
38
}
@@ -79,19 +80,19 @@ fn encode_inner(s: ~str, full_url: bool) -> ~str {
79
80
}
80
81
}
81
82
82
- /**
83
- * Encodes a URI by replacing reserved characters with percent encoded character
84
- * sequences.
83
+ /**
84
+ * Encodes a URI by replacing reserved characters with percent encoded
85
+ * character sequences.
85
86
*
86
87
* This function is compliant with RFC 3986.
87
88
*/
88
89
fn encode ( s : ~str ) -> ~str {
89
90
encode_inner ( s, true )
90
91
}
91
92
92
- /**
93
- * Encodes a URI component by replacing reserved characters with percent encoded
94
- * character sequences.
93
+ /**
94
+ * Encodes a URI component by replacing reserved characters with percent
95
+ * encoded character sequences.
95
96
*
96
97
* This function is compliant with RFC 3986.
97
98
*/
@@ -139,14 +140,14 @@ fn decode_inner(s: ~str, full_url: bool) -> ~str {
139
140
140
141
/**
141
142
* Decode a string encoded with percent encoding.
142
- *
143
+ *
143
144
* This will only decode escape sequences generated by encode_uri.
144
145
*/
145
146
fn decode ( s : ~str ) -> ~str {
146
147
decode_inner ( s, true )
147
148
}
148
149
149
- /**
150
+ /**
150
151
* Decode a string encoded with percent encoding.
151
152
*/
152
153
fn decode_component ( s : ~str ) -> ~str {
@@ -172,7 +173,7 @@ fn encode_plus(s: ~str) -> ~str {
172
173
}
173
174
}
174
175
175
- /**
176
+ /**
176
177
* Encode a hashmap to the 'application/x-www-form-urlencoded' media type.
177
178
*/
178
179
fn encode_form_urlencoded ( m : hashmap < ~str , @dvec < @~str > > ) -> ~str {
@@ -190,18 +191,19 @@ fn encode_form_urlencoded(m: hashmap<~str, @dvec<@~str>>) -> ~str {
190
191
first = false ;
191
192
}
192
193
193
- out += #fmt ( "%s=%s" , key, encode_plus ( * value) ) ;
194
+ out += #fmt ( "%s=%s" , key, encode_plus ( * value) ) ;
194
195
}
195
196
}
196
197
197
198
out
198
199
}
199
200
200
- /**
201
+ /**
201
202
* Decode a string encoded with the 'application/x-www-form-urlencoded' media
202
203
* type into a hashmap.
203
204
*/
204
- fn decode_form_urlencoded ( s : ~[ u8 ] ) -> hashmap < ~str , @dvec < @~str > > {
205
+ fn decode_form_urlencoded ( s : ~[ u8 ] ) ->
206
+ map:: hashmap < ~str , @dvec:: dvec < @~str > > {
205
207
do io:: with_bytes_reader ( s) |rdr| {
206
208
let m = str_hash ( ) ;
207
209
let mut key = ~"";
@@ -282,7 +284,8 @@ fn split_char_first(s: ~str, c: char) -> (~str, ~str) {
282
284
if index+match_ == len {
283
285
return ( str:: slice ( s, 0 , index) , ~"") ;
284
286
} else {
285
- return ( str:: slice ( s, 0 , index) , str:: slice ( s, index + match_, str:: len ( s) ) ) ;
287
+ return ( str:: slice ( s, 0 , index) ,
288
+ str:: slice ( s, index + match_, str:: len ( s) ) ) ;
286
289
}
287
290
}
288
291
@@ -355,7 +358,7 @@ fn get_scheme(rawurl: ~str) -> result::result<(~str, ~str), @~str> {
355
358
356
359
// returns userinfo, host, port, and unparsed part, or an error
357
360
// currently doesn't handle IPv6 addresses.
358
- fn get_authority ( rawurl : ~str ) ->
361
+ fn get_authority ( rawurl : ~str ) ->
359
362
result:: result < ( option < userinfo > , ~str , option < ~str > , ~str ) , @~str > {
360
363
if !str:: starts_with ( rawurl, ~"//") {
361
364
// there is no authority.
@@ -373,7 +376,7 @@ fn get_authority(rawurl: ~str) ->
373
376
enum input {
374
377
digit, // all digits
375
378
hex, // digits and letters a-f
376
- unreserved // all other legal characters in usernames, passwords, hosts
379
+ unreserved // all other legal characters
377
380
}
378
381
let len = str:: len ( rawurl) ;
379
382
let mut st : state = start;
@@ -399,8 +402,8 @@ fn get_authority(rawurl: ~str) ->
399
402
alt c {
400
403
'0' to ' 9 ' { }
401
404
'A' to 'F' | 'a' to 'f' {
402
- if in == digit {
403
- in = hex;
405
+ if in == digit {
406
+ in = hex;
404
407
}
405
408
}
406
409
'G' to 'Z' | 'g' to 'z' | '-' | '.' | '_' | '~' | '%' |
@@ -415,7 +418,7 @@ fn get_authority(rawurl: ~str) ->
415
418
}
416
419
}
417
420
418
- // now state machine
421
+ // now process states
419
422
alt c {
420
423
':' {
421
424
colon_count += 1 ;
@@ -465,14 +468,14 @@ fn get_authority(rawurl: ~str) ->
465
468
alt st {
466
469
start {
467
470
let user = str:: slice ( rawurl, begin, i) ;
468
- userinfo = option:: some ( { user : user,
471
+ userinfo = option:: some ( { user : user,
469
472
pass: option:: none} ) ;
470
473
st = in_host;
471
474
}
472
475
pass_host_port {
473
476
let user = str:: slice ( rawurl, begin, pos) ;
474
477
let pass = str:: slice ( rawurl, pos+1 , i) ;
475
- userinfo = option:: some ( { user: user,
478
+ userinfo = option:: some ( { user: user,
476
479
pass: option:: some ( pass) } ) ;
477
480
st = in_host;
478
481
}
@@ -482,7 +485,7 @@ fn get_authority(rawurl: ~str) ->
482
485
}
483
486
begin = i+1 ;
484
487
}
485
-
488
+
486
489
'?' | '#' | '/' {
487
490
break ;
488
491
}
@@ -517,14 +520,14 @@ fn get_authority(rawurl: ~str) ->
517
520
}
518
521
}
519
522
520
- let rest = if i+1 == len { ~"" }
523
+ let rest = if i+1 == len { ~"" }
521
524
else { str:: slice ( rawurl, i, len) } ;
522
525
return result:: ok ( ( userinfo, host, port, rest) ) ;
523
526
}
524
527
525
528
526
529
// returns the path and unparsed part of url, or an error
527
- fn get_path ( rawurl : ~str , authority : bool ) ->
530
+ fn get_path ( rawurl : ~str , authority : bool ) ->
528
531
result:: result < ( ~str , ~str ) , @~str > {
529
532
let len = str:: len ( rawurl) ;
530
533
let mut end = len;
@@ -548,27 +551,27 @@ fn get_path(rawurl: ~str, authority : bool) ->
548
551
'/' in presence of authority. ") ;
549
552
}
550
553
}
551
-
552
- return result:: ok ( ( decode_component ( str:: slice ( rawurl, 0 , end) ) ,
554
+
555
+ return result:: ok ( ( decode_component ( str:: slice ( rawurl, 0 , end) ) ,
553
556
str:: slice ( rawurl, end, len) ) ) ;
554
557
}
555
558
556
559
// returns the parsed query and the fragment, if present
557
- fn get_query_fragment( rawurl : ~str ) ->
560
+ fn get_query_fragment( rawurl : ~str ) ->
558
561
result:: result < ( query , option < ~str > ) , @~str > {
559
562
if !str:: starts_with ( rawurl, ~"?") {
560
563
if str:: starts_with ( rawurl, ~"#") {
561
- let f = decode_component(str::slice(rawurl,
562
- 1,
564
+ let f = decode_component(str::slice(rawurl,
565
+ 1,
563
566
str::len(rawurl)));
564
567
return result::ok((~[], option::some(f)));
565
568
} else {
566
569
return result::ok((~[], option::none));
567
570
}
568
571
}
569
- let (q, r) = split_char_first(str::slice(rawurl, 1,
572
+ let (q, r) = split_char_first(str::slice(rawurl, 1,
570
573
str::len(rawurl)), '#');
571
- let f = if str::len(r) != 0 {
574
+ let f = if str::len(r) != 0 {
572
575
option::some(decode_component(r)) } else { option::none };
573
576
return result::ok((query_from_str(q), f));
574
577
}
@@ -606,7 +609,7 @@ fn from_str(rawurl: ~str) -> result::result<url, ~str> {
606
609
let mut pth = get_path ( rest, has_authority) ;
607
610
if result:: is_err( pth) {
608
611
return result:: err ( copy * result:: get_err ( pth) ) ;
609
- }
612
+ }
610
613
let ( path, rest) = result:: unwrap ( pth) ;
611
614
612
615
// query and fragment
@@ -616,7 +619,7 @@ fn from_str(rawurl: ~str) -> result::result<url, ~str> {
616
619
}
617
620
let ( query, fragment) = result:: unwrap ( qry) ;
618
621
619
- return result:: ok ( url ( scheme, userinfo, host,
622
+ return result:: ok ( url ( scheme, userinfo, host,
620
623
port, path, query, fragment) ) ;
621
624
}
622
625
@@ -647,7 +650,8 @@ fn to_str(url: url) -> ~str {
647
650
str:: concat ( ~[ ~"?", query_to_str ( url. query ) ] )
648
651
} ;
649
652
let fragment = if option:: is_some ( url. fragment ) {
650
- str:: concat( ~[ ~"#", encode_component(option::unwrap(copy url.fragment))])
653
+ str:: concat( ~[ ~"#", encode_component(
654
+ option::unwrap(copy url.fragment))])
651
655
} else {
652
656
~" "
653
657
} ;
@@ -674,7 +678,7 @@ mod tests {
674
678
let ( u, v) = split_char_first ( ~"hello, sweet world", ',' ) ;
675
679
assert u == ~"hello";
676
680
assert v == ~" sweet world";
677
-
681
+
678
682
let ( u, v) = split_char_first ( ~"hello sweet world", ',' ) ;
679
683
assert u == ~"hello sweet world";
680
684
assert v == ~"";
@@ -684,7 +688,7 @@ mod tests {
684
688
fn test_get_authority ( ) {
685
689
let ( u, h, p, r) = result:: unwrap ( get_authority (
686
690
~"//user: pass@rust-lang. org /something") ) ;
687
- assert u == option:: some ( { user: ~"user",
691
+ assert u == option:: some ( { user: ~"user",
688
692
pass: option:: some ( ~"pass") } ) ;
689
693
assert h == ~"rust-lang. org ";
690
694
assert option:: is_none ( p) ;
@@ -696,7 +700,7 @@ mod tests {
696
700
assert h == ~"rust-lang. org ";
697
701
assert p == option:: some ( ~"8000 ") ;
698
702
assert r == ~"?something";
699
-
703
+
700
704
let ( u, h, p, r) = result:: unwrap ( get_authority (
701
705
~"//rust-lang. org#blah ") ) ;
702
706
assert option:: is_none ( u) ;
@@ -718,50 +722,57 @@ mod tests {
718
722
~"//us: p@2001 : 0 db8: 85 a3: 0042 : 0000 : 8 a2e: 0370 : 7334 : 8000 #blah") ) ;
719
723
assert u == option:: some ( { user: ~"us", pass : option:: some ( ~"p") } ) ;
720
724
assert h == ~"2001 : 0 db8: 85 a3: 0042 : 0000 : 8 a2e: 0370 : 7334 ";
721
- assert p == option:: some ( ~"8000 ") ;
725
+ assert p == option:: some ( ~"8000 ") ;
722
726
723
- // invalid authorities;
724
- assert result:: is_err ( get_authority ( ~"//user: pass@rust-lang: something") ) ;
725
- assert result:: is_err ( get_authority ( ~"//user@rust-lang: something: /path") ) ;
727
+ // invalid authorities;
728
+ assert result:: is_err ( get_authority (
729
+ ~"//user: pass@rust-lang: something") ) ;
730
+ assert result:: is_err ( get_authority (
731
+ ~"//user@rust-lang: something: /path") ) ;
726
732
assert result:: is_err ( get_authority (
727
733
~"//2001 : 0 db8: 85 a3: 0042 : 0000 : 8 a2e: 0370 : 7334 : 800 a") ) ;
728
734
assert result:: is_err ( get_authority (
729
735
~"//2001 : 0 db8: 85 a3: 0042 : 0000 : 8 a2e: 0370 : 7334 : 8000 : 00 ") ) ;
730
736
731
737
// these parse as empty, because they don't start with '//'
732
- let ( _, h, _, _) = result:: unwrap ( get_authority ( ~"user: pass@rust-lang") ) ;
738
+ let ( _, h, _, _) = result:: unwrap (
739
+ get_authority ( ~"user: pass@rust-lang") ) ;
733
740
assert h == ~"";
734
- let ( _, h, _, _) = result:: unwrap ( get_authority ( ~"rust-lang. org ") ) ;
741
+ let ( _, h, _, _) = result:: unwrap (
742
+ get_authority ( ~"rust-lang. org ") ) ;
735
743
assert h == ~"";
736
744
737
745
}
738
746
739
747
#[ test]
740
748
fn test_get_path ( ) {
741
- let ( p, r) = result:: unwrap ( get_path ( ~"/something+%20 orother", true ) ) ;
749
+ let ( p, r) = result:: unwrap ( get_path (
750
+ ~"/something+%20 orother", true ) ) ;
742
751
assert p == ~"/something+ orother";
743
752
assert r == ~"";
744
- let ( p, r) = result:: unwrap ( get_path ( ~"test@email. com#fragment ", false ) ) ;
753
+ let ( p, r) = result:: unwrap ( get_path (
754
+ ~"test@email. com#fragment ", false ) ) ;
745
755
assert p == ~"test@email. com ";
746
756
assert r == ~"#fragment";
747
757
let ( p, r) = result:: unwrap ( get_path ( ~"/gen/: addr=?q=v", false ) ) ;
748
758
assert p == ~"/gen/: addr=";
749
759
assert r == ~" ?q=v";
750
-
760
+
751
761
//failure cases
752
762
assert result:: is_err ( get_path ( ~"something?q", true ) ) ;
753
-
763
+
754
764
}
755
765
756
766
#[ test]
757
767
fn test_url_parse ( ) {
758
768
let url = ~"http
: //user:[email protected] /doc?s=v#something";
759
-
769
+
760
770
let up = from_str ( url) ;
761
771
let u = result:: unwrap ( up) ;
762
772
assert u. scheme == ~"http";
763
773
assert option:: unwrap ( copy u. user ) . user == ~"user";
764
- assert option:: unwrap ( copy option:: unwrap ( copy u. user ) . pass ) == ~"pass";
774
+ assert option:: unwrap ( copy option:: unwrap ( copy u. user ) . pass )
775
+ == ~"pass";
765
776
assert u. host == ~"rust-lang. org ";
766
777
assert u. path == ~"/doc";
767
778
assert u. query . find ( |kv| kv. first ( ) == ~"s") . get ( ) . second ( ) == ~"v";
0 commit comments