@@ -790,14 +790,14 @@ def host_port_subcomponent(self) -> Union[str, None]:
790790 """
791791 if (raw := self .raw_host ) is None :
792792 return None
793- port = self .explicit_port
794793 if raw [- 1 ] == "." :
795794 # Remove all trailing dots from the netloc as while
796795 # they are valid FQDNs in DNS, TLS validation fails.
797796 # See https://github.com/aio-libs/aiohttp/issues/3636.
798797 # To avoid string manipulation we only call rstrip if
799798 # the last character is a dot.
800799 raw = raw .rstrip ("." )
800+ port = self .explicit_port
801801 if port is None or port == DEFAULT_PORTS .get (self ._scheme ):
802802 return f"[{ raw } ]" if ":" in raw else raw
803803 return f"[{ raw } ]:{ port } " if ":" in raw else f"{ raw } :{ port } "
@@ -831,7 +831,7 @@ def raw_path(self) -> str:
831831 / for absolute URLs without path part.
832832
833833 """
834- return "/" if not self ._path and self ._netloc else self . _path
834+ return self . _path if self ._path or not self ._netloc else "/"
835835
836836 @cached_property
837837 def path (self ) -> str :
@@ -840,7 +840,7 @@ def path(self) -> str:
840840 / for absolute URLs without path part.
841841
842842 """
843- return PATH_UNQUOTER (self .raw_path )
843+ return PATH_UNQUOTER (self ._path ) if self . _path else "/" if self . _netloc else ""
844844
845845 @cached_property
846846 def path_safe (self ) -> str :
@@ -851,7 +851,9 @@ def path_safe(self) -> str:
851851 / (%2F) and % (%25) are not decoded
852852
853853 """
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 ""
855857
856858 @cached_property
857859 def _parsed_query (self ) -> list [tuple [str , str ]]:
@@ -884,7 +886,7 @@ def query_string(self) -> str:
884886 Empty string if query is missing.
885887
886888 """
887- return QS_UNQUOTER (self ._query )
889+ return QS_UNQUOTER (self ._query ) if self . _query else ""
888890
889891 @cached_property
890892 def path_qs (self ) -> str :
@@ -894,8 +896,9 @@ def path_qs(self) -> str:
894896 @cached_property
895897 def raw_path_qs (self ) -> str :
896898 """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 "/"
899902
900903 @cached_property
901904 def raw_fragment (self ) -> str :
@@ -913,7 +916,7 @@ def fragment(self) -> str:
913916 Empty string if fragment is missing.
914917
915918 """
916- return UNQUOTER (self ._fragment )
919+ return UNQUOTER (self ._fragment ) if self . _fragment else ""
917920
918921 @cached_property
919922 def raw_parts (self ) -> tuple [str , ...]:
0 commit comments