From 2279de3c70fb28c3fef89da7a7afcd91a667fb16 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Sat, 12 Jul 2014 23:24:13 +0300 Subject: [PATCH] Fixed a couple of connection request issues. --- lib/api-functions.coffee | 17 ++++++++++++++--- lib/auth.coffee | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/api-functions.coffee b/lib/api-functions.coffee index 65b75f6..8f91b71 100644 --- a/lib/api-functions.coffee +++ b/lib/api-functions.coffee @@ -11,6 +11,17 @@ connectionRequest = (deviceId, callback) -> # ensure socket is reused in case of digest authentication 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) -> options = URL.parse(url) options.agent = agent @@ -19,7 +30,7 @@ connectionRequest = (deviceId, callback) -> options.headers = {'Authorization' : authString} 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']) callback(res.statusCode, authHeader) # 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) conReq(connectionRequestUrl, authString, (statusCode, authHeader) -> - callback() + callback(statusToError(statusCode)) ) else - callback() + callback(statusToError(statusCode)) ) ) diff --git a/lib/auth.coffee b/lib/auth.coffee index fdab312..c951e62 100644 --- a/lib/auth.coffee +++ b/lib/auth.coffee @@ -7,11 +7,11 @@ exports.parseAuthHeader = (authHeader) -> res['method'] = authHeader.slice(0, i) 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 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 res[r[1]] = r[2] return res