Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

# Licensed under the MIT license 

# http://opensource.org/licenses/mit-license.php 

 

import time 

from urllib.parse import urlsplit 

 

# Copyright (C) 2006 Fluendo, S.A. (www.fluendo.com). 

# Copyright 2006,2007,2008,2009 Frank Scholz <coherence@beebits.net> 

from lxml import etree 

from twisted.internet import reactor, defer 

from twisted.internet.protocol import Protocol, ClientCreator, _InstanceFactory 

from twisted.web import resource 

from twisted.web.http import datetimeToString 

 

import coherence.extern.louie as louie 

from coherence import log, SERVER_ID 

from coherence.upnp.core import utils 

 

global hostname, web_server_port 

hostname = None 

web_server_port = None 

 

 

class EventServer(resource.Resource, log.LogAble): 

    logCategory = 'event_server' 

 

    def __init__(self, control_point): 

        log.LogAble.__init__(self) 

        resource.Resource.__init__(self) 

        self.coherence = control_point.coherence 

        self.control_point = control_point 

        self.coherence.add_web_resource('events', 

                                        self) 

        global hostname, web_server_port 

        hostname = self.coherence.hostname 

        web_server_port = self.coherence.web_server_port 

        self.info("EventServer ready...") 

 

    def render_NOTIFY(self, request): 

        self.info("EventServer received notify from %s, code: %d", 

                  request.client, request.code) 

        data = request.content.getvalue() 

        request.setResponseCode(200) 

 

        command = {'method': request.method, 'path': request.path} 

        headers = request.responseHeaders 

        louie.send( 

            'UPnP.Event.Server.message_received', 

            None, command, headers, data) 

 

        if request.code != 200: 

            self.info("data: %s", data) 

        else: 

            self.debug("data: %s", data) 

            headers = request.getAllHeaders() 

            sid = headers[b'sid'] 

            try: 

                tree = etree.fromstring(data) 

            except (SyntaxError, AttributeError): 

                self.warning("malformed event notification from %r", 

                             request.client) 

                self.debug("data: %r", data) 

                request.setResponseCode(400) 

                return "" 

 

            event = Event(sid, tree, raw=data) 

            if len(event) != 0: 

                self.control_point.propagate(event) 

        return "" 

 

 

