Try to read the Header information from the internal buffer expecting the last header to contain '\r\n\r\n'. Exceptions: LookupError The content-length header was not found. ValueError The content-length contained a inv
(self)
| 332 | raise |
| 333 | |
| 334 | def try_read_headers(self): |
| 335 | """ |
| 336 | Try to read the Header information from the internal buffer expecting the last |
| 337 | header to contain '\r\n\r\n'. |
| 338 | Exceptions: |
| 339 | LookupError |
| 340 | The content-length header was not found. |
| 341 | ValueError |
| 342 | The content-length contained a invalid literal for int. |
| 343 | """ |
| 344 | # Scan the buffer up until right before the CRLFCRLF. |
| 345 | scan_offset = self.read_offset |
| 346 | while (scan_offset + 3 < self.buffer_end_offset and |
| 347 | (self.buffer[scan_offset] != self.CR or |
| 348 | self.buffer[scan_offset + 1] != self.LF or |
| 349 | self.buffer[scan_offset + 2] != self.CR or |
| 350 | self.buffer[scan_offset + 3] != self.LF)): |
| 351 | scan_offset += 1 |
| 352 | |
| 353 | # if we reached the end |
| 354 | if scan_offset + 3 >= self.buffer_end_offset: |
| 355 | return False |
| 356 | |
| 357 | # Split the headers by new line |
| 358 | try: |
| 359 | headers_read = self.buffer[self.read_offset:scan_offset].decode( |
| 360 | u'ascii') |
| 361 | for header in headers_read.split(u'\n'): |
| 362 | colon_index = header.find(u':') |
| 363 | |
| 364 | if colon_index == -1: |
| 365 | logger.debug( |
| 366 | u'JSON RPC Reader encountered missing colons in try_read_headers()') |
| 367 | raise KeyError( |
| 368 | u'Colon missing from Header: {}.'.format(header)) |
| 369 | |
| 370 | # Case insensitive. |
| 371 | header_key = header[:colon_index].lower() |
| 372 | header_value = header[colon_index + 1:] |
| 373 | |
| 374 | self.headers[header_key] = header_value |
| 375 | |
| 376 | # Was content-length header found? |
| 377 | if 'content-length' not in self.headers: |
| 378 | logger.debug( |
| 379 | u'JSON RPC Reader did not find Content-Length in the headers') |
| 380 | raise LookupError( |
| 381 | u'Content-Length was not found in headers received.') |
| 382 | |
| 383 | self.expected_content_length = int(self.headers[u'content-length']) |
| 384 | |
| 385 | except ValueError: |
| 386 | # Content-length contained invalid literal for int. |
| 387 | self.trim_buffer_and_resize(scan_offset + 4) |
| 388 | raise |
| 389 | |
| 390 | # Pushing read pointer past the newline characters. |
| 391 | self.read_offset = scan_offset + 4 |