aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/web/_abnf.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-11-12 07:54:50 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-11-12 08:05:59 +0300
commit55cec9f6b0618fb3570fc8ef66aad151f4932591 (patch)
tree9198c2ca0b0305269062c3674ce79f19c4990e65 /contrib/python/Twisted/py3/twisted/web/_abnf.py
parentb77b1fbf262ea4f40e33a60ce32c4db4e5e49015 (diff)
downloadydb-55cec9f6b0618fb3570fc8ef66aad151f4932591.tar.gz
Intermediate changes
commit_hash:c229701a8b4f4d9ee57ce1ed763099d862d53fa6
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/web/_abnf.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/web/_abnf.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/python/Twisted/py3/twisted/web/_abnf.py b/contrib/python/Twisted/py3/twisted/web/_abnf.py
new file mode 100644
index 0000000000..8029943bea
--- /dev/null
+++ b/contrib/python/Twisted/py3/twisted/web/_abnf.py
@@ -0,0 +1,68 @@
+# -*- test-case-name: twisted.web.test.test_abnf -*-
+# Copyright (c) Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+"""
+Tools for pedantically processing the HTTP protocol.
+"""
+
+
+def _istoken(b: bytes) -> bool:
+ """
+ Is the string a token per RFC 9110 section 5.6.2?
+ """
+ for c in b:
+ if c not in (
+ b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # ALPHA
+ b"0123456789" # DIGIT
+ b"!#$%&'*+-.^_`|~"
+ ):
+ return False
+ return b != b""
+
+
+def _decint(data: bytes) -> int:
+ """
+ Parse a decimal integer of the form C{1*DIGIT}, i.e. consisting only of
+ decimal digits. The integer may be embedded in whitespace (space and
+ horizontal tab). This differs from the built-in L{int()} function by
+ disallowing a leading C{+} character and various forms of whitespace
+ (note that we sanitize linear whitespace in header values in
+ L{twisted.web.http_headers.Headers}).
+
+ @param data: Value to parse.
+
+ @returns: A non-negative integer.
+
+ @raises ValueError: When I{value} contains non-decimal characters.
+ """
+ data = data.strip(b" \t")
+ if not data.isdigit():
+ raise ValueError(f"Value contains non-decimal digits: {data!r}")
+ return int(data)
+
+
+def _ishexdigits(b: bytes) -> bool:
+ """
+ Is the string case-insensitively hexidecimal?
+
+ It must be composed of one or more characters in the ranges a-f, A-F
+ and 0-9.
+ """
+ for c in b:
+ if c not in b"0123456789abcdefABCDEF":
+ return False
+ return b != b""
+
+
+def _hexint(b: bytes) -> int:
+ """
+ Decode a hexadecimal integer.
+
+ Unlike L{int(b, 16)}, this raises L{ValueError} when the integer has
+ a prefix like C{b'0x'}, C{b'+'}, or C{b'-'}, which is desirable when
+ parsing network protocols.
+ """
+ if not _ishexdigits(b):
+ raise ValueError(b)
+ return int(b, 16)