-
-
Notifications
You must be signed in to change notification settings - Fork 859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URL.port becomes Optional[int] #1080
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -543,10 +543,13 @@ def _transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport: | |||||||||||||||
enforce_http_url(url) | ||||||||||||||||
|
||||||||||||||||
if self._proxies and not should_not_be_proxied(url): | ||||||||||||||||
is_default_port = (url.scheme == "http" and url.port == 80) or ( | ||||||||||||||||
url.scheme == "https" and url.port == 443 | ||||||||||||||||
default_port = {"http": 80, "https": 443}[url.scheme] | ||||||||||||||||
is_default_port = url.port is None or url.port == default_port | ||||||||||||||||
hostname = ( | ||||||||||||||||
f"{url.host}:{default_port}" | ||||||||||||||||
if url.port is None | ||||||||||||||||
else f"{url.host}:{url.port}" | ||||||||||||||||
) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be simplified:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great call yup. |
||||||||||||||||
hostname = f"{url.host}:{url.port}" | ||||||||||||||||
proxy_keys = ( | ||||||||||||||||
f"{url.scheme}://{hostname}", | ||||||||||||||||
f"{url.scheme}://{url.host}" if is_default_port else None, | ||||||||||||||||
|
@@ -1076,10 +1079,13 @@ def _transport_for_url(self, url: URL) -> httpcore.AsyncHTTPTransport: | |||||||||||||||
enforce_http_url(url) | ||||||||||||||||
|
||||||||||||||||
if self._proxies and not should_not_be_proxied(url): | ||||||||||||||||
is_default_port = (url.scheme == "http" and url.port == 80) or ( | ||||||||||||||||
url.scheme == "https" and url.port == 443 | ||||||||||||||||
default_port = {"http": 80, "https": 443}[url.scheme] | ||||||||||||||||
is_default_port = url.port is None or url.port == default_port | ||||||||||||||||
hostname = ( | ||||||||||||||||
f"{url.host}:{default_port}" | ||||||||||||||||
if url.port is None | ||||||||||||||||
else f"{url.host}:{url.port}" | ||||||||||||||||
) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above |
||||||||||||||||
hostname = f"{url.host}:{url.port}" | ||||||||||||||||
proxy_keys = ( | ||||||||||||||||
f"{url.scheme}://{hostname}", | ||||||||||||||||
f"{url.scheme}://{url.host}" if is_default_port else None, | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,12 +273,20 @@ def enforce_http_url(url: "URL") -> None: | |
raise InvalidURL('URL scheme must be "http" or "https".') | ||
|
||
|
||
def port_or_default(url: "URL") -> typing.Optional[int]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be used above? |
||
if url.port is not None: | ||
return url.port | ||
return {"http": 80, "https": 443}.get(url.scheme) | ||
|
||
|
||
def same_origin(url: "URL", other: "URL") -> bool: | ||
""" | ||
Return 'True' if the given URLs share the same origin. | ||
""" | ||
return ( | ||
url.scheme == other.scheme and url.host == other.host and url.port == other.port | ||
url.scheme == other.scheme | ||
and url.host == other.host | ||
and port_or_default(url) == port_or_default(other) | ||
) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a
.get()
to avoidKeyError: 'ftp'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, because we've called
enforce_http_url(url)
before doing anything else, sourl.scheme
will always behttp|https
here, anddefault_port
will be anint
rather than anOptional[int]
.(Agree that it's a bit futzy, and I think it'd potentially clean up a bit if we rethought the proxy_keys/mount API.)