Added config options to be passed to libxmljs when parsing request XML. Fixes #15.

This commit is contained in:
Zaid Abdulla committed 2014-06-13 13:01:16 +03:00
1 parent e725ba5be7
commit 82b02ff175
4 files changed
+16 -25

No files matched your search

+1
View File
@@ -19,6 +19,7 @@ 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.LIBXMLJS_OPTIONS = {} # Options passed directly to libxmljs
exports.MONGODB_OPTIONS = {
db : {
+7 -11
View File
@@ -25,16 +25,14 @@ holdUntil = Date.now()
writeResponse = (currentRequest, res) ->
data = new Buffer(res.data)
if config.DEBUG_DEVICES[currentRequest.deviceId]
dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{data}\n\n"
dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{res.data}\n\n"
fs = require('fs').appendFile("../debug/#{currentRequest.deviceId}.dump", dump, (err) ->
throw err if err
)
# respond using the same content-encoding as the request
if currentRequest.httpRequest.headers['content-encoding']? and data.length > 0
if currentRequest.httpRequest.headers['content-encoding']? and res.data.length > 0
switch currentRequest.httpRequest.headers['content-encoding']
when 'gzip'
res.headers['Content-Encoding'] = 'gzip'
@@ -44,15 +42,15 @@ writeResponse = (currentRequest, res) ->
compress = zlib.deflate
if compress?
compress(data, (err, data) ->
compress(res.data, (err, data) ->
res.headers['Content-Length'] = data.length
currentRequest.httpResponse.writeHead(res.code, res.headers)
currentRequest.httpResponse.end(data)
)
else
res.headers['Content-Length'] = data.length
res.headers['Content-Length'] = res.data.length
currentRequest.httpResponse.writeHead(res.code, res.headers)
currentRequest.httpResponse.end(data)
currentRequest.httpResponse.end(res.data)
updateDevice = (currentRequest, actions, callback) ->
@@ -428,7 +426,7 @@ listener = (httpRequest, httpResponse) ->
bytes += chunk.length
)
httpRequest.getBody = (encoding) ->
httpRequest.getBody = () ->
# Write all chunks into a Buffer
body = new Buffer(bytes)
offset = 0
@@ -436,9 +434,7 @@ listener = (httpRequest, httpResponse) ->
chunk.copy(body, offset, 0, chunk.length)
offset += chunk.length
)
#Return encoded (default to UTF8) string
return body.toString(encoding || 'utf8', 0, body.byteLength)
return body
stream.on('end', () ->
cwmpRequest = soap.request(httpRequest)
+4 -6
View File
@@ -48,7 +48,7 @@ else
chunks.push(chunk)
bytes += chunk.length
request.getBody = (encoding) ->
request.getBody = () ->
# Write all chunks into a Buffer
body = new Buffer(bytes)
offset = 0
@@ -56,9 +56,7 @@ else
chunk.copy(body, offset, 0, chunk.length)
offset += chunk.length
)
# Return encoded (default to UTF8) string
return body.toString(encoding || 'utf8', 0, body.byteLength)
return body
request.addListener 'end', () ->
body = request.getBody()
@@ -161,7 +159,7 @@ else
else if DEVICE_TASKS_REGEX.test(urlParts.pathname)
if request.method == 'POST'
deviceId = querystring.unescape(DEVICE_TASKS_REGEX.exec(urlParts.pathname)[1])
if body
if body.length
task = JSON.parse(body)
task.device = deviceId
db.getAliases((aliases) ->
@@ -264,7 +262,7 @@ else
gs = new mongodb.GridStore(db.mongoDb, filename, 'w', {metadata : metadata})
gs.open((err, gs) ->
gs.write(request.getBody('binary'), (err, res) ->
gs.write(body, (err, res) ->
throw err if err
gs.close((err) ->
throw err if err
+4 -8
View File
@@ -272,11 +272,7 @@ exports.request = (httpRequest) ->
data = httpRequest.getBody()
if data.length > 0
try
xml = libxmljs.parseXml data
catch err
# some devices send invalid utf8 characters
xml = libxmljs.parseXml httpRequest.getBody('binary')
xml = libxmljs.parseXml(data, config.LIBXMLJS_OPTIONS)
if not cwmpRequest.cwmpVersion?
# cwmpVersion doesn't exist in cookies, thus it's an inform request
@@ -360,7 +356,6 @@ createSoapEnv = (cwmpVersion) ->
exports.response = (cwmpResponse) ->
headers = {
'Content-Type' : 'text/xml; charset="utf-8"',
'Server' : SERVER_NAME,
'SOAPServer' : SERVER_NAME,
}
@@ -412,6 +407,7 @@ exports.response = (cwmpResponse) ->
headers['Set-Cookie'] = cookiesToStr(cwmpResponse.cookies)
if env?
return {code: 200, headers: headers, data: env.doc().toString()}
headers['Content-Type'] = 'text/xml; charset="utf-8"'
return {code: 200, headers: headers, data: new Buffer(env.doc().toString(false))}
else
return {code: 204, headers: headers, data: ''}
return {code: 204, headers: headers, data: new Buffer(0)}