comparison sat/plugins/plugin_comp_ap_gateway/regex.py @ 3741:eddab3798aca

comp AP gateway: regular expression to parse `Signature` header: rel 364
author Goffi <goffi@goffi.org>
date Tue, 22 Mar 2022 17:00:42 +0100
parents
children 381340b9a9ee
comparison
equal deleted inserted replaced
3740:ea6fda69bb9f 3741:eddab3798aca
1 #!/usr/bin/env python3
2
3 # Libervia ActivityPub Gateway
4 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 """Regular Expression to parse "Signature" header"""
20
21 # those expression have been generated with abnf-to-regex
22 # (https://github.com/aas-core-works/abnf-to-regexp)
23
24 # the base RFC 7320 ABNF rules come from https://github.com/EricGT/ABNF
25
26 # here is the ABNF file used:
27 # ---
28 # BWS = OWS
29 # OWS = *( SP / HTAB )
30 # tchar = "!" / "#" / "$" / "%" / "&" / "`" / "*" / "+" / "-" / "." / "^" / "_" / "\'" / "|" / "~" / DIGIT / ALPHA
31 # token = 1*tchar
32 # sig-param = token BWS "=" BWS ( token / quoted-string )
33 # quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
34 # qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'['
35 # / %x5D-7E ; ']'-'~'
36 # / obs-text
37 # quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
38 # obs-text = %x80-FF
39 # ---
40
41 import re
42
43 ows = '[ \t]*'
44 bws = f'{ows}'
45 obs_text = '[\\x80-\\xff]'
46 qdtext = f'([\t !#-\\[\\]-~]|{obs_text})'
47 quoted_pair = f'\\\\([\t !-~]|{obs_text})'
48 quoted_string = f'"({qdtext}|{quoted_pair})*"'
49 tchar = "([!#$%&`*+\\-.^_]|\\\\'|[|~0-9a-zA-Z])"
50 token = f'({tchar})+'
51 RE_SIG_PARAM = re.compile(
52 f'(?P<key>{token}{bws})={bws}'
53 f'((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))'
54 )