1// Return a single-proxy result, which encodes ALL the arguments that were
2// passed to FindProxyForURL().
3
4function FindProxyForURL(url, host) {
5  if (arguments.length != 2) {
6    throw "Wrong number of arguments passed to FindProxyForURL!";
7    return "FAIL";
8  }
9
10  return "PROXY " + makePseudoHost(url + "." + host);
11}
12
13// Form a string that kind-of resembles a host. We will replace any
14// non-alphanumeric character with a dot, then fix up the oddly placed dots.
15function makePseudoHost(str) {
16  var result = "";
17
18  for (var i = 0; i < str.length; ++i) {
19    var c = str.charAt(i);
20    if (!isValidPseudoHostChar(c)) {
21      c = '.';  // Replace unsupported characters with a dot.
22    }
23
24    // Take care not to place multiple adjacent dots,
25    // a dot at the beginning, or a dot at the end.
26    if (c == '.' &&
27        (result.length == 0 ||
28         i == str.length - 1 ||
29         result.charAt(result.length - 1) == '.')) {
30      continue;
31    }
32    result += c;
33  }
34  return result;
35}
36
37function isValidPseudoHostChar(c) {
38  if (c >= '0' && c <= '9')
39    return true;
40  if (c >= 'a' && c <= 'z')
41    return true;
42  if (c >= 'A' && c <= 'Z')
43    return true;
44  return false;
45}
46