class EventSubscriptionServer(resource.Resource, log.LogAble): 

    """ 

    This class ist the server part on the device side. It listens 

    to subscribe requests and registers the subscriber to send 

    event messages to this device. 

    If an unsubscribe request is received, the subscription is cancelled 

    and no more event messages will be sent. 

 

    we receive a subscription request 

    {'callback': 

        '<http://192.168.213.130:9083/BYvZMzfTSQkjHwzOThaP/ConnectionManager>', 

     'host': '192.168.213.107:30020', 

     'nt': 'upnp:event', 

     'content-length': '0', 

     'timeout': 'Second-300'} 

 

    modify the callback value 

    callback = callback[1:len(callback)-1] 

    and pack it into a subscriber dict 

 

    {'uuid:oAQbxiNlyYojCAdznJnC': 

        { 

        'callback': 

        '<http://192.168.213.130:9083/BYvZMzfTSQkjHwzOThaP/ConnectionManager>', 

        'created': 1162374189.257338, 

        'timeout': 'Second-300', 

        'sid': 'uuid:oAQbxiNlyYojCAdznJnC'}} 

    """ 

    logCategory = 'event_subscription_server' 

 

    def __init__(self, service): 

        resource.Resource.__init__(self) 

        log.LogAble.__init__(self) 

        self.service = service 

        self.subscribers = service.get_subscribers() 

        try: 

            self.backend_name = self.service.backend.name 

        except AttributeError: 

            self.backend_name = self.service.backend 

 

    def render_SUBSCRIBE(self, request): 

        self.info( 

            "EventSubscriptionServer %s (%s) received subscribe request " 

            "from %s, code: %d", 

            self.service.id, 

            self.backend_name, 

            request.client, request.code) 

        data = request.content.getvalue() 

        request.setResponseCode(200) 

 

        command = {'method': request.method, 'path': request.path} 

        headers = request.responseHeaders 

        louie.send('UPnP.Event.Client.message_received', 

                   None, command, headers, data) 

 

        if request.code != 200: 

            self.debug("data: %s", data) 

        else: 

            headers = request.getAllHeaders() 

            try: 

                if headers[b'sid'] in self.subscribers: 

                    s = self.subscribers[headers[b'sid']] 

                    s['timeout'] = headers[b'timeout'] 

                    s['created'] = time.time() 

                elif b'callback' not in headers: 

                    request.setResponseCode(404) 

                    request.setHeader(b'SERVER', SERVER_ID.encode('ascii')) 

                    request.setHeader(b'CONTENT-LENGTH', 0) 

                    return b"" 

            except Exception as e: 

                self.warning('render_SUBSCRIBE: %r' % e) 

                from .uuid import UUID 

                sid = UUID() 

                s = {'sid': str(sid), 

                     'callback': 

                         headers[b'callback'][1:len(headers[b'callback']) - 1], 

                     'seq': 0, 

                     'timeout': headers[b'timeout'], 

                     'created': time.time()} 

                self.service.new_subscriber(s) 

 

            request.setHeader(b'SID', s['sid']) 

 

            # wrong example in the UPnP UUID spec? 

            # request.setHeader(b'Subscription-ID', sid) 

 

            request.setHeader(b'TIMEOUT', s['timeout']) 

            request.setHeader(b'SERVER', SERVER_ID.encode('ascii')) 

            request.setHeader(b'CONTENT-LENGTH', 0) 

        return b"" 

 

    def render_UNSUBSCRIBE(self, request): 

        self.info( 

            "EventSubscriptionServer %s (%s) received unsubscribe request " 

            "from %s, code: %d", self.service.id, self.backend_name, 

            request.client, request.code) 

        data = request.content.getvalue() 

        request.setResponseCode(200) 

 

        command = {'method': request.method, 'path': request.path} 

        headers = request.responseHeaders 

        louie.send('UPnP.Event.Client.message_received', 

                   None, command, headers, data) 

 

        if request.code != 200: 

            self.debug("data: %s", data) 

        else: 

            headers = request.getAllHeaders() 

            self.subscribers.pop(headers[b'sid'], None) 

            # print self.subscribers 

        return "" 

 

 

class Event(dict, log.LogAble): 

    logCategory = 'event' 

    ns = "urn:schemas-upnp-org:event-1-0" 

 

    def __init__(self, sid, elements=None, raw=None): 

        dict.__init__(self) 

        log.LogAble.__init__(self) 

        self._sid = sid 

        self.raw = raw 

        if elements is not None: 

            self.from_elements(elements) 

 

    def get_sid(self): 

        return self._sid 

 

    def from_elements(self, elements): 

        for prop in elements.findall('{%s}property' % self.ns): 

            self._update_event(prop) 

        if len(self) == 0: 

            self.warning("event notification without property elements") 

            self.debug("data: %r", self.raw) 

            for prop in elements.findall('property'): 

                self._update_event(prop) 

 

    def _update_event(self, prop): 

        for var in prop.getchildren(): 

            tag = var.tag 

            idx = tag.find('}') + 1 

            value = var.text 

            if value is None: 

                value = '' 

            self.update({tag[idx:]: value}) 

 

 

