| 328 | return version, status, reason |
| 329 | |
| 330 | def begin(self, *, _max_headers=None): |
| 331 | if self.headers is not None: |
| 332 | # we've already started reading the response |
| 333 | return |
| 334 | |
| 335 | # read until we get a non-100 response |
| 336 | while True: |
| 337 | version, status, reason = self._read_status() |
| 338 | if status != CONTINUE: |
| 339 | break |
| 340 | # skip the header from the 100 response |
| 341 | skipped_headers = _read_headers(self.fp, _max_headers) |
| 342 | if self.debuglevel > 0: |
| 343 | print("headers:", skipped_headers) |
| 344 | del skipped_headers |
| 345 | |
| 346 | self.code = self.status = status |
| 347 | self.reason = reason.strip() |
| 348 | if version in ("HTTP/1.0", "HTTP/0.9"): |
| 349 | # Some servers might still return "0.9", treat it as 1.0 anyway |
| 350 | self.version = 10 |
| 351 | elif version.startswith("HTTP/1."): |
| 352 | self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 |
| 353 | else: |
| 354 | raise UnknownProtocol(version) |
| 355 | |
| 356 | self.headers = self.msg = parse_headers( |
| 357 | self.fp, _max_headers=_max_headers |
| 358 | ) |
| 359 | |
| 360 | if self.debuglevel > 0: |
| 361 | for hdr, val in self.headers.items(): |
| 362 | print("header:", hdr + ":", val) |
| 363 | |
| 364 | # are we using the chunked-style of transfer encoding? |
| 365 | tr_enc = self.headers.get("transfer-encoding") |
| 366 | if tr_enc and tr_enc.lower() == "chunked": |
| 367 | self.chunked = True |
| 368 | self.chunk_left = None |
| 369 | else: |
| 370 | self.chunked = False |
| 371 | |
| 372 | # will the connection close at the end of the response? |
| 373 | self.will_close = self._check_close() |
| 374 | |
| 375 | # do we have a Content-Length? |
| 376 | # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" |
| 377 | self.length = None |
| 378 | length = self.headers.get("content-length") |
| 379 | if length and not self.chunked: |
| 380 | try: |
| 381 | self.length = int(length) |
| 382 | except ValueError: |
| 383 | self.length = None |
| 384 | else: |
| 385 | if self.length < 0: # ignore nonsensical negative lengths |
| 386 | self.length = None |
| 387 | else: |