@@ -790,14 +790,14 @@ def host_port_subcomponent(self) -> Union[str, None]:
790
790
"""
791
791
if (raw := self .raw_host ) is None :
792
792
return None
793
- port = self .explicit_port
794
793
if raw [- 1 ] == "." :
795
794
# Remove all trailing dots from the netloc as while
796
795
# they are valid FQDNs in DNS, TLS validation fails.
797
796
# See https://github.com/aio-libs/aiohttp/issues/3636.
798
797
# To avoid string manipulation we only call rstrip if
799
798
# the last character is a dot.
800
799
raw = raw .rstrip ("." )
800
+ port = self .explicit_port
801
801
if port is None or port == DEFAULT_PORTS .get (self ._scheme ):
802
802
return f"[{ raw } ]" if ":" in raw else raw
803
803
return f"[{ raw } ]:{ port } " if ":" in raw else f"{ raw } :{ port } "
@@ -831,7 +831,7 @@ def raw_path(self) -> str:
831
831
/ for absolute URLs without path part.
832
832
833
833
"""
834
- return "/" if not self ._path and self ._netloc else self . _path
834
+ return self . _path if self ._path or not self ._netloc else "/"
835
835
836
836
@cached_property
837
837
def path (self ) -> str :
@@ -840,7 +840,7 @@ def path(self) -> str:
840
840
/ for absolute URLs without path part.
841
841
842
842
"""
843
- return PATH_UNQUOTER (self .raw_path )
843
+ return PATH_UNQUOTER (self ._path ) if self . _path else "/" if self . _netloc else ""
844
844
845
845
@cached_property
846
846
def path_safe (self ) -> str :
@@ -851,7 +851,9 @@ def path_safe(self) -> str:
851
851
/ (%2F) and % (%25) are not decoded
852
852
853
853
"""
854
- return PATH_SAFE_UNQUOTER (self .raw_path )
854
+ if self ._path :
855
+ return PATH_SAFE_UNQUOTER (self ._path )
856
+ return "/" if self ._netloc else ""
855
857
856
858
@cached_property
857
859
def _parsed_query (self ) -> list [tuple [str , str ]]:
@@ -884,7 +886,7 @@ def query_string(self) -> str:
884
886
Empty string if query is missing.
885
887
886
888
"""
887
- return QS_UNQUOTER (self ._query )
889
+ return QS_UNQUOTER (self ._query ) if self . _query else ""
888
890
889
891
@cached_property
890
892
def path_qs (self ) -> str :
@@ -894,8 +896,9 @@ def path_qs(self) -> str:
894
896
@cached_property
895
897
def raw_path_qs (self ) -> str :
896
898
"""Encoded path of URL with query."""
897
- query = self ._query
898
- return self .raw_path if not query else f"{ self .raw_path } ?{ query } "
899
+ if q := self ._query :
900
+ return f"{ self ._path } ?{ q } " if self ._path or not self ._netloc else f"/?{ q } "
901
+ return self ._path if self ._path or not self ._netloc else "/"
899
902
900
903
@cached_property
901
904
def raw_fragment (self ) -> str :
@@ -913,7 +916,7 @@ def fragment(self) -> str:
913
916
Empty string if fragment is missing.
914
917
915
918
"""
916
- return UNQUOTER (self ._fragment )
919
+ return UNQUOTER (self ._fragment ) if self . _fragment else ""
917
920
918
921
@cached_property
919
922
def raw_parts (self ) -> tuple [str , ...]:
0 commit comments