From de0774edeb86c34afd55b5c83007e334ae213ff7 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Sat, 10 May 2014 16:22:40 +0300 Subject: [PATCH] Added config option (IGNORE_XML_NAMESPACES) to make XML traversal using element local names only as a workaroud for XML namespace issues in some devices. Fixes #7. --- lib/config.coffee | 1 + lib/soap.coffee | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/config.coffee b/lib/config.coffee index fdd646e..ddb823e 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -18,6 +18,7 @@ exports.LOG_INFORMS = false exports.DEBUG_DEVICES = {} # {'202BC1-BM632w-8KA8WA1151100043' : true} exports.DEVICE_ONLINE_THRESHOLD = 4000 exports.RETRY_DELAY = 300 +exports.IGNORE_XML_NAMESPACES = false # Traverse XML using element's local name only exports.MONGODB_OPTIONS = { db : { diff --git a/lib/soap.coffee b/lib/soap.coffee index 496fdcf..0a222ce 100644 --- a/lib/soap.coffee +++ b/lib/soap.coffee @@ -1,3 +1,4 @@ +config = require './config' libxmljs = require 'libxmljs' SERVER_NAME = "GenieACS/#{require('../package.json').version}" @@ -41,6 +42,16 @@ NAMESPACES = { } +# Workaround for devices that don't use correct namespace declarations or prefixes +if config.IGNORE_XML_NAMESPACES + libxmljs.Element.prototype.__find = libxmljs.Element.prototype.find + libxmljs.Element.prototype.find = (xpath, namespaces) -> + # Modify xpath queries to work regardless of element's namespace + p = xpath.replace(/([^\/:]+:)?([^\/]+)(\/|$)/g, '$2$3') \ + .replace(/([a-zA-Z0-9_-]+)([^\/]*)(\/|$)/g, "*[local-name()='$1']$2$3") + libxmljs.Element.prototype.__find.call(this, p, namespaces) + + # Separation by comma is important as some devices don't comform to standard COOKIE_REGEX = /\s*([a-zA-Z0-9\-_]+?)\s*=\s*"?([a-zA-Z0-9\-_]*?)"?\s*(,|;|$)/g