summaryrefslogtreecommitdiff
path: root/waitress/rfc7230.py
diff options
context:
space:
mode:
authorBert JW Regeer <bertjw@regeer.org>2019-12-23 14:36:41 +0100
committerBert JW Regeer <bertjw@regeer.org>2019-12-23 14:36:41 +0100
commit2a11d6812f703e01150c2aedbca7ec2a6590f254 (patch)
treef6fc297dbeb725eb76c6d35cc0ca10148da4af57 /waitress/rfc7230.py
parentf11093a6b3240fc26830b6111e826128af7771c3 (diff)
downloadwaitress-2a11d6812f703e01150c2aedbca7ec2a6590f254.tar.gz
Add file with RFC7230 definitions/ABNF -> regex
Currently it is used for header name/fields only, but this should get expanded as necessary. Other stuff from other parts of waitress may move here too to create a sort utility library of useful RFC standards based functions.
Diffstat (limited to 'waitress/rfc7230.py')
-rw-r--r--waitress/rfc7230.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/waitress/rfc7230.py b/waitress/rfc7230.py
new file mode 100644
index 0000000..a9f047c
--- /dev/null
+++ b/waitress/rfc7230.py
@@ -0,0 +1,44 @@
+"""
+This contains a bunch of RFC7230 definitions and regular expressions that are
+needed to properly parse HTTP messages.
+"""
+
+import re
+
+from .compat import tobytes
+
+WS = "[ \t]"
+OWS = WS + "{0,}?"
+RWS = WS + "{1,}?"
+BWS = OWS
+
+# RFC 7230 Section 3.2.6 "Field Value Components":
+# tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
+# / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
+# / DIGIT / ALPHA
+# obs-text = %x80-FF
+TCHAR = r"[!#$%&'*+\-.^_`|~0-9A-Za-z]"
+OBS_TEXT = r"\x80-\xff"
+
+TOKEN = TCHAR + "{1,}"
+
+# RFC 5234 Appendix B.1 "Core Rules":
+# VCHAR = %x21-7E
+# ; visible (printing) characters
+VCHAR = r"\x21-\x7e"
+
+# header-field = field-name ":" OWS field-value OWS
+# field-name = token
+# field-value = *( field-content / obs-fold )
+# field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
+# field-vchar = VCHAR / obs-text
+
+FIELD_VCHAR = "[" + VCHAR + OBS_TEXT + "]"
+FIELD_CONTENT = FIELD_VCHAR + "(" + RWS + FIELD_VCHAR + "){0,}"
+FIELD_VALUE = "(" + FIELD_CONTENT + "){0,}"
+
+HEADER_FIELD = re.compile(
+ tobytes(
+ "^(?P<name>" + TOKEN + "):" + OWS + "(?P<value>" + FIELD_VALUE + ")" + OWS + "$"
+ )
+)