Skip to content

Commit a4bf07d

Browse files
Daniel Pattersonbrson
authored andcommitted
---
yaml --- r: 20850 b: refs/heads/snap-stage3 c: 2118720 h: refs/heads/master v: v3
1 parent ba72b38 commit a4bf07d

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: b57f6b73badafede3f9242ce177bb7aa3c695150
4+
refs/heads/snap-stage3: 21187206c3521f10d91627771d682c513a4356dc
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/net_url.rs

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ type userinfo = {
3030

3131
type query = ~[(~str, ~str)];
3232

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 {
3536
{ scheme: scheme, user: user, host: host, port: port,
3637
path: path, query: query, fragment: fragment }
3738
}
@@ -79,19 +80,19 @@ fn encode_inner(s: ~str, full_url: bool) -> ~str {
7980
}
8081
}
8182

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.
8586
*
8687
* This function is compliant with RFC 3986.
8788
*/
8889
fn encode(s: ~str) -> ~str {
8990
encode_inner(s, true)
9091
}
9192

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.
9596
*
9697
* This function is compliant with RFC 3986.
9798
*/
@@ -139,14 +140,14 @@ fn decode_inner(s: ~str, full_url: bool) -> ~str {
139140

140141
/**
141142
* Decode a string encoded with percent encoding.
142-
*
143+
*
143144
* This will only decode escape sequences generated by encode_uri.
144145
*/
145146
fn decode(s: ~str) -> ~str {
146147
decode_inner(s, true)
147148
}
148149

149-
/**
150+
/**
150151
* Decode a string encoded with percent encoding.
151152
*/
152153
fn decode_component(s: ~str) -> ~str {
@@ -172,7 +173,7 @@ fn encode_plus(s: ~str) -> ~str {
172173
}
173174
}
174175

175-
/**
176+
/**
176177
* Encode a hashmap to the 'application/x-www-form-urlencoded' media type.
177178
*/
178179
fn encode_form_urlencoded(m: hashmap<~str, @dvec<@~str>>) -> ~str {
@@ -190,18 +191,19 @@ fn encode_form_urlencoded(m: hashmap<~str, @dvec<@~str>>) -> ~str {
190191
first = false;
191192
}
192193

193-
out += #fmt("%s=%s", key, encode_plus(*value));
194+
out += #fmt("%s=%s", key, encode_plus(*value));
194195
}
195196
}
196197

197198
out
198199
}
199200

200-
/**
201+
/**
201202
* Decode a string encoded with the 'application/x-www-form-urlencoded' media
202203
* type into a hashmap.
203204
*/
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>> {
205207
do io::with_bytes_reader(s) |rdr| {
206208
let m = str_hash();
207209
let mut key = ~"";
@@ -282,7 +284,8 @@ fn split_char_first(s: ~str, c: char) -> (~str, ~str) {
282284
if index+match_ == len {
283285
return (str::slice(s, 0, index), ~"");
284286
} 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)));
286289
}
287290
}
288291

