domotz-remote-pawn
Version:
Domotz Agent
81 lines (66 loc) • 2.5 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
* Created by Massimiliano Cuzzoli <mcuzzoli@domotz.com> on 20/04/15.
*
*/
var factory = function (resourceLocator) {
var host = resourceLocator.discovery.host;
var server = resourceLocator.http.createServer();
var ret = {
start: function () {
server.listen(0);
console.log("HTTP UPNP Server listening on port:" + server.address().port);
return ret;
},
getHttpServerUrl: function() {
return 'http://' + host.getIP() + ':' + server.address().port;
}
};
var handleEvent = function (request, response) {
if (request.method !== 'NOTIFY') {
console.log('Received unknown method : ' + request.method);
response.writeHead(400);
response.end();
return;
}
var errorCode = checkForHeadersErrors(request.headers);
if (errorCode) {
console.log('Invalid headers : ' + JSON.stringify(request.headers));
response.writeHead(errorCode);
response.end();
return;
}
resourceLocator.upnp.eventHandler.handleUpnpEvent(request, response, resourceLocator);
};
var checkForHeadersErrors = function (headers) {
if (!headers.nt || !headers.nts) {
return 400;
}
if (headers.nt !== 'upnp:event' || headers.nts !== 'upnp:propchange') {
return 412;
}
if (!headers.sid || !resourceLocator.upnp.registry.getSubscriptionById(headers.sid))
{
console.error("Unknown subscription id:" + headers.sid);
return 412;
}
};
server.on('request', handleEvent);
return ret;
};
module.exports.httpServer = factory;