encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
(value, terminal_type='vtext')
| 1056 | return fws, newvalue |
| 1057 | |
| 1058 | def get_encoded_word(value, terminal_type='vtext'): |
| 1059 | """ encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" |
| 1060 | |
| 1061 | """ |
| 1062 | ew = EncodedWord() |
| 1063 | if not value.startswith('=?'): |
| 1064 | raise errors.HeaderParseError( |
| 1065 | "expected encoded word but found {}".format(value)) |
| 1066 | tok, *remainder = value[2:].split('?=', 1) |
| 1067 | if tok == value[2:]: |
| 1068 | raise errors.HeaderParseError( |
| 1069 | "expected encoded word but found {}".format(value)) |
| 1070 | remstr = ''.join(remainder) |
| 1071 | if (len(remstr) > 1 and |
| 1072 | remstr[0] in hexdigits and |
| 1073 | remstr[1] in hexdigits and |
| 1074 | tok.count('?') < 2): |
| 1075 | # The ? after the CTE was followed by an encoded word escape (=XX). |
| 1076 | rest, *remainder = remstr.split('?=', 1) |
| 1077 | tok = tok + '?=' + rest |
| 1078 | if len(tok.split()) > 1: |
| 1079 | ew.defects.append(errors.InvalidHeaderDefect( |
| 1080 | "whitespace inside encoded word")) |
| 1081 | ew.cte = value |
| 1082 | value = ''.join(remainder) |
| 1083 | try: |
| 1084 | text, charset, lang, defects = _ew.decode('=?' + tok + '?=') |
| 1085 | except (ValueError, KeyError): |
| 1086 | raise _InvalidEwError( |
| 1087 | "encoded word format invalid: '{}'".format(ew.cte)) |
| 1088 | ew.charset = charset |
| 1089 | ew.lang = lang |
| 1090 | ew.defects.extend(defects) |
| 1091 | while text: |
| 1092 | if text[0] in WSP: |
| 1093 | token, text = get_fws(text) |
| 1094 | ew.append(token) |
| 1095 | continue |
| 1096 | chars, *remainder = _wsp_splitter(text, 1) |
| 1097 | vtext = ValueTerminal(chars, terminal_type) |
| 1098 | _validate_xtext(vtext) |
| 1099 | ew.append(vtext) |
| 1100 | text = ''.join(remainder) |
| 1101 | # Encoded words should be followed by a WS |
| 1102 | if value and value[0] not in WSP: |
| 1103 | ew.defects.append(errors.InvalidHeaderDefect( |
| 1104 | "missing trailing whitespace after encoded-word")) |
| 1105 | return ew, value |
| 1106 | |
| 1107 | def get_unstructured(value): |
| 1108 | """unstructured = (*([FWS] vchar) *WSP) / obs-unstruct |
no test coverage detected