Fixed a couple of connection request issues.

This commit is contained in:
Zaid Abdulla committed 2014-07-12 23:24:13 +03:00
1 parent ec2dd87693
commit 2279de3c70
2 files changed
+16 -5

No files matched your search

+14 -3
View File
@@ -11,6 +11,17 @@ connectionRequest = (deviceId, callback) ->
# ensure socket is reused in case of digest authentication # ensure socket is reused in case of digest authentication
agent = new http.Agent({maxSockets : 1}) agent = new http.Agent({maxSockets : 1})
statusToError = (statusCode) ->
switch statusCode
when 200, 204
null
when 401
new Error('Incorrect connection request credentials')
when 0
new Error('Device is offline')
else
new Error("Unexpected response code from device: #{statusCode}")
conReq = (url, authString, callback) -> conReq = (url, authString, callback) ->
options = URL.parse(url) options = URL.parse(url)
options.agent = agent options.agent = agent
@@ -19,7 +30,7 @@ connectionRequest = (deviceId, callback) ->
options.headers = {'Authorization' : authString} options.headers = {'Authorization' : authString}
request = http.get(options, (res) -> request = http.get(options, (res) ->
if res.statusCode == 401 if res.statusCode == 401 and res.headers['www-authenticate']?
authHeader = auth.parseAuthHeader(res.headers['www-authenticate']) authHeader = auth.parseAuthHeader(res.headers['www-authenticate'])
callback(res.statusCode, authHeader) callback(res.statusCode, authHeader)
# don't need body, go ahead and emit events to free up the socket for possible reuse # don't need body, go ahead and emit events to free up the socket for possible reuse
@@ -56,10 +67,10 @@ connectionRequest = (deviceId, callback) ->
authString = auth.digest(username, password, uri.path, 'GET', null, authHeader) authString = auth.digest(username, password, uri.path, 'GET', null, authHeader)
conReq(connectionRequestUrl, authString, (statusCode, authHeader) -> conReq(connectionRequestUrl, authString, (statusCode, authHeader) ->
callback() callback(statusToError(statusCode))
) )
else else
callback() callback(statusToError(statusCode))
) )
) )
+2 -2
View File
@@ -7,11 +7,11 @@ exports.parseAuthHeader = (authHeader) ->
res['method'] = authHeader.slice(0, i) res['method'] = authHeader.slice(0, i)
options = authHeader.slice(i + 1) options = authHeader.slice(i + 1)
regex = /([a-z]+)="([a-zA-Z0-9\/\.@\-,= ]+)"/g regex = /([a-z]+)="([a-zA-Z0-9\/\.@\-\+,= ]+)"/g
while (r = regex.exec(options)) != null while (r = regex.exec(options)) != null
res[r[1]] = r[2] res[r[1]] = r[2]
regex = /([a-z]+)=([a-zA-Z0-9\/\.@\-]+)/g regex = /([a-z]+)=([a-zA-Z0-9\/\.@\-\+]+)/g
while (r = regex.exec(options)) != null while (r = regex.exec(options)) != null
res[r[1]] = r[2] res[r[1]] = r[2]
return res return res