michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: michael@0: // A helper class that knows how to parse from and serialize to michael@0: // protocol4. This is a simple, historical format used by some Google michael@0: // interfaces, for example the Toolbar (i.e., ancient services). michael@0: // michael@0: // Protocol4 consists of a newline-separated sequence of name/value michael@0: // pairs (strings). Each line consists of the name, the value length, michael@0: // and the value itself, all separated by colons. Example: michael@0: // michael@0: // foo:6:barbaz\n michael@0: // fritz:33:issickofdynamicallytypedlanguages\n michael@0: michael@0: michael@0: /** michael@0: * This class knows how to serialize/deserialize maps to/from their michael@0: * protocol4 representation. michael@0: * michael@0: * @constructor michael@0: */ michael@0: function G_Protocol4Parser() { michael@0: this.debugZone = "protocol4"; michael@0: michael@0: this.protocol4RegExp_ = new RegExp("([^:]+):\\d+:(.*)$"); michael@0: this.newlineRegExp_ = new RegExp("(\\r)?\\n"); michael@0: } michael@0: michael@0: /** michael@0: * Create a map from a protocol4 string. Silently skips invalid lines. michael@0: * michael@0: * @param text String holding the protocol4 representation michael@0: * michael@0: * @returns Object as an associative array with keys and values michael@0: * given in text. The empty object is returned if none michael@0: * are parsed. michael@0: */ michael@0: G_Protocol4Parser.prototype.parse = function(text) { michael@0: michael@0: var response = {}; michael@0: if (!text) michael@0: return response; michael@0: michael@0: // Responses are protocol4: (repeated) name:numcontentbytes:content\n michael@0: var lines = text.split(this.newlineRegExp_); michael@0: for (var i = 0; i < lines.length; i++) michael@0: if (this.protocol4RegExp_.exec(lines[i])) michael@0: response[RegExp.$1] = RegExp.$2; michael@0: michael@0: return response; michael@0: } michael@0: michael@0: /** michael@0: * Create a protocol4 string from a map (object). Throws an error on michael@0: * an invalid input. michael@0: * michael@0: * @param map Object as an associative array with keys and values michael@0: * given as strings. michael@0: * michael@0: * @returns text String holding the protocol4 representation michael@0: */ michael@0: G_Protocol4Parser.prototype.serialize = function(map) { michael@0: if (typeof map != "object") michael@0: throw new Error("map must be an object"); michael@0: michael@0: var text = ""; michael@0: for (var key in map) { michael@0: if (typeof map[key] != "string") michael@0: throw new Error("Keys and values must be strings"); michael@0: michael@0: text += key + ":" + map[key].length + ":" + map[key] + "\n"; michael@0: } michael@0: michael@0: return text; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: /** michael@0: * Cheesey unittests michael@0: */ michael@0: function TEST_G_Protocol4Parser() { michael@0: if (G_GDEBUG) { michael@0: var z = "protocol4 UNITTEST"; michael@0: G_debugService.enableZone(z); michael@0: michael@0: G_Debug(z, "Starting"); michael@0: michael@0: var p = new G_Protocol4Parser(); michael@0: michael@0: function isEmpty(map) { michael@0: for (var key in map) michael@0: return false; michael@0: return true; michael@0: }; michael@0: michael@0: G_Assert(z, isEmpty(p.parse(null)), "Parsing null broken"); michael@0: G_Assert(z, isEmpty(p.parse("")), "Parsing nothing broken"); michael@0: michael@0: var t = "foo:3:bar"; michael@0: G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing one line broken"); michael@0: michael@0: t = "foo:3:bar\n"; michael@0: G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing line with lf broken"); michael@0: michael@0: t = "foo:3:bar\r\n"; michael@0: G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing with crlf broken"); michael@0: michael@0: michael@0: t = "foo:3:bar\nbar:3:baz\r\nbom:3:yaz\n"; michael@0: G_Assert(z, p.parse(t)["foo"] === "bar", "First in multiline"); michael@0: G_Assert(z, p.parse(t)["bar"] === "baz", "Second in multiline"); michael@0: G_Assert(z, p.parse(t)["bom"] === "yaz", "Third in multiline"); michael@0: G_Assert(z, p.parse(t)[""] === undefined, "Nonexistent in multiline"); michael@0: michael@0: // Test serialization michael@0: michael@0: var original = { michael@0: "1": "1", michael@0: "2": "2", michael@0: "foobar": "baz", michael@0: "hello there": "how are you?" , michael@0: }; michael@0: var deserialized = p.parse(p.serialize(original)); michael@0: for (var key in original) michael@0: G_Assert(z, original[key] === deserialized[key], michael@0: "Trouble (de)serializing " + key); michael@0: michael@0: G_Debug(z, "PASSED"); michael@0: } michael@0: } michael@0: #endif