@@ -218,6 +218,9 @@ def _apply_required_fields(
218218 request .fields .set_field (
219219 Field (name = "X-Amz-Security-Token" , values = [identity .session_token ])
220220 )
221+ if "host" not in request .fields :
222+ host = self ._normalize_host_field (uri = request .destination )
223+ request .fields .set_field (Field (name = "host" , values = [host ]))
221224
222225 def canonical_request (
223226 self , * , signing_properties : SigV4SigningProperties , request : AWSRequest
@@ -335,11 +338,6 @@ def _normalize_signing_fields(self, *, request: AWSRequest) -> dict[str, str]:
335338 for field in request .fields
336339 if self ._is_signable_header (field .name .lower ())
337340 }
338- if "host" not in normalized_fields :
339- normalized_fields ["host" ] = self ._normalize_host_field (
340- uri = request .destination # type: ignore - TODO(pyright)
341- )
342-
343341 return dict (sorted (normalized_fields .items ()))
344342
345343 def _is_signable_header (self , field_name : str ):
@@ -352,7 +350,10 @@ def _normalize_host_field(self, *, uri: URI) -> str:
352350 uri_dict = uri .to_dict ()
353351 uri_dict .update ({"port" : None })
354352 uri = URI (** uri_dict )
355- return uri .netloc
353+ host_header = uri .hostname
354+ if port := uri .port :
355+ host_header += f":{ port } "
356+ return host_header
356357
357358 def _format_canonical_fields (self , * , fields : dict [str , str ]) -> str :
358359 return "" .join (
@@ -601,6 +602,9 @@ async def _apply_required_fields(
601602 request .fields .set_field (
602603 Field (name = "X-Amz-Security-Token" , values = [identity .session_token ])
603604 )
605+ if "host" not in request .fields :
606+ host = await self ._normalize_host_field (uri = request .destination )
607+ request .fields .set_field (Field (name = "host" , values = [host ]))
604608
605609 async def canonical_request (
606610 self , * , signing_properties : SigV4SigningProperties , request : AWSRequest
@@ -721,10 +725,6 @@ async def _normalize_signing_fields(self, *, request: AWSRequest) -> dict[str, s
721725 for field in request .fields
722726 if self ._is_signable_header (field .name .lower ())
723727 }
724- if "host" not in normalized_fields :
725- normalized_fields ["host" ] = await self ._normalize_host_field (
726- uri = request .destination # type: ignore - TODO(pyright)
727- )
728728
729729 return dict (sorted (normalized_fields .items ()))
730730
@@ -738,7 +738,10 @@ async def _normalize_host_field(self, *, uri: URI) -> str:
738738 uri_dict = uri .to_dict ()
739739 uri_dict .update ({"port" : None })
740740 uri = URI (** uri_dict )
741- return uri .netloc
741+ host_header = uri .hostname
742+ if port := uri .port :
743+ host_header += f":{ port } "
744+ return host_header
742745
743746 async def _format_canonical_fields (self , * , fields : dict [str , str ]) -> str :
744747 return "" .join (
0 commit comments