Exemple #1
0
 def check_mime(self, ctx):
     ctx.encoding = None
     # MIME-Version:
     mime_versions = ctx.metadata['MIME-Version']
     if len(mime_versions) > 1:
         self.tag('duplicate-header-field-mime-version')
         mime_versions = sorted(set(mime_versions))
     for mime_version in mime_versions:
         if mime_version != '1.0':
             self.tag('invalid-mime-version', mime_version, '=>', '1.0')
     if len(mime_versions) == 0:
         self.tag('no-mime-version-header-field',
                  tags.safestr('MIME-Version: 1.0'))
     # Content-Transfer-Encoding:
     ctes = ctx.metadata['Content-Transfer-Encoding']
     if len(ctes) > 1:
         self.tag('duplicate-header-field-content-transfer-encoding')
         ctes = sorted(set(ctes))
     for cte in ctes:
         if cte != '8bit':
             self.tag('invalid-content-transfer-encoding', cte, '=>',
                      '8bit')
     if len(ctes) == 0:
         self.tag('no-content-transfer-encoding-header-field',
                  tags.safestr('Content-Transfer-Encoding: 8bit'))
     # Content-Type:
     cts = ctx.metadata['Content-Type']
     if len(cts) > 1:
         self.tag('duplicate-header-field-content-type')
         cts = sorted(set(cts))
     elif len(cts) == 0:
         content_type_hint = 'text/plain; charset=<encoding>'
         self.tag('no-content-type-header-field',
                  tags.safestr('Content-Type: ' + content_type_hint))
         return
     encodings = set()
     for ct in cts:
         content_type_hint = 'text/plain; charset=<encoding>'
         match = re.search(r'(\Atext/plain; )?\bcharset=([^\s;]+)\Z', ct)
         if match:
             encoding = match.group(2)
             try:
                 is_ascii_compatible = encinfo.is_ascii_compatible_encoding(
                     encoding, missing_ok=False)
             except encinfo.EncodingLookupError:
                 if encoding == 'CHARSET':
                     if not ctx.is_template:
                         self.tag('boilerplate-in-content-type', ct)
                 else:
                     self.tag('unknown-encoding', encoding)
                 encoding = None
             else:
                 if not is_ascii_compatible:
                     self.tag('non-ascii-compatible-encoding', encoding)
                 elif encinfo.is_portable_encoding(encoding):
                     pass
                 else:
                     new_encoding = encinfo.propose_portable_encoding(
                         encoding)
                     if new_encoding is not None:
                         self.tag('non-portable-encoding', encoding, '=>',
                                  new_encoding)
                         encoding = new_encoding
                     else:
                         self.tag('non-portable-encoding', encoding)
                 if ctx.language is not None:
                     unrepresentable_characters = ctx.language.get_unrepresentable_characters(
                         encoding)
                     if unrepresentable_characters:
                         if len(unrepresentable_characters) > 5:
                             unrepresentable_characters[4:] = ['...']
                         self.tag('unrepresentable-characters', encoding,
                                  *unrepresentable_characters)
             if match.group(1) is None:
                 if encoding is not None:
                     content_type_hint = content_type_hint.replace(
                         '<encoding>', encoding)
                 self.tag('invalid-content-type', ct, '=>',
                          content_type_hint)
             if encoding is not None:
                 encodings.add(encoding)
         else:
             self.tag('invalid-content-type', ct, '=>', content_type_hint)
     if len(encodings) == 1:
         [ctx.encoding] = encodings
Exemple #2
0
 def test_found_nonpython(self):
     assert_false(E.is_portable_encoding('KOI8-T'))
     assert_true(E.is_portable_encoding('KOI8-T', python=False))
Exemple #3
0
 def test_notfound(self):
     assert_false(E.is_portable_encoding('ISO-8859-16'))
     assert_false(E.is_portable_encoding('ISO-8859-16', python=False))
Exemple #4
0
 def test_found_(self):
     assert_true(E.is_portable_encoding('ISO_8859-2'))