| 57 | self.sock.setblocking(False) |
| 58 | |
| 59 | def init(self): |
| 60 | # Guard against double initialization |
| 61 | if self.initialized: |
| 62 | return |
| 63 | self.initialized = True |
| 64 | self.sock.setblocking(True) |
| 65 | |
| 66 | if self.parser is None: |
| 67 | # wrap the socket if needed |
| 68 | if self.cfg.is_ssl: |
| 69 | self.sock = sock.ssl_wrap_socket(self.sock, self.cfg) |
| 70 | |
| 71 | # Complete the handshake to ensure ALPN negotiation is done |
| 72 | # (needed if do_handshake_on_connect is False) |
| 73 | if not self.cfg.do_handshake_on_connect: |
| 74 | self.sock.do_handshake() |
| 75 | |
| 76 | # Check if HTTP/2 was negotiated via ALPN |
| 77 | if sock.is_http2_negotiated(self.sock): |
| 78 | self.is_http2 = True |
| 79 | self.parser = http.get_parser( |
| 80 | self.cfg, self.sock, self.client, http2_connection=True |
| 81 | ) |
| 82 | self.parser.initiate_connection() |
| 83 | return |
| 84 | |
| 85 | # initialize the HTTP/1.x parser |
| 86 | self.parser = http.get_parser(self.cfg, self.sock, self.client) |
| 87 | |
| 88 | def set_timeout(self): |
| 89 | # Use monotonic clock for reliability (time.time() can jump due to NTP) |