提交 dbf6abf6 编写于 作者: B Brian Anderson

std: Camel case net modules

上级 afc1ccd2
......@@ -21,21 +21,21 @@
import ll = uv::ll;
import comm = core::comm;
export ip_addr, parse_addr_err;
export IpAddr, parse_addr_err;
export format_addr;
export v4, v6;
export get_addr;
export ipv4, ipv6;
export Ipv4, Ipv6;
/// An IP address
enum ip_addr {
enum IpAddr {
/// An IPv4 address
ipv4(sockaddr_in),
ipv6(sockaddr_in6)
Ipv4(sockaddr_in),
Ipv6(sockaddr_in6)
}
/// Human-friendly feedback on why a parse_addr attempt failed
type parse_addr_err = {
type ParseAddrErr = {
err_msg: ~str
};
......@@ -46,16 +46,16 @@ enum ip_addr {
*
* * ip - a `std::net::ip::ip_addr`
*/
fn format_addr(ip: ip_addr) -> ~str {
fn format_addr(ip: IpAddr) -> ~str {
match ip {
ipv4(addr) => unsafe {
Ipv4(addr) => unsafe {
let result = uv_ip4_name(&addr);
if result == ~"" {
fail ~"failed to convert inner sockaddr_in address to str"
}
result
},
ipv6(addr) => unsafe {
Ipv6(addr) => unsafe {
let result = uv_ip6_name(&addr);
if result == ~"" {
fail ~"failed to convert inner sockaddr_in address to str"
......@@ -66,8 +66,8 @@ fn format_addr(ip: ip_addr) -> ~str {
}
/// Represents errors returned from `net::ip::get_addr()`
enum ip_get_addr_err {
get_addr_unknown_error
enum IpGetAddrErr {
GetAddrUnknownError
}
/**
......@@ -85,13 +85,13 @@ enum ip_get_addr_err {
* object in the case of failure
*/
fn get_addr(++node: ~str, iotask: iotask)
-> result::Result<~[ip_addr], ip_get_addr_err> {
-> result::Result<~[IpAddr], IpGetAddrErr> {
do core::comm::listen |output_ch| {
do str::as_buf(node) |node_ptr, len| unsafe {
log(debug, fmt!("slice len %?", len));
let handle = create_uv_getaddrinfo_t();
let handle_ptr = ptr::addr_of(handle);
let handle_data: get_addr_data = {
let handle_data: GetAddrData = {
output_ch: output_ch
};
let handle_data_ptr = ptr::addr_of(handle_data);
......@@ -108,7 +108,7 @@ fn get_addr(++node: ~str, iotask: iotask)
set_data_for_req(handle_ptr, handle_data_ptr);
}
_ => {
output_ch.send(result::Err(get_addr_unknown_error));
output_ch.send(result::Err(GetAddrUnknownError));
}
}
};
......@@ -133,7 +133,7 @@ mod v4 {
*
* * an `ip_addr` of the `ipv4` variant
*/
fn parse_addr(ip: ~str) -> ip_addr {
fn parse_addr(ip: ~str) -> IpAddr {
match try_parse_addr(ip) {
result::Ok(addr) => copy(addr),
result::Err(err_data) => fail err_data.err_msg
......@@ -141,19 +141,19 @@ fn parse_addr(ip: ~str) -> ip_addr {
}
// the simple, old style numberic representation of
// ipv4
type ipv4_rep = { a: u8, b: u8, c: u8, d:u8 };
type Ipv4Rep = { a: u8, b: u8, c: u8, d:u8 };
trait as_unsafe_u32 {
trait AsUnsafeU32 {
unsafe fn as_u32() -> u32;
}
impl ipv4_rep: as_unsafe_u32 {
impl Ipv4Rep: AsUnsafeU32 {
// this is pretty dastardly, i know
unsafe fn as_u32() -> u32 {
*((ptr::addr_of(self)) as *u32)
}
}
fn parse_to_ipv4_rep(ip: ~str) -> result::Result<ipv4_rep, ~str> {
fn parse_to_ipv4_rep(ip: ~str) -> result::Result<Ipv4Rep, ~str> {
let parts = vec::map(str::split_char(ip, '.'), |s| {
match uint::from_str(s) {
Some(n) if n <= 255u => n,
......@@ -171,7 +171,7 @@ fn parse_to_ipv4_rep(ip: ~str) -> result::Result<ipv4_rep, ~str> {
c: parts[2] as u8, d: parts[3] as u8})
}
}
fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
fn try_parse_addr(ip: ~str) -> result::Result<IpAddr,ParseAddrErr> {
unsafe {
let INADDR_NONE = ll::get_INADDR_NONE();
let ip_rep_result = parse_to_ipv4_rep(ip);
......@@ -198,7 +198,7 @@ fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
{err_msg: ~"uv_ip4_name produced invalid result."})
}
else {
result::Ok(ipv4(copy(new_addr)))
result::Ok(Ipv4(copy(new_addr)))
}
}
}
......@@ -219,13 +219,13 @@ mod v6 {
*
* * an `ip_addr` of the `ipv6` variant
*/
fn parse_addr(ip: ~str) -> ip_addr {
fn parse_addr(ip: ~str) -> IpAddr {
match try_parse_addr(ip) {
result::Ok(addr) => copy(addr),
result::Err(err_data) => fail err_data.err_msg
}
}
fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
fn try_parse_addr(ip: ~str) -> result::Result<IpAddr,ParseAddrErr> {
unsafe {
// need to figure out how to establish a parse failure..
let new_addr = uv_ip6_addr(ip, 22);
......@@ -239,21 +239,21 @@ fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
ip)})
}
else {
result::Ok(ipv6(new_addr))
result::Ok(Ipv6(new_addr))
}
}
}
}
type get_addr_data = {
output_ch: comm::Chan<result::Result<~[ip_addr],ip_get_addr_err>>
type GetAddrData = {
output_ch: comm::Chan<result::Result<~[IpAddr],IpGetAddrErr>>
};
extern fn get_addr_cb(handle: *uv_getaddrinfo_t, status: libc::c_int,
res: *addrinfo) unsafe {
log(debug, ~"in get_addr_cb");
let handle_data = get_data_for_req(handle) as
*get_addr_data;
*GetAddrData;
if status == 0i32 {
if res != (ptr::null::<addrinfo>()) {
let mut out_vec = ~[];
......@@ -261,18 +261,18 @@ fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
let mut curr_addr = res;
loop {
let new_ip_addr = if ll::is_ipv4_addrinfo(curr_addr) {
ipv4(copy((
Ipv4(copy((
*ll::addrinfo_as_sockaddr_in(curr_addr))))
}
else if ll::is_ipv6_addrinfo(curr_addr) {
ipv6(copy((
Ipv6(copy((
*ll::addrinfo_as_sockaddr_in6(curr_addr))))
}
else {
log(debug, ~"curr_addr is not of family AF_INET or "+
~"AF_INET6. Error.");
(*handle_data).output_ch.send(
result::Err(get_addr_unknown_error));
result::Err(GetAddrUnknownError));
break;
};
out_vec += ~[new_ip_addr];
......@@ -294,13 +294,13 @@ fn try_parse_addr(ip: ~str) -> result::Result<ip_addr,parse_addr_err> {
else {
log(debug, ~"addrinfo pointer is NULL");
(*handle_data).output_ch.send(
result::Err(get_addr_unknown_error));
result::Err(GetAddrUnknownError));
}
}
else {
log(debug, ~"status != 0 error in get_addr_cb");
(*handle_data).output_ch.send(
result::Err(get_addr_unknown_error));
result::Err(GetAddrUnknownError));
}
if res != (ptr::null::<addrinfo>()) {
uv_freeaddrinfo(res);
......@@ -365,8 +365,8 @@ fn test_ip_get_addr() {
localhost_name, vec::len(results)));
for vec::each(results) |r| {
let ipv_prefix = match r {
ipv4(_) => ~"IPv4",
ipv6(_) => ~"IPv6"
Ipv4(_) => ~"IPv4",
Ipv6(_) => ~"IPv6"
};
log(debug, fmt!("test_get_addr: result %s: '%s'",
ipv_prefix, format_addr(r)));
......
此差异已折叠。
......@@ -6,7 +6,7 @@
import io::{Reader, ReaderUtil};
import dvec::DVec;
export url, userinfo, query;
export Url, userinfo, query;
export from_str, to_str;
export get_scheme;
......@@ -14,31 +14,31 @@
export encode_component, decode_component;
export encode_form_urlencoded, decode_form_urlencoded;
type url = {
type Url = {
scheme: ~str,
user: Option<userinfo>,
user: Option<UserInfo>,
host: ~str,
port: Option<~str>,
path: ~str,
query: query,
query: Query,
fragment: Option<~str>
};
type userinfo = {
type UserInfo = {
user: ~str,
pass: Option<~str>
};
type query = ~[(~str, ~str)];
type Query = ~[(~str, ~str)];
fn url(-scheme: ~str, -user: Option<userinfo>, -host: ~str,
-port: Option<~str>, -path: ~str, -query: query,
-fragment: Option<~str>) -> url {
fn url(-scheme: ~str, -user: Option<UserInfo>, -host: ~str,
-port: Option<~str>, -path: ~str, -query: Query,
-fragment: Option<~str>) -> Url {
{ scheme: scheme, user: user, host: host, port: port,
path: path, query: query, fragment: fragment }
}
fn userinfo(-user: ~str, -pass: Option<~str>) -> userinfo {
fn userinfo(-user: ~str, -pass: Option<~str>) -> UserInfo {
{user: user, pass: pass}
}
......@@ -290,7 +290,7 @@ fn split_char_first(s: ~str, c: char) -> (~str, ~str) {
}
}
fn userinfo_from_str(uinfo: ~str) -> userinfo {
fn userinfo_from_str(uinfo: ~str) -> UserInfo {
let (user, p) = split_char_first(uinfo, ':');
let pass = if str::len(p) == 0 {
option::None
......@@ -300,7 +300,7 @@ fn userinfo_from_str(uinfo: ~str) -> userinfo {
return userinfo(user, pass);
}
fn userinfo_to_str(-userinfo: userinfo) -> ~str {
fn userinfo_to_str(-userinfo: UserInfo) -> ~str {
if option::is_some(userinfo.pass) {
return str::concat(~[copy userinfo.user, ~":",
option::unwrap(copy userinfo.pass),
......@@ -310,14 +310,14 @@ fn userinfo_to_str(-userinfo: userinfo) -> ~str {
}
}
impl userinfo : Eq {
pure fn eq(&&other: userinfo) -> bool {
impl UserInfo : Eq {
pure fn eq(&&other: UserInfo) -> bool {
self.user == other.user && self.pass == other.pass
}
}
fn query_from_str(rawquery: ~str) -> query {
let mut query: query = ~[];
fn query_from_str(rawquery: ~str) -> Query {
let mut query: Query = ~[];
if str::len(rawquery) != 0 {
for str::split_char(rawquery, '&').each |p| {
let (k, v) = split_char_first(p, '=');
......@@ -327,7 +327,7 @@ fn query_from_str(rawquery: ~str) -> query {
return query;
}
fn query_to_str(query: query) -> ~str {
fn query_to_str(query: Query) -> ~str {
let mut strvec = ~[];
for query.each |kv| {
let (k, v) = copy kv;
......@@ -363,47 +363,47 @@ fn get_scheme(rawurl: ~str) -> result::Result<(~str, ~str), @~str> {
return result::Err(@~"url: Scheme must be terminated with a colon.");
}
enum input {
digit, // all digits
hex, // digits and letters a-f
unreserved // all other legal characters
enum Input {
Digit, // all digits
Hex, // digits and letters a-f
Unreserved // all other legal characters
}
impl input: Eq {
pure fn eq(&&other: input) -> bool {
impl Input: Eq {
pure fn eq(&&other: Input) -> bool {
match (self, other) {
(digit, digit) => true,
(hex, hex) => true,
(unreserved, unreserved) => true,
(digit, _) => false,
(hex, _) => false,
(unreserved, _) => false
(Digit, Digit) => true,
(Hex, Hex) => true,
(Unreserved, Unreserved) => true,
(Digit, _) => false,
(Hex, _) => false,
(Unreserved, _) => false
}
}
}
// returns userinfo, host, port, and unparsed part, or an error
fn get_authority(rawurl: ~str) ->
result::Result<(Option<userinfo>, ~str, Option<~str>, ~str), @~str> {
result::Result<(Option<UserInfo>, ~str, Option<~str>, ~str), @~str> {
if !str::starts_with(rawurl, ~"//") {
// there is no authority.
return result::Ok((option::None, ~"", option::None, copy rawurl));
}
enum state {
start, // starting state
pass_host_port, // could be in user or port
ip6_port, // either in ipv6 host or port
ip6_host, // are in an ipv6 host
in_host, // are in a host - may be ipv6, but don't know yet
in_port // are in port
enum State {
Start, // starting state
PassHostPort, // could be in user or port
Ip6Port, // either in ipv6 host or port
Ip6Host, // are in an ipv6 host
InHost, // are in a host - may be ipv6, but don't know yet
InPort // are in port
}
let len = str::len(rawurl);
let mut st : state = start;
let mut in : input = digit; // most restricted, start here.
let mut st : State = Start;
let mut in : Input = Digit; // most restricted, start here.
let mut userinfo : Option<userinfo> = option::None;
let mut userinfo : Option<UserInfo> = option::None;
let mut host : ~str = ~"";
let mut port : option::Option<~str> = option::None;
......@@ -417,13 +417,13 @@ enum state {
match c {
'0' to '9' => (),
'A' to 'F' | 'a' to 'f' => {
if in == digit {
in = hex;
if in == Digit {
in = Hex;
}
}
'G' to 'Z' | 'g' to 'z' | '-' | '.' | '_' | '~' | '%' |
'&' |'\'' | '(' | ')' | '+' | '!' | '*' | ',' | ';' | '=' => {
in = unreserved;
in = Unreserved;
}
':' | '@' | '?' | '#' | '/' => {
// separators, don't change anything
......@@ -438,62 +438,62 @@ enum state {
':' => {
colon_count += 1;
match st {
start => {
Start => {
pos = i;
st = pass_host_port;
st = PassHostPort;
}
pass_host_port => {
PassHostPort => {
// multiple colons means ipv6 address.
if in == unreserved {
if in == Unreserved {
return result::Err(
@~"Illegal characters in IPv6 address.");
}
st = ip6_host;
st = Ip6Host;
}
in_host => {
InHost => {
pos = i;
// can't be sure whether this is an ipv6 address or a port
if in == unreserved {
if in == Unreserved {
return result::Err(@~"Illegal characters in authority.");
}
st = ip6_port;
st = Ip6Port;
}
ip6_port => {
if in == unreserved {
Ip6Port => {
if in == Unreserved {
return result::Err(@~"Illegal characters in authority.");
}
st = ip6_host;
st = Ip6Host;
}
ip6_host => {
Ip6Host => {
if colon_count > 7 {
host = str::slice(rawurl, begin, i);
pos = i;
st = in_port;
st = InPort;
}
}
_ => {
return result::Err(@~"Invalid ':' in authority.");
}
}
in = digit; // reset input class
in = Digit; // reset input class
}
'@' => {
in = digit; // reset input class
in = Digit; // reset input class
colon_count = 0; // reset count
match st {
start => {
Start => {
let user = str::slice(rawurl, begin, i);
userinfo = option::Some({user : user,
pass: option::None});
st = in_host;
st = InHost;
}
pass_host_port => {
PassHostPort => {
let user = str::slice(rawurl, begin, pos);
let pass = str::slice(rawurl, pos+1, i);
userinfo = option::Some({user: user,
pass: option::Some(pass)});
st = in_host;
st = InHost;
}
_ => {
return result::Err(@~"Invalid '@' in authority.");
......@@ -520,25 +520,25 @@ enum state {
// finish up
match st {
start => {
Start => {
if host_is_end_plus_one() {
host = str::slice(rawurl, begin, end+1);
} else {
host = str::slice(rawurl, begin, end);
}
}
pass_host_port | ip6_port => {
if in != digit {
PassHostPort | Ip6Port => {
if in != Digit {
return result::Err(@~"Non-digit characters in port.");
}
host = str::slice(rawurl, begin, pos);
port = option::Some(str::slice(rawurl, pos+1, end));
}
ip6_host | in_host => {
Ip6Host | InHost => {
host = str::slice(rawurl, begin, end);
}
in_port => {
if in != digit {
InPort => {
if in != Digit {
return result::Err(@~"Non-digit characters in port.");
}
port = option::Some(str::slice(rawurl, pos+1, end));
......@@ -584,7 +584,7 @@ fn get_path(rawurl: ~str, authority : bool) ->
// returns the parsed query and the fragment, if present
fn get_query_fragment(rawurl: ~str) ->
result::Result<(query, Option<~str>), @~str> {
result::Result<(Query, Option<~str>), @~str> {
if !str::starts_with(rawurl, ~"?") {
if str::starts_with(rawurl, ~"#") {
let f = decode_component(str::slice(rawurl,
......@@ -615,7 +615,7 @@ fn get_query_fragment(rawurl: ~str) ->
*
*/
fn from_str(rawurl: ~str) -> result::Result<url, ~str> {
fn from_str(rawurl: ~str) -> result::Result<Url, ~str> {
// scheme
let mut schm = get_scheme(rawurl);
if result::is_err(schm) {
......@@ -664,7 +664,7 @@ fn from_str(rawurl: ~str) -> result::Result<url, ~str> {
* result in just "http://somehost.com".
*
*/
fn to_str(url: url) -> ~str {
fn to_str(url: Url) -> ~str {
let user = if option::is_some(url.user) {
userinfo_to_str(option::unwrap(copy url.user))
} else {
......@@ -695,7 +695,7 @@ fn to_str(url: url) -> ~str {
fragment]);
}
impl url: to_str::ToStr {
impl Url: to_str::ToStr {
fn to_str() -> ~str {
to_str(self)
}
......
......@@ -30,9 +30,13 @@ export cell;
// General io and system-services modules
#[warn(non_camel_case_types)]
mod net;
#[warn(non_camel_case_types)]
mod net_ip;
#[warn(non_camel_case_types)]
mod net_tcp;
#[warn(non_camel_case_types)]
mod net_url;
// libuv modules
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册