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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: let WSP = {}; michael@0: Cu.import("resource://gre/modules/WspPduHelper.jsm", WSP); michael@0: let WBXML = {}; michael@0: Cu.import("resource://gre/modules/WbxmlPduHelper.jsm", WBXML); michael@0: michael@0: // set to true to see debug messages michael@0: let DEBUG = WBXML.DEBUG_ALL | false; michael@0: michael@0: /** michael@0: * Public identifier for SL michael@0: * michael@0: * @see http://technical.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.aspx michael@0: */ michael@0: const PUBLIC_IDENTIFIER_SL = "-//WAPFORUM//DTD SL 1.0//EN"; michael@0: michael@0: this.PduHelper = { michael@0: michael@0: /** michael@0: * @param data michael@0: * A wrapped object containing raw PDU data. michael@0: * @param contentType michael@0: * Content type of incoming SL message, should be "text/vnd.wap.sl" or michael@0: * "application/vnd.wap.slc". michael@0: * michael@0: * @return A message object containing attribute content and contentType. michael@0: * |content| will contain string of decoded SL message if successfully michael@0: * decoded, or raw data if failed. michael@0: * |contentType| will be string representing corresponding type of michael@0: * content. michael@0: */ michael@0: parse: function parse_sl(data, contentType) { michael@0: // We only need content and contentType michael@0: let msg = { michael@0: contentType: contentType michael@0: }; michael@0: michael@0: /** michael@0: * Message is compressed by WBXML, decode into string. michael@0: * michael@0: * @see WAP-192-WBXML-20010725-A michael@0: */ michael@0: if (contentType === "application/vnd.wap.slc") { michael@0: let appToken = { michael@0: publicId: PUBLIC_IDENTIFIER_SL, michael@0: tagTokenList: SL_TAG_FIELDS, michael@0: attrTokenList: SL_ATTRIBUTE_FIELDS, michael@0: valueTokenList: SL_VALUE_FIELDS, michael@0: globalTokenOverride: null michael@0: } michael@0: michael@0: try { michael@0: let parseResult = WBXML.PduHelper.parse(data, appToken); michael@0: msg.content = parseResult.content; michael@0: msg.contentType = "text/vnd.wap.sl"; michael@0: } catch (e) { michael@0: // Provide raw data if we failed to parse. michael@0: msg.content = data.array; michael@0: } michael@0: michael@0: return msg; michael@0: } michael@0: michael@0: /** michael@0: * Message is plain text, transform raw to string. michael@0: */ michael@0: try { michael@0: let stringData = WSP.Octet.decodeMultiple(data, data.array.length); michael@0: msg.content = WSP.PduHelper.decodeStringContent(stringData, "UTF-8"); michael@0: } catch (e) { michael@0: // Provide raw data if we failed to parse. michael@0: msg.content = data.array; michael@0: } michael@0: return msg; michael@0: michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Tag tokens michael@0: * michael@0: * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.1 michael@0: */ michael@0: const SL_TAG_FIELDS = (function () { michael@0: let names = {}; michael@0: function add(name, codepage, number) { michael@0: let entry = { michael@0: name: name, michael@0: number: number, michael@0: }; michael@0: if (!names[codepage]) { michael@0: names[codepage] = {}; michael@0: } michael@0: names[codepage][number] = entry; michael@0: } michael@0: michael@0: add("sl", 0, 0x05); michael@0: michael@0: return names; michael@0: })(); michael@0: michael@0: /** michael@0: * Attribute Tokens michael@0: * michael@0: * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.2 michael@0: */ michael@0: const SL_ATTRIBUTE_FIELDS = (function () { michael@0: let names = {}; michael@0: function add(name, value, codepage, number) { michael@0: let entry = { michael@0: name: name, michael@0: value: value, michael@0: number: number, michael@0: }; michael@0: if (!names[codepage]) { michael@0: names[codepage] = {}; michael@0: } michael@0: names[codepage][number] = entry; michael@0: } michael@0: michael@0: add("action", "execute-low", 0, 0x05); michael@0: add("action", "execute-high", 0, 0x06); michael@0: add("action", "cache", 0, 0x07); michael@0: add("href", "", 0, 0x08); michael@0: add("href", "http://", 0, 0x09); michael@0: add("href", "http://www.", 0, 0x0A); michael@0: add("href", "https://", 0, 0x0B); michael@0: add("href", "https://www.", 0, 0x0C); michael@0: michael@0: return names; michael@0: })(); michael@0: michael@0: const SL_VALUE_FIELDS = (function () { michael@0: let names = {}; michael@0: function add(value, codepage, number) { michael@0: let entry = { michael@0: value: value, michael@0: number: number, michael@0: }; michael@0: if (!names[codepage]) { michael@0: names[codepage] = {}; michael@0: } michael@0: names[codepage][number] = entry; michael@0: } michael@0: michael@0: add(".com/", 0, 0x85); michael@0: add(".edu/", 0, 0x86); michael@0: add(".net/", 0, 0x87); michael@0: add(".org/", 0, 0x88); michael@0: michael@0: return names; michael@0: })(); michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: // Parser michael@0: "PduHelper", michael@0: ];