1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Connection string parsing and options.
use Result;
use Error::ArgumentError;
use std::ascii::AsciiExt;
use std::collections::BTreeMap;

pub const DEFAULT_PORT: u16 = 27017;
pub const URI_SCHEME: &'static str = "mongodb://";

/// Encapsulates the hostname and port of a host.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Host {
    /// The hostname for this host.
    pub host_name: String,
    /// The inter-process communication file path, if this is an IPC host.
    pub ipc: String,
    /// The port to communicate on.
    pub port: u16,
}

impl Host {
    // Creates a new Host struct.
    fn new(host_name: String, port: u16) -> Host {
        Host {
            host_name: host_name,
            port: port,
            ipc: String::new(),
        }
    }

    fn with_ipc(ipc: String) -> Host {
        Host {
            host_name: String::new(),
            port: DEFAULT_PORT,
            ipc: ipc,
        }
    }

    pub fn has_ipc(&self) -> bool {
        !self.ipc.is_empty()
    }
}

/// Encapsulates the options and read preference tags of a MongoDB connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectionOptions {
    pub options: BTreeMap<String, String>,
    pub read_pref_tags: Vec<String>,
}

impl ConnectionOptions {
    /// Creates a new ConnectionOptions struct.
    pub fn new(options: BTreeMap<String, String>, read_pref_tags: Vec<String>) -> ConnectionOptions {
        ConnectionOptions {
            options: options,
            read_pref_tags: read_pref_tags,
        }
    }

    // Helper method to retrieve an option from the map.
    pub fn get(&self, key: &str) -> Option<&String> {
        self.options.get(key)
    }
}

/// Encapsulates information for connection to a single MongoDB host or replicated set.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectionString {
    pub hosts: Vec<Host>,
    pub string: Option<String>,
    pub user: Option<String>,
    pub password: Option<String>,
    pub database: Option<String>,
    pub collection: Option<String>,
    pub options: Option<ConnectionOptions>,
}

impl ConnectionString {
    /// Creates a new ConnectionString for a single, unreplicated host.
    pub fn new(host_name: &str, port: u16) -> ConnectionString {
        let host = Host::new(host_name.to_owned(), port);
        ConnectionString::with_host(host)
    }

    fn with_host(host: Host) -> ConnectionString {
        ConnectionString {
            hosts: vec![host],
            string: None,
            user: None,
            password: None,
            database: Some("test".to_owned()),
            collection: None,
            options: None,
        }
    }
}

/// Parses a MongoDB connection string URI as defined by
/// [the manual](http://docs.mongodb.org/manual/reference/connection-string/).
pub fn parse(address: &str) -> Result<ConnectionString> {
    if !address.starts_with(URI_SCHEME) {
        return Err(ArgumentError("MongoDB connection string must start with 'mongodb://'.".to_owned()))
    }

    // Remove scheme
    let addr = &address[URI_SCHEME.len()..];

    let hosts: Vec<Host>;
    let mut user: Option<String> = None;
    let mut password: Option<String> = None;
    let mut database: Option<String> = Some("test".to_owned());
    let mut collection: Option<String> = None;
    let mut options: Option<ConnectionOptions> = None;

    // Split on host/path
    let (host_str, path_str) = if addr.contains(".sock") {
        // Partition ipc socket
        let (host_part, path_part) = rsplit(addr, ".sock");
        if path_part.starts_with("/") {
            (host_part, &path_part[1..])
        } else {
            (host_part, path_part)
        }
    } else {
        // Partition standard format
        partition(addr, "/")
    };

    if path_str.is_empty() && host_str.contains("?") {
        return Err(ArgumentError("A '/' is required between the host list and any options.".to_owned()));
    }

    // Split on authentication and hosts
    if host_str.contains("@") {
        let (user_info, host_string) = rpartition(host_str, "@");
        let (u,p) = try!(parse_user_info(user_info));
        user = Some(u.to_owned());
        password = Some(p.to_owned());
        hosts = try!(split_hosts(host_string));
    } else {
        hosts = try!(split_hosts(host_str));
    }

    let mut opts = "";

    // Split on database name, collection, and options
    if path_str.len() > 0 {
        if path_str.starts_with("?") {
            opts = &path_str[1..];
        } else {
            let (dbase, options) = partition(path_str, "?");
            let (dbase_new, coll) = partition(dbase, ".");
            database = Some(dbase_new.to_owned());
            collection = Some(coll.to_owned());
            opts = options;
        }
    }

    // Collect options if any exist
    if opts.len() > 0 {
        options = Some(split_options(opts).unwrap());
    }

    Ok(ConnectionString {
        hosts: hosts,
        string: Some(address.to_owned()),
        user: user,
        password: password,
        database: database,
        collection: collection,
        options: options,
    })
}

// Parse user information of the form user:password
fn parse_user_info(user_info: &str) -> Result<(&str, &str)> {
    let (user, password) = rpartition(user_info, ":");
    if user_info.contains("@") || user.contains(":") {
        return Err(ArgumentError("':' or '@' characters in a username or password must be escaped according to RFC 2396.".to_owned()))
    }
    if user.is_empty() {
        return Err(ArgumentError("The empty string is not a valid username.".to_owned()))
    }
    Ok((user, password))
}

// Parses a literal IPv6 literal host entity of the form [host] or [host]:port
fn parse_ipv6_literal_host(entity: &str) -> Result<Host> {
    match entity.find("]") {
        Some(_) => {
            match entity.find("]:") {
                Some(idx) => {
                    let port = &entity[idx+2..];
                    match port.parse::<u16>() {
                        Ok(val) => Ok(Host::new(entity[1..idx].to_ascii_lowercase(), val)),
                        Err(_) => Err(ArgumentError("Port must be an integer.".to_owned())),
                    }
                },
                None => Ok(Host::new(entity[1..].to_ascii_lowercase(), DEFAULT_PORT)),
            }
        },
        None => Err(ArgumentError("An IPv6 address must be enclosed in '[' and ']' according to RFC 2732.".to_owned())),
    }
}

// Parses a host entity of the form host or host:port, and redirects IPv6 entities.
// All host names are lowercased.
pub fn parse_host(entity: &str) -> Result<Host> {
    if entity.starts_with("[") {
        // IPv6 host
        parse_ipv6_literal_host(entity)
    } else if entity.contains(":") {
        // Common host:port format
        let (host, port) = partition(entity, ":");
        if port.contains(":") {
            return Err(ArgumentError("Reserved characters such as ':' must
                        be escaped according to RFC 2396. An IPv6 address literal
                        must be enclosed in '[' and according to RFC 2732.".to_owned()));
        }
        match port.parse::<u16>() {
            Ok(val) => Ok(Host::new(host.to_ascii_lowercase(), val)),
            Err(_) => Err(ArgumentError("Port must be an unsigned integer.".to_owned())),
        }
    } else if entity.contains(".sock") {
        // IPC socket
        Ok(Host::with_ipc(entity.to_ascii_lowercase()))
    } else {
        // Host with no port specified
        Ok(Host::new(entity.to_ascii_lowercase(), DEFAULT_PORT))
    }
}

// Splits and parses comma-separated hosts.
fn split_hosts(host_str: &str) -> Result<Vec<Host>> {
    let mut hosts: Vec<Host> = Vec::new();
    for entity in host_str.split(",") {
        if entity.is_empty() {
            return Err(ArgumentError("Empty host, or extra comma in host list.".to_owned()));
        }
        let host = try!(parse_host(entity));
        hosts.push(host);
    }
    Ok(hosts)
}

// Parses the delimited string into its options and Read Preference Tags.
fn parse_options(opts: &str, delim: Option<&str>) -> ConnectionOptions {
    let mut options: BTreeMap<String, String> = BTreeMap::new();
    let mut read_pref_tags: Vec<String> = Vec::new();

    // Split and collect options into a vec
    let opt_list = match delim {
        Some(delim) => opts.split(delim).collect(),
        None => vec!(opts)
    };

    // Build the map and tag vec
    for opt in opt_list {
        let (key, val) = partition(opt, "=");
        if key.to_ascii_lowercase() == "readpreferencetags" {
            read_pref_tags.push(val.to_owned());
        } else {
            options.insert(key.to_owned(), val.to_owned());
        }
    }

    ConnectionOptions::new(options, read_pref_tags)
}

// Determines the option delimiter and offloads parsing to parse_options.
fn split_options(opts: &str) -> Result<ConnectionOptions> {
    let and_idx = opts.find("&");
    let semi_idx = opts.find(";");
    let mut delim = None;

    if and_idx != None && semi_idx != None {
        return Err(ArgumentError("Cannot mix '&' and ';' for option separators.".to_owned()));
    } else if and_idx != None {
        delim = Some("&");
    } else if semi_idx != None {
        delim = Some(";");
    } else if opts.find("=") == None {
        return Err(ArgumentError("InvalidURI: MongoDB URI options are key=value pairs.".to_owned()));
    }
    let options = parse_options(opts, delim);
    Ok(options)
}

// Partitions a string around the left-most occurrence of the separator, if it exists.
fn partition<'a>(string: &'a str, sep: &str) -> (&'a str, &'a str) {
    match string.find(sep) {
        Some(idx) => (&string[..idx], &string[idx+sep.len()..]),
        None => (string, ""),
    }
}

// Partitions a string around the right-most occurrence of the separator, if it exists.
fn rpartition<'a>(string: &'a str, sep: &str) -> (&'a str, &'a str) {
    match string.rfind(sep) {
        Some(idx) => (&string[..idx], &string[idx+sep.len()..]),
        None => (string, ""),
    }
}

// Splits a string around the right-most occurrence of the separator, if it exists.
fn rsplit<'a>(string: &'a str, sep: &str) -> (&'a str, &'a str) {
    match string.rfind(sep) {
        Some(idx) => (&string[..idx+sep.len()], &string[idx+sep.len()..]),
        None => (string, ""),
    }
}