Merge branch 'achille-roussel-wsaccel'

This commit is contained in:
liris
2015-07-16 09:27:22 +09:00
3 changed files with 70 additions and 48 deletions

View File

@@ -26,6 +26,24 @@ import os
from ._exceptions import *
from ._utils import validate_utf8
try:
# If wsaccel is available we use compiled routines to mask data.
from wsaccel.xormask import XorMaskerSimple
def _mask(_m, _d):
return XorMaskerSimple(_m).process(_d)
except ImportError:
# wsaccel is not available, we rely on python implementations.
def _mask(_m, _d):
for i in range(len(_d)):
_d[i] ^= _m[i % 4]
if six.PY3:
return _d.tobytes()
else:
return _d.tostring()
# closing frame status codes.
STATUS_NORMAL = 1000
STATUS_GOING_AWAY = 1001
@@ -208,6 +226,7 @@ class ABNF(object):
"""
if data == None:
data = ""
if isinstance(mask_key, six.text_type):
mask_key = six.b(mask_key)
@@ -216,14 +235,7 @@ class ABNF(object):
_m = array.array("B", mask_key)
_d = array.array("B", data)
for i in range(len(_d)):
_d[i] ^= _m[i % 4]
if six.PY3:
return _d.tobytes()
else:
return _d.tostring()
return _mask(_m, _d)
class frame_buffer(object):
_HEADER_MASK_INDEX = 5

View File

@@ -119,7 +119,7 @@ def _open_socket(addrinfo_list, sockopt, timeout):
def _can_use_sni():
return six.Py2 and sys.version_info >= (2, 7, 9) or sys.version_info >= (3, 2)
return six.PY2 and sys.version_info >= (2, 7, 9) or sys.version_info >= (3, 2)
def _wrap_sni_socket(sock, sslopt, hostname, check_hostname):

View File

@@ -31,12 +31,20 @@ class NoLock(object):
def __exit__(self, type, value, traceback):
pass
try:
# If wsaccel is availabe we use compiled routines to validate UTF-8
# strings.
from wsaccel.utf8validator import Utf8Validator
def _validate_utf8(utfbytes):
return Utf8Validator().validate(utfbytes)[0]
except ImportError:
# UTF-8 validator
# python implementation of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
UTF8_ACCEPT = 0
UTF8_REJECT=12
_UTF8_ACCEPT = 0
_UTF8_REJECT = 12
_UTF8D = [
# The first part of the table maps bytes to character classes that
@@ -61,28 +69,30 @@ _UTF8D = [
def _decode(state, codep, ch):
tp = _UTF8D[ch]
codep = (ch & 0x3f ) | (codep << 6) if (state != UTF8_ACCEPT) else (0xff >> tp) & (ch)
codep = (ch & 0x3f ) | (codep << 6) if (state != _UTF8_ACCEPT) else (0xff >> tp) & (ch)
state = _UTF8D[256 + state + tp]
return state, codep;
def _validate_utf8(utfbytes):
state = _UTF8_ACCEPT
codep = 0
for i in utfbytes:
if six.PY2:
i = ord(i)
state, codep = _decode(state, codep, i)
if state == _UTF8_REJECT:
return False
return True
def validate_utf8(utfbytes):
"""
validate utf8 byte string.
utfbytes: utf byte string to check.
return value: if valid utf8 string, return true. Otherwise, return false.
"""
state = UTF8_ACCEPT
codep = 0
for i in utfbytes:
if six.PY2:
i = ord(i)
state, codep = _decode(state, codep, i)
if state == UTF8_REJECT:
return False
return True
return _validate_utf8(utfbytes)
def extract_err_message(exception):
if exception.args: