Support for gzip and deflate content encoding

This commit is contained in:
Zaid Abdulla committed 2014-05-05 01:14:41 +03:00
1 parent 4048de35d0
commit bf8fc877d4
2 files changed
+44 -15

No files matched your search

+42 -10
View File
@@ -14,6 +14,7 @@ apiFunctions = require './api-functions'
customCommands = require './custom-commands' customCommands = require './custom-commands'
crypto = require 'crypto' crypto = require 'crypto'
parameters = require './parameters' parameters = require './parameters'
zlib = require 'zlib'
OBJECT_REGEX = /\.$/ OBJECT_REGEX = /\.$/
INSTANCE_REGEX = /\.[\d]+\.$/ INSTANCE_REGEX = /\.[\d]+\.$/
@@ -24,20 +25,37 @@ holdUntil = Date.now()
writeResponse = (currentRequest, res) -> writeResponse = (currentRequest, res) ->
if res.headers['Content-Length'] == 0 data = new Buffer(res.data)
if data.length == 0
# no more requests. terminate TCP connection # no more requests. terminate TCP connection
res.headers['Connection'] = 'close' if currentRequest.httpRequest.httpVersion == '1.1' res.headers['Connection'] = 'close'
else
res.headers['Connection'] = 'Keep-Alive' if currentRequest.httpRequest.httpVersion == '1.0'
if config.DEBUG_DEVICES[currentRequest.deviceId] if config.DEBUG_DEVICES[currentRequest.deviceId]
dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{res.data}\n\n" dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{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
if currentRequest.httpRequest.headers['content-encoding']? and data.length > 0
switch currentRequest.httpRequest.headers['content-encoding']
when 'gzip'
res.headers['Content-Encoding'] = 'gzip'
compress = zlib.gzip
when 'deflate'
res.headers['Content-Encoding'] = 'deflate'
compress = zlib.deflate
if compress?
compress(data, (err, data) ->
res.headers['Content-Length'] = data.length
currentRequest.httpResponse.writeHead(res.code, res.headers) currentRequest.httpResponse.writeHead(res.code, res.headers)
currentRequest.httpResponse.end(res.data) currentRequest.httpResponse.end(data)
)
else
res.headers['Content-Length'] = data.length
currentRequest.httpResponse.writeHead(res.code, res.headers)
currentRequest.httpResponse.end(data)
updateDevice = (currentRequest, actions, callback) -> updateDevice = (currentRequest, actions, callback) ->
@@ -123,7 +141,7 @@ updateDevice = (currentRequest, actions, callback) ->
inform = (currentRequest, cwmpRequest) -> inform = (currentRequest, cwmpRequest) ->
# If overloaded, ask CPE to retry in 5 mins # If overloaded, ask CPE to retry in 5 mins
if Date.now() < holdUntil if Date.now() < holdUntil
res = {code : 503, headers : {'Content-Length' : 0, 'Retry-After' : 300}, data : ''} res = {code : 503, headers : {'Retry-After' : 300}, data : ''}
return writeResponse(currentRequest, res) return writeResponse(currentRequest, res)
now = new Date(Date.now()) now = new Date(Date.now())
@@ -389,13 +407,27 @@ listener = (httpRequest, httpResponse) ->
httpResponse.end('405 Method Not Allowed') httpResponse.end('405 Method Not Allowed')
return return
if httpRequest.headers['content-encoding']?
switch httpRequest.headers['content-encoding']
when 'gzip'
stream = httpRequest.pipe(zlib.createGunzip())
when 'deflate'
stream = httpRequest.pipe(zlib.createInflate())
else
httpResponse.writeHead(415)
httpResponse.end('415 Unsupported Media Type')
return
else
stream = httpRequest
chunks = [] chunks = []
bytes = 0 bytes = 0
cookies = {} cookies = {}
httpRequest.addListener 'data', (chunk) -> stream.on('data', (chunk) ->
chunks.push(chunk) chunks.push(chunk)
bytes += chunk.length bytes += chunk.length
)
httpRequest.getBody = (encoding) -> httpRequest.getBody = (encoding) ->
# Write all chunks into a Buffer # Write all chunks into a Buffer
@@ -409,7 +441,7 @@ listener = (httpRequest, httpResponse) ->
#Return encoded (default to UTF8) string #Return encoded (default to UTF8) string
return body.toString(encoding || 'utf8', 0, body.byteLength) return body.toString(encoding || 'utf8', 0, body.byteLength)
httpRequest.addListener 'end', () -> stream.on('end', () ->
cwmpRequest = soap.request(httpRequest) cwmpRequest = soap.request(httpRequest)
currentRequest = { currentRequest = {
@@ -530,7 +562,7 @@ listener = (httpRequest, httpResponse) ->
else else
# cpe sent empty response. start sending acs requests # cpe sent empty response. start sending acs requests
nextTask(currentRequest) nextTask(currentRequest)
)
cluster = require 'cluster' cluster = require 'cluster'
numCPUs = require('os').cpus().length numCPUs = require('os').cpus().length
+2 -5
View File
@@ -257,7 +257,7 @@ exports.request = (httpRequest) ->
data = httpRequest.getBody() data = httpRequest.getBody()
if +httpRequest.headers['Content-Length'] > 0 || data.length > 0 if data.length > 0
try try
xml = libxmljs.parseXml data xml = libxmljs.parseXml data
catch err catch err
@@ -398,9 +398,6 @@ exports.response = (cwmpResponse) ->
headers['Set-Cookie'] = cookiesToStr(cwmpResponse.cookies) headers['Set-Cookie'] = cookiesToStr(cwmpResponse.cookies)
if env? if env?
data = env.doc().toString() return {code: 200, headers: headers, data: env.doc().toString()}
headers['Content-Length'] = data.length
return {code: 200, headers: headers, data: data}
else else
headers['Content-Length'] = 0
return {code: 204, headers: headers, data: ''} return {code: 204, headers: headers, data: ''}