Use a more robust method of validating an encoding.

This commit is contained in:
Ryan Stein 2017-11-03 12:45:24 -04:00
parent 6f91e1e546
commit 131f7157c4
3 changed files with 6 additions and 3 deletions

View file

@ -21,6 +21,7 @@ method. Otherwise all text will be returned to all connected sessions.
from builtins import range from builtins import range
import time import time
from codecs import lookup as codecs_lookup
from django.conf import settings from django.conf import settings
from evennia.server.sessionhandler import SESSIONS from evennia.server.sessionhandler import SESSIONS
from evennia.utils import utils, create, search, evtable from evennia.utils import utils, create, search, evtable
@ -533,7 +534,7 @@ class CmdOption(COMMAND_DEFAULT_CLASS):
def validate_encoding(new_encoding): def validate_encoding(new_encoding):
# helper: change encoding # helper: change encoding
try: try:
b"test-string".decode(new_encoding) codecs_lookup(new_encoding)
except LookupError: except LookupError:
raise RuntimeError("The encoding '|w%s|n' is invalid. " % new_encoding) raise RuntimeError("The encoding '|w%s|n' is invalid. " % new_encoding)
return val return val

View file

@ -3,6 +3,7 @@ Commands that are available from the connect screen.
""" """
import re import re
import time import time
from codecs import lookup as codecs_lookup
from collections import defaultdict from collections import defaultdict
from random import getrandbits from random import getrandbits
from django.conf import settings from django.conf import settings
@ -481,7 +482,7 @@ class CmdUnconnectedEncoding(COMMAND_DEFAULT_CLASS):
old_encoding = self.session.protocol_flags.get("ENCODING", None) old_encoding = self.session.protocol_flags.get("ENCODING", None)
encoding = self.args encoding = self.args
try: try:
utils.to_str(b"test-string".decode(encoding)) codecs_lookup(encoding)
except LookupError: except LookupError:
string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n"\ string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n"\
% (encoding, old_encoding) % (encoding, old_encoding)

View file

@ -21,6 +21,7 @@ settings.INPUT_FUNC_MODULES.
from future.utils import viewkeys from future.utils import viewkeys
import importlib import importlib
from codecs import lookup as codecs_lookup
from django.conf import settings from django.conf import settings
from evennia.commands.cmdhandler import cmdhandler from evennia.commands.cmdhandler import cmdhandler
from evennia.accounts.models import AccountDB from evennia.accounts.models import AccountDB
@ -176,7 +177,7 @@ def client_options(session, *args, **kwargs):
def validate_encoding(val): def validate_encoding(val):
# helper: change encoding # helper: change encoding
try: try:
b"test-string".decode(val) codecs_lookup(val)
except LookupError: except LookupError:
raise RuntimeError("The encoding '|w%s|n' is invalid. " % val) raise RuntimeError("The encoding '|w%s|n' is invalid. " % val)
return val return val