diff --git a/lib/config.coffee b/lib/config.coffee index 78be512..cae2e4b 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -47,20 +47,19 @@ options = { LOG_INFORMS : {type : 'bool', default : true}, DEBUG : {type : 'bool', default : false}, RETRY_DELAY : {type : 'int', default : 300}, - IGNORE_XML_NAMESPACES : {type : 'bool', default : false}, SESSION_TIMEOUT : {type : 'int', default : 30}, GET_PARAMETER_NAMES_DEPTH_THRESHOLD : {type : 'int', default : 0}, TASK_PARAMETERS_BATCH_SIZE : {type : 'int', default : 32}, MAX_DEPTH : {type: 'int', default : 16}, COOKIES_PATH : {type : 'string'}, - # Libxml related configuration - XML_PARSE_RECOVER : {type : 'bool'}, - XML_PARSE_NOENT : {type : 'bool'}, - XML_PARSE_NOBLANKS : {type : 'bool'}, - XML_PARSE_NSCLEAN : {type : 'bool'}, - XML_PARSE_NOCDATA : {type : 'bool'}, - XML_PARSE_IGNORE_ENC : {type : 'bool'}, + # XML configuration + XML_RECOVER : {type : 'bool', default : false}, + XML_IGNORE_ENC : {type : 'bool', default : false}, + XML_FORMAT : {type : 'bool', default : false}, + XML_NO_DECL : {type : 'bool', default : false}, + XML_NO_EMPTY : {type : 'bool', default : false}, + XML_IGNORE_NAMESPACE : {type : 'bool', default : false}, # Should probably never be changed PRESETS_TIME_PADDING : {type : 'int', default : 1}, diff --git a/lib/soap.coffee b/lib/soap.coffee index 2848009..7a5503d 100644 --- a/lib/soap.coffee +++ b/lib/soap.coffee @@ -81,15 +81,24 @@ NAMESPACES = { } } -# Generate Libxmljs options -LIBXMLJS_OPTIONS = {} -for k, v of config.allConfig - if k.lastIndexOf('XML_PARSE_', 0) == 0 - LIBXMLJS_OPTIONS[k] = v +LIBXMLJS_PARSE_OPTIONS = { + 'nocdata' : true, + 'noent' : true, + 'recover' : config.get('XML_RECOVER'), + 'ignore_enc' : config.get('XML_IGNORE_ENC') +} + +LIBXMLJS_SAVE_OPTIONS = { + 'format' : config.get('XML_FORMAT'), + 'declaration' : not config.get('XML_NO_DECL'), + 'selfCloseEmpty' : not config.get('XML_NO_EMPTY') +} + +XML_IGNORE_NAMESPACE = config.get('XML_IGNORE_NAMESPACE') # Workaround for devices that don't use correct namespace declarations or prefixes -if config.get('IGNORE_XML_NAMESPACES') +if XML_IGNORE_NAMESPACE libxmljs.Element.prototype.__find = libxmljs.Element.prototype.find libxmljs.Element.prototype.find = (xpath, namespaces) -> # Modify xpath queries to work regardless of element's namespace @@ -319,7 +328,7 @@ exports.request = (httpRequest, cwmpVersion) -> data = httpRequest.getBody() if data.length > 0 - xml = libxmljs.parseXml(data, LIBXMLJS_OPTIONS) + xml = libxmljs.parseXml(data, LIBXMLJS_PARSE_OPTIONS) if not cwmpRequest.cwmpVersion? # cwmpVersion not passed, thus it's an inform request @@ -346,7 +355,7 @@ exports.request = (httpRequest, cwmpVersion) -> cwmpRequest.id = try xml.get('/soap-env:Envelope/soap-env:Header/cwmp:ID', NAMESPACES[cwmpRequest.cwmpVersion]).text() catch then null - if methodElement? and not (config.get('IGNORE_XML_NAMESPACES') and methodElement.name() is 'Fault') + if methodElement? and not (XML_IGNORE_NAMESPACE and methodElement.name() is 'Fault') switch methodElement.name() when 'Inform' cwmpRequest.methodRequest = acsInform(methodElement) @@ -450,6 +459,6 @@ exports.response = (cwmpResponse) -> if env? headers['Content-Type'] = 'text/xml; charset="utf-8"' - return {code: 200, headers: headers, data: new Buffer(env.doc().toString(false))} + return {code: 200, headers: headers, data: new Buffer(env.doc().toString(LIBXMLJS_SAVE_OPTIONS))} else return {code: 204, headers: headers, data: new Buffer(0)}