UNPKG

domotz-remote-pawn

Version:

Domotz Agent

78 lines (66 loc) 2.54 kB
/** 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 myConsole = resourceLocator.log.decorateLogs(); var host = resourceLocator.discovery.host; var server = resourceLocator.http.createServer(); var ret = { start: function () { server.listen(0); myConsole.info('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') { myConsole.info('Received unknown method : ' + request.method); response.writeHead(400); response.end(); return; } var errorCode = checkForHeadersErrors(request.headers); if (errorCode) { myConsole.info('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)) { myConsole.error('Unknown subscription id:' + headers.sid); return 412; } }; server.on('request', handleEvent); return ret; }; module.exports.httpServer = factory;