class EventProtocol(Protocol, log.LogAble): 

    logCategory = 'event_protocol' 

 

    def __init__(self, service, action): 

        log.LogAble.__init__(self) 

        self.service = service 

        self.action = action 

 

    def teardown(self): 

        self.transport.loseConnection() 

        self.service.event_connection = None 

 

    def connectionMade(self): 

        self.timeout_checker = reactor.callLater(30, self.teardown) 

 

    def dataReceived(self, data): 

        try: 

            self.timeout_checker.cancel() 

        except Exception: 

            pass 

        self.info("response received from the Service Events HTTP server ") 

        # self.debug(data) 

        cmd, headers = utils.parse_http_response(data) 

        self.debug("%r %r", cmd, headers) 

        if int(cmd[1]) != 200: 

            self.warning( 

                "response with error code %r received upon our %r request", 

                cmd[1], self.action) 

            # XXX get around devices that return an 

            # error on our event subscribe request 

            self.service.process_event({}) 

        else: 

            try: 

                self.service.set_sid(headers['sid']) 

                timeout = headers['timeout'] 

                self.debug("%r %r", headers['sid'], headers['timeout']) 

                if timeout == 'infinite': 

                    self.service.set_timeout( 

                        time.time() + 4294967296)  # FIXME: that's lame 

                elif timeout.startswith('Second-'): 

                    timeout = int(timeout[len('Second-'):]) 

                    self.service.set_timeout(timeout) 

            except Exception as e: 

                self.warning('EventProtocol.dataReceived: %r' % e) 

        self.teardown() 

 

    def connectionLost(self, reason): 

        try: 

            self.timeout_checker.cancel() 

        except Exception: 

            pass 

        self.debug("connection closed %r from the Service Events HTTP server", 

                   reason) 

 

 

def unsubscribe(service, action='unsubscribe'): 

    return subscribe(service, action) 

 

 

def subscribe(service, action='subscribe'): 

    """ 

    send a subscribe/renewal/unsubscribe request to a service 

    return the device response 

    """ 

 

    logger = log.get_logger("event_protocol") 

    logger.info("event.subscribe, action: %r", action) 

 

    service_base = service.get_base_url().decode('utf-8') 

    _, host_port, path, _, _ = urlsplit(service_base) 

    if host_port.find(':') != -1: 

        host, port = tuple(host_port.split(':')) 

        port = int(port) 

    else: 

        host = host_port 

        port = 80 

 

    def send_request(p, action): 

        logger.info("event.subscribe.send_request %r, action: %r %r", 

                    p, action, service.get_event_sub_url()) 

        _, _, event_path, _, _ = urlsplit(service.get_event_sub_url()) 

        if action == 'subscribe': 

            timeout = service.timeout 

            if timeout == 0: 

                timeout = 1800 

            request = ["SUBSCRIBE %s HTTP/1.1" % event_path, 

                       "HOST: %s:%d" % (host, port), 

                       "TIMEOUT: Second-%d" % timeout, 

                       ] 

            service.event_connection = p 

        else: 

            request = ["UNSUBSCRIBE %s HTTP/1.1" % event_path, 

                       "HOST: %s:%d" % (host, port), 

                       ] 

 

        if service.get_sid(): 

            request.append("SID: %s" % service.get_sid()) 

        else: 

            # XXX use address and port set in the coherence instance 

            # ip_address = p.transport.getHost().host 

            global hostname, web_server_port 

            # print hostname, web_server_port 

            url = 'http://%s:%d/events' % (hostname, web_server_port) 

            request.append("CALLBACK: <%s>" % url) 

            request.append("NT: upnp:event") 

 

        request.append('Date: %s' % datetimeToString()) 

        request.append("Content-Length: 0") 

        request.append("") 

        request.append("") 

        request = '\r\n'.join(request).encode('ascii') 

        logger.debug("event.subscribe.send_request %r %r", request, p) 

        try: 

            p.transport.writeSomeData(request) 

        except AttributeError: 

            logger.info("transport for event %r already gone", action) 

        # logger.debug("event.subscribe.send_request ", request) 

        # return d 

 

    def got_error(failure, action): 

        logger.info("error on %s request with %s", action, 

                    service.get_base_url()) 

        logger.debug(failure) 

 

    def teardown_connection(c, d): 

        logger.info("event.subscribe.teardown_connection") 

        del d 

        del c 

 

    def prepare_connection(service, action): 

        logger.info("event.subscribe.prepare_connection action: %r %r", 

                    action, service.event_connection) 

        if service.event_connection is None: 

            c = ClientCreator(reactor, EventProtocol, service=service, 

                              action=action) 

            logger.info("event.subscribe.prepare_connection: %r %r", 

                        host, port) 

            d = c.connectTCP(host, port) 

            d.addCallback(send_request, action=action) 

            d.addErrback(got_error, action) 

            # reactor.callLater(3, teardown_connection, c, d) 

        else: 

            d = defer.Deferred() 

            d.addCallback(send_request, action=action) 

            d.callback(service.event_connection) 

            # send_request(service.event_connection, action) 

        return d 

 

    """ FIXME: 

        we need to find a way to be sure that our unsubscribe calls get through 

        on shutdown 

        reactor.addSystemEventTrigger( 

            'before', 'shutdown', prepare_connection, service, action) 

    """ 

 

    # logger.debug("event.subscribe finished") 

    return prepare_connection(service, action) 

 

 

