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