toolkit/components/url-classifier/content/moz/protocol4.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/url-classifier/content/moz/protocol4.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +
     1.9 +// A helper class that knows how to parse from and serialize to
    1.10 +// protocol4. This is a simple, historical format used by some Google
    1.11 +// interfaces, for example the Toolbar (i.e., ancient services).
    1.12 +//
    1.13 +// Protocol4 consists of a newline-separated sequence of name/value
    1.14 +// pairs (strings). Each line consists of the name, the value length,
    1.15 +// and the value itself, all separated by colons. Example:
    1.16 +//
    1.17 +// foo:6:barbaz\n
    1.18 +// fritz:33:issickofdynamicallytypedlanguages\n
    1.19 +
    1.20 +
    1.21 +/**
    1.22 + * This class knows how to serialize/deserialize maps to/from their
    1.23 + * protocol4 representation.
    1.24 + *
    1.25 + * @constructor
    1.26 + */
    1.27 +function G_Protocol4Parser() {
    1.28 +  this.debugZone = "protocol4";
    1.29 +
    1.30 +  this.protocol4RegExp_ = new RegExp("([^:]+):\\d+:(.*)$");
    1.31 +  this.newlineRegExp_ = new RegExp("(\\r)?\\n");
    1.32 +}
    1.33 +
    1.34 +/**
    1.35 + * Create a map from a protocol4 string. Silently skips invalid lines.
    1.36 + *
    1.37 + * @param text String holding the protocol4 representation
    1.38 + * 
    1.39 + * @returns Object as an associative array with keys and values 
    1.40 + *          given in text. The empty object is returned if none
    1.41 + *          are parsed.
    1.42 + */
    1.43 +G_Protocol4Parser.prototype.parse = function(text) {
    1.44 +
    1.45 +  var response = {};
    1.46 +  if (!text)
    1.47 +    return response;
    1.48 +
    1.49 +  // Responses are protocol4: (repeated) name:numcontentbytes:content\n
    1.50 +  var lines = text.split(this.newlineRegExp_);
    1.51 +  for (var i = 0; i < lines.length; i++)
    1.52 +    if (this.protocol4RegExp_.exec(lines[i]))
    1.53 +      response[RegExp.$1] = RegExp.$2;
    1.54 +
    1.55 +  return response;
    1.56 +}
    1.57 +
    1.58 +/**
    1.59 + * Create a protocol4 string from a map (object). Throws an error on 
    1.60 + * an invalid input.
    1.61 + *
    1.62 + * @param map Object as an associative array with keys and values 
    1.63 + *            given as strings.
    1.64 + *
    1.65 + * @returns text String holding the protocol4 representation
    1.66 + */
    1.67 +G_Protocol4Parser.prototype.serialize = function(map) {
    1.68 +  if (typeof map != "object")
    1.69 +    throw new Error("map must be an object");
    1.70 +
    1.71 +  var text = "";
    1.72 +  for (var key in map) {
    1.73 +    if (typeof map[key] != "string")
    1.74 +      throw new Error("Keys and values must be strings");
    1.75 +    
    1.76 +    text += key + ":" + map[key].length + ":" + map[key] + "\n";
    1.77 +  }
    1.78 +  
    1.79 +  return text;
    1.80 +}
    1.81 +
    1.82 +#ifdef DEBUG
    1.83 +/**
    1.84 + * Cheesey unittests
    1.85 + */
    1.86 +function TEST_G_Protocol4Parser() {
    1.87 +  if (G_GDEBUG) {
    1.88 +    var z = "protocol4 UNITTEST";
    1.89 +    G_debugService.enableZone(z);
    1.90 +
    1.91 +    G_Debug(z, "Starting");
    1.92 +
    1.93 +    var p = new G_Protocol4Parser();
    1.94 +    
    1.95 +    function isEmpty(map) {
    1.96 +      for (var key in map) 
    1.97 +        return false;
    1.98 +      return true;
    1.99 +    };
   1.100 +
   1.101 +    G_Assert(z, isEmpty(p.parse(null)), "Parsing null broken");
   1.102 +    G_Assert(z, isEmpty(p.parse("")), "Parsing nothing broken");
   1.103 +
   1.104 +    var t = "foo:3:bar";
   1.105 +    G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing one line broken");
   1.106 +
   1.107 +    t = "foo:3:bar\n";
   1.108 +    G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing line with lf broken");
   1.109 +
   1.110 +    t = "foo:3:bar\r\n";
   1.111 +    G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing with crlf broken");
   1.112 +
   1.113 +
   1.114 +    t = "foo:3:bar\nbar:3:baz\r\nbom:3:yaz\n";
   1.115 +    G_Assert(z, p.parse(t)["foo"] === "bar", "First in multiline");
   1.116 +    G_Assert(z, p.parse(t)["bar"] === "baz", "Second in multiline");
   1.117 +    G_Assert(z, p.parse(t)["bom"] === "yaz", "Third in multiline");
   1.118 +    G_Assert(z, p.parse(t)[""] === undefined, "Nonexistent in multiline");
   1.119 +    
   1.120 +    // Test serialization
   1.121 +
   1.122 +    var original = { 
   1.123 +      "1": "1", 
   1.124 +      "2": "2", 
   1.125 +      "foobar": "baz",
   1.126 +      "hello there": "how are you?" ,
   1.127 +    };
   1.128 +    var deserialized = p.parse(p.serialize(original));
   1.129 +    for (var key in original) 
   1.130 +      G_Assert(z, original[key] === deserialized[key], 
   1.131 +               "Trouble (de)serializing " + key);
   1.132 +
   1.133 +    G_Debug(z, "PASSED");  
   1.134 +  }
   1.135 +}
   1.136 +#endif

mercurial