Update destination host, port and connection type (ssl).
(self, url)
| 97 | self.update_expect_continue(expect100) |
| 98 | |
| 99 | def update_host(self, url): |
| 100 | """Update destination host, port and connection type (ssl).""" |
| 101 | url_parsed = urllib.parse.urlsplit(url) |
| 102 | |
| 103 | # check for network location part |
| 104 | netloc = url_parsed.netloc |
| 105 | if not netloc: |
| 106 | raise ValueError('Host could not be detected.') |
| 107 | |
| 108 | # get host/port |
| 109 | host = url_parsed.hostname |
| 110 | if not host: |
| 111 | raise ValueError('Host could not be detected.') |
| 112 | |
| 113 | try: |
| 114 | port = url_parsed.port |
| 115 | except ValueError: |
| 116 | raise ValueError( |
| 117 | 'Port number could not be converted.') from None |
| 118 | |
| 119 | # check domain idna encoding |
| 120 | try: |
| 121 | netloc = netloc.encode('idna').decode('utf-8') |
| 122 | host = host.encode('idna').decode('utf-8') |
| 123 | except UnicodeError: |
| 124 | raise ValueError('URL has an invalid label.') |
| 125 | |
| 126 | # basic auth info |
| 127 | username, password = url_parsed.username, url_parsed.password |
| 128 | if username: |
| 129 | self.auth = helpers.BasicAuth(username, password or '') |
| 130 | netloc = netloc.split('@', 1)[1] |
| 131 | |
| 132 | # Record entire netloc for usage in host header |
| 133 | self.netloc = netloc |
| 134 | |
| 135 | scheme = url_parsed.scheme |
| 136 | self.ssl = scheme in ('https', 'wss') |
| 137 | |
| 138 | # set port number if it isn't already set |
| 139 | if not port: |
| 140 | if self.ssl: |
| 141 | port = HTTPS_PORT |
| 142 | else: |
| 143 | port = HTTP_PORT |
| 144 | |
| 145 | self.host, self.port, self.scheme = host, port, scheme |
| 146 | |
| 147 | def update_version(self, version): |
| 148 | """Convert request version to two elements tuple. |