Skip to content

Commit

Permalink
Use command "locale charmap" to detect encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
huashengdun committed Aug 27, 2018
1 parent 80bdddc commit 688ca78
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
10 changes: 4 additions & 6 deletions tests/sshserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ class Server(paramiko.ServerInterface):
b'UWT10hcuO4Ks8=')
good_pub_key = paramiko.RSAKey(data=decodebytes(data))

langs = ['en_US.UTF-8', 'zh_CN.GBK']
encodings = ['UTF-8', 'GBK']

def __init__(self):
self.shell_event = threading.Event()
self.exec_event = threading.Event()
self.lang = random.choice(self.langs)
self.encoding = self.lang.split('.')[-1]
self.encoding = random.choice(self.encodings)

def check_channel_request(self, kind, chanid):
if kind == 'session':
Expand All @@ -82,12 +81,11 @@ def get_allowed_auths(self, username):
return 'password,publickey'

def check_channel_exec_request(self, channel, command):
if command != b'locale':
if command != b'locale charmap':
ret = False
else:
ret = True
result = 'LANG={lang}\nLANGUAGE=\nLC_CTYPE="{lang}"\n'.format(lang=self.lang) # noqa
channel.send(result)
channel.send(self.encoding)
channel.shutdown(1)
self.exec_event.set()
return ret
Expand Down
19 changes: 1 addition & 18 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,7 @@

from tornado.httputil import HTTPServerRequest
from tests.utils import read_file, make_tests_data_path
from webssh.handler import (
MixinHandler, IndexHandler, parse_encoding, InvalidValueError
)


class TestHandler(unittest.TestCase):

def test_parse_encoding(self):
data = ''
self.assertIsNone(parse_encoding(data))
data = 'UTF-8'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'en_US.UTF-8'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'LANG=en_US.UTF-8\nLANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'LANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n'
self.assertEqual(parse_encoding(data), 'UTF-8')
from webssh.handler import MixinHandler, IndexHandler, InvalidValueError


class TestMixinHandler(unittest.TestCase):
Expand Down
12 changes: 2 additions & 10 deletions webssh/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
KEY_MAX_SIZE = 16384


def parse_encoding(data):
for line in data.split('\n'):
s = line.split('=')[-1]
if s:
return s.strip('"').split('.')[-1]


class InvalidValueError(Exception):
pass

Expand Down Expand Up @@ -176,12 +169,11 @@ def get_client_addr(self):

def get_default_encoding(self, ssh):
try:
_, stdout, _ = ssh.exec_command('locale')
_, stdout, _ = ssh.exec_command('locale charmap')
except paramiko.SSHException:
result = None
else:
data = stdout.read()
result = parse_encoding(to_str(data))
result = to_str(stdout.read())

return result if result else 'utf-8'

Expand Down

0 comments on commit 688ca78

Please sign in to comment.