Blue avatar Blue committed d1ae5b1

Found better way to handle invalid xml characters, and fixed a bug in updating.

Comments (0)

Files changed (2)

spotnet/connection.py

             # TODO: don't give up so easily
             logger("Could not get raw post, error was %r" % e)
             return False
-        try:
-            snp = Post.from_raw(raw, id=existing_id)
         except InvalidPost as e:
             logger(
                 "Skipped invalid post %s %s: %s"
                 % (postnumber, messageid, e)
             )
             return False
+        snp = Post.from_raw(raw, id=existing_id)
 
         try:
             snp.save()
 from xml.dom.minidom import parseString, Node, Text
 from datetime import datetime, timedelta
 from django.utils.html import escape
-from nzb import decode_nzb, DecodeNzbError
 
 
 class InvalidPost(Exception):
     pass
 
 
+def remove_invalid_chars(string):
+    "Removes ascii control chars."
+    ret = []
+    for char in string:
+        if 0 <= ord(char) <= 31:
+            pass
+        elif ord(char) == 127:
+            pass
+        else:
+            ret.append(char)
+    return ''.join(ret)
+
+
 def wrap_with_cdata(xml_string, tag):
     # TODO: this function does not handle spaces (or attributes) in tags!
     cdata_open = '<![CDATA['
                     wrapped = [
                         xml_string[:open_tag_end],
                         cdata_open,
-                        content.replace('\x10', '').encode('utf8'),
+                        remove_invalid_chars(content),
                         cdata_close,
                         xml_string[close_tag_start:],
                     ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.