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(); |
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 ) ) { |
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. |