Skip to content

Commit 1add70f

Browse files
committed
🐛 fix engine unicode errror
1 parent e4f80ab commit 1add70f

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

leancloud/engine/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
import sys
99
import json
10+
import warnings
1011

1112
from werkzeug.wrappers import Request
1213
from werkzeug.wrappers import Response
1314
from werkzeug.serving import run_simple
1415

1516
import leancloud
17+
from . import utils
1618
from . import leanengine
1719
from .authorization import AuthorizationMiddleware
1820
from .cookie_session import CookieSessionMiddleware
@@ -61,7 +63,7 @@ def __call__(self, environ, start_response):
6163
environ['leanengine.request'] = request # cache werkzeug request for other middlewares
6264

6365
if request.path in ('/__engine/1/ping', '/__engine/1.1/ping/'):
64-
start_response('200 OK', [('Content-Type', 'application/json')])
66+
start_response(utils.to_native('200 OK'), [(utils.to_native('Content-Type'), utils.to_native('application/json'))])
6567
version = sys.version_info
6668
return Response(json.dumps({
6769
'version': leancloud.__version__,

leancloud/engine/authorization.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import os
99
import json
1010

11-
import six
1211
from werkzeug.wrappers import Response
1312

1413
from . import utils

leancloud/engine/cors.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
from __future__ import print_function
66
from __future__ import unicode_literals
77

8+
from . import utils
9+
810

911
class CORSMiddleware(object):
10-
ALLOW_ORIGIN = "*"
11-
ALLOW_HEADERS = ', '.join([
12+
ALLOW_ORIGIN = utils.to_native('*')
13+
ALLOW_HEADERS = utils.to_native(', '.join([
1214
'Content-Type',
1315
'X-AVOSCloud-Application-Id',
1416
'X-AVOSCloud-Application-Key',
@@ -30,28 +32,28 @@ class CORSMiddleware(object):
3032
'X-LC-Session',
3133
'X-LC-Sign',
3234
'X-LC-UA',
33-
])
34-
ALLOW_METHODS = ', '.join(['PUT', 'GET', 'POST', 'DELETE', 'OPTIONS'])
35-
MAX_AGE = '86400'
35+
]))
36+
ALLOW_METHODS = utils.to_native(', '.join(['PUT', 'GET', 'POST', 'DELETE', 'OPTIONS']))
37+
MAX_AGE = utils.to_native('86400')
3638

3739
def __init__(self, app):
3840
self.app = app
3941

4042
def __call__(self, environ, start_response):
4143
if environ['REQUEST_METHOD'] == 'OPTIONS':
42-
start_response('200 OK', [
43-
('Access-Control-Allow-Origin', environ.get('HTTP_ORIGIN', self.ALLOW_ORIGIN)),
44-
('Access-Control-Allow-Headers', self.ALLOW_HEADERS),
45-
('Access-Control-Allow-Methods', self.ALLOW_METHODS),
46-
('Access-Control-Max-Age', self.MAX_AGE)
44+
start_response(utils.to_native('200 OK'), [
45+
(utils.to_native('Access-Control-Allow-Origin'), environ.get('HTTP_ORIGIN', self.ALLOW_ORIGIN)),
46+
(utils.to_native('Access-Control-Allow-Headers'), self.ALLOW_HEADERS),
47+
(utils.to_native('Access-Control-Allow-Methods'), self.ALLOW_METHODS),
48+
(utils.to_native('Access-Control-Max-Age'), self.MAX_AGE)
4749
])
48-
return [b'']
50+
return [utils.to_native('')]
4951
else:
5052
def cors_start_response(status, headers, exc_info=None):
51-
headers.append(('Access-Control-Allow-Origin', self.ALLOW_ORIGIN))
52-
headers.append(('Access-Control-Allow-Headers', self.ALLOW_HEADERS))
53-
headers.append(('Access-Control-Allow-Methods', self.ALLOW_METHODS))
54-
headers.append(('Access-Control-Max-Age', self.MAX_AGE))
53+
headers.append((utils.to_native('Access-Control-Allow-Origin'), self.ALLOW_ORIGIN))
54+
headers.append((utils.to_native('Access-Control-Allow-Headers'), self.ALLOW_HEADERS))
55+
headers.append((utils.to_native('Access-Control-Allow-Methods'), self.ALLOW_METHODS))
56+
headers.append((utils.to_native('Access-Control-Max-Age'), self.MAX_AGE))
5557
return start_response(status, headers, exc_info)
5658

5759
return self.app(environ, cors_start_response)

leancloud/engine/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@
77

88
import hashlib
99

10+
import six
11+
12+
1013
__author__ = 'asaka <lan@leancloud.rocks>'
1114

1215

1316
def sign_by_key(timestamp, key):
1417
s = '{0}{1}'.format(timestamp, key)
1518
return hashlib.md5(s.encode('utf-8')).hexdigest()
19+
20+
21+
if six.PY2:
22+
def to_native(s):
23+
if isinstance(s, unicode):
24+
return s.encode('utf-8')
25+
return s
26+
else:
27+
def to_native(s):
28+
if isinstance(s, bytes):
29+
return s.decode('utf-8')
30+
return s

0 commit comments

Comments
 (0)