summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/modules/LDAPURLParser.jsm
blob: 2c19be1386b692e7625c07f04f9591fa043d0cce (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = ["LDAPURLParser"];

/**
 * @implements {nsILDAPURLParser}
 */
class LDAPURLParser {
  QueryInterface = ChromeUtils.generateQI(["nsILDAPURLParser"]);

  parse(spec) {
    // The url is in the form of scheme://hostport/dn?attributes?scope?filter,
    // see RFC2255.
    let matches =
      /^(ldaps?):\/\/\[?([^\s\]/]+)\]?:?(\d*)\/([^\s?]*)\??(.*)$/.exec(spec);
    if (!matches) {
      throw Components.Exception(
        `Invalid LDAP URL: ${spec}`,
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }
    let [, scheme, host, port, dn, query] = matches;
    let [attributes, scopeString, filter] = query.split("?");
    let scope =
      {
        one: Ci.nsILDAPURL.SCOPE_ONELEVEL,
        sub: Ci.nsILDAPURL.SCOPE_SUBTREE,
      }[scopeString] || Ci.nsILDAPURL.SCOPE_BASE;
    return {
      QueryInterface: ChromeUtils.generateQI(["nsILDAPURLParserResult"]),
      host,
      port,
      dn: decodeURIComponent(dn),
      attributes,
      scope,
      filter: filter ? decodeURIComponent(filter) : "(objectclass=*)",
      options: scheme == "ldaps" ? Ci.nsILDAPURL.OPT_SECURE : 0,
    };
  }
}