Debian Codegraph @

Lib/http/client.py

raw
1 r"""HTTP/1.1 client library
2
3 <intro stuff goes here>
4 <other stuff, too>
5
6 HTTPConnection goes through a number of "states", which define when a client
7 may legally make another request or fetch the response for a particular
8 request. This diagram details these state transitions:
9
10 (null)
11 |
12 | HTTPConnection()
13 v
14 Idle
15 |
16 | putrequest()
17 v
18 Request-started
19 |
20 | ( putheader() )* endheaders()
21 v
22 Request-sent
23 |\_____________________________
24 | | getresponse() raises
25 | response = getresponse() | ConnectionError
26 v v
27 Unread-response Idle
28 [Response-headers-read]
29 |\____________________
30 | |
31 | response.read() | putrequest()
32 v v
33 Idle Req-started-unread-response
34 ______/|
35 / |
36 response.read() | | ( putheader() )* endheaders()
37 v v
38 Request-started Req-sent-unread-response
39 |
40 | response.read()
41 v
42 Request-sent
43
44 This diagram presents the following rules:
45 -- a second request may not be started until {response-headers-read}
46 -- a response [object] cannot be retrieved until {request-sent}
47 -- there is no differentiation between an unread response body and a
48 partially read response body
49
50 Note: this enforcement is applied by the HTTPConnection class. The
51 HTTPResponse class does not enforce this state machine, which
52 implies sophisticated clients may accelerate the request/response
53 pipeline. Caution should be taken, though: accelerating the states
54 beyond the above pattern may imply knowledge of the server's
55 connection-close behavior for certain requests. For example, it
56 is impossible to tell whether the server will close the connection
57 UNTIL the response headers have been read; this means that further
58 requests cannot be placed into the pipeline until it is known that
59 the server will NOT be closing the connection.
60
61 Logical State __state __response
62 ------------- ------- ----------
63 Idle _CS_IDLE None
64 Request-started _CS_REQ_STARTED None
65 Request-sent _CS_REQ_SENT None
66 Unread-response _CS_IDLE <response_class>
67 Req-started-unread-response _CS_REQ_STARTED <response_class>
68 Req-sent-unread-response _CS_REQ_SENT <response_class>
69 """
70
71 import email.parser
72 import email.message
73 import errno
74 import http
75 import io
76 import re
77 import socket
78 import sys
79 import collections.abc
80 from urllib.parse import urlsplit
81
82 # HTTPMessage, parse_headers(), and the HTTP status code constants are
83 # intentionally omitted for simplicity
84 __all__ = ["HTTPResponse", "HTTPConnection",
85 "HTTPException", "NotConnected", "UnknownProtocol",
86 "UnknownTransferEncoding", "UnimplementedFileMode",
87 "IncompleteRead", "InvalidURL", "ImproperConnectionState",
88 "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
89 "BadStatusLine", "LineTooLong", "RemoteDisconnected", "error",
90 "responses"]
91
92 HTTP_PORT = 80
93 HTTPS_PORT = 443
94
95 _UNKNOWN = 'UNKNOWN'
96
97 # connection states
98 _CS_IDLE = 'Idle'
99 _CS_REQ_STARTED = 'Request-started'
100 _CS_REQ_SENT = 'Request-sent'
101
102
103 # hack to maintain backwards compatibility
104 globals().update(http.HTTPStatus.__members__)
105
106 # another hack to maintain backwards compatibility
107 # Mapping status codes to official W3C names
108 responses = {v: v.phrase for v in http.HTTPStatus.__members__.values()}
109
110 # maximal line length when calling readline().
111 _MAXLINE = 65536
112 _MAXHEADERS = 100
113
114 # maximal number of interim (1xx) responses tolerated before the final
115 # response. Real servers send at most a few; without a bound, a server
116 # streaming "100 Continue" responses would hang getresponse() forever.
117 # A socket timeout cannot detect that as data keeps arriving within every
118 # timeout window.
119 _MAXINTERIMRESPONSES = 100
120
121 # Data larger than this will be read in chunks, to prevent extreme
122 # overallocation.
123 _MIN_READ_BUF_SIZE = 1 << 20
124
125
126 # Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
127 #
128 # VCHAR = %x21-7E
129 # obs-text = %x80-FF
130 # header-field = field-name ":" OWS field-value OWS
131 # field-name = token
132 # field-value = *( field-content / obs-fold )
133 # field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
134 # field-vchar = VCHAR / obs-text
135 #
136 # obs-fold = CRLF 1*( SP / HTAB )
137 # ; obsolete line folding
138 # ; see Section 3.2.4
139
140 # token = 1*tchar
141 #
142 # tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
143 # / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
144 # / DIGIT / ALPHA
145 # ; any VCHAR, except delimiters
146 #
147 # VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
148
149 # the patterns for both name and value are more lenient than RFC
150 # definitions to allow for backwards compatibility
151 _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
152 _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
153
154 # These characters are not allowed within HTTP URL paths.
155 # See https://tools.ietf.org/html/rfc3986#section-3.3 and the
156 # https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.
157 # Prevents CVE-2019-9740. Includes control characters such as \r\n.
158 # We don't restrict chars above \x7f as putrequest() limits us to ASCII.
159 _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
160 # Arguably only these _should_ allowed:
161 # _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
162 # We are more lenient for assumed real world compatibility purposes.
163
164 # These characters are not allowed within HTTP method names
165 # to prevent http header injection.
166 _contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
167
168 # We always set the Content-Length header for these methods because some
169 # servers will otherwise respond with a 411
170 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
171
172
173 def _encode(data, name='data'):
174 """Call data.encode("latin-1") but show a better error message."""
175 try:
176 return data.encode("latin-1")
177 except UnicodeEncodeError as err:
178 raise UnicodeEncodeError(
179 err.encoding,
180 err.object,
181 err.start,
182 err.end,
183 "%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') "
184 "if you want to send it encoded in UTF-8." %
185 (name.title(), data[err.start:err.end], name)) from None
186
187 def _strip_ipv6_iface(enc_name: bytes) -> bytes:
188 """Remove interface scope from IPv6 address."""
189 enc_name, percent, _ = enc_name.partition(b"%")
190 if percent:
191 assert enc_name.startswith(b'['), enc_name
192 enc_name += b']'
193 return enc_name
194
195 class HTTPMessage(email.message.Message):
196 # XXX The only usage of this method is in
197 # http.server.CGIHTTPRequestHandler. Maybe move the code there so
198 # that it doesn't need to be part of the public API. The API has
199 # never been defined so this could cause backwards compatibility
200 # issues.
201
202 def getallmatchingheaders(self, name):
203 """Find all header lines matching a given header name.
204
205 Look through the list of headers and find all lines matching a given
206 header name (and their continuation lines). A list of the lines is
207 returned, without interpretation. If the header does not occur, an
208 empty list is returned. If the header occurs multiple times, all
209 occurrences are returned. Case is not important in the header name.
210
211 """
212 name = name.lower() + ':'
213 n = len(name)
214 lst = []
215 hit = 0
216 for line in self.keys():
217 if line[:n].lower() == name:
218 hit = 1
219 elif not line[:1].isspace():
220 hit = 0
221 if hit:
222 lst.append(line)
223 return lst
224
225 def _read_headers(fp):
226 """Reads potential header lines into a list from a file pointer.
227
228 Length of line is limited by _MAXLINE, and number of
229 headers is limited by _MAXHEADERS.
230 """
231 headers = []
232 while True:
233 line = fp.readline(_MAXLINE + 1)
234 if len(line) > _MAXLINE:
235 raise LineTooLong("header line")
236 headers.append(line)
237 if len(headers) > _MAXHEADERS:
238 raise HTTPException("got more than %d headers" % _MAXHEADERS)
239 if line in (b'\r\n', b'\n', b''):
240 break
241 return headers
242
243 def _parse_header_lines(header_lines, _class=HTTPMessage):
244 """
245 Parses only RFC 5322 headers from header lines.
246
247 email Parser wants to see strings rather than bytes.
248 But a TextIOWrapper around self.rfile would buffer too many bytes
249 from the stream, bytes which we later need to read as bytes.
250 So we read the correct bytes here, as bytes, for email Parser
251 to parse.
252
253 """
254 hstring = b''.join(header_lines).decode('iso-8859-1')
255 return email.parser.Parser(_class=_class).parsestr(hstring)
256
257 def parse_headers(fp, _class=HTTPMessage):
258 """Parses only RFC 5322 headers from a file pointer."""
259
260 headers = _read_headers(fp)
261 return _parse_header_lines(headers, _class)
262
263
264 class HTTPResponse(io.BufferedIOBase):
265
266 # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
267
268 # The bytes from the socket object are iso-8859-1 strings.
269 # See RFC 2616 sec 2.2 which notes an exception for MIME-encoded
270 # text following RFC 2047. The basic status line parsing only
271 # accepts iso-8859-1.
272
273 def __init__(self, sock, debuglevel=0, method=None, url=None):
274 # If the response includes a content-length header, we need to
275 # make sure that the client doesn't read more than the
276 # specified number of bytes. If it does, it will block until
277 # the server times out and closes the connection. This will
278 # happen if a self.fp.read() is done (without a size) whether
279 # self.fp is buffered or not. So, no self.fp.read() by
280 # clients unless they know what they are doing.
281 self.fp = sock.makefile("rb")
282 self.debuglevel = debuglevel
283 self._method = method
284
285 # The HTTPResponse object is returned via urllib. The clients
286 # of http and urllib expect different attributes for the
287 # headers. headers is used here and supports urllib. msg is
288 # provided as a backwards compatibility layer for http
289 # clients.
290
291 self.headers = self.msg = None
292
293 # from the Status-Line of the response
294 self.version = _UNKNOWN # HTTP-Version
295 self.status = _UNKNOWN # Status-Code
296 self.reason = _UNKNOWN # Reason-Phrase
297
298 self.chunked = _UNKNOWN # is "chunked" being used?
299 self.chunk_left = _UNKNOWN # bytes left to read in current chunk
300 self.length = _UNKNOWN # number of bytes left in response
301 self.will_close = _UNKNOWN # conn will close at end of response
302
303 def _read_status(self):
304 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
305 if len(line) > _MAXLINE:
306 raise LineTooLong("status line")
307 if self.debuglevel > 0:
308 print("reply:", repr(line))
309 if not line:
310 # Presumably, the server closed the connection before
311 # sending a valid response.
312 raise RemoteDisconnected("Remote end closed connection without"
313 " response")
314 try:
315 version, status, reason = line.split(None, 2)
316 except ValueError:
317 try:
318 version, status = line.split(None, 1)
319 reason = ""
320 except ValueError:
321 # empty version will cause next test to fail.
322 version = ""
323 if not version.startswith("HTTP/"):
324 self._close_conn()
325 raise BadStatusLine(line)
326
327 # The status code is a three-digit number
328 try:
329 status = int(status)
330 if status < 100 or status > 999:
331 raise BadStatusLine(line)
332 except ValueError:
333 raise BadStatusLine(line)
334 return version, status, reason
335
336 def begin(self):
337 if self.headers is not None:
338 # we've already started reading the response
339 return
340
341 # read until we get a non-100 response
342 for _ in range(_MAXINTERIMRESPONSES):
343 version, status, reason = self._read_status()
344 if status != CONTINUE:
345 break
346 # skip the header from the 100 response
347 skipped_headers = _read_headers(self.fp)
348 if self.debuglevel > 0:
349 print("headers:", skipped_headers)
350 del skipped_headers
351 else:
352 raise HTTPException(
353 f"got more than {_MAXINTERIMRESPONSES} interim responses")
354
355 self.code = self.status = status
356 self.reason = reason.strip()
357 if version in ("HTTP/1.0", "HTTP/0.9"):
358 # Some servers might still return "0.9", treat it as 1.0 anyway
359 self.version = 10
360 elif version.startswith("HTTP/1."):
361 self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
362 else:
363 raise UnknownProtocol(version)
364
365 self.headers = self.msg = parse_headers(self.fp)
366
367 if self.debuglevel > 0:
368 for hdr, val in self.headers.items():
369 print("header:", hdr + ":", val)
370
371 # are we using the chunked-style of transfer encoding?
372 tr_enc = self.headers.get("transfer-encoding")
373 if tr_enc and tr_enc.lower() == "chunked":
374 self.chunked = True
375 self.chunk_left = None
376 else:
377 self.chunked = False
378
379 # will the connection close at the end of the response?
380 self.will_close = self._check_close()
381
382 # do we have a Content-Length?
383 # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
384 self.length = None
385 length = self.headers.get("content-length")
386 if length and not self.chunked:
387 try:
388 self.length = int(length)
389 except ValueError:
390 self.length = None
391 else:
392 if self.length < 0: # ignore nonsensical negative lengths
393 self.length = None
394 else:
395 self.length = None
396
397 # does the body have a fixed length? (of zero)
398 if (status == NO_CONTENT or status == NOT_MODIFIED or
399 100 <= status < 200 or # 1xx codes
400 self._method == "HEAD"):
401 self.length = 0
402
403 # if the connection remains open, and we aren't using chunked, and
404 # a content-length was not provided, then assume that the connection
405 # WILL close.
406 if (not self.will_close and
407 not self.chunked and
408 self.length is None):
409 self.will_close = True
410
411 def _check_close(self):
412 conn = self.headers.get("connection")
413 if self.version == 11:
414 # An HTTP/1.1 proxy is assumed to stay open unless
415 # explicitly closed.
416 if conn and "close" in conn.lower():
417 return True
418 return False
419
420 # Some HTTP/1.0 implementations have support for persistent
421 # connections, using rules different than HTTP/1.1.
422
423 # For older HTTP, Keep-Alive indicates persistent connection.
424 if self.headers.get("keep-alive"):
425 return False
426
427 # At least Akamai returns a "Connection: Keep-Alive" header,
428 # which was supposed to be sent by the client.
429 if conn and "keep-alive" in conn.lower():
430 return False
431
432 # Proxy-Connection is a netscape hack.
433 pconn = self.headers.get("proxy-connection")
434 if pconn and "keep-alive" in pconn.lower():
435 return False
436
437 # otherwise, assume it will close
438 return True
439
440 def _close_conn(self):
441 fp = self.fp
442 self.fp = None
443 fp.close()
444
445 def close(self):
446 try:
447 super().close() # set "closed" flag
448 finally:
449 if self.fp:
450 self._close_conn()
451
452 # These implementations are for the benefit of io.BufferedReader.
453
454 # XXX This class should probably be revised to act more like
455 # the "raw stream" that BufferedReader expects.
456
457 def flush(self):
458 super().flush()
459 if self.fp:
460 self.fp.flush()
461
462 def readable(self):
463 """Always returns True"""
464 return True
465
466 # End of "raw stream" methods
467
468 def isclosed(self):
469 """True if the connection is closed."""
470 # NOTE: it is possible that we will not ever call self.close(). This
471 # case occurs when will_close is TRUE, length is None, and we
472 # read up to the last byte, but NOT past it.
473 #
474 # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
475 # called, meaning self.isclosed() is meaningful.
476 return self.fp is None
477
478 def read(self, amt=None):
479 """Read and return the response body, or up to the next amt bytes."""
480 if self.fp is None:
481 return b""
482
483 if self._method == "HEAD":
484 self._close_conn()
485 return b""
486
487 if self.chunked:
488 return self._read_chunked(amt)
489
490 if amt is not None and amt >= 0:
491 if self.length is not None and amt > self.length:
492 # clip the read to the "end of response"
493 amt = self.length
494 s = self.fp.read(amt)
495 if not s and amt:
496 # Ideally, we would raise IncompleteRead if the content-length
497 # wasn't satisfied, but it might break compatibility.
498 self._close_conn()
499 elif self.length is not None:
500 self.length -= len(s)
501 if not self.length:
502 self._close_conn()
503 return s
504 else:
505 # Amount is not given (unbounded read) so we must check self.length
506 if self.length is None:
507 s = self.fp.read()
508 else:
509 try:
510 s = self._safe_read(self.length)
511 except IncompleteRead:
512 self._close_conn()
513 raise
514 self.length = 0
515 self._close_conn() # we read everything
516 return s
517
518 def readinto(self, b):
519 """Read up to len(b) bytes into bytearray b and return the number
520 of bytes read.
521 """
522
523 if self.fp is None:
524 return 0
525
526 if self._method == "HEAD":
527 self._close_conn()
528 return 0
529
530 if self.chunked:
531 return self._readinto_chunked(b)
532
533 if self.length is not None:
534 if len(b) > self.length:
535 # clip the read to the "end of response"
536 b = memoryview(b)[0:self.length]
537
538 # we do not use _safe_read() here because this may be a .will_close
539 # connection, and the user is reading more bytes than will be provided
540 # (for example, reading in 1k chunks)
541 n = self.fp.readinto(b)
542 if not n and b:
543 # Ideally, we would raise IncompleteRead if the content-length
544 # wasn't satisfied, but it might break compatibility.
545 self._close_conn()
546 elif self.length is not None:
547 self.length -= n
548 if not self.length:
549 self._close_conn()
550 return n
551
552 def _read_next_chunk_size(self):
553 # Read the next chunk size from the file
554 line = self.fp.readline(_MAXLINE + 1)
555 if len(line) > _MAXLINE:
556 raise LineTooLong("chunk size")
557 i = line.find(b";")
558 if i >= 0:
559 line = line[:i] # strip chunk-extensions
560 try:
561 return int(line, 16)
562 except ValueError:
563 # close the connection as protocol synchronisation is
564 # probably lost
565 self._close_conn()
566 raise
567
568 def _read_and_discard_trailer(self):
569 # read and discard trailer up to the CRLF terminator
570 ### note: we shouldn't have any trailers!
571 trailers_read = 0
572 while True:
573 line = self.fp.readline(_MAXLINE + 1)
574 if len(line) > _MAXLINE:
575 raise LineTooLong("trailer line")
576 if not line:
577 # a vanishingly small number of sites EOF without
578 # sending the trailer
579 break
580 if line in (b'\r\n', b'\n', b''):
581 break
582 # Bound the trailer count just as response headers are bounded.
583 # A server streaming trailer lines forever would otherwise hang
584 # the client; a socket timeout cannot detect that as data keeps
585 # arriving within every timeout window.
586 trailers_read += 1
587 if trailers_read > _MAXHEADERS:
588 raise HTTPException(
589 f"got more than {_MAXHEADERS} trailers")
590
591 def _get_chunk_left(self):
592 # return self.chunk_left, reading a new chunk if necessary.
593 # chunk_left == 0: at the end of the current chunk, need to close it
594 # chunk_left == None: No current chunk, should read next.
595 # This function returns non-zero or None if the last chunk has
596 # been read.
597 chunk_left = self.chunk_left
598 if not chunk_left: # Can be 0 or None
599 if chunk_left is not None:
600 # We are at the end of chunk, discard chunk end
601 self._safe_read(2) # toss the CRLF at the end of the chunk
602 try:
603 chunk_left = self._read_next_chunk_size()
604 except ValueError:
605 raise IncompleteRead(b'')
606 if chunk_left == 0:
607 # last chunk: 1*("0") [ chunk-extension ] CRLF
608 self._read_and_discard_trailer()
609 # we read everything; close the "file"
610 self._close_conn()
611 chunk_left = None
612 self.chunk_left = chunk_left
613 return chunk_left
614
615 def _read_chunked(self, amt=None):
616 assert self.chunked != _UNKNOWN
617 if amt is not None and amt < 0:
618 amt = None
619 value = []
620 try:
621 while (chunk_left := self._get_chunk_left()) is not None:
622 if amt is not None and amt <= chunk_left:
623 value.append(self._safe_read(amt))
624 self.chunk_left = chunk_left - amt
625 break
626
627 value.append(self._safe_read(chunk_left))
628 if amt is not None:
629 amt -= chunk_left
630 self.chunk_left = 0
631 return b''.join(value)
632 except IncompleteRead as exc:
633 raise IncompleteRead(b''.join(value)) from exc
634
635 def _readinto_chunked(self, b):
636 assert self.chunked != _UNKNOWN
637 total_bytes = 0
638 mvb = memoryview(b)
639 try:
640 while True:
641 chunk_left = self._get_chunk_left()
642 if chunk_left is None:
643 return total_bytes
644
645 if len(mvb) <= chunk_left:
646 n = self._safe_readinto(mvb)
647 self.chunk_left = chunk_left - n
648 return total_bytes + n
649
650 temp_mvb = mvb[:chunk_left]
651 n = self._safe_readinto(temp_mvb)
652 mvb = mvb[n:]
653 total_bytes += n
654 self.chunk_left = 0
655
656 except IncompleteRead:
657 raise IncompleteRead(bytes(b[0:total_bytes]))
658
659 def _safe_read(self, amt):
660 """Read the number of bytes requested.
661
662 This function should be used when <amt> bytes "should" be present for
663 reading. If the bytes are truly not available (due to EOF), then the
664 IncompleteRead exception can be used to detect the problem.
665 """
666 cursize = min(amt, _MIN_READ_BUF_SIZE)
667 data = self.fp.read(cursize)
668 if len(data) >= amt:
669 return data
670 if len(data) < cursize:
671 raise IncompleteRead(data, amt - len(data))
672
673 data = io.BytesIO(data)
674 data.seek(0, 2)
675 while True:
676 # This is a geometric increase in read size (never more than
677 # doubling out the current length of data per loop iteration).
678 delta = min(cursize, amt - cursize)
679 data.write(self.fp.read(delta))
680 if data.tell() >= amt:
681 return data.getvalue()
682 cursize += delta
683 if data.tell() < cursize:
684 raise IncompleteRead(data.getvalue(), amt - data.tell())
685
686 def _safe_readinto(self, b):
687 """Same as _safe_read, but for reading into a buffer."""
688 amt = len(b)
689 n = self.fp.readinto(b)
690 if n < amt:
691 raise IncompleteRead(bytes(b[:n]), amt-n)
692 return n
693
694 def read1(self, n=-1):
695 """Read with at most one underlying system call. If at least one
696 byte is buffered, return that instead.
697 """
698 if self.fp is None or self._method == "HEAD":
699 return b""
700 if self.chunked:
701 return self._read1_chunked(n)
702 if self.length is not None and (n < 0 or n > self.length):
703 n = self.length
704 result = self.fp.read1(n)
705 if not result and n:
706 self._close_conn()
707 elif self.length is not None:
708 self.length -= len(result)
709 if not self.length:
710 self._close_conn()
711 return result
712
713 def peek(self, n=-1):
714 # Having this enables IOBase.readline() to read more than one
715 # byte at a time
716 if self.fp is None or self._method == "HEAD":
717 return b""
718 if self.chunked:
719 return self._peek_chunked(n)
720 return self.fp.peek(n)
721
722 def readline(self, limit=-1):
723 if self.fp is None or self._method == "HEAD":
724 return b""
725 if self.chunked:
726 # Fallback to IOBase readline which uses peek() and read()
727 return super().readline(limit)
728 if self.length is not None and (limit < 0 or limit > self.length):
729 limit = self.length
730 result = self.fp.readline(limit)
731 if not result and limit:
732 self._close_conn()
733 elif self.length is not None:
734 self.length -= len(result)
735 if not self.length:
736 self._close_conn()
737 return result
738
739 def _read1_chunked(self, n):
740 # Strictly speaking, _get_chunk_left() may cause more than one read,
741 # but that is ok, since that is to satisfy the chunked protocol.
742 chunk_left = self._get_chunk_left()
743 if chunk_left is None or n == 0:
744 return b''
745 if not (0 <= n <= chunk_left):
746 n = chunk_left # if n is negative or larger than chunk_left
747 read = self.fp.read1(n)
748 self.chunk_left -= len(read)
749 if not read:
750 raise IncompleteRead(b"")
751 return read
752
753 def _peek_chunked(self, n):
754 # Strictly speaking, _get_chunk_left() may cause more than one read,
755 # but that is ok, since that is to satisfy the chunked protocol.
756 try:
757 chunk_left = self._get_chunk_left()
758 except IncompleteRead:
759 return b'' # peek doesn't worry about protocol
760 if chunk_left is None:
761 return b'' # eof
762 # peek is allowed to return more than requested. Just request the
763 # entire chunk, and truncate what we get.
764 return self.fp.peek(chunk_left)[:chunk_left]
765
766 def fileno(self):
767 return self.fp.fileno()
768
769 def getheader(self, name, default=None):
770 '''Returns the value of the header matching *name*.
771
772 If there are multiple matching headers, the values are
773 combined into a single string separated by commas and spaces.
774
775 If no matching header is found, returns *default* or None if
776 the *default* is not specified.
777
778 If the headers are unknown, raises http.client.ResponseNotReady.
779
780 '''
781 if self.headers is None:
782 raise ResponseNotReady()
783 headers = self.headers.get_all(name) or default
784 if isinstance(headers, str) or not hasattr(headers, '__iter__'):
785 return headers
786 else:
787 return ', '.join(headers)
788
789 def getheaders(self):
790 """Return list of (header, value) tuples."""
791 if self.headers is None:
792 raise ResponseNotReady()
793 return list(self.headers.items())
794
795 # We override IOBase.__iter__ so that it doesn't check for closed-ness
796
797 def __iter__(self):
798 return self
799
800 # For compatibility with old-style urllib responses.
801
802 def info(self):
803 '''Returns an instance of the class mimetools.Message containing
804 meta-information associated with the URL.
805
806 When the method is HTTP, these headers are those returned by
807 the server at the head of the retrieved HTML page (including
808 Content-Length and Content-Type).
809
810 When the method is FTP, a Content-Length header will be
811 present if (as is now usual) the server passed back a file
812 length in response to the FTP retrieval request. A
813 Content-Type header will be present if the MIME type can be
814 guessed.
815
816 When the method is local-file, returned headers will include
817 a Date representing the file's last-modified time, a
818 Content-Length giving file size, and a Content-Type
819 containing a guess at the file's type. See also the
820 description of the mimetools module.
821
822 '''
823 return self.headers
824
825 def geturl(self):
826 '''Return the real URL of the page.
827
828 In some cases, the HTTP server redirects a client to another
829 URL. The urlopen() function handles this transparently, but in
830 some cases the caller needs to know which URL the client was
831 redirected to. The geturl() method can be used to get at this
832 redirected URL.
833
834 '''
835 return self.url
836
837 def getcode(self):
838 '''Return the HTTP status code that was sent with the response,
839 or None if the URL is not an HTTP URL.
840
841 '''
842 return self.status
843
844
845 def _create_https_context(http_version):
846 # Function also used by urllib.request to be able to set the check_hostname
847 # attribute on a context object.
848 context = ssl._create_default_https_context()
849 # send ALPN extension to indicate HTTP/1.1 protocol
850 if http_version == 11:
851 context.set_alpn_protocols(['http/1.1'])
852 # enable PHA for TLS 1.3 connections if available
853 if context.post_handshake_auth is not None:
854 context.post_handshake_auth = True
855 return context
856
857
858 class HTTPConnection:
859
860 _http_vsn = 11
861 _http_vsn_str = 'HTTP/1.1'
862
863 response_class = HTTPResponse
864 default_port = HTTP_PORT
865 auto_open = 1
866 debuglevel = 0
867
868 @staticmethod
869 def _is_textIO(stream):
870 """Test whether a file-like object is a text or a binary stream.
871 """
872 return isinstance(stream, io.TextIOBase)
873
874 @staticmethod
875 def _get_content_length(body, method):
876 """Get the content-length based on the body.
877
878 If the body is None, we set Content-Length: 0 for methods that expect
879 a body (RFC 7230, Section 3.3.2). We also set the Content-Length for
880 any method if the body is a str or bytes-like object and not a file.
881 """
882 if body is None:
883 # do an explicit check for not None here to distinguish
884 # between unset and set but empty
885 if method.upper() in _METHODS_EXPECTING_BODY:
886 return 0
887 else:
888 return None
889
890 if hasattr(body, 'read'):
891 # file-like object.
892 return None
893
894 try:
895 # does it implement the buffer protocol (bytes, bytearray, array)?
896 mv = memoryview(body)
897 return mv.nbytes
898 except TypeError:
899 pass
900
901 if isinstance(body, str):
902 return len(body)
903
904 return None
905
906 def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
907 source_address=None, blocksize=8192):
908 self.timeout = timeout
909 self.source_address = source_address
910 self.blocksize = blocksize
911 self.sock = None
912 self._buffer = []
913 self.__response = None
914 self.__state = _CS_IDLE
915 self._method = None
916 self._tunnel_host = None
917 self._tunnel_port = None
918 self._tunnel_headers = {}
919 self._raw_proxy_headers = None
920
921 (self.host, self.port) = self._get_hostport(host, port)
922
923 self._validate_host(self.host)
924
925 # This is stored as an instance variable to allow unit
926 # tests to replace it with a suitable mockup
927 self._create_connection = socket.create_connection
928
929 def set_tunnel(self, host, port=None, headers=None):
930 """Set up host and port for HTTP CONNECT tunnelling.
931
932 In a connection that uses HTTP CONNECT tunnelling, the host passed to
933 the constructor is used as a proxy server that relays all communication
934 to the endpoint passed to `set_tunnel`. This done by sending an HTTP
935 CONNECT request to the proxy server when the connection is established.
936
937 This method must be called before the HTTP connection has been
938 established.
939
940 The headers argument should be a mapping of extra HTTP headers to send
941 with the CONNECT request.
942
943 As HTTP/1.1 is used for HTTP CONNECT tunnelling request, as per the RFC
944 (https://tools.ietf.org/html/rfc7231#section-4.3.6), a HTTP Host:
945 header must be provided, matching the authority-form of the request
946 target provided as the destination for the CONNECT request. If a
947 HTTP Host: header is not provided via the headers argument, one
948 is generated and transmitted automatically.
949 """
950
951 if self.sock:
952 raise RuntimeError("Can't set up tunnel for established connection")
953
954 self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)
955 if headers:
956 self._tunnel_headers = headers.copy()
957 else:
958 self._tunnel_headers.clear()
959
960 if not any(header.lower() == "host" for header in self._tunnel_headers):
961 encoded_host = self._tunnel_host.encode("idna").decode("ascii")
962 self._tunnel_headers["Host"] = "%s:%d" % (
963 encoded_host, self._tunnel_port)
964
965 def _get_hostport(self, host, port):
966 if port is None:
967 i = host.rfind(':')
968 j = host.rfind(']') # ipv6 addresses have [...]
969 if i > j:
970 try:
971 port = int(host[i+1:])
972 except ValueError:
973 if host[i+1:] == "": # http://foo.com:/ == http://foo.com/
974 port = self.default_port
975 else:
976 raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
977 host = host[:i]
978 else:
979 port = self.default_port
980 if host and host[0] == '[' and host[-1] == ']':
981 host = host[1:-1]
982
983 return (host, port)
984
985 def set_debuglevel(self, level):
986 self.debuglevel = level
987
988 def _wrap_ipv6(self, ip):
989 if b':' in ip and ip[0] != b'['[0]:
990 return b"[" + ip + b"]"
991 return ip
992
993 def _tunnel(self):
994 if _contains_disallowed_url_pchar_re.search(self._tunnel_host):
995 raise ValueError('Tunnel host can\'t contain control characters %r'
996 % (self._tunnel_host,))
997 connect = b"CONNECT %s:%d %s\r\n" % (
998 self._wrap_ipv6(self._tunnel_host.encode("idna")),
999 self._tunnel_port,
1000 self._http_vsn_str.encode("ascii"))
1001 headers = [connect]
1002 for header, value in self._tunnel_headers.items():
1003 header_bytes = header.encode("latin-1")
1004 value_bytes = value.encode("latin-1")
1005 if not _is_legal_header_name(header_bytes):
1006 raise ValueError('Invalid header name %r' % (header_bytes,))
1007 if _is_illegal_header_value(value_bytes):
1008 raise ValueError('Invalid header value %r' % (value_bytes,))
1009 headers.append(b"%s: %s\r\n" % (header_bytes, value_bytes))
1010 headers.append(b"\r\n")
1011 # Making a single send() call instead of one per line encourages
1012 # the host OS to use a more optimal packet size instead of
1013 # potentially emitting a series of small packets.
1014 self.send(b"".join(headers))
1015 del headers
1016
1017 response = self.response_class(self.sock, method=self._method)
1018 try:
1019 (version, code, message) = response._read_status()
1020
1021 self._raw_proxy_headers = _read_headers(response.fp)
1022
1023 if self.debuglevel > 0:
1024 for header in self._raw_proxy_headers:
1025 print('header:', header.decode())
1026
1027 if code != http.HTTPStatus.OK:
1028 self.close()
1029 raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
1030
1031 finally:
1032 response.close()
1033
1034 def get_proxy_response_headers(self):
1035 """
1036 Returns a dictionary with the headers of the response
1037 received from the proxy server to the CONNECT request
1038 sent to set the tunnel.
1039
1040 If the CONNECT request was not sent, the method returns None.
1041 """
1042 return (
1043 _parse_header_lines(self._raw_proxy_headers)
1044 if self._raw_proxy_headers is not None
1045 else None
1046 )
1047
1048 def connect(self):
1049 """Connect to the host and port specified in __init__."""
1050 sys.audit("http.client.connect", self, self.host, self.port)
1051 self.sock = self._create_connection(
1052 (self.host,self.port), self.timeout, self.source_address)
1053 # Might fail in OSs that don't implement TCP_NODELAY
1054 try:
1055 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
1056 except OSError as e:
1057 if e.errno != errno.ENOPROTOOPT:
1058 raise
1059
1060 if self._tunnel_host:
1061 self._tunnel()
1062
1063 def close(self):
1064 """Close the connection to the HTTP server."""
1065 self.__state = _CS_IDLE
1066 try:
1067 sock = self.sock
1068 if sock:
1069 self.sock = None
1070 sock.close() # close it manually... there may be other refs
1071 finally:
1072 response = self.__response
1073 if response:
1074 self.__response = None
1075 response.close()
1076
1077 def send(self, data):
1078 """Send 'data' to the server.
1079 ``data`` can be a string object, a bytes object, an array object, a
1080 file-like object that supports a .read() method, or an iterable object.
1081 """
1082
1083 if self.sock is None:
1084 if self.auto_open:
1085 self.connect()
1086 else:
1087 raise NotConnected()
1088
1089 if self.debuglevel > 0:
1090 print("send:", repr(data))
1091 if hasattr(data, "read") :
1092 if self.debuglevel > 0:
1093 print("sending a readable")
1094 encode = self._is_textIO(data)
1095 if encode and self.debuglevel > 0:
1096 print("encoding file using iso-8859-1")
1097 while datablock := data.read(self.blocksize):
1098 if encode:
1099 datablock = datablock.encode("iso-8859-1")
1100 sys.audit("http.client.send", self, datablock)
1101 self.sock.sendall(datablock)
1102 return
1103 sys.audit("http.client.send", self, data)
1104 try:
1105 self.sock.sendall(data)
1106 except TypeError:
1107 if isinstance(data, collections.abc.Iterable):
1108 for d in data:
1109 self.sock.sendall(d)
1110 else:
1111 raise TypeError("data should be a bytes-like object "
1112 "or an iterable, got %r" % type(data))
1113
1114 def _output(self, s):
1115 """Add a line of output to the current request buffer.
1116
1117 Assumes that the line does *not* end with \\r\\n.
1118 """
1119 self._buffer.append(s)
1120
1121 def _read_readable(self, readable):
1122 if self.debuglevel > 0:
1123 print("reading a readable")
1124 encode = self._is_textIO(readable)
1125 if encode and self.debuglevel > 0:
1126 print("encoding file using iso-8859-1")
1127 while datablock := readable.read(self.blocksize):
1128 if encode:
1129 datablock = datablock.encode("iso-8859-1")
1130 yield datablock
1131
1132 def _send_output(self, message_body=None, encode_chunked=False):
1133 """Send the currently buffered request and clear the buffer.
1134
1135 Appends an extra \\r\\n to the buffer.
1136 A message_body may be specified, to be appended to the request.
1137 """
1138 self._buffer.extend((b"", b""))
1139 msg = b"\r\n".join(self._buffer)
1140 del self._buffer[:]
1141 self.send(msg)
1142
1143 if message_body is not None:
1144
1145 # create a consistent interface to message_body
1146 if hasattr(message_body, 'read'):
1147 # Let file-like take precedence over byte-like. This
1148 # is needed to allow the current position of mmap'ed
1149 # files to be taken into account.
1150 chunks = self._read_readable(message_body)
1151 else:
1152 try:
1153 # this is solely to check to see if message_body
1154 # implements the buffer API. it /would/ be easier
1155 # to capture if PyObject_CheckBuffer was exposed
1156 # to Python.
1157 memoryview(message_body)
1158 except TypeError:
1159 try:
1160 chunks = iter(message_body)
1161 except TypeError:
1162 raise TypeError("message_body should be a bytes-like "
1163 "object or an iterable, got %r"
1164 % type(message_body))
1165 else:
1166 # the object implements the buffer interface and
1167 # can be passed directly into socket methods
1168 chunks = (message_body,)
1169
1170 for chunk in chunks:
1171 if not chunk:
1172 if self.debuglevel > 0:
1173 print('Zero length chunk ignored')
1174 continue
1175
1176 if encode_chunked and self._http_vsn == 11:
1177 # chunked encoding
1178 chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
1179 + b'\r\n'
1180 self.send(chunk)
1181
1182 if encode_chunked and self._http_vsn == 11:
1183 # end chunked transfer
1184 self.send(b'0\r\n\r\n')
1185
1186 def putrequest(self, method, url, skip_host=False,
1187 skip_accept_encoding=False):
1188 """Send a request to the server.
1189
1190 'method' specifies an HTTP request method, e.g. 'GET'.
1191 'url' specifies the object being requested, e.g. '/index.html'.
1192 'skip_host' if True does not add automatically a 'Host:' header
1193 'skip_accept_encoding' if True does not add automatically an
1194 'Accept-Encoding:' header
1195 """
1196
1197 # if a prior response has been completed, then forget about it.
1198 if self.__response and self.__response.isclosed():
1199 self.__response = None
1200
1201
1202 # in certain cases, we cannot issue another request on this connection.
1203 # this occurs when:
1204 # 1) we are in the process of sending a request. (_CS_REQ_STARTED)
1205 # 2) a response to a previous request has signalled that it is going
1206 # to close the connection upon completion.
1207 # 3) the headers for the previous response have not been read, thus
1208 # we cannot determine whether point (2) is true. (_CS_REQ_SENT)
1209 #
1210 # if there is no prior response, then we can request at will.
1211 #
1212 # if point (2) is true, then we will have passed the socket to the
1213 # response (effectively meaning, "there is no prior response"), and
1214 # will open a new one when a new request is made.
1215 #
1216 # Note: if a prior response exists, then we *can* start a new request.
1217 # We are not allowed to begin fetching the response to this new
1218 # request, however, until that prior response is complete.
1219 #
1220 if self.__state == _CS_IDLE:
1221 self.__state = _CS_REQ_STARTED
1222 else:
1223 raise CannotSendRequest(self.__state)
1224
1225 self._validate_method(method)
1226
1227 # Save the method for use later in the response phase
1228 self._method = method
1229
1230 url = url or '/'
1231 self._validate_path(url)
1232
1233 request = '%s %s %s' % (method, url, self._http_vsn_str)
1234
1235 self._output(self._encode_request(request))
1236
1237 if self._http_vsn == 11:
1238 # Issue some standard headers for better HTTP/1.1 compliance
1239
1240 if not skip_host:
1241 # this header is issued *only* for HTTP/1.1
1242 # connections. more specifically, this means it is
1243 # only issued when the client uses the new
1244 # HTTPConnection() class. backwards-compat clients
1245 # will be using HTTP/1.0 and those clients may be
1246 # issuing this header themselves. we should NOT issue
1247 # it twice; some web servers (such as Apache) barf
1248 # when they see two Host: headers
1249
1250 # If we need a non-standard port,include it in the
1251 # header. If the request is going through a proxy,
1252 # but the host of the actual URL, not the host of the
1253 # proxy.
1254
1255 netloc = ''
1256 if url.startswith('http'):
1257 nil, netloc, nil, nil, nil = urlsplit(url)
1258
1259 if netloc:
1260 try:
1261 netloc_enc = netloc.encode("ascii")
1262 except UnicodeEncodeError:
1263 netloc_enc = netloc.encode("idna")
1264 self.putheader('Host', _strip_ipv6_iface(netloc_enc))
1265 else:
1266 if self._tunnel_host:
1267 host = self._tunnel_host
1268 port = self._tunnel_port
1269 else:
1270 host = self.host
1271 port = self.port
1272
1273 try:
1274 host_enc = host.encode("ascii")
1275 except UnicodeEncodeError:
1276 host_enc = host.encode("idna")
1277
1278 # As per RFC 273, IPv6 address should be wrapped with []
1279 # when used as Host header
1280 host_enc = self._wrap_ipv6(host_enc)
1281 if ":" in host:
1282 host_enc = _strip_ipv6_iface(host_enc)
1283
1284 if port == self.default_port:
1285 self.putheader('Host', host_enc)
1286 else:
1287 host_enc = host_enc.decode("ascii")
1288 self.putheader('Host', "%s:%s" % (host_enc, port))
1289
1290 # note: we are assuming that clients will not attempt to set these
1291 # headers since *this* library must deal with the
1292 # consequences. this also means that when the supporting
1293 # libraries are updated to recognize other forms, then this
1294 # code should be changed (removed or updated).
1295
1296 # we only want a Content-Encoding of "identity" since we don't
1297 # support encodings such as x-gzip or x-deflate.
1298 if not skip_accept_encoding:
1299 self.putheader('Accept-Encoding', 'identity')
1300
1301 # we can accept "chunked" Transfer-Encodings, but no others
1302 # NOTE: no TE header implies *only* "chunked"
1303 #self.putheader('TE', 'chunked')
1304
1305 # if TE is supplied in the header, then it must appear in a
1306 # Connection header.
1307 #self.putheader('Connection', 'TE')
1308
1309 else:
1310 # For HTTP/1.0, the server will assume "not chunked"
1311 pass
1312
1313 def _encode_request(self, request):
1314 # ASCII also helps prevent CVE-2019-9740.
1315 return request.encode('ascii')
1316
1317 def _validate_method(self, method):
1318 """Validate a method name for putrequest."""
1319 # prevent http header injection
1320 match = _contains_disallowed_method_pchar_re.search(method)
1321 if match:
1322 raise ValueError(
1323 f"method can't contain control characters. {method!r} "
1324 f"(found at least {match.group()!r})")
1325
1326 def _validate_path(self, url):
1327 """Validate a url for putrequest."""
1328 # Prevent CVE-2019-9740.
1329 match = _contains_disallowed_url_pchar_re.search(url)
1330 if match:
1331 raise InvalidURL(f"URL can't contain control characters. {url!r} "
1332 f"(found at least {match.group()!r})")
1333
1334 def _validate_host(self, host):
1335 """Validate a host so it doesn't contain control characters."""
1336 # Prevent CVE-2019-18348.
1337 match = _contains_disallowed_url_pchar_re.search(host)
1338 if match:
1339 raise InvalidURL(f"URL can't contain control characters. {host!r} "
1340 f"(found at least {match.group()!r})")
1341
1342 def putheader(self, header, *values):
1343 """Send a request header line to the server.
1344
1345 For example: h.putheader('Accept', 'text/html')
1346 """
1347 if self.__state != _CS_REQ_STARTED:
1348 raise CannotSendHeader()
1349
1350 if hasattr(header, 'encode'):
1351 header = header.encode('ascii')
1352
1353 if not _is_legal_header_name(header):
1354 raise ValueError('Invalid header name %r' % (header,))
1355
1356 values = list(values)
1357 for i, one_value in enumerate(values):
1358 if hasattr(one_value, 'encode'):
1359 values[i] = one_value.encode('latin-1')
1360 elif isinstance(one_value, int):
1361 values[i] = str(one_value).encode('ascii')
1362
1363 if _is_illegal_header_value(values[i]):
1364 raise ValueError('Invalid header value %r' % (values[i],))
1365
1366 value = b'\r\n\t'.join(values)
1367 header = header + b': ' + value
1368 self._output(header)
1369
1370 def endheaders(self, message_body=None, *, encode_chunked=False):
1371 """Indicate that the last header line has been sent to the server.
1372
1373 This method sends the request to the server. The optional message_body
1374 argument can be used to pass a message body associated with the
1375 request.
1376 """
1377 if self.__state == _CS_REQ_STARTED:
1378 self.__state = _CS_REQ_SENT
1379 else:
1380 raise CannotSendHeader()
1381 self._send_output(message_body, encode_chunked=encode_chunked)
1382
1383 def request(self, method, url, body=None, headers={}, *,
1384 encode_chunked=False):
1385 """Send a complete request to the server."""
1386 self._send_request(method, url, body, headers, encode_chunked)
1387
1388 def _send_request(self, method, url, body, headers, encode_chunked):
1389 # Honor explicitly requested Host: and Accept-Encoding: headers.
1390 header_names = frozenset(k.lower() for k in headers)
1391 skips = {}
1392 if 'host' in header_names:
1393 skips['skip_host'] = 1
1394 if 'accept-encoding' in header_names:
1395 skips['skip_accept_encoding'] = 1
1396
1397 self.putrequest(method, url, **skips)
1398
1399 # chunked encoding will happen if HTTP/1.1 is used and either
1400 # the caller passes encode_chunked=True or the following
1401 # conditions hold:
1402 # 1. content-length has not been explicitly set
1403 # 2. the body is a file or iterable, but not a str or bytes-like
1404 # 3. Transfer-Encoding has NOT been explicitly set by the caller
1405
1406 if 'content-length' not in header_names:
1407 # only chunk body if not explicitly set for backwards
1408 # compatibility, assuming the client code is already handling the
1409 # chunking
1410 if 'transfer-encoding' not in header_names:
1411 # if content-length cannot be automatically determined, fall
1412 # back to chunked encoding
1413 encode_chunked = False
1414 content_length = self._get_content_length(body, method)
1415 if content_length is None:
1416 if body is not None:
1417 if self.debuglevel > 0:
1418 print('Unable to determine size of %r' % body)
1419 encode_chunked = True
1420 self.putheader('Transfer-Encoding', 'chunked')
1421 else:
1422 self.putheader('Content-Length', str(content_length))
1423 else:
1424 encode_chunked = False
1425
1426 for hdr, value in headers.items():
1427 self.putheader(hdr, value)
1428 if isinstance(body, str):
1429 # RFC 2616 Section 3.7.1 says that text default has a
1430 # default charset of iso-8859-1.
1431 body = _encode(body, 'body')
1432 self.endheaders(body, encode_chunked=encode_chunked)
1433
1434 def getresponse(self):
1435 """Get the response from the server.
1436
1437 If the HTTPConnection is in the correct state, returns an
1438 instance of HTTPResponse or of whatever object is returned by
1439 the response_class variable.
1440
1441 If a request has not been sent or if a previous response has
1442 not be handled, ResponseNotReady is raised. If the HTTP
1443 response indicates that the connection should be closed, then
1444 it will be closed before the response is returned. When the
1445 connection is closed, the underlying socket is closed.
1446 """
1447
1448 # if a prior response has been completed, then forget about it.
1449 if self.__response and self.__response.isclosed():
1450 self.__response = None
1451
1452 # if a prior response exists, then it must be completed (otherwise, we
1453 # cannot read this response's header to determine the connection-close
1454 # behavior)
1455 #
1456 # note: if a prior response existed, but was connection-close, then the
1457 # socket and response were made independent of this HTTPConnection
1458 # object since a new request requires that we open a whole new
1459 # connection
1460 #
1461 # this means the prior response had one of two states:
1462 # 1) will_close: this connection was reset and the prior socket and
1463 # response operate independently
1464 # 2) persistent: the response was retained and we await its
1465 # isclosed() status to become true.
1466 #
1467 if self.__state != _CS_REQ_SENT or self.__response:
1468 raise ResponseNotReady(self.__state)
1469
1470 if self.debuglevel > 0:
1471 response = self.response_class(self.sock, self.debuglevel,
1472 method=self._method)
1473 else:
1474 response = self.response_class(self.sock, method=self._method)
1475
1476 try:
1477 try:
1478 response.begin()
1479 except ConnectionError:
1480 self.close()
1481 raise
1482 assert response.will_close != _UNKNOWN
1483 self.__state = _CS_IDLE
1484
1485 if response.will_close:
1486 # this effectively passes the connection to the response
1487 self.close()
1488 else:
1489 # remember this, so we can tell when it is complete
1490 self.__response = response
1491
1492 return response
1493 except:
1494 response.close()
1495 raise
1496
1497 try:
1498 import ssl
1499 except ImportError:
1500 pass
1501 else:
1502 class HTTPSConnection(HTTPConnection):
1503 "This class allows communication via SSL."
1504
1505 default_port = HTTPS_PORT
1506
1507 def __init__(self, host, port=None,
1508 *, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
1509 source_address=None, context=None, blocksize=8192):
1510 super(HTTPSConnection, self).__init__(host, port, timeout,
1511 source_address,
1512 blocksize=blocksize)
1513 if context is None:
1514 context = _create_https_context(self._http_vsn)
1515 self._context = context
1516
1517 def connect(self):
1518 "Connect to a host on a given (SSL) port."
1519
1520 super().connect()
1521
1522 if self._tunnel_host:
1523 server_hostname = self._tunnel_host
1524 else:
1525 server_hostname = self.host
1526
1527 self.sock = self._context.wrap_socket(self.sock,
1528 server_hostname=server_hostname)
1529
1530 __all__.append("HTTPSConnection")
1531
1532 class HTTPException(Exception):
1533 # Subclasses that define an __init__ must call Exception.__init__
1534 # or define self.args. Otherwise, str() will fail.
1535 pass
1536
1537 class NotConnected(HTTPException):
1538 pass
1539
1540 class InvalidURL(HTTPException):
1541 pass
1542
1543 class UnknownProtocol(HTTPException):
1544 def __init__(self, version):
1545 self.args = version,
1546 self.version = version
1547
1548 class UnknownTransferEncoding(HTTPException):
1549 pass
1550
1551 class UnimplementedFileMode(HTTPException):
1552 pass
1553
1554 class IncompleteRead(HTTPException):
1555 def __init__(self, partial, expected=None):
1556 self.args = partial,
1557 self.partial = partial
1558 self.expected = expected
1559 def __repr__(self):
1560 if self.expected is not None:
1561 e = ', %i more expected' % self.expected
1562 else:
1563 e = ''
1564 return '%s(%i bytes read%s)' % (self.__class__.__name__,
1565 len(self.partial), e)
1566 __str__ = object.__str__
1567
1568 class ImproperConnectionState(HTTPException):
1569 pass
1570
1571 class CannotSendRequest(ImproperConnectionState):
1572 pass
1573
1574 class CannotSendHeader(ImproperConnectionState):
1575 pass
1576
1577 class ResponseNotReady(ImproperConnectionState):
1578 pass
1579
1580 class BadStatusLine(HTTPException):
1581 def __init__(self, line):
1582 if not line:
1583 line = repr(line)
1584 self.args = line,
1585 self.line = line
1586
1587 class LineTooLong(HTTPException):
1588 def __init__(self, line_type):
1589 HTTPException.__init__(self, "got more than %d bytes when reading %s"
1590 % (_MAXLINE, line_type))
1591
1592 class RemoteDisconnected(ConnectionResetError, BadStatusLine):
1593 def __init__(self, *pos, **kw):
1594 BadStatusLine.__init__(self, "")
1595 ConnectionResetError.__init__(self, *pos, **kw)
1596
1597 # for backwards compatibility
1598 error = HTTPException