Module: check_mk
Branch: master
Commit: 49f1cbeb134a80d888354a1619a0cd505303c77b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=49f1cbeb134a80…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed May 23 08:59:23 2018 +0200
Simplified htmllib.Encoder()
* Replaced Check_MK specific encoding logic with standard urllib calls
* html.urlencode() now uses html.urlencode_plus() logic
* Cleaned up html.urlencode_plus()
Change-Id: I4838ca293d508ba4ebf50b238293bb2409b42cb1
---
tests/unit/web/test_htmllib_Encoder.py | 28 +++---------------
web/htdocs/htmllib.py | 54 +++++++++-------------------------
web/plugins/views/painters.py | 2 +-
3 files changed, 19 insertions(+), 65 deletions(-)
diff --git a/tests/unit/web/test_htmllib_Encoder.py
b/tests/unit/web/test_htmllib_Encoder.py
index b0bd054..74dfa83 100644
--- a/tests/unit/web/test_htmllib_Encoder.py
+++ b/tests/unit/web/test_htmllib_Encoder.py
@@ -10,7 +10,6 @@ def test_htmllib_integration(register_builtin_html):
assert html.urlencode_vars([]) == ""
assert html.urlencode("") == ""
- assert html.urlencode_plus("") == ""
@pytest.mark.parametrize("inp,out", [
@@ -21,8 +20,8 @@ def test_htmllib_integration(register_builtin_html):
([("a", "_-.")], "a=_-."),
([("a", "#")], "a=%23"),
([("a", "+")], "a=%2B"),
- ([("a", " ")], "a=%20"),
- ([("a", "/")], "a=/"),
+ ([("a", " ")], "a=+"),
+ ([("a", "/")], "a=%2F"),
])
def test_urlencode_vars(inp, out):
result = htmllib.Encoder().urlencode_vars(inp)
@@ -31,24 +30,6 @@ def test_urlencode_vars(inp, out):
@pytest.mark.parametrize("inp,out", [
- (u"välue", "v%c3%a4lue"),
- # TODO: None / int handling inconsistent with urlencode_vars()
- (None, ""),
- ("ä", "%c3%a4"),
- ("_-.", "_-."),
- ("#", "%23"),
- ("+", "%2b"),
- # TODO: Why + instead of %20?
- (" ", "+"),
- ("/", "/"),
-])
-def test_urlencode(inp, out):
- result = htmllib.Encoder().urlencode(inp)
- assert type(result) == str
- assert result == out
-
-
-(a)pytest.mark.parametrize("inp,out",out", [
(u"välue", "v%C3%A4lue"),
# TODO: None / int handling inconsistent with urlencode_vars()
(None, ""),
@@ -56,11 +37,10 @@ def test_urlencode(inp, out):
("_-.", "_-."),
("#", "%23"),
("+", "%2B"),
- # TODO: Why + instead of %20?
(" ", "+"),
("/", "%2F"),
])
-def test_urlencode_plus(inp, out):
- result = htmllib.Encoder().urlencode_plus(inp)
+def test_urlencode(inp, out):
+ result = htmllib.Encoder().urlencode(inp)
assert type(result) == str
assert result == out
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 75e3cc7..b97610f 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -165,20 +165,15 @@ class Escaper(object):
# '----------------------------------------------------------------------'
class Encoder(object):
- # This function returns a str object, never unicode!
- # Beware: this code is crucial for the performance of Multisite!
- # Changing from the self coded urlencode to urllib.quote
- # is saving more then 90% of the total HTML generating time
- # on more complex pages!
- #
- # TODO: Cleanup self.urlencode_vars, self.urlencode and self.urlencode_plus.
- # urlencode_vars() should directly use urlencode or urlencode_vars and
- # not fallback to self.urlencode on it's own. self.urlencode() should
- # work for a single value exacly as urlencode_vars() does for multiple
- # TODO: Refactor to use urllib.urlencode()
def urlencode_vars(self, vars):
+ """Convert a mapping object or a sequence of two-element tuples to
a “percent-encoded” string
+
+ This function returns a str object, never unicode!
+ Note: This should be changed once we change everything to
+ unicode internally.
+ """
assert type(vars) == list
- output = []
+ pairs = []
for varname, value in sorted(vars):
assert type(varname) == str
@@ -189,35 +184,18 @@ class Encoder(object):
#assert type(value) == str, "%s: %s" % (varname, value)
- try:
- # urllib is not able to encode non-Ascii characters. Yurks
- output.append(varname + '=' + urllib.quote(value))
- except:
- output.append(varname + '=' + self.urlencode(value)) # slow but
working
+ pairs.append((varname, value))
- return '&'.join(output)
+ return urllib.urlencode(pairs)
def urlencode(self, value):
- if type(value) == unicode:
- value = value.encode("utf-8")
- elif value == None:
- return ""
+ """Replace special characters in string using the %xx escape.
- assert type(value) == str
-
- ret = ""
- for c in value:
- if c == " ":
- c = "+"
- elif ord(c) <= 32 or ord(c) > 127 or c in [ '#', '+',
'"', "'", "=", "&", ":",
"%" ]:
- c = "%%%02x" % ord(c)
- ret += c
- return ret
-
-
- # Like urllib.quote() but also replaces spaces and /
- def urlencode_plus(self, value):
+ This function returns a str object, never unicode!
+ Note: This should be changed once we change everything to
+ unicode internally.
+ """
if type(value) == unicode:
value = value.encode("utf-8")
elif value == None:
@@ -1387,10 +1365,6 @@ class html(HTMLGenerator):
def urlencode(self, value):
return self.encoder.urlencode(value)
- # TODO: Cleanup all call sites to self.encoder.*
- def urlencode_plus(self, value):
- return self.encoder.urlencode_plus(value)
-
#
# escaping - deprecated functions
#
diff --git a/web/plugins/views/painters.py b/web/plugins/views/painters.py
index 2c12297..07ffb00 100644
--- a/web/plugins/views/painters.py
+++ b/web/plugins/views/painters.py
@@ -402,7 +402,7 @@ def replace_action_url_macros(url, what, row):
for key, val in macros.items():
url = url.replace("$%s$" % key, val)
- url = url.replace("$%s_URL_ENCODED$" % key, html.urlencode_plus(val))
+ url = url.replace("$%s_URL_ENCODED$" % key, html.urlencode(val))
return url