@@ -2809,48 +2809,54 @@ bool VSIS3HandleHelper::CanRestartOnError(const char *pszErrorMsg,
28092809 }
28102810 return false ;
28112811 }
2812- if (!m_bUseVirtualHosting &&
2813- strncmp (pszEndpoint, m_osBucket.c_str (), m_osBucket.size ()) == 0 &&
2814- pszEndpoint[m_osBucket.size ()] == ' .' )
2812+
2813+ /* If we have a body with
2814+ <Error><Code>PermanentRedirect</Code><Message>The bucket you are
2815+ attempting to access must be addressed using the specified endpoint.
2816+ Please send all future requests to this
2817+ endpoint.</Message><Bucket>bucket</Bucket><Endpoint>bucket.region.s3.amazonaws.com</Endpoint></Error>
2818+ and headers like
2819+ x-amz-bucket-region: eu-west-1
2820+ then we must use s3-$(x-amz-bucket-region).amazon.com as endpoint. */
2821+ const char *pszRegionPtr =
2822+ (pszHeaders != nullptr )
2823+ ? strstr (pszHeaders, " x-amz-bucket-region: " )
2824+ : nullptr ;
2825+ if (pszRegionPtr != nullptr )
28152826 {
2816- /* If we have a body with
2817- <Error><Code>PermanentRedirect</Code><Message>The bucket you are
2818- attempting to access must be addressed using the specified endpoint.
2819- Please send all future requests to this
2820- endpoint.</Message><Bucket>bucket.with.dot</Bucket><Endpoint>bucket.with.dot.s3.amazonaws.com</Endpoint></Error>
2821- and headers like
2822- x-amz-bucket-region: eu-west-1
2823- and the bucket name has dot in it,
2824- then we must use s3.$(x-amz-bucket-region).amazon.com as endpoint.
2825- See #7154 */
2826- const char *pszRegionPtr =
2827- (pszHeaders != nullptr )
2828- ? strstr (pszHeaders, " x-amz-bucket-region: " )
2829- : nullptr ;
2830- if (strchr (m_osBucket.c_str (), ' .' ) != nullptr &&
2831- pszRegionPtr != nullptr )
2827+ std::string osRegion (pszRegionPtr +
2828+ strlen (" x-amz-bucket-region: " ));
2829+ size_t nPos = osRegion.find (' \r ' );
2830+ if (nPos != std::string::npos)
2831+ osRegion.resize (nPos);
2832+ if (strncmp (pszEndpoint, m_osBucket.c_str (), m_osBucket.size ()) ==
2833+ 0 &&
2834+ pszEndpoint[m_osBucket.size ()] == ' .' )
28322835 {
2833- std::string osRegion (pszRegionPtr +
2834- strlen (" x-amz-bucket-region: " ));
2835- size_t nPos = osRegion.find (' \r ' );
2836- if (nPos != std::string::npos)
2837- osRegion.resize (nPos);
2838- SetEndpoint (
2839- CPLSPrintf (" s3.%s.amazonaws.com" , osRegion.c_str ()));
2840- SetRegion (osRegion.c_str ());
2841- CPLDebug (AWS_DEBUG_KEY , " Switching to endpoint %s" ,
2842- m_osEndpoint.c_str ());
2843- CPLDebug (AWS_DEBUG_KEY , " Switching to region %s" ,
2844- m_osRegion.c_str ());
2845- CPLDestroyXMLNode (psTree);
2846- if (!bIsTemporaryRedirect)
2847- VSIS3UpdateParams::UpdateMapFromHandle (this );
2848- return true ;
2836+ pszEndpoint += m_osBucket.size () + 1 ;
28492837 }
28502838
2839+ SetEndpoint (pszEndpoint);
2840+ SetRegion (osRegion.c_str ());
2841+ CPLDebug (AWS_DEBUG_KEY , " Switching to endpoint %s" ,
2842+ m_osEndpoint.c_str ());
2843+ CPLDebug (AWS_DEBUG_KEY , " Switching to region %s" ,
2844+ m_osRegion.c_str ());
2845+ CPLDestroyXMLNode (psTree);
2846+ if (!bIsTemporaryRedirect)
2847+ VSIS3UpdateParams::UpdateMapFromHandle (this );
2848+ return true ;
2849+ }
2850+
2851+ if (!m_bUseVirtualHosting &&
2852+ m_osBucket.find (' .' ) == std::string::npos &&
2853+ strncmp (pszEndpoint, m_osBucket.c_str (), m_osBucket.size ()) == 0 &&
2854+ pszEndpoint[m_osBucket.size ()] == ' .' )
2855+ {
28512856 m_bUseVirtualHosting = true ;
28522857 CPLDebug (AWS_DEBUG_KEY , " Switching to virtual hosting" );
28532858 }
2859+
28542860 SetEndpoint (m_bUseVirtualHosting ? pszEndpoint + m_osBucket.size () + 1
28552861 : pszEndpoint);
28562862 CPLDebug (AWS_DEBUG_KEY , " Switching to endpoint %s" ,
0 commit comments