decode with correct encoding, relies on header
(self, rep)
| 261 | return self.rep.getvalue() |
| 262 | |
| 263 | def decodeResponse(self, rep): |
| 264 | """ decode with correct encoding, relies on header """ |
| 265 | header = self.header.splitlines() |
| 266 | encoding = "utf8" # default encoding |
| 267 | |
| 268 | for line in header: |
| 269 | line = line.lower().replace(" ", "") |
| 270 | if not line.startswith("content-type:") or\ |
| 271 | ("text" not in line and "application" not in line): |
| 272 | continue |
| 273 | |
| 274 | none, delemiter, charset = line.rpartition("charset=") |
| 275 | if delemiter: |
| 276 | charset = charset.split(";") |
| 277 | if charset: |
| 278 | encoding = charset[0] |
| 279 | |
| 280 | try: |
| 281 | #self.log.debug("Decoded %s" % encoding ) |
| 282 | if lookup(encoding).name == 'utf-8' and rep.startswith(BOM_UTF8): |
| 283 | encoding = 'utf-8-sig' |
| 284 | |
| 285 | decoder = getincrementaldecoder(encoding)("replace") |
| 286 | rep = decoder.decode(rep, True) |
| 287 | |
| 288 | #TODO: html_unescape as default |
| 289 | |
| 290 | except LookupError: |
| 291 | self.log.debug("No Decoder foung for %s" % encoding) |
| 292 | except Exception: |
| 293 | self.log.debug("Error when decoding string from %s." % encoding) |
| 294 | |
| 295 | return rep |
| 296 | |
| 297 | def write(self, buf): |
| 298 | """ writes response """ |