@@ -1227,75 +1227,41 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
12271227 return CURLE_OUT_OF_MEMORY ;
12281228 }
12291229 else {
1230- uc = curl_url_get (data -> state .uh , CURLUPART_URL , & follow_url , 0 );
1231- if (uc )
1230+ bool same_origin ;
1231+ CURLcode result ;
1232+ CURLU * u = curl_url ();
1233+ if (!u )
1234+ return CURLE_OUT_OF_MEMORY ;
1235+ uc = curl_url_set (u , CURLUPART_URL ,
1236+ Curl_bufref_ptr (& data -> state .url ),
1237+ CURLU_URLENCODE | CURLU_ALLOW_SPACE );
1238+ if (!uc )
1239+ uc = curl_url_get (data -> state .uh , CURLUPART_URL , & follow_url , 0 );
1240+ if (uc ) {
1241+ curl_url_cleanup (u );
12321242 return Curl_uc_to_curlcode (uc );
1243+ }
12331244
1234- /* Clear auth if this redirects to a different port number or protocol,
1235- unless permitted */
1236- if (!data -> set .allow_auth_to_other_hosts && (type != FOLLOW_FAKE )) {
1237- uint16_t port ;
1238- bool clear = FALSE;
1239-
1240- if (data -> set .use_port && data -> state .allow_port )
1241- /* a custom port is used */
1242- port = data -> set .use_port ;
1243- else {
1244- curl_off_t value ;
1245- char * portnum ;
1246- const char * p ;
1247- uc = curl_url_get (data -> state .uh , CURLUPART_PORT , & portnum ,
1248- CURLU_DEFAULT_PORT );
1249- if (uc ) {
1250- curlx_free (follow_url );
1251- return Curl_uc_to_curlcode (uc );
1252- }
1253- p = portnum ;
1254- curlx_str_number (& p , & value , 0xffff );
1255- port = (uint16_t )value ;
1256- curlx_free (portnum );
1257- }
1258- if (port != data -> info .conn_remote_port ) {
1259- infof (data , "Clear auth, redirects to port from %d to %d" ,
1260- data -> info .conn_remote_port , port );
1261- clear = TRUE;
1262- }
1263- else {
1264- char * scheme ;
1265- const struct Curl_scheme * p ;
1266- uc = curl_url_get (data -> state .uh , CURLUPART_SCHEME , & scheme , 0 );
1267- if (uc ) {
1268- curlx_free (follow_url );
1269- return Curl_uc_to_curlcode (uc );
1270- }
1245+ same_origin = Curl_url_same_origin (u , data -> state .uh );
1246+ curl_url_cleanup (u );
12711247
1272- p = Curl_get_scheme (scheme );
1273- if (p && (p -> protocol != data -> info .conn_protocol )) {
1274- infof (data , "Clear auth, redirects scheme from %s to %s" ,
1275- data -> info .conn_scheme , scheme );
1276- clear = TRUE;
1277- }
1278- curlx_free (scheme );
1279- }
1280- if (clear ) {
1281- CURLcode result = Curl_reset_userpwd (data );
1282- if (result ) {
1283- curlx_free (follow_url );
1284- return result ;
1285- }
1286- curlx_safefree (data -> state .aptr .user );
1287- curlx_safefree (data -> state .aptr .passwd );
1248+ if ((!same_origin && !data -> set .allow_auth_to_other_hosts ) ||
1249+ !data -> set .str [STRING_USERNAME ]) {
1250+ result = Curl_reset_userpwd (data );
1251+ if (result ) {
1252+ curlx_free (follow_url );
1253+ return result ;
12881254 }
1255+ curlx_safefree (data -> state .aptr .user );
1256+ curlx_safefree (data -> state .aptr .passwd );
12891257 }
1290- }
1291- DEBUGASSERT (follow_url );
1292- {
1293- CURLcode result = Curl_reset_proxypwd (data );
1258+ result = Curl_reset_proxypwd (data );
12941259 if (result ) {
12951260 curlx_free (follow_url );
12961261 return result ;
12971262 }
12981263 }
1264+ DEBUGASSERT (follow_url );
12991265
13001266 if (type == FOLLOW_FAKE ) {
13011267 /* we are only figuring out the new URL if we would have followed locations
0 commit comments