Skip to content

Commit 4e0a623

Browse files
committed
Added more robust Autherization headers
1 parent c60a6d5 commit 4e0a623

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

rtsp.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RTSPClient(threading.Thread):
7070
def __init__(self, url, dest_ip=''):
7171
global CUR_RANGE
7272
threading.Thread.__init__(self)
73+
self._auth = None
7374
self._sock = None
7475
self._orig_url = url
7576
self._cseq = 0
@@ -211,7 +212,7 @@ def _add_auth(self, msg):
211212
msg_dict['nonce'],
212213
self._parsed_url.path,
213214
response)
214-
return auth_string
215+
self._auth = auth_string
215216
else: # Some other failure
216217
PRINT('Authentication failure')
217218
self.do_teardown()
@@ -251,9 +252,8 @@ def _process_response(self, msg):
251252
if self._cseq_map[rsp_cseq] != 'GET_PARAMETER':
252253
PRINT(self._get_time_str() + '\n' + msg)
253254
if status == 401:
254-
auth_string = self._add_auth(headers['www-authenticate'])
255-
if self._cseq_map[self._cseq] == 'DESCRIBE':
256-
self.do_describe({'Authorization':auth_string})
255+
self._add_auth(headers['www-authenticate'])
256+
self.do_replay_request()
257257
#self.do_teardown()
258258
elif status == 302:
259259
self.location = headers['location']
@@ -342,6 +342,8 @@ def _get_transport_type(self):
342342
return transport_str
343343

344344
def do_describe(self, headers={}):
345+
if self._auth:
346+
headers['Authorization'] = self._auth
345347
headers['Accept'] = 'application/sdp'
346348
if ENABLE_ARQ:
347349
headers['x-Retrans'] = 'yes'
@@ -351,27 +353,55 @@ def do_describe(self, headers={}):
351353
self._sendmsg('DESCRIBE', self._orig_url, headers)
352354

353355
def do_setup(self, track_id_str='', headers={}):
356+
if self._auth:
357+
headers['Authorization'] = self._auth
354358
headers['Transport'] = self._get_transport_type()
355359
self._sendmsg('SETUP', self._orig_url+'/'+track_id_str, headers)
356360

357361
def do_play(self, range='npt=end-', scale=1, headers={}):
362+
if self._auth:
363+
headers['Authorization'] = self._auth
358364
headers['Range'] = range
359365
headers['Scale'] = scale
360366
self._sendmsg('PLAY', self._orig_url, headers)
361367

362368
def do_pause(self, headers={}):
369+
if self._auth:
370+
headers['Authorization'] = self._auth
363371
self._sendmsg('PAUSE', self._orig_url, headers)
364372

365373
def do_teardown(self, headers={}):
374+
if self._auth:
375+
headers['Authorization'] = self._auth
366376
self._sendmsg('TEARDOWN', self._orig_url, headers)
367377
self.running = False
368378

369379
def do_options(self, headers={}):
380+
if self._auth:
381+
headers['Authorization'] = self._auth
370382
self._sendmsg('OPTIONS', self._orig_url, headers)
371383

372384
def do_get_parameter(self, headers={}):
385+
if self._auth:
386+
headers['Authorization'] = self._auth
373387
self._sendmsg('GET_PARAMETER', self._orig_url, headers)
374388

389+
def do_replay_request(self, headers={}):
390+
if self._cseq_map[self._cseq] == 'DESCRIBE':
391+
self.do_describe()
392+
elif self._cseq_map[self._cseq] == 'SETUP':
393+
self.do_setup()
394+
elif self._cseq_map[self._cseq] == 'PLAY':
395+
self.do_play()
396+
elif self._cseq_map[self._cseq] == 'PAUSE':
397+
self.do_pause()
398+
elif self._cseq_map[self._cseq] == 'TEARDOWN':
399+
self.do_teardown()
400+
elif self._cseq_map[self._cseq] == 'OPTIONS':
401+
self.do_options()
402+
elif self._cseq_map[self._cseq] == 'GET_PARAMETER':
403+
self.do_get_parameter()
404+
375405
def send_heart_beat_msg(self):
376406
'''Timed sending GET_PARAMETER message keep alive'''
377407
if not self.running:

0 commit comments

Comments
 (0)