class NotificationProtocol(Protocol, log.LogAble): 

    logCategory = "notification_protocol" 

 

    def connectionMade(self): 

        self.timeout_checker = reactor.callLater( 

            30, lambda: self.transport.loseConnection()) 

 

    def dataReceived(self, data): 

        try: 

            self.timeout_checker.cancel() 

        except Exception: 

            pass 

        if isinstance(data, bytes): 

            d = str(data) 

        else: 

            d = data 

        cmd, headers = utils.parse_http_response(d) 

        self.debug("notification response received %r %r", cmd, headers) 

        try: 

            if int(cmd[1]) != 200: 

                self.warning( 

                    "response with error code %r " 

                    "received upon our notification", cmd[1]) 

        except (IndexError, ValueError): 

            self.debug( 

                "response without error code received upon our notification") 

        self.transport.loseConnection() 

 

    def connectionLost(self, reason): 

        try: 

            self.timeout_checker.cancel() 

        except Exception: 

            pass 

        self.debug("connection closed %r", reason) 

 

 

def send_notification(s, xml): 

    """ 

    send a notification a subscriber 

    return its response 

    """ 

    logger = log.get_logger("notification_protocol") 

    # logger.debug('\t-send_notification s is: {}'.format(s)) 

    # logger.debug('\t-send_notification xml is: {}'.format(xml)) 

 

    _, host_port, path, _, _ = urlsplit(s['callback']) 

    # logger.debug('\t-send_notification host_port is: {}'.format(host_port)) 

    # logger.debug('\t-send_notification path is: {}'.format(path)) 

    if path == b'': 

        path = b'/' 

    if host_port.find(b':') != -1: 

        host, port = tuple(host_port.split(b':')) 

        port = int(port) 

    else: 

        host = host_port 

        port = 80 

 

    def send_request(p, port_item): 

        request = [b'NOTIFY %r HTTP/1.1' % path, 

                   b'HOST:  %r:%r' % (host, port), 

                   b'SEQ:  %r' % s['seq'], 

                   b'CONTENT-TYPE:  text/xml;charset="utf-8"', 

                   b'SID:  %r' % s['sid'], 

                   b'NTS:  upnp:propchange', 

                   b'NT:  upnp:event', 

                   b'Content-Length: %r' % len(xml), 

                   b'', 

                   xml] 

 

        request = b'\r\n'.join(request) 

        logger.info("send_notification.send_request to %r %r", s['sid'], 

                    s['callback']) 

        logger.debug("request: %r", request) 

        s['seq'] += 1 

        if s['seq'] > 0xffffffff: 

            s['seq'] = 1 

        p.transport.write(request) 

        port_item.disconnect() 

 

    def got_error(failure, port_item): 

        port_item.disconnect() 

        logger.info("error sending notification to %r %r", s['sid'], 

                    s['callback']) 

        logger.debug(failure) 

 

    d = defer.Deferred() 

    f = _InstanceFactory(reactor, NotificationProtocol(), d) 

    port_item = reactor.connectTCP( 

        host, port, f, timeout=30, bindAddress=None) 

 

    d.addCallback(send_request, port_item) 

    d.addErrback(got_error, port_item) 

 

    return d, port_item