diff options
Diffstat (limited to 'pyload/lib/mod_pywebsocket/util.py')
-rw-r--r-- | pyload/lib/mod_pywebsocket/util.py | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/pyload/lib/mod_pywebsocket/util.py b/pyload/lib/mod_pywebsocket/util.py index 7bb0b5d9e..fc8451be7 100644 --- a/pyload/lib/mod_pywebsocket/util.py +++ b/pyload/lib/mod_pywebsocket/util.py @@ -56,6 +56,11 @@ import socket import traceback import zlib +try: + from mod_pywebsocket import fast_masking +except ImportError: + pass + def get_stack_trace(): """Get the current stack trace as string. @@ -169,26 +174,40 @@ class RepeatedXorMasker(object): ended and resumes from that point on the next mask method call. """ - def __init__(self, mask): - self._mask = map(ord, mask) - self._mask_size = len(self._mask) - self._count = 0 + def __init__(self, masking_key): + self._masking_key = masking_key + self._masking_key_index = 0 - def mask(self, s): + def _mask_using_swig(self, s): + masked_data = fast_masking.mask( + s, self._masking_key, self._masking_key_index) + self._masking_key_index = ( + (self._masking_key_index + len(s)) % len(self._masking_key)) + return masked_data + + def _mask_using_array(self, s): result = array.array('B') result.fromstring(s) + # Use temporary local variables to eliminate the cost to access # attributes - count = self._count - mask = self._mask - mask_size = self._mask_size + masking_key = map(ord, self._masking_key) + masking_key_size = len(masking_key) + masking_key_index = self._masking_key_index + for i in xrange(len(result)): - result[i] ^= mask[count] - count = (count + 1) % mask_size - self._count = count + result[i] ^= masking_key[masking_key_index] + masking_key_index = (masking_key_index + 1) % masking_key_size + + self._masking_key_index = masking_key_index return result.tostring() + if 'fast_masking' in globals(): + mask = _mask_using_swig + else: + mask = _mask_using_array + class DeflateRequest(object): """A wrapper class for request object to intercept send and recv to perform @@ -252,6 +271,7 @@ class _Deflater(object): self._logger.debug('Compress result %r', compressed_bytes) return compressed_bytes + class _Inflater(object): def __init__(self): @@ -346,6 +366,7 @@ class _RFC1979Deflater(object): return self._deflater.compress_and_flush(bytes)[:-4] return self._deflater.compress(bytes) + class _RFC1979Inflater(object): """A decompressor class for byte sequence compressed and flushed following the algorithm described in the RFC1979 section 2.1. |