diff --git a/lib/config.coffee b/lib/config.coffee index ddb823e..a832897 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -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 : { diff --git a/lib/cwmp.coffee b/lib/cwmp.coffee index 84216ab..2b873f8 100644 --- a/lib/cwmp.coffee +++ b/lib/cwmp.coffee @@ -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) diff --git a/lib/nbi.coffee b/lib/nbi.coffee index bb39077..06f77f8 100644 --- a/lib/nbi.coffee +++ b/lib/nbi.coffee @@ -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 diff --git a/lib/soap.coffee b/lib/soap.coffee index b92962c..27ae3ba 100644 --- a/lib/soap.coffee +++ b/lib/soap.coffee @@ -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)}