| 282 | self._buffer += r |
| 283 | |
| 284 | def run(self): |
| 285 | try: |
| 286 | content_len = -1 |
| 287 | |
| 288 | while not self._kill: |
| 289 | line = self._read_line() |
| 290 | |
| 291 | if not line: |
| 292 | break |
| 293 | |
| 294 | if SHOW_WRITES_AND_READS: |
| 295 | show_line = line |
| 296 | show_line = line.decode("utf-8") |
| 297 | |
| 298 | print( |
| 299 | "%s Received %s" |
| 300 | % ( |
| 301 | self.name, |
| 302 | show_line, |
| 303 | ) |
| 304 | ) |
| 305 | |
| 306 | if line.startswith(b"Content-Length:"): |
| 307 | content_len = int(line.strip().split(b":", 1)[1]) |
| 308 | continue |
| 309 | |
| 310 | if content_len != -1: |
| 311 | # If we previously received a content length, read until a '\r\n'. |
| 312 | if line == b"\r\n": |
| 313 | json_contents = self._read(content_len) |
| 314 | content_len = -1 |
| 315 | |
| 316 | if len(json_contents) == 0: |
| 317 | self.handle_except() |
| 318 | return # Finished communication. |
| 319 | |
| 320 | msg = json_contents |
| 321 | msg = msg.decode("utf-8") |
| 322 | print("Test Reader Thread Received %s" % (msg,)) |
| 323 | self._queue.put(msg) |
| 324 | |
| 325 | continue |
| 326 | else: |
| 327 | # No content len, regular line-based protocol message (remove trailing new-line). |
| 328 | if line.endswith(b"\n\n"): |
| 329 | line = line[:-2] |
| 330 | |
| 331 | elif line.endswith(b"\n"): |
| 332 | line = line[:-1] |
| 333 | |
| 334 | elif line.endswith(b"\r"): |
| 335 | line = line[:-1] |
| 336 | |
| 337 | msg = line |
| 338 | msg = msg.decode("utf-8") |
| 339 | print("Test Reader Thread Received %s" % (msg,)) |
| 340 | self._queue.put(msg) |
| 341 | |