wp/wp-includes/http.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    25 	}
    25 	}
    26 	return $http;
    26 	return $http;
    27 }
    27 }
    28 
    28 
    29 /**
    29 /**
    30  * Retrieve the raw response from a safe HTTP request.
    30  * Retrieves the raw response from a safe HTTP request.
    31  *
    31  *
    32  * This function is ideal when the HTTP request is being made to an arbitrary
    32  * This function is ideal when the HTTP request is being made to an arbitrary
    33  * URL. The URL is validated to avoid redirection and request forgery attacks.
    33  * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url()
       
    34  * to avoid Server Side Request Forgery attacks (SSRF).
    34  *
    35  *
    35  * @since 3.6.0
    36  * @since 3.6.0
    36  *
    37  *
    37  * @see wp_remote_request() For more information on the response array format.
    38  * @see wp_remote_request() For more information on the response array format.
    38  * @see WP_Http::request() For default arguments information.
    39  * @see WP_Http::request() For default arguments information.
       
    40  * @see wp_http_validate_url() For more information about how the URL is validated.
       
    41  *
       
    42  * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery
    39  *
    43  *
    40  * @param string $url  URL to retrieve.
    44  * @param string $url  URL to retrieve.
    41  * @param array  $args Optional. Request arguments. Default empty array.
    45  * @param array  $args Optional. Request arguments. Default empty array.
       
    46  *                     See WP_Http::request() for information on accepted arguments.
    42  * @return array|WP_Error The response or WP_Error on failure.
    47  * @return array|WP_Error The response or WP_Error on failure.
    43  */
    48  */
    44 function wp_safe_remote_request( $url, $args = array() ) {
    49 function wp_safe_remote_request( $url, $args = array() ) {
    45 	$args['reject_unsafe_urls'] = true;
    50 	$args['reject_unsafe_urls'] = true;
    46 	$http                       = _wp_http_get_object();
    51 	$http                       = _wp_http_get_object();
    47 	return $http->request( $url, $args );
    52 	return $http->request( $url, $args );
    48 }
    53 }
    49 
    54 
    50 /**
    55 /**
    51  * Retrieve the raw response from a safe HTTP request using the GET method.
    56  * Retrieves the raw response from a safe HTTP request using the GET method.
    52  *
    57  *
    53  * This function is ideal when the HTTP request is being made to an arbitrary
    58  * This function is ideal when the HTTP request is being made to an arbitrary
    54  * URL. The URL is validated to avoid redirection and request forgery attacks.
    59  * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url()
       
    60  * to avoid Server Side Request Forgery attacks (SSRF).
    55  *
    61  *
    56  * @since 3.6.0
    62  * @since 3.6.0
    57  *
    63  *
    58  * @see wp_remote_request() For more information on the response array format.
    64  * @see wp_remote_request() For more information on the response array format.
    59  * @see WP_Http::request() For default arguments information.
    65  * @see WP_Http::request() For default arguments information.
       
    66  * @see wp_http_validate_url() For more information about how the URL is validated.
       
    67  *
       
    68  * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery
    60  *
    69  *
    61  * @param string $url  URL to retrieve.
    70  * @param string $url  URL to retrieve.
    62  * @param array  $args Optional. Request arguments. Default empty array.
    71  * @param array  $args Optional. Request arguments. Default empty array.
       
    72  *                     See WP_Http::request() for information on accepted arguments.
    63  * @return array|WP_Error The response or WP_Error on failure.
    73  * @return array|WP_Error The response or WP_Error on failure.
    64  */
    74  */
    65 function wp_safe_remote_get( $url, $args = array() ) {
    75 function wp_safe_remote_get( $url, $args = array() ) {
    66 	$args['reject_unsafe_urls'] = true;
    76 	$args['reject_unsafe_urls'] = true;
    67 	$http                       = _wp_http_get_object();
    77 	$http                       = _wp_http_get_object();
    68 	return $http->get( $url, $args );
    78 	return $http->get( $url, $args );
    69 }
    79 }
    70 
    80 
    71 /**
    81 /**
    72  * Retrieve the raw response from a safe HTTP request using the POST method.
    82  * Retrieves the raw response from a safe HTTP request using the POST method.
    73  *
    83  *
    74  * This function is ideal when the HTTP request is being made to an arbitrary
    84  * This function is ideal when the HTTP request is being made to an arbitrary
    75  * URL. The URL is validated to avoid redirection and request forgery attacks.
    85  * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url()
       
    86  * to avoid Server Side Request Forgery attacks (SSRF).
    76  *
    87  *
    77  * @since 3.6.0
    88  * @since 3.6.0
    78  *
    89  *
    79  * @see wp_remote_request() For more information on the response array format.
    90  * @see wp_remote_request() For more information on the response array format.
    80  * @see WP_Http::request() For default arguments information.
    91  * @see WP_Http::request() For default arguments information.
       
    92  * @see wp_http_validate_url() For more information about how the URL is validated.
       
    93  *
       
    94  * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery
    81  *
    95  *
    82  * @param string $url  URL to retrieve.
    96  * @param string $url  URL to retrieve.
    83  * @param array  $args Optional. Request arguments. Default empty array.
    97  * @param array  $args Optional. Request arguments. Default empty array.
       
    98  *                     See WP_Http::request() for information on accepted arguments.
    84  * @return array|WP_Error The response or WP_Error on failure.
    99  * @return array|WP_Error The response or WP_Error on failure.
    85  */
   100  */
    86 function wp_safe_remote_post( $url, $args = array() ) {
   101 function wp_safe_remote_post( $url, $args = array() ) {
    87 	$args['reject_unsafe_urls'] = true;
   102 	$args['reject_unsafe_urls'] = true;
    88 	$http                       = _wp_http_get_object();
   103 	$http                       = _wp_http_get_object();
    89 	return $http->post( $url, $args );
   104 	return $http->post( $url, $args );
    90 }
   105 }
    91 
   106 
    92 /**
   107 /**
    93  * Retrieve the raw response from a safe HTTP request using the HEAD method.
   108  * Retrieves the raw response from a safe HTTP request using the HEAD method.
    94  *
   109  *
    95  * This function is ideal when the HTTP request is being made to an arbitrary
   110  * This function is ideal when the HTTP request is being made to an arbitrary
    96  * URL. The URL is validated to avoid redirection and request forgery attacks.
   111  * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url()
       
   112  * to avoid Server Side Request Forgery attacks (SSRF).
    97  *
   113  *
    98  * @since 3.6.0
   114  * @since 3.6.0
    99  *
   115  *
   100  * @see wp_remote_request() For more information on the response array format.
   116  * @see wp_remote_request() For more information on the response array format.
   101  * @see WP_Http::request() For default arguments information.
   117  * @see WP_Http::request() For default arguments information.
       
   118  * @see wp_http_validate_url() For more information about how the URL is validated.
       
   119  *
       
   120  * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery
   102  *
   121  *
   103  * @param string $url  URL to retrieve.
   122  * @param string $url  URL to retrieve.
   104  * @param array  $args Optional. Request arguments. Default empty array.
   123  * @param array  $args Optional. Request arguments. Default empty array.
       
   124  *                     See WP_Http::request() for information on accepted arguments.
   105  * @return array|WP_Error The response or WP_Error on failure.
   125  * @return array|WP_Error The response or WP_Error on failure.
   106  */
   126  */
   107 function wp_safe_remote_head( $url, $args = array() ) {
   127 function wp_safe_remote_head( $url, $args = array() ) {
   108 	$args['reject_unsafe_urls'] = true;
   128 	$args['reject_unsafe_urls'] = true;
   109 	$http                       = _wp_http_get_object();
   129 	$http                       = _wp_http_get_object();
   123  *
   143  *
   124  * @see WP_Http::request() For information on default arguments.
   144  * @see WP_Http::request() For information on default arguments.
   125  *
   145  *
   126  * @param string $url  URL to retrieve.
   146  * @param string $url  URL to retrieve.
   127  * @param array  $args Optional. Request arguments. Default empty array.
   147  * @param array  $args Optional. Request arguments. Default empty array.
       
   148  *                     See WP_Http::request() for information on accepted arguments.
   128  * @return array|WP_Error {
   149  * @return array|WP_Error {
   129  *     The response array or a WP_Error on failure.
   150  *     The response array or a WP_Error on failure.
   130  *
   151  *
   131  *     @type string[]                       $headers       Array of response headers keyed by their name.
   152  *     @type string[]                       $headers       Array of response headers keyed by their name.
   132  *     @type string                         $body          Response body.
   153  *     @type string                         $body          Response body.
   153  * @see wp_remote_request() For more information on the response array format.
   174  * @see wp_remote_request() For more information on the response array format.
   154  * @see WP_Http::request() For default arguments information.
   175  * @see WP_Http::request() For default arguments information.
   155  *
   176  *
   156  * @param string $url  URL to retrieve.
   177  * @param string $url  URL to retrieve.
   157  * @param array  $args Optional. Request arguments. Default empty array.
   178  * @param array  $args Optional. Request arguments. Default empty array.
       
   179  *                     See WP_Http::request() for information on accepted arguments.
   158  * @return array|WP_Error The response or WP_Error on failure.
   180  * @return array|WP_Error The response or WP_Error on failure.
   159  */
   181  */
   160 function wp_remote_get( $url, $args = array() ) {
   182 function wp_remote_get( $url, $args = array() ) {
   161 	$http = _wp_http_get_object();
   183 	$http = _wp_http_get_object();
   162 	return $http->get( $url, $args );
   184 	return $http->get( $url, $args );
   170  * @see wp_remote_request() For more information on the response array format.
   192  * @see wp_remote_request() For more information on the response array format.
   171  * @see WP_Http::request() For default arguments information.
   193  * @see WP_Http::request() For default arguments information.
   172  *
   194  *
   173  * @param string $url  URL to retrieve.
   195  * @param string $url  URL to retrieve.
   174  * @param array  $args Optional. Request arguments. Default empty array.
   196  * @param array  $args Optional. Request arguments. Default empty array.
       
   197  *                     See WP_Http::request() for information on accepted arguments.
   175  * @return array|WP_Error The response or WP_Error on failure.
   198  * @return array|WP_Error The response or WP_Error on failure.
   176  */
   199  */
   177 function wp_remote_post( $url, $args = array() ) {
   200 function wp_remote_post( $url, $args = array() ) {
   178 	$http = _wp_http_get_object();
   201 	$http = _wp_http_get_object();
   179 	return $http->post( $url, $args );
   202 	return $http->post( $url, $args );
   187  * @see wp_remote_request() For more information on the response array format.
   210  * @see wp_remote_request() For more information on the response array format.
   188  * @see WP_Http::request() For default arguments information.
   211  * @see WP_Http::request() For default arguments information.
   189  *
   212  *
   190  * @param string $url  URL to retrieve.
   213  * @param string $url  URL to retrieve.
   191  * @param array  $args Optional. Request arguments. Default empty array.
   214  * @param array  $args Optional. Request arguments. Default empty array.
       
   215  *                     See WP_Http::request() for information on accepted arguments.
   192  * @return array|WP_Error The response or WP_Error on failure.
   216  * @return array|WP_Error The response or WP_Error on failure.
   193  */
   217  */
   194 function wp_remote_head( $url, $args = array() ) {
   218 function wp_remote_head( $url, $args = array() ) {
   195 	$http = _wp_http_get_object();
   219 	$http = _wp_http_get_object();
   196 	return $http->head( $url, $args );
   220 	return $http->head( $url, $args );
   197 }
   221 }
   198 
   222 
   199 /**
   223 /**
   200  * Retrieve only the headers from the raw response.
   224  * Retrieves only the headers from the raw response.
   201  *
   225  *
   202  * @since 2.7.0
   226  * @since 2.7.0
   203  * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
   227  * @since 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance.
   204  *
   228  *
   205  * @see \Requests_Utility_CaseInsensitiveDictionary
   229  * @see \WpOrg\Requests\Utility\CaseInsensitiveDictionary
   206  *
   230  *
   207  * @param array|WP_Error $response HTTP response.
   231  * @param array|WP_Error $response HTTP response.
   208  * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
   232  * @return \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array The headers of the response, or empty array
       
   233  *                                                                 if incorrect parameter given.
   209  */
   234  */
   210 function wp_remote_retrieve_headers( $response ) {
   235 function wp_remote_retrieve_headers( $response ) {
   211 	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
   236 	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
   212 		return array();
   237 		return array();
   213 	}
   238 	}
   214 
   239 
   215 	return $response['headers'];
   240 	return $response['headers'];
   216 }
   241 }
   217 
   242 
   218 /**
   243 /**
   219  * Retrieve a single header by name from the raw response.
   244  * Retrieves a single header by name from the raw response.
   220  *
   245  *
   221  * @since 2.7.0
   246  * @since 2.7.0
   222  *
   247  *
   223  * @param array|WP_Error $response HTTP response.
   248  * @param array|WP_Error $response HTTP response.
   224  * @param string         $header   Header name to retrieve value from.
   249  * @param string         $header   Header name to retrieve value from.
   236 
   261 
   237 	return '';
   262 	return '';
   238 }
   263 }
   239 
   264 
   240 /**
   265 /**
   241  * Retrieve only the response code from the raw response.
   266  * Retrieves only the response code from the raw response.
   242  *
   267  *
   243  * Will return an empty string if incorrect parameter value is given.
   268  * Will return an empty string if incorrect parameter value is given.
   244  *
   269  *
   245  * @since 2.7.0
   270  * @since 2.7.0
   246  *
   271  *
   247  * @param array|WP_Error $response HTTP response.
   272  * @param array|WP_Error $response HTTP response.
   248  * @return int|string The response code as an integer. Empty string on incorrect parameter given.
   273  * @return int|string The response code as an integer. Empty string if incorrect parameter given.
   249  */
   274  */
   250 function wp_remote_retrieve_response_code( $response ) {
   275 function wp_remote_retrieve_response_code( $response ) {
   251 	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
   276 	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
   252 		return '';
   277 		return '';
   253 	}
   278 	}
   254 
   279 
   255 	return $response['response']['code'];
   280 	return $response['response']['code'];
   256 }
   281 }
   257 
   282 
   258 /**
   283 /**
   259  * Retrieve only the response message from the raw response.
   284  * Retrieves only the response message from the raw response.
   260  *
   285  *
   261  * Will return an empty string if incorrect parameter value is given.
   286  * Will return an empty string if incorrect parameter value is given.
   262  *
   287  *
   263  * @since 2.7.0
   288  * @since 2.7.0
   264  *
   289  *
   265  * @param array|WP_Error $response HTTP response.
   290  * @param array|WP_Error $response HTTP response.
   266  * @return string The response message. Empty string on incorrect parameter given.
   291  * @return string The response message. Empty string if incorrect parameter given.
   267  */
   292  */
   268 function wp_remote_retrieve_response_message( $response ) {
   293 function wp_remote_retrieve_response_message( $response ) {
   269 	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
   294 	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
   270 		return '';
   295 		return '';
   271 	}
   296 	}
   272 
   297 
   273 	return $response['response']['message'];
   298 	return $response['response']['message'];
   274 }
   299 }
   275 
   300 
   276 /**
   301 /**
   277  * Retrieve only the body from the raw response.
   302  * Retrieves only the body from the raw response.
   278  *
   303  *
   279  * @since 2.7.0
   304  * @since 2.7.0
   280  *
   305  *
   281  * @param array|WP_Error $response HTTP response.
   306  * @param array|WP_Error $response HTTP response.
   282  * @return string The body of the response. Empty string if no body or incorrect parameter given.
   307  * @return string The body of the response. Empty string if no body or incorrect parameter given.
   288 
   313 
   289 	return $response['body'];
   314 	return $response['body'];
   290 }
   315 }
   291 
   316 
   292 /**
   317 /**
   293  * Retrieve only the cookies from the raw response.
   318  * Retrieves only the cookies from the raw response.
   294  *
   319  *
   295  * @since 4.4.0
   320  * @since 4.4.0
   296  *
   321  *
   297  * @param array|WP_Error $response HTTP response.
   322  * @param array|WP_Error $response HTTP response.
   298  * @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error.
   323  * @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response.
       
   324  *                          Empty array if there are none, or the response is a WP_Error.
   299  */
   325  */
   300 function wp_remote_retrieve_cookies( $response ) {
   326 function wp_remote_retrieve_cookies( $response ) {
   301 	if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
   327 	if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
   302 		return array();
   328 		return array();
   303 	}
   329 	}
   304 
   330 
   305 	return $response['cookies'];
   331 	return $response['cookies'];
   306 }
   332 }
   307 
   333 
   308 /**
   334 /**
   309  * Retrieve a single cookie by name from the raw response.
   335  * Retrieves a single cookie by name from the raw response.
   310  *
   336  *
   311  * @since 4.4.0
   337  * @since 4.4.0
   312  *
   338  *
   313  * @param array|WP_Error $response HTTP response.
   339  * @param array|WP_Error $response HTTP response.
   314  * @param string         $name     The name of the cookie to retrieve.
   340  * @param string         $name     The name of the cookie to retrieve.
   315  * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
   341  * @return WP_Http_Cookie|string The `WP_Http_Cookie` object, or empty string
       
   342  *                               if the cookie is not present in the response.
   316  */
   343  */
   317 function wp_remote_retrieve_cookie( $response, $name ) {
   344 function wp_remote_retrieve_cookie( $response, $name ) {
   318 	$cookies = wp_remote_retrieve_cookies( $response );
   345 	$cookies = wp_remote_retrieve_cookies( $response );
   319 
   346 
   320 	if ( empty( $cookies ) ) {
   347 	if ( empty( $cookies ) ) {
   329 
   356 
   330 	return '';
   357 	return '';
   331 }
   358 }
   332 
   359 
   333 /**
   360 /**
   334  * Retrieve a single cookie's value by name from the raw response.
   361  * Retrieves a single cookie's value by name from the raw response.
   335  *
   362  *
   336  * @since 4.4.0
   363  * @since 4.4.0
   337  *
   364  *
   338  * @param array|WP_Error $response HTTP response.
   365  * @param array|WP_Error $response HTTP response.
   339  * @param string         $name     The name of the cookie to retrieve.
   366  * @param string         $name     The name of the cookie to retrieve.
   340  * @return string The value of the cookie. Empty string if the cookie isn't present in the response.
   367  * @return string The value of the cookie, or empty string
       
   368  *                if the cookie is not present in the response.
   341  */
   369  */
   342 function wp_remote_retrieve_cookie_value( $response, $name ) {
   370 function wp_remote_retrieve_cookie_value( $response, $name ) {
   343 	$cookie = wp_remote_retrieve_cookie( $response, $name );
   371 	$cookie = wp_remote_retrieve_cookie( $response, $name );
   344 
   372 
   345 	if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
   373 	if ( ! ( $cookie instanceof WP_Http_Cookie ) ) {
   346 		return '';
   374 		return '';
   347 	}
   375 	}
   348 
   376 
   349 	return $cookie->value;
   377 	return $cookie->value;
   350 }
   378 }
   366 	$capabilities = wp_parse_args( $capabilities );
   394 	$capabilities = wp_parse_args( $capabilities );
   367 
   395 
   368 	$count = count( $capabilities );
   396 	$count = count( $capabilities );
   369 
   397 
   370 	// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array.
   398 	// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array.
   371 	if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
   399 	if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) === $count ) {
   372 		$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
   400 		$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
   373 	}
   401 	}
   374 
   402 
   375 	if ( $url && ! isset( $capabilities['ssl'] ) ) {
   403 	if ( $url && ! isset( $capabilities['ssl'] ) ) {
   376 		$scheme = parse_url( $url, PHP_URL_SCHEME );
   404 		$scheme = parse_url( $url, PHP_URL_SCHEME );
   381 
   409 
   382 	return (bool) $http->_get_first_available_transport( $capabilities );
   410 	return (bool) $http->_get_first_available_transport( $capabilities );
   383 }
   411 }
   384 
   412 
   385 /**
   413 /**
   386  * Get the HTTP Origin of the current request.
   414  * Gets the HTTP Origin of the current request.
   387  *
   415  *
   388  * @since 3.4.0
   416  * @since 3.4.0
   389  *
   417  *
   390  * @return string URL of the origin. Empty string if no origin.
   418  * @return string URL of the origin. Empty string if no origin.
   391  */
   419  */
   394 	if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) {
   422 	if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) {
   395 		$origin = $_SERVER['HTTP_ORIGIN'];
   423 		$origin = $_SERVER['HTTP_ORIGIN'];
   396 	}
   424 	}
   397 
   425 
   398 	/**
   426 	/**
   399 	 * Change the origin of an HTTP request.
   427 	 * Changes the origin of an HTTP request.
   400 	 *
   428 	 *
   401 	 * @since 3.4.0
   429 	 * @since 3.4.0
   402 	 *
   430 	 *
   403 	 * @param string $origin The original origin for the request.
   431 	 * @param string $origin The original origin for the request.
   404 	 */
   432 	 */
   405 	return apply_filters( 'http_origin', $origin );
   433 	return apply_filters( 'http_origin', $origin );
   406 }
   434 }
   407 
   435 
   408 /**
   436 /**
   409  * Retrieve list of allowed HTTP origins.
   437  * Retrieves list of allowed HTTP origins.
   410  *
   438  *
   411  * @since 3.4.0
   439  * @since 3.4.0
   412  *
   440  *
   413  * @return string[] Array of origin URLs.
   441  * @return string[] Array of origin URLs.
   414  */
   442  */
   425 			'https://' . $home_origin['host'],
   453 			'https://' . $home_origin['host'],
   426 		)
   454 		)
   427 	);
   455 	);
   428 
   456 
   429 	/**
   457 	/**
   430 	 * Change the origin types allowed for HTTP requests.
   458 	 * Changes the origin types allowed for HTTP requests.
   431 	 *
   459 	 *
   432 	 * @since 3.4.0
   460 	 * @since 3.4.0
   433 	 *
   461 	 *
   434 	 * @param string[] $allowed_origins {
   462 	 * @param string[] $allowed_origins {
   435 	 *     Array of default allowed HTTP origins.
   463 	 *     Array of default allowed HTTP origins.
   446 /**
   474 /**
   447  * Determines if the HTTP origin is an authorized one.
   475  * Determines if the HTTP origin is an authorized one.
   448  *
   476  *
   449  * @since 3.4.0
   477  * @since 3.4.0
   450  *
   478  *
   451  * @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used.
   479  * @param string|null $origin Origin URL. If not provided, the value of get_http_origin() is used.
   452  * @return string Origin URL if allowed, empty string if not.
   480  * @return string Origin URL if allowed, empty string if not.
   453  */
   481  */
   454 function is_allowed_http_origin( $origin = null ) {
   482 function is_allowed_http_origin( $origin = null ) {
   455 	$origin_arg = $origin;
   483 	$origin_arg = $origin;
   456 
   484 
   461 	if ( $origin && ! in_array( $origin, get_allowed_http_origins(), true ) ) {
   489 	if ( $origin && ! in_array( $origin, get_allowed_http_origins(), true ) ) {
   462 		$origin = '';
   490 		$origin = '';
   463 	}
   491 	}
   464 
   492 
   465 	/**
   493 	/**
   466 	 * Change the allowed HTTP origin result.
   494 	 * Changes the allowed HTTP origin result.
   467 	 *
   495 	 *
   468 	 * @since 3.4.0
   496 	 * @since 3.4.0
   469 	 *
   497 	 *
   470 	 * @param string $origin     Origin URL if allowed, empty string if not.
   498 	 * @param string $origin     Origin URL if allowed, empty string if not.
   471 	 * @param string $origin_arg Original origin string passed into is_allowed_http_origin function.
   499 	 * @param string $origin_arg Original origin string passed into is_allowed_http_origin function.
   472 	 */
   500 	 */
   473 	return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
   501 	return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
   474 }
   502 }
   475 
   503 
   476 /**
   504 /**
   477  * Send Access-Control-Allow-Origin and related headers if the current request
   505  * Sends Access-Control-Allow-Origin and related headers if the current request
   478  * is from an allowed origin.
   506  * is from an allowed origin.
   479  *
   507  *
   480  * If the request is an OPTIONS request, the script exits with either access
   508  * If the request is an OPTIONS request, the script exits with either access
   481  * control headers sent, or a 403 response if the origin is not allowed. For
   509  * control headers sent, or a 403 response if the origin is not allowed. For
   482  * other request methods, you will receive a return value.
   510  * other request methods, you will receive a return value.
   505 
   533 
   506 	return false;
   534 	return false;
   507 }
   535 }
   508 
   536 
   509 /**
   537 /**
   510  * Validate a URL for safe use in the HTTP API.
   538  * Validates a URL for safe use in the HTTP API.
       
   539  *
       
   540  * Examples of URLs that are considered unsafe:
       
   541  *
       
   542  * - ftp://example.com/caniload.php - Invalid protocol - only http and https are allowed.
       
   543  * - http:///example.com/caniload.php - Malformed URL.
       
   544  * - http://user:pass@example.com/caniload.php - Login information.
       
   545  * - http://exampleeeee.com/caniload.php - Invalid hostname, as the IP cannot be looked up in DNS.
       
   546  *
       
   547  * Examples of URLs that are considered unsafe by default:
       
   548  *
       
   549  * - http://192.168.0.1/caniload.php - IPs from LAN networks.
       
   550  *   This can be changed with the {@see 'http_request_host_is_external'} filter.
       
   551  * - http://198.143.164.252:81/caniload.php - By default, only 80, 443, and 8080 ports are allowed.
       
   552  *   This can be changed with the {@see 'http_allowed_safe_ports'} filter.
   511  *
   553  *
   512  * @since 3.5.2
   554  * @since 3.5.2
   513  *
   555  *
   514  * @param string $url Request URL.
   556  * @param string $url Request URL.
   515  * @return string|false URL or false on failure.
   557  * @return string|false URL or false on failure.
   557 				|| ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
   599 				|| ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
   558 				|| ( 192 === $parts[0] && 168 === $parts[1] )
   600 				|| ( 192 === $parts[0] && 168 === $parts[1] )
   559 			) {
   601 			) {
   560 				// If host appears local, reject unless specifically allowed.
   602 				// If host appears local, reject unless specifically allowed.
   561 				/**
   603 				/**
   562 				 * Check if HTTP request is external or not.
   604 				 * Checks if HTTP request is external or not.
   563 				 *
   605 				 *
   564 				 * Allows to change and allow external requests for the HTTP request.
   606 				 * Allows to change and allow external requests for the HTTP request.
   565 				 *
   607 				 *
   566 				 * @since 3.6.0
   608 				 * @since 3.6.0
   567 				 *
   609 				 *
   587 	 *
   629 	 *
   588 	 * Allows to change and allow external requests for the HTTP request.
   630 	 * Allows to change and allow external requests for the HTTP request.
   589 	 *
   631 	 *
   590 	 * @since 5.9.0
   632 	 * @since 5.9.0
   591 	 *
   633 	 *
   592 	 * @param array  $allowed_ports Array of integers for valid ports.
   634 	 * @param int[]  $allowed_ports Array of integers for valid ports.
   593 	 * @param string $host          Host name of the requested URL.
   635 	 * @param string $host          Host name of the requested URL.
   594 	 * @param string $url           Requested URL.
   636 	 * @param string $url           Requested URL.
   595 	 */
   637 	 */
   596 	$allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
   638 	$allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
   597 	if ( is_array( $allowed_ports ) && in_array( $port, $allowed_ports, true ) ) {
   639 	if ( is_array( $allowed_ports ) && in_array( $port, $allowed_ports, true ) ) {
   604 
   646 
   605 	return false;
   647 	return false;
   606 }
   648 }
   607 
   649 
   608 /**
   650 /**
   609  * Mark allowed redirect hosts safe for HTTP requests as well.
   651  * Marks allowed redirect hosts safe for HTTP requests as well.
   610  *
   652  *
   611  * Attached to the {@see 'http_request_host_is_external'} filter.
   653  * Attached to the {@see 'http_request_host_is_external'} filter.
   612  *
   654  *
   613  * @since 3.6.0
   655  * @since 3.6.0
   614  *
   656  *
   680  */
   722  */
   681 function wp_parse_url( $url, $component = -1 ) {
   723 function wp_parse_url( $url, $component = -1 ) {
   682 	$to_unset = array();
   724 	$to_unset = array();
   683 	$url      = (string) $url;
   725 	$url      = (string) $url;
   684 
   726 
   685 	if ( '//' === substr( $url, 0, 2 ) ) {
   727 	if ( str_starts_with( $url, '//' ) ) {
   686 		$to_unset[] = 'scheme';
   728 		$to_unset[] = 'scheme';
   687 		$url        = 'placeholder:' . $url;
   729 		$url        = 'placeholder:' . $url;
   688 	} elseif ( '/' === substr( $url, 0, 1 ) ) {
   730 	} elseif ( str_starts_with( $url, '/' ) ) {
   689 		$to_unset[] = 'scheme';
   731 		$to_unset[] = 'scheme';
   690 		$to_unset[] = 'host';
   732 		$to_unset[] = 'host';
   691 		$url        = 'placeholder://placeholder' . $url;
   733 		$url        = 'placeholder://placeholder' . $url;
   692 	}
   734 	}
   693 
   735 
   705 
   747 
   706 	return _get_component_from_parsed_url_array( $parts, $component );
   748 	return _get_component_from_parsed_url_array( $parts, $component );
   707 }
   749 }
   708 
   750 
   709 /**
   751 /**
   710  * Retrieve a specific component from a parsed URL array.
   752  * Retrieves a specific component from a parsed URL array.
   711  *
   753  *
   712  * @internal
   754  * @internal
   713  *
   755  *
   714  * @since 4.7.0
   756  * @since 4.7.0
   715  * @access private
   757  * @access private
   737 		return null;
   779 		return null;
   738 	}
   780 	}
   739 }
   781 }
   740 
   782 
   741 /**
   783 /**
   742  * Translate a PHP_URL_* constant to the named array keys PHP uses.
   784  * Translates a PHP_URL_* constant to the named array keys PHP uses.
   743  *
   785  *
   744  * @internal
   786  * @internal
   745  *
   787  *
   746  * @since 4.7.0
   788  * @since 4.7.0
   747  * @access private
   789  * @access private