diff options
| author | Yuval Adam <_@yuv.al> | 2018-02-19 21:51:57 +0100 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-02-19 21:51:57 +0100 |
| commit | 945dadfae8047c8c74d8b2b50486156198926568 (patch) | |
| tree | 79cdf234b9328ff6623a357f1eccd6ee2fd1ad80 | |
| parent | b78bb2cc81bfa47ac96ea5233eb7cbaedbb38934 (diff) | |
Add string support
| -rw-r--r-- | tests/test_core.py | 8 | ||||
| -rw-r--r-- | viewstate/core.py | 7 |
2 files changed, 15 insertions, 0 deletions
diff --git a/tests/test_core.py b/tests/test_core.py index 1b132ac..be4c587 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -29,6 +29,12 @@ class TestViewState(object): vs.raw = b'\xff\x01\x67' assert vs.decode() is True + def test_parse_string_value(self): + vs = ViewState() + s = 'abcdefghij' + vs.raw = b'\xff\x01\x05\x0a' + s.encode() + assert vs.decode() == s + def test_parse_simple_pair(self): vs = ViewState() vs.raw = b'\xff\x01\x0f\x67\x68' @@ -40,6 +46,8 @@ class TestViewState(object): assert vs.decode() == (True, (False, 0)) vs.raw = b'\xff\x01\x0f\x0f\x67\x68\x66' assert vs.decode() == ((True, False), 0) + vs.raw = b'\xff\x01\x0f\x0f\x67\x05\x061q2w3e\x66' + assert vs.decode() == ((True, '1q2w3e'), 0) def test_parse_unknown(self): vs = ViewState() diff --git a/viewstate/core.py b/viewstate/core.py index e060a11..fb78743 100644 --- a/viewstate/core.py +++ b/viewstate/core.py @@ -14,6 +14,11 @@ CONSTS = { def parse_const(b): return CONSTS.get(b, None) +def parse_string(b): + n = b[0] + s = b[1:n+1] + return s.decode(), b[n+1:] + def parse_pair(b): first, remain = _parse(b) second, remain = _parse(remain) @@ -29,6 +34,8 @@ def _parse(b): if 100 <= b[0] <= 104: return parse_const(b[0]), b[1:] + elif b[0] == 0x5: + return parse_string(b[1:]) elif b[0] == 0xf: return parse_pair(b[1:]) else: |
