encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
(value, terminal_type='vtext')
| 1073 | return fws, newvalue |
| 1074 | |
| 1075 | def get_encoded_word(value, terminal_type='vtext'): |
| 1076 | """ encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" |
| 1077 | |
| 1078 | """ |
| 1079 | ew = EncodedWord() |
| 1080 | if not value.startswith('=?'): |
| 1081 | raise errors.HeaderParseError( |
| 1082 | "expected encoded word but found {}".format(value)) |
| 1083 | tok, *remainder = value[2:].split('?=', 1) |
| 1084 | if tok == value[2:]: |
| 1085 | raise errors.HeaderParseError( |
| 1086 | "expected encoded word but found {}".format(value)) |
| 1087 | remstr = ''.join(remainder) |
| 1088 | if (len(remstr) > 1 and |
| 1089 | remstr[0] in hexdigits and |
| 1090 | remstr[1] in hexdigits and |
| 1091 | tok.count('?') < 2): |
| 1092 | # The ? after the CTE was followed by an encoded word escape (=XX). |
| 1093 | rest, *remainder = remstr.split('?=', 1) |
| 1094 | tok = tok + '?=' + rest |
| 1095 | if len(tok.split()) > 1: |
| 1096 | ew.defects.append(errors.InvalidHeaderDefect( |
| 1097 | "whitespace inside encoded word")) |
| 1098 | ew.cte = value |
| 1099 | value = ''.join(remainder) |
| 1100 | try: |
| 1101 | text, charset, lang, defects = _ew.decode('=?' + tok + '?=') |
| 1102 | except (ValueError, KeyError): |
| 1103 | raise _InvalidEwError( |
| 1104 | "encoded word format invalid: '{}'".format(ew.cte)) |
| 1105 | ew.charset = charset |
| 1106 | ew.lang = lang |
| 1107 | ew.defects.extend(defects) |
| 1108 | while text: |
| 1109 | if text[0] in WSP: |
| 1110 | token, text = get_fws(text) |
| 1111 | ew.append(token) |
| 1112 | continue |
| 1113 | chars, *remainder = _wsp_splitter(text, 1) |
| 1114 | vtext = ValueTerminal(chars, terminal_type) |
| 1115 | _validate_xtext(vtext) |
| 1116 | ew.append(vtext) |
| 1117 | text = ''.join(remainder) |
| 1118 | # Encoded words should be followed by a WS |
| 1119 | if value and value[0] not in WSP: |
| 1120 | ew.defects.append(errors.InvalidHeaderDefect( |
| 1121 | "missing trailing whitespace after encoded-word")) |
| 1122 | return ew, value |
| 1123 | |
| 1124 | def get_unstructured(value): |
| 1125 | """unstructured = (*([FWS] vchar) *WSP) / obs-unstruct |
no test coverage detected
searching dependent graphs…