@@ -355,7 +358,7 @@ fn get_scheme(rawurl: ~str) -> result::result<(~str, ~str), @~str> {
355358

356359
// returns userinfo, host, port, and unparsed part, or an error
357360
// currently doesn't handle IPv6 addresses.
358-
fn get_authority(rawurl: ~str) ->
361+
fn get_authority(rawurl: ~str) ->
359362
result::result<(option<userinfo>, ~str, option<~str>, ~str), @~str> {
360363
if !str::starts_with(rawurl, ~"//") {
361364
// there is no authority.
@@ -373,7 +376,7 @@ fn get_authority(rawurl: ~str) ->
373376
enum input {
374377
digit, // all digits
375378
hex, // digits and letters a-f
376-
unreserved // all other legal characters in usernames, passwords, hosts
379+
unreserved // all other legal characters
377380
}
378381
let len = str::len(rawurl);
379382
let mut st : state = start;
@@ -399,8 +402,8 @@ fn get_authority(rawurl: ~str) ->
399402
alt c {
400403
'0' to '9' { }
401404
'A' to 'F' | 'a' to 'f' {
402-
if in == digit {
403-
in = hex;
405+
if in == digit {
406+
in = hex;
404407
}
405408
}
406409
'G' to 'Z' | 'g' to 'z' | '-' | '.' | '_' | '~' | '%' |
@@ -415,7 +418,7 @@ fn get_authority(rawurl: ~str) ->
415418
}
416419
}
417420

418-
// now state machine
421+
// now process states
419422
alt c {
420423
':' {
421424
colon_count += 1;
@@ -465,14 +468,14 @@ fn get_authority(rawurl: ~str) ->
465468
alt st {
466469
start {
467470
let user = str::slice(rawurl, begin, i);
468-
userinfo = option::some({user : user,
471+
userinfo = option::some({user : user,
469472
pass: option::none});
470473
st = in_host;
471474
}
472475
pass_host_port {
473476
let user = str::slice(rawurl, begin, pos);
474477
let pass = str::slice(rawurl, pos+1, i);
475-
userinfo = option::some({user: user,
478+
userinfo = option::some({user: user,
476479
pass: option::some(pass)});
477480
st = in_host;
478481
}
@@ -482,7 +485,7 @@ fn get_authority(rawurl: ~str) ->
482485
}
483486
begin = i+1;
484487
}
485-
488+
486489
'?' | '#' | '/' {
487490
break;
488491
}
@@ -517,14 +520,14 @@ fn get_authority(rawurl: ~str) ->
517520
}
518521
}
519522

520-
let rest = if i+1 == len { ~"" }
523+
let rest = if i+1 == len { ~"" }
521524
else { str::slice(rawurl, i, len) };
522525
return result::ok((userinfo, host, port, rest));
523526
}
524527

525528

526529
// 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) ->
528531
result::result<(~str, ~str), @~str> {
529532
let len = str::len(rawurl);
530533
let mut end = len;
@@ -548,27 +551,27 @@ fn get_path(rawurl: ~str, authority : bool) ->
548551
'/' in presence of authority.");
549552
}
550553
}
551-
552-
return result::ok((decode_component(str::slice(rawurl, 0, end)),
554+
555+
return result::ok((decode_component(str::slice(rawurl, 0, end)),
553556
str::slice(rawurl, end, len)));
554557
}
555558

556559
// returns the parsed query and the fragment, if present
557-
fn get_query_fragment(rawurl: ~str) ->
560+
fn get_query_fragment(rawurl: ~str) ->
558561
result::result<(query, option<~str>), @~str> {
559562
if !str::starts_with(rawurl, ~"?") {
560563
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,
563566
str::len(rawurl)));
564567
return result::ok((~[], option::some(f)));
565568
} else {
566569
return result::ok((~[], option::none));
567570
}
568571
}
569-
let (q, r) = split_char_first(str::slice(rawurl, 1,
572+
let (q, r) = split_char_first(str::slice(rawurl, 1,
570573
str::len(rawurl)), '#');
571-
let f = if str::len(r) != 0 {
574+
let f = if str::len(r) != 0 {
572575
option::some(decode_component(r)) } else { option::none };
573576
return result::ok((query_from_str(q), f));
574577
}
@@ -606,7 +609,7 @@ fn from_str(rawurl: ~str) -> result::result<url, ~str> {
606609
let mut pth = get_path(rest, has_authority);
607610
if result::is_err(pth) {
608611
return result::err(copy *result::get_err(pth));
609-
}
612+
}
610613
let (path, rest) = result::unwrap(pth);
611614

612615
// query and fragment
@@ -616,7 +619,7 @@ fn from_str(rawurl: ~str) -> result::result<url, ~str> {
616619
}
617620
let (query, fragment) = result::unwrap(qry);
618621

619-
return result::ok(url(scheme, userinfo, host,
622+
return result::ok(url(scheme, userinfo, host,
620623
port, path, query, fragment));
621624
}
622625

@@ -647,7 +650,8 @@ fn to_str(url: url) -> ~str {
647650
str::concat(~[~"?", query_to_str(url.query)])
648651
};
649652
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))])
651655
} else {
652656
~""
653657
};
@@ -674,7 +678,7 @@ mod tests {
674678
let (u,v) = split_char_first(~"hello, sweet world", ',');
675679
assert u == ~"hello";
676680
assert v == ~" sweet world";
677-
681+
678682
let (u,v) = split_char_first(~"hello sweet world", ',');
679683
assert u == ~"hello sweet world";
680684
assert v == ~"";
@@ -684,7 +688,7 @@ mod tests {
684688
fn test_get_authority() {
685689
let (u, h, p, r) = result::unwrap(get_authority(
686690
~"//user:pass@rust-lang.org/something"));
687-
assert u == option::some({user: ~"user",
691+
assert u == option::some({user: ~"user",
688692
pass: option::some(~"pass")});
689693
assert h == ~"rust-lang.org";
690694
assert option::is_none(p);
@@ -696,7 +700,7 @@ mod tests {
696700
assert h == ~"rust-lang.org";
697701
assert p == option::some(~"8000");
698702
assert r == ~"?something";
699-
703+
700704
let (u, h, p, r) = result::unwrap(get_authority(
701705
~"//rust-lang.org#blah"));
702706
assert option::is_none(u);
@@ -718,50 +722,57 @@ mod tests {
718722
~"//us:p@2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah"));
719723
assert u == option::some({user: ~"us", pass : option::some(~"p")});
720724
assert h == ~"2001:0db8:85a3:0042:0000:8a2e:0370:7334";
721-
assert p == option::some(~"8000");
725+
assert p == option::some(~"8000");
722726

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"));
726732
assert result::is_err(get_authority(
727733
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a"));
728734
assert result::is_err(get_authority(
729735
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00"));
730736

731737
// 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"));
733740
assert h == ~"";
734-
let (_, h, _, _) = result::unwrap(get_authority(~"rust-lang.org"));
741+
let (_, h, _, _) = result::unwrap(
742+
get_authority(~"rust-lang.org"));
735743
assert h == ~"";
736744

737745
}
738746

739747
#[test]
740748
fn test_get_path() {
741-
let (p, r) = result::unwrap(get_path(~"/something+%20orother", true));
749+
let (p, r) = result::unwrap(get_path(
750+
~"/something+%20orother", true));
742751
assert p == ~"/something+ orother";
743752
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));
745755
assert p == ~"test@email.com";
746756
assert r == ~"#fragment";
747757
let (p, r) = result::unwrap(get_path(~"/gen/:addr=?q=v", false));
748758
assert p == ~"/gen/:addr=";
749759
assert r == ~"?q=v";
750-
760+
751761
//failure cases
752762
assert result::is_err(get_path(~"something?q", true));
753-
763+
754764
}
755765

756766
#[test]
757767
fn test_url_parse() {
758768
let url = ~"http://user:[email protected]/doc?s=v#something";
759-
769+
760770
let up = from_str(url);
761771
let u = result::unwrap(up);
762772
assert u.scheme == ~"http";
763773
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";
765776
assert u.host == ~"rust-lang.org";
766777
assert u.path == ~"/doc";
767778
assert u.query.find(|kv| kv.first() == ~"s").get().second() == ~"v";

0 commit comments

Comments
 (0)