From: Calin Crisan Date: Sun, 31 May 2015 08:30:27 +0000 (+0300) Subject: fixed issues with special characters in camera device paths X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=413e5ee41b6e688992cb9a77a10335a7c6eea1a1;p=motioneye-debian fixed issues with special characters in camera device paths --- diff --git a/src/config.py b/src/config.py index d4e48b8..af8d1f2 100644 --- a/src/config.py +++ b/src/config.py @@ -150,7 +150,7 @@ def set_main(main_config): lines = _dict_to_conf(lines, main_config, list_names=['thread']) try: - file.writelines([l + '\n' for l in lines]) + file.writelines([utils.make_str(l) + '\n' for l in lines]) except Exception as e: logging.error('could not write main config file %(path)s: %(msg)s' % { @@ -420,7 +420,7 @@ def set_camera(camera_id, camera_config): lines = _dict_to_conf(lines, camera_config) try: - file.writelines([l + '\n' for l in lines]) + file.writelines([utils.make_str(l) + '\n' for l in lines]) except Exception as e: logging.error('could not write camera config file %(path)s: %(msg)s' % { diff --git a/src/utils.py b/src/utils.py index b7aaf87..32eb0c0 100644 --- a/src/utils.py +++ b/src/utils.py @@ -37,6 +37,9 @@ except: from ordereddict import OrderedDict # @UnusedImport @Reimport +_SIGNATURE_REGEX = re.compile('[^a-zA-Z0-9/?_.=&{}\[\]":, _-]') + + def pretty_date_time(date_time, tzinfo=None, short=False): if date_time is None: return '('+ _('never') + ')' @@ -230,6 +233,36 @@ def pretty_http_error(response): return msg +def make_str(s): + if isinstance(s, str): + return s + + try: + return str(s) + + except: + try: + return unicode(s, encoding='utf8').encode('utf8') + + except: + return unicode(s).encode('utf8') + + +def make_unicode(s): + if isinstance(s, unicode): + return s + + try: + return unicode(s, encoding='utf8') + + except: + try: + return unicode(s) + + except: + return str(s).decode('utf8') + + def get_disk_usage(path): logging.debug('getting disk usage for path %(path)s...' % { 'path': path}) @@ -359,10 +392,14 @@ def compute_signature(method, uri, body, key): parts[0] = parts[1] = '' parts[3] = query uri = urlparse.urlunsplit(parts) - + uri = _SIGNATURE_REGEX.sub('-', uri) + key = _SIGNATURE_REGEX.sub('-', key) + if body and body.startswith('---'): body = None # file attachment + body = body and _SIGNATURE_REGEX.sub('-', body.decode('utf8')) + return hashlib.sha1('%s:%s:%s:%s' % (method, uri, body or '', key)).hexdigest().lower() diff --git a/src/v4l2ctl.py b/src/v4l2ctl.py index 8c17bd9..3be3467 100644 --- a/src/v4l2ctl.py +++ b/src/v4l2ctl.py @@ -22,6 +22,7 @@ import re import stat import subprocess import time +import utils _resolutions_cache = {} @@ -109,6 +110,8 @@ def list_devices(): def list_resolutions(device): global _resolutions_cache + device = utils.make_str(device) + if device in _resolutions_cache: return _resolutions_cache[device] @@ -117,7 +120,7 @@ def list_resolutions(device): resolutions = set() output = '' started = time.time() - p = subprocess.Popen('v4l2-ctl -d %(device)s --list-formats-ext | grep -vi stepwise | grep -oE "[0-9]+x[0-9]+" || true' % { + p = subprocess.Popen('v4l2-ctl -d "%(device)s" --list-formats-ext | grep -vi stepwise | grep -oE "[0-9]+x[0-9]+" || true' % { 'device': device}, shell=True, stdout=subprocess.PIPE, bufsize=1) fd = p.stdout.fileno() @@ -198,6 +201,8 @@ def list_resolutions(device): def device_present(device): + device = utils.make_str(device) + try: st = os.stat(device) return stat.S_ISCHR(st.st_mode) @@ -207,6 +212,8 @@ def device_present(device): def find_persistent_device(device): + device = utils.make_str(device) + try: devs_by_id = os.listdir(_DEV_V4L_BY_ID) @@ -256,6 +263,8 @@ def set_hue(device, value): def _get_ctrl(device, control): global _ctrl_values_cache + device = utils.make_str(device) + if not device_present(device): return None @@ -292,6 +301,8 @@ def _get_ctrl(device, control): def _set_ctrl(device, control, value): global _ctrl_values_cache + device = utils.make_str(device) + if not device_present(device): return @@ -321,7 +332,7 @@ def _set_ctrl(device, control, value): output = '' started = time.time() - p = subprocess.Popen('v4l2-ctl -d %(device)s --set-ctrl %(control)s=%(value)s' % { + p = subprocess.Popen('v4l2-ctl -d "%(device)s" --set-ctrl %(control)s=%(value)s' % { 'device': device, 'control': control, 'value': value}, shell=True, stdout=subprocess.PIPE, bufsize=1) fd = p.stdout.fileno() @@ -358,13 +369,15 @@ def _set_ctrl(device, control, value): def _list_ctrls(device): global _ctrls_cache + + device = utils.make_str(device) if device in _ctrls_cache: return _ctrls_cache[device] output = '' started = time.time() - p = subprocess.Popen('v4l2-ctl -d %(device)s --list-ctrls' % { + p = subprocess.Popen('v4l2-ctl -d "%(device)s" --list-ctrls' % { 'device': device}, shell=True, stdout=subprocess.PIPE, bufsize=1) fd = p.stdout.fileno() diff --git a/static/js/main.js b/static/js/main.js index c0a575b..8c0ddfb 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -8,6 +8,7 @@ var refreshInterval = 50; /* milliseconds */ var username = ''; var password = ''; var baseUri = null; +var signatureRegExp = new RegExp('[^a-zA-Z0-9/?_.=&{}\\[\\]":, _-]', 'g'); /* utils */ @@ -150,9 +151,12 @@ function computeSignature(method, uri, body) { query = query.filter(function (q) {return q.key !== '_signature';}); query.sortKey(function (q) {return q.key;}); query = query.map(function (q) {return q.key + '=' + encodeURIComponent(q.value);}).join('&'); - uri = baseUrl + '?' + query; + uri = baseUrl + '?' + query; + uri = uri.replace(signatureRegExp, '-'); + body = body && body.replace(signatureRegExp, '-'); + var password = window.password.replace(signatureRegExp, '-'); - return sha1(method + ':' + uri + ':' + (body || '') + ':' + window.password).toLowerCase(); + return sha1(method + ':' + uri + ':' + (body || '') + ':' + password).toLowerCase(); } function addAuthParams(method, url, body) {