diff --git a/heart/heartbeat.py b/heart/heartbeat.py index ba4d9ff..e918f9d 100644 --- a/heart/heartbeat.py +++ b/heart/heartbeat.py @@ -57,6 +57,24 @@ def stripnul(s): idx = s.find("\x00") return (s if idx == -1 else s[:idx]) +def replace_char_all(s, f, t): + v = ord(f) + s = s.replace(f, t) + s = s.replace(chr(0xC0 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xE0) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xF0) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xF8) + chr(0x80) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xFC) + chr(0x80) + chr(0x80) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + # TODO: handle the 6-bit and 8-bit variants and whatnot + return s + +def sanestr(s): + s = str(s) + s = replace_char_all(s, "&", "&") + s = replace_char_all(s, "<", "<") + s = replace_char_all(s, ">", ">") + return s + class HTTPClient: def __init__(self, ct, reactor, server, sockfd): self.reactor = reactor @@ -95,13 +113,13 @@ class HTTPClient: s += "\n" for d in l: s += "