author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* HTTP API: WP_Http class |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage HTTP |
|
7 |
* @since 2.7.0 |
|
8 |
*/ |
|
9 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
if ( ! class_exists( 'Requests' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
require( ABSPATH . WPINC . '/class-requests.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
Requests::register_autoloader(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
|
0 | 17 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* Core class used for managing HTTP transports and making HTTP requests. |
0 | 19 |
* |
20 |
* This class is used to consistently make outgoing HTTP requests easy for developers |
|
21 |
* while still being compatible with the many PHP configurations under which |
|
22 |
* WordPress runs. |
|
23 |
* |
|
24 |
* Debugging includes several actions, which pass different variables for debugging the HTTP API. |
|
25 |
* |
|
26 |
* @since 2.7.0 |
|
27 |
*/ |
|
28 |
class WP_Http { |
|
29 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
// Aliases for HTTP response codes. |
9 | 31 |
const HTTP_CONTINUE = 100; |
32 |
const SWITCHING_PROTOCOLS = 101; |
|
33 |
const PROCESSING = 102; |
|
34 |
const EARLY_HINTS = 103; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
|
9 | 36 |
const OK = 200; |
37 |
const CREATED = 201; |
|
38 |
const ACCEPTED = 202; |
|
39 |
const NON_AUTHORITATIVE_INFORMATION = 203; |
|
40 |
const NO_CONTENT = 204; |
|
41 |
const RESET_CONTENT = 205; |
|
42 |
const PARTIAL_CONTENT = 206; |
|
43 |
const MULTI_STATUS = 207; |
|
44 |
const IM_USED = 226; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
|
9 | 46 |
const MULTIPLE_CHOICES = 300; |
47 |
const MOVED_PERMANENTLY = 301; |
|
48 |
const FOUND = 302; |
|
49 |
const SEE_OTHER = 303; |
|
50 |
const NOT_MODIFIED = 304; |
|
51 |
const USE_PROXY = 305; |
|
52 |
const RESERVED = 306; |
|
53 |
const TEMPORARY_REDIRECT = 307; |
|
54 |
const PERMANENT_REDIRECT = 308; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
const BAD_REQUEST = 400; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
const UNAUTHORIZED = 401; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
const PAYMENT_REQUIRED = 402; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
const FORBIDDEN = 403; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
const NOT_FOUND = 404; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
const METHOD_NOT_ALLOWED = 405; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
const NOT_ACCEPTABLE = 406; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
const PROXY_AUTHENTICATION_REQUIRED = 407; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
const REQUEST_TIMEOUT = 408; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
const CONFLICT = 409; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
const GONE = 410; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
const LENGTH_REQUIRED = 411; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
const PRECONDITION_FAILED = 412; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
const REQUEST_ENTITY_TOO_LARGE = 413; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
const REQUEST_URI_TOO_LONG = 414; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
const UNSUPPORTED_MEDIA_TYPE = 415; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
const EXPECTATION_FAILED = 417; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
const IM_A_TEAPOT = 418; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
const MISDIRECTED_REQUEST = 421; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
const UNPROCESSABLE_ENTITY = 422; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
const LOCKED = 423; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
const FAILED_DEPENDENCY = 424; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
const UPGRADE_REQUIRED = 426; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
const PRECONDITION_REQUIRED = 428; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
const TOO_MANY_REQUESTS = 429; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
const UNAVAILABLE_FOR_LEGAL_REASONS = 451; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
const INTERNAL_SERVER_ERROR = 500; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
const NOT_IMPLEMENTED = 501; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
const BAD_GATEWAY = 502; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
const SERVICE_UNAVAILABLE = 503; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
const GATEWAY_TIMEOUT = 504; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
const HTTP_VERSION_NOT_SUPPORTED = 505; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
const VARIANT_ALSO_NEGOTIATES = 506; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
const INSUFFICIENT_STORAGE = 507; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
const NOT_EXTENDED = 510; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
const NETWORK_AUTHENTICATION_REQUIRED = 511; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
|
0 | 96 |
/** |
5 | 97 |
* Send an HTTP request to a URI. |
0 | 98 |
* |
5 | 99 |
* Please note: The only URI that are supported in the HTTP Transport implementation |
100 |
* are the HTTP and HTTPS protocols. |
|
0 | 101 |
* |
102 |
* @since 2.7.0 |
|
103 |
* |
|
5 | 104 |
* @param string $url The request URL. |
105 |
* @param string|array $args { |
|
106 |
* Optional. Array or string of HTTP request arguments. |
|
107 |
* |
|
9 | 108 |
* @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', |
109 |
* 'TRACE', 'OPTIONS', or 'PATCH'. |
|
5 | 110 |
* Some transports technically allow others, but should not be |
111 |
* assumed. Default 'GET'. |
|
112 |
* @type int $timeout How long the connection should stay open in seconds. Default 5. |
|
113 |
* @type int $redirection Number of allowed redirects. Not supported by all transports |
|
114 |
* Default 5. |
|
115 |
* @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'. |
|
116 |
* Default '1.0'. |
|
117 |
* @type string $user-agent User-agent value sent. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* @type bool $reject_unsafe_urls Whether to pass URLs through wp_http_validate_url(). |
5 | 120 |
* Default false. |
121 |
* @type bool $blocking Whether the calling code requires the result of the request. |
|
122 |
* If set to false, the request will be sent to the remote server, |
|
123 |
* and processing returned to the calling code immediately, the caller |
|
124 |
* will know if the request succeeded or failed, but will not receive |
|
125 |
* any response from the remote server. Default true. |
|
126 |
* @type string|array $headers Array or string of headers to send with the request. |
|
127 |
* Default empty array. |
|
128 |
* @type array $cookies List of cookies to send with the request. Default empty array. |
|
129 |
* @type string|array $body Body to send with the request. Default null. |
|
130 |
* @type bool $compress Whether to compress the $body when sending the request. |
|
131 |
* Default false. |
|
132 |
* @type bool $decompress Whether to decompress a compressed response. If set to false and |
|
133 |
* compressed content is returned in the response anyway, it will |
|
134 |
* need to be separately decompressed. Default true. |
|
135 |
* @type bool $sslverify Whether to verify SSL for the request. Default true. |
|
136 |
* @type string sslcertificates Absolute path to an SSL certificate .crt file. |
|
137 |
* Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'. |
|
138 |
* @type bool $stream Whether to stream to a file. If set to true and no filename was |
|
139 |
* given, it will be droped it in the WP temp dir and its name will |
|
140 |
* be set using the basename of the URL. Default false. |
|
141 |
* @type string $filename Filename of the file to write to when streaming. $stream must be |
|
142 |
* set to true. Default null. |
|
143 |
* @type int $limit_response_size Size in bytes to limit the response to. Default null. |
|
144 |
* |
|
145 |
* } |
|
146 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. |
|
147 |
* A WP_Error instance upon error. |
|
0 | 148 |
*/ |
5 | 149 |
public function request( $url, $args = array() ) { |
0 | 150 |
$defaults = array( |
9 | 151 |
'method' => 'GET', |
5 | 152 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* Filters the timeout value for an HTTP request. |
5 | 154 |
* |
155 |
* @since 2.7.0 |
|
9 | 156 |
* @since 5.1.0 The `$url` parameter was added. |
5 | 157 |
* |
9 | 158 |
* @param int $timeout_value Time in seconds until a request times out. Default 5. |
159 |
* @param string $url The request URL. |
|
5 | 160 |
*/ |
9 | 161 |
'timeout' => apply_filters( 'http_request_timeout', 5, $url ), |
5 | 162 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* Filters the number of redirects allowed during an HTTP request. |
5 | 164 |
* |
165 |
* @since 2.7.0 |
|
9 | 166 |
* @since 5.1.0 The `$url` parameter was added. |
5 | 167 |
* |
9 | 168 |
* @param int $redirect_count Number of redirects allowed. Default 5. |
169 |
* @param string $url The request URL. |
|
5 | 170 |
*/ |
9 | 171 |
'redirection' => apply_filters( 'http_request_redirection_count', 5, $url ), |
5 | 172 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* Filters the version of the HTTP protocol used in a request. |
5 | 174 |
* |
175 |
* @since 2.7.0 |
|
9 | 176 |
* @since 5.1.0 The `$url` parameter was added. |
5 | 177 |
* |
9 | 178 |
* @param string $version Version of HTTP used. Accepts '1.0' and '1.1'. Default '1.0'. |
179 |
* @param string $url The request URL. |
|
5 | 180 |
*/ |
9 | 181 |
'httpversion' => apply_filters( 'http_request_version', '1.0', $url ), |
5 | 182 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* Filters the user agent value sent with an HTTP request. |
5 | 184 |
* |
185 |
* @since 2.7.0 |
|
9 | 186 |
* @since 5.1.0 The `$url` parameter was added. |
5 | 187 |
* |
188 |
* @param string $user_agent WordPress user agent string. |
|
9 | 189 |
* @param string $url The request URL. |
5 | 190 |
*/ |
9 | 191 |
'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $url ), |
5 | 192 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* Filters whether to pass URLs through wp_http_validate_url() in an HTTP request. |
5 | 194 |
* |
195 |
* @since 3.6.0 |
|
9 | 196 |
* @since 5.1.0 The `$url` parameter was added. |
5 | 197 |
* |
9 | 198 |
* @param bool $pass_url Whether to pass URLs through wp_http_validate_url(). Default false. |
199 |
* @param string $url The request URL. |
|
5 | 200 |
*/ |
9 | 201 |
'reject_unsafe_urls' => apply_filters( 'http_request_reject_unsafe_urls', false, $url ), |
202 |
'blocking' => true, |
|
203 |
'headers' => array(), |
|
204 |
'cookies' => array(), |
|
205 |
'body' => null, |
|
206 |
'compress' => false, |
|
207 |
'decompress' => true, |
|
208 |
'sslverify' => true, |
|
209 |
'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt', |
|
210 |
'stream' => false, |
|
211 |
'filename' => null, |
|
0 | 212 |
'limit_response_size' => null, |
213 |
); |
|
214 |
||
215 |
// Pre-parse for the HEAD checks. |
|
216 |
$args = wp_parse_args( $args ); |
|
217 |
||
218 |
// By default, Head requests do not cause redirections. |
|
9 | 219 |
if ( isset( $args['method'] ) && 'HEAD' == $args['method'] ) { |
0 | 220 |
$defaults['redirection'] = 0; |
9 | 221 |
} |
0 | 222 |
|
223 |
$r = wp_parse_args( $args, $defaults ); |
|
5 | 224 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* Filters the arguments used in an HTTP request. |
5 | 226 |
* |
227 |
* @since 2.7.0 |
|
228 |
* |
|
229 |
* @param array $r An array of HTTP request arguments. |
|
230 |
* @param string $url The request URL. |
|
231 |
*/ |
|
0 | 232 |
$r = apply_filters( 'http_request_args', $r, $url ); |
233 |
||
234 |
// The transports decrement this, store a copy of the original value for loop purposes. |
|
9 | 235 |
if ( ! isset( $r['_redirection'] ) ) { |
0 | 236 |
$r['_redirection'] = $r['redirection']; |
9 | 237 |
} |
0 | 238 |
|
5 | 239 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* Filters whether to preempt an HTTP request's return value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
* Returning a non-false value from the filter will short-circuit the HTTP request and return |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* early with that value. A filter should return either: |
5 | 244 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
* - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* - A WP_Error instance |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
* - boolean false (to avoid short-circuiting the response) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
* Returning any other value may result in unexpected behaviour. |
5 | 250 |
* |
251 |
* @since 2.9.0 |
|
252 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* @param false|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @param array $r HTTP request arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @param string $url The request URL. |
5 | 256 |
*/ |
0 | 257 |
$pre = apply_filters( 'pre_http_request', false, $r, $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
|
9 | 259 |
if ( false !== $pre ) { |
0 | 260 |
return $pre; |
9 | 261 |
} |
0 | 262 |
|
263 |
if ( function_exists( 'wp_kses_bad_protocol' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
if ( $r['reject_unsafe_urls'] ) { |
0 | 265 |
$url = wp_http_validate_url( $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
} |
5 | 267 |
if ( $url ) { |
268 |
$url = wp_kses_bad_protocol( $url, array( 'http', 'https', 'ssl' ) ); |
|
269 |
} |
|
0 | 270 |
} |
271 |
||
272 |
$arrURL = @parse_url( $url ); |
|
273 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
if ( empty( $url ) || empty( $arrURL['scheme'] ) ) { |
9 | 275 |
return new WP_Error( 'http_request_failed', __( 'A valid URL was not provided.' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
} |
0 | 277 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
if ( $this->block_request( $url ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) ); |
5 | 280 |
} |
0 | 281 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
// If we are streaming to a file but no filename was given drop it in the WP temp dir |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
// and pick its name using the basename of the $url |
0 | 284 |
if ( $r['stream'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
if ( empty( $r['filename'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
$r['filename'] = get_temp_dir() . basename( $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
// Force some settings if we are streaming to a file and check for existence and perms of destination directory |
0 | 290 |
$r['blocking'] = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
if ( ! wp_is_writable( dirname( $r['filename'] ) ) ) { |
0 | 292 |
return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
} |
0 | 294 |
} |
295 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
if ( is_null( $r['headers'] ) ) { |
0 | 297 |
$r['headers'] = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
} |
0 | 299 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
// WP allows passing in headers as a string, weirdly. |
0 | 301 |
if ( ! is_array( $r['headers'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
$processedHeaders = WP_Http::processHeaders( $r['headers'] ); |
9 | 303 |
$r['headers'] = $processedHeaders['headers']; |
0 | 304 |
} |
305 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
// Setup arguments |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
$headers = $r['headers']; |
9 | 308 |
$data = $r['body']; |
309 |
$type = $r['method']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
$options = array( |
9 | 311 |
'timeout' => $r['timeout'], |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
'useragent' => $r['user-agent'], |
9 | 313 |
'blocking' => $r['blocking'], |
314 |
'hooks' => new WP_HTTP_Requests_Hooks( $url, $r ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
// Ensure redirects follow browser behaviour. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'browser_redirect_compatibility' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
// Validate redirected URLs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
if ( function_exists( 'wp_kses_bad_protocol' ) && $r['reject_unsafe_urls'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'validate_redirects' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
if ( $r['stream'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
$options['filename'] = $r['filename']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
if ( empty( $r['redirection'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
$options['follow_redirects'] = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
$options['redirects'] = $r['redirection']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
// Use byte limit, if we can |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
if ( isset( $r['limit_response_size'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
$options['max_bytes'] = $r['limit_response_size']; |
0 | 337 |
} |
338 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
// If we've got cookies, use and convert them to Requests_Cookie. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
if ( ! empty( $r['cookies'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
$options['cookies'] = WP_Http::normalize_cookies( $r['cookies'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
// SSL certificate handling |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
if ( ! $r['sslverify'] ) { |
9 | 346 |
$options['verify'] = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
$options['verifyname'] = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
$options['verify'] = $r['sslcertificates']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
// All non-GET/HEAD requests should put the arguments in the form body. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
if ( 'HEAD' !== $type && 'GET' !== $type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
$options['data_format'] = 'body'; |
0 | 355 |
} |
356 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
* Filters whether SSL should be verified for non-local requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* @since 2.8.0 |
9 | 361 |
* @since 5.1.0 The `$url` parameter was added. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* |
9 | 363 |
* @param bool $ssl_verify Whether to verify the SSL connection. Default true. |
364 |
* @param string $url The request URL. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
*/ |
9 | 366 |
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'], $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
// Check for proxies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
$proxy = new WP_HTTP_Proxy(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' . $proxy->port() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
if ( $proxy->use_authentication() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
$options['proxy']->use_authentication = true; |
9 | 375 |
$options['proxy']->user = $proxy->username(); |
376 |
$options['proxy']->pass = $proxy->password(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
} |
0 | 378 |
} |
379 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
// Avoid issues where mbstring.func_overload is enabled |
0 | 381 |
mbstring_binary_safe_encoding(); |
382 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
$requests_response = Requests::request( $url, $headers, $data, $type, $options ); |
0 | 385 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
// Convert the response into an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
$http_response = new WP_HTTP_Requests_Response( $requests_response, $r['filename'] ); |
9 | 388 |
$response = $http_response->to_array(); |
0 | 389 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
// Add the original object to the array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
$response['http_response'] = $http_response; |
9 | 392 |
} catch ( Requests_Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
$response = new WP_Error( 'http_request_failed', $e->getMessage() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
} |
0 | 395 |
|
396 |
reset_mbstring_encoding(); |
|
397 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* Fires after an HTTP API response is received and before the response is returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* @param array|WP_Error $response HTTP response or WP_Error object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* @param string $context Context under which the hook is fired. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* @param string $class HTTP transport used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* @param array $r HTTP request arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* @param string $url The request URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
do_action( 'http_api_debug', $response, 'response', 'Requests', $r, $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
if ( is_wp_error( $response ) ) { |
0 | 411 |
return $response; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
if ( ! $r['blocking'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
return array( |
9 | 416 |
'headers' => array(), |
417 |
'body' => '', |
|
418 |
'response' => array( |
|
419 |
'code' => false, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
'message' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
), |
9 | 422 |
'cookies' => array(), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
'http_response' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
} |
0 | 426 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
* Filters the HTTP API response immediately before the response is returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
* @since 2.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
* @param array $response HTTP response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* @param array $r HTTP request arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
* @param string $url The request URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
return apply_filters( 'http_response', $response, $r, $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
* Normalizes cookies for using in Requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* |
9 | 444 |
* @param array $cookies Array of cookies to send with the request. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* @return Requests_Cookie_Jar Cookie holder object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
public static function normalize_cookies( $cookies ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
$cookie_jar = new Requests_Cookie_Jar(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
foreach ( $cookies as $name => $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
if ( $value instanceof WP_Http_Cookie ) { |
9 | 452 |
$cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
} elseif ( is_scalar( $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
$cookie_jar[ $name ] = new Requests_Cookie( $name, $value ); |
0 | 455 |
} |
456 |
} |
|
457 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
return $cookie_jar; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* Match redirect behaviour to browser handling. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
* Changes 302 redirects from POST to GET to match browser handling. Per |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
* RFC 7231, user agents can deviate from the strict reading of the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
* specification for compatibility purposes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
* @param string $location URL to redirect to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* @param array $headers Headers for the redirect. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
* @param string|array $data Body to send with the request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
* @param array $options Redirect request options. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
* @param Requests_Response $original Response object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
public static function browser_redirect_compatibility( $location, $headers, $data, &$options, $original ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
// Browser compat |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
if ( $original->status_code === 302 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
$options['type'] = Requests::GET; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* Validate redirected URLs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* @since 4.7.5 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
* @throws Requests_Exception On unsuccessful URL validation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
489 |
* @param string $location URL to redirect to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
public static function validate_redirects( $location ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
if ( ! wp_http_validate_url( $location ) ) { |
9 | 493 |
throw new Requests_Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
} |
0 | 495 |
} |
496 |
||
497 |
/** |
|
498 |
* Tests which transports are capable of supporting the request. |
|
499 |
* |
|
500 |
* @since 3.2.0 |
|
501 |
* |
|
502 |
* @param array $args Request arguments |
|
503 |
* @param string $url URL to Request |
|
504 |
* |
|
5 | 505 |
* @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request. |
0 | 506 |
*/ |
507 |
public function _get_first_available_transport( $args, $url = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
$transports = array( 'curl', 'streams' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
|
5 | 510 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* Filters which HTTP transports are available and in what order. |
5 | 512 |
* |
513 |
* @since 3.7.0 |
|
514 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
* @param array $transports Array of HTTP transports to check. Default array contains |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
* 'curl', and 'streams', in that order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
* @param array $args HTTP request arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
* @param string $url The URL to request. |
5 | 519 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
$request_order = apply_filters( 'http_api_transports', $transports, $args, $url ); |
0 | 521 |
|
5 | 522 |
// Loop over each transport on each HTTP request looking for one which will serve this request's needs. |
0 | 523 |
foreach ( $request_order as $transport ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
if ( in_array( $transport, $transports ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
$transport = ucfirst( $transport ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
$class = 'WP_Http_' . $transport; |
0 | 528 |
|
5 | 529 |
// Check to see if this transport is a possibility, calls the transport statically. |
9 | 530 |
if ( ! call_user_func( array( $class, 'test' ), $args, $url ) ) { |
0 | 531 |
continue; |
9 | 532 |
} |
0 | 533 |
|
534 |
return $class; |
|
535 |
} |
|
536 |
||
537 |
return false; |
|
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* Dispatches a HTTP request to a supporting transport. |
|
542 |
* |
|
543 |
* Tests each transport in order to find a transport which matches the request arguments. |
|
544 |
* Also caches the transport instance to be used later. |
|
545 |
* |
|
546 |
* The order for requests is cURL, and then PHP Streams. |
|
547 |
* |
|
548 |
* @since 3.2.0 |
|
9 | 549 |
* @deprecated 5.1.0 Use WP_Http::request() |
550 |
* @see WP_Http::request() |
|
0 | 551 |
* |
552 |
* @param string $url URL to Request |
|
553 |
* @param array $args Request arguments |
|
5 | 554 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
0 | 555 |
*/ |
556 |
private function _dispatch_request( $url, $args ) { |
|
557 |
static $transports = array(); |
|
558 |
||
559 |
$class = $this->_get_first_available_transport( $args, $url ); |
|
9 | 560 |
if ( ! $class ) { |
0 | 561 |
return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) ); |
9 | 562 |
} |
0 | 563 |
|
564 |
// Transport claims to support request, instantiate it and give it a whirl. |
|
9 | 565 |
if ( empty( $transports[ $class ] ) ) { |
566 |
$transports[ $class ] = new $class; |
|
567 |
} |
|
0 | 568 |
|
9 | 569 |
$response = $transports[ $class ]->request( $url, $args ); |
0 | 570 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
/** This action is documented in wp-includes/class-http.php */ |
0 | 572 |
do_action( 'http_api_debug', $response, 'response', $class, $args, $url ); |
573 |
||
9 | 574 |
if ( is_wp_error( $response ) ) { |
0 | 575 |
return $response; |
9 | 576 |
} |
0 | 577 |
|
9 | 578 |
/** This filter is documented in wp-includes/class-http.php */ |
0 | 579 |
return apply_filters( 'http_response', $response, $args, $url ); |
580 |
} |
|
581 |
||
582 |
/** |
|
583 |
* Uses the POST HTTP method. |
|
584 |
* |
|
585 |
* Used for sending data that is expected to be in the body. |
|
586 |
* |
|
587 |
* @since 2.7.0 |
|
588 |
* |
|
5 | 589 |
* @param string $url The request URL. |
590 |
* @param string|array $args Optional. Override the defaults. |
|
591 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
|
0 | 592 |
*/ |
9 | 593 |
public function post( $url, $args = array() ) { |
594 |
$defaults = array( 'method' => 'POST' ); |
|
595 |
$r = wp_parse_args( $args, $defaults ); |
|
596 |
return $this->request( $url, $r ); |
|
0 | 597 |
} |
598 |
||
599 |
/** |
|
600 |
* Uses the GET HTTP method. |
|
601 |
* |
|
602 |
* Used for sending data that is expected to be in the body. |
|
603 |
* |
|
604 |
* @since 2.7.0 |
|
605 |
* |
|
5 | 606 |
* @param string $url The request URL. |
607 |
* @param string|array $args Optional. Override the defaults. |
|
608 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
|
0 | 609 |
*/ |
9 | 610 |
public function get( $url, $args = array() ) { |
611 |
$defaults = array( 'method' => 'GET' ); |
|
612 |
$r = wp_parse_args( $args, $defaults ); |
|
613 |
return $this->request( $url, $r ); |
|
0 | 614 |
} |
615 |
||
616 |
/** |
|
617 |
* Uses the HEAD HTTP method. |
|
618 |
* |
|
619 |
* Used for sending data that is expected to be in the body. |
|
620 |
* |
|
621 |
* @since 2.7.0 |
|
622 |
* |
|
5 | 623 |
* @param string $url The request URL. |
624 |
* @param string|array $args Optional. Override the defaults. |
|
625 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
|
0 | 626 |
*/ |
9 | 627 |
public function head( $url, $args = array() ) { |
628 |
$defaults = array( 'method' => 'HEAD' ); |
|
629 |
$r = wp_parse_args( $args, $defaults ); |
|
630 |
return $this->request( $url, $r ); |
|
0 | 631 |
} |
632 |
||
633 |
/** |
|
634 |
* Parses the responses and splits the parts into headers and body. |
|
635 |
* |
|
636 |
* @since 2.7.0 |
|
637 |
* |
|
638 |
* @param string $strResponse The full response string |
|
639 |
* @return array Array with 'headers' and 'body' keys. |
|
640 |
*/ |
|
9 | 641 |
public static function processResponse( $strResponse ) { |
642 |
$res = explode( "\r\n\r\n", $strResponse, 2 ); |
|
0 | 643 |
|
9 | 644 |
return array( |
645 |
'headers' => $res[0], |
|
646 |
'body' => isset( $res[1] ) ? $res[1] : '', |
|
647 |
); |
|
0 | 648 |
} |
649 |
||
650 |
/** |
|
651 |
* Transform header string into an array. |
|
652 |
* |
|
653 |
* If an array is given then it is assumed to be raw header data with numeric keys with the |
|
654 |
* headers as the values. No headers must be passed that were already processed. |
|
655 |
* |
|
656 |
* @since 2.7.0 |
|
657 |
* |
|
658 |
* @param string|array $headers |
|
659 |
* @param string $url The URL that was requested |
|
660 |
* @return array Processed string headers. If duplicate headers are encountered, |
|
9 | 661 |
* Then a numbered array is returned as the value of that header-key. |
0 | 662 |
*/ |
663 |
public static function processHeaders( $headers, $url = '' ) { |
|
5 | 664 |
// Split headers, one per array element. |
9 | 665 |
if ( is_string( $headers ) ) { |
5 | 666 |
// Tolerate line terminator: CRLF = LF (RFC 2616 19.3). |
9 | 667 |
$headers = str_replace( "\r\n", "\n", $headers ); |
5 | 668 |
/* |
669 |
* Unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>, |
|
670 |
* <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2). |
|
671 |
*/ |
|
9 | 672 |
$headers = preg_replace( '/\n[ \t]/', ' ', $headers ); |
5 | 673 |
// Create the headers array. |
9 | 674 |
$headers = explode( "\n", $headers ); |
0 | 675 |
} |
676 |
||
9 | 677 |
$response = array( |
678 |
'code' => 0, |
|
679 |
'message' => '', |
|
680 |
); |
|
0 | 681 |
|
5 | 682 |
/* |
683 |
* If a redirection has taken place, The headers for each page request may have been passed. |
|
684 |
* In this case, determine the final HTTP header and parse from there. |
|
685 |
*/ |
|
9 | 686 |
for ( $i = count( $headers ) - 1; $i >= 0; $i-- ) { |
687 |
if ( ! empty( $headers[ $i ] ) && false === strpos( $headers[ $i ], ':' ) ) { |
|
688 |
$headers = array_splice( $headers, $i ); |
|
0 | 689 |
break; |
690 |
} |
|
691 |
} |
|
692 |
||
9 | 693 |
$cookies = array(); |
0 | 694 |
$newheaders = array(); |
695 |
foreach ( (array) $headers as $tempheader ) { |
|
9 | 696 |
if ( empty( $tempheader ) ) { |
0 | 697 |
continue; |
9 | 698 |
} |
0 | 699 |
|
9 | 700 |
if ( false === strpos( $tempheader, ':' ) ) { |
701 |
$stack = explode( ' ', $tempheader, 3 ); |
|
0 | 702 |
$stack[] = ''; |
703 |
list( , $response['code'], $response['message']) = $stack; |
|
704 |
continue; |
|
705 |
} |
|
706 |
||
9 | 707 |
list($key, $value) = explode( ':', $tempheader, 2 ); |
0 | 708 |
|
9 | 709 |
$key = strtolower( $key ); |
0 | 710 |
$value = trim( $value ); |
711 |
||
712 |
if ( isset( $newheaders[ $key ] ) ) { |
|
9 | 713 |
if ( ! is_array( $newheaders[ $key ] ) ) { |
714 |
$newheaders[ $key ] = array( $newheaders[ $key ] ); |
|
715 |
} |
|
0 | 716 |
$newheaders[ $key ][] = $value; |
717 |
} else { |
|
718 |
$newheaders[ $key ] = $value; |
|
719 |
} |
|
9 | 720 |
if ( 'set-cookie' == $key ) { |
0 | 721 |
$cookies[] = new WP_Http_Cookie( $value, $url ); |
9 | 722 |
} |
0 | 723 |
} |
724 |
||
5 | 725 |
// Cast the Response Code to an int |
726 |
$response['code'] = intval( $response['code'] ); |
|
727 |
||
9 | 728 |
return array( |
729 |
'response' => $response, |
|
730 |
'headers' => $newheaders, |
|
731 |
'cookies' => $cookies, |
|
732 |
); |
|
0 | 733 |
} |
734 |
||
735 |
/** |
|
736 |
* Takes the arguments for a ::request() and checks for the cookie array. |
|
737 |
* |
|
738 |
* If it's found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances, |
|
739 |
* which are each parsed into strings and added to the Cookie: header (within the arguments array). |
|
740 |
* Edits the array by reference. |
|
741 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
* @since 2.8.0 |
0 | 743 |
* |
744 |
* @param array $r Full array of args passed into ::request() |
|
745 |
*/ |
|
746 |
public static function buildCookieHeader( &$r ) { |
|
9 | 747 |
if ( ! empty( $r['cookies'] ) ) { |
5 | 748 |
// Upgrade any name => value cookie pairs to WP_HTTP_Cookie instances. |
0 | 749 |
foreach ( $r['cookies'] as $name => $value ) { |
9 | 750 |
if ( ! is_object( $value ) ) { |
751 |
$r['cookies'][ $name ] = new WP_Http_Cookie( |
|
752 |
array( |
|
753 |
'name' => $name, |
|
754 |
'value' => $value, |
|
755 |
) |
|
756 |
); |
|
757 |
} |
|
0 | 758 |
} |
759 |
||
760 |
$cookies_header = ''; |
|
761 |
foreach ( (array) $r['cookies'] as $cookie ) { |
|
762 |
$cookies_header .= $cookie->getHeaderValue() . '; '; |
|
763 |
} |
|
764 |
||
9 | 765 |
$cookies_header = substr( $cookies_header, 0, -2 ); |
0 | 766 |
$r['headers']['cookie'] = $cookies_header; |
767 |
} |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Decodes chunk transfer-encoding, based off the HTTP 1.1 specification. |
|
772 |
* |
|
773 |
* Based off the HTTP http_encoding_dechunk function. |
|
774 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
* @link https://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding. |
0 | 776 |
* |
777 |
* @since 2.7.0 |
|
778 |
* |
|
779 |
* @param string $body Body content |
|
780 |
* @return string Chunked decoded body on success or raw body on failure. |
|
781 |
*/ |
|
782 |
public static function chunkTransferDecode( $body ) { |
|
783 |
// The body is not chunked encoded or is malformed. |
|
9 | 784 |
if ( ! preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', trim( $body ) ) ) { |
0 | 785 |
return $body; |
9 | 786 |
} |
0 | 787 |
|
788 |
$parsed_body = ''; |
|
5 | 789 |
|
790 |
// We'll be altering $body, so need a backup in case of error. |
|
791 |
$body_original = $body; |
|
0 | 792 |
|
793 |
while ( true ) { |
|
794 |
$has_chunk = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $body, $match ); |
|
9 | 795 |
if ( ! $has_chunk || empty( $match[1] ) ) { |
0 | 796 |
return $body_original; |
9 | 797 |
} |
0 | 798 |
|
9 | 799 |
$length = hexdec( $match[1] ); |
0 | 800 |
$chunk_length = strlen( $match[0] ); |
801 |
||
5 | 802 |
// Parse out the chunk of data. |
0 | 803 |
$parsed_body .= substr( $body, $chunk_length, $length ); |
804 |
||
5 | 805 |
// Remove the chunk from the raw data. |
0 | 806 |
$body = substr( $body, $length + $chunk_length ); |
807 |
||
5 | 808 |
// End of the document. |
9 | 809 |
if ( '0' === trim( $body ) ) { |
0 | 810 |
return $parsed_body; |
9 | 811 |
} |
0 | 812 |
} |
813 |
} |
|
814 |
||
815 |
/** |
|
816 |
* Block requests through the proxy. |
|
817 |
* |
|
818 |
* Those who are behind a proxy and want to prevent access to certain hosts may do so. This will |
|
819 |
* prevent plugins from working and core functionality, if you don't include api.wordpress.org. |
|
820 |
* |
|
821 |
* You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
* file and this will only allow localhost and your site to make requests. The constant |
0 | 823 |
* WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the |
824 |
* WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains |
|
825 |
* are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted. |
|
826 |
* |
|
827 |
* @since 2.8.0 |
|
5 | 828 |
* @link https://core.trac.wordpress.org/ticket/8927 Allow preventing external requests. |
829 |
* @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS |
|
0 | 830 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
* @staticvar array|null $accessible_hosts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
* @staticvar array $wildcard_regex |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
* |
0 | 834 |
* @param string $uri URI of url. |
835 |
* @return bool True to block, false to allow. |
|
836 |
*/ |
|
9 | 837 |
public function block_request( $uri ) { |
0 | 838 |
// We don't need to block requests, because nothing is blocked. |
9 | 839 |
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) { |
0 | 840 |
return false; |
9 | 841 |
} |
0 | 842 |
|
9 | 843 |
$check = parse_url( $uri ); |
844 |
if ( ! $check ) { |
|
0 | 845 |
return true; |
9 | 846 |
} |
0 | 847 |
|
9 | 848 |
$home = parse_url( get_option( 'siteurl' ) ); |
0 | 849 |
|
5 | 850 |
// Don't block requests back to ourselves by default. |
851 |
if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) { |
|
852 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
* Filters whether to block local requests through the proxy. |
5 | 854 |
* |
855 |
* @since 2.8.0 |
|
856 |
* |
|
857 |
* @param bool $block Whether to block local requests through proxy. |
|
858 |
* Default false. |
|
859 |
*/ |
|
860 |
return apply_filters( 'block_local_requests', false ); |
|
861 |
} |
|
0 | 862 |
|
9 | 863 |
if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) ) { |
0 | 864 |
return true; |
9 | 865 |
} |
0 | 866 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
static $accessible_hosts = null; |
9 | 868 |
static $wildcard_regex = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
if ( null === $accessible_hosts ) { |
9 | 870 |
$accessible_hosts = preg_split( '|,\s*|', WP_ACCESSIBLE_HOSTS ); |
0 | 871 |
|
9 | 872 |
if ( false !== strpos( WP_ACCESSIBLE_HOSTS, '*' ) ) { |
0 | 873 |
$wildcard_regex = array(); |
9 | 874 |
foreach ( $accessible_hosts as $host ) { |
0 | 875 |
$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) ); |
9 | 876 |
} |
877 |
$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i'; |
|
0 | 878 |
} |
879 |
} |
|
880 |
||
9 | 881 |
if ( ! empty( $wildcard_regex ) ) { |
882 |
return ! preg_match( $wildcard_regex, $check['host'] ); |
|
883 |
} else { |
|
884 |
return ! in_array( $check['host'], $accessible_hosts ); //Inverse logic, If it's in the array, then we can't access it. |
|
885 |
} |
|
0 | 886 |
|
887 |
} |
|
888 |
||
5 | 889 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
* Used as a wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7. |
5 | 891 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
892 |
* @deprecated 4.4.0 Use wp_parse_url() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
* @see wp_parse_url() |
5 | 894 |
* |
895 |
* @param string $url The URL to parse. |
|
896 |
* @return bool|array False on failure; Array of URL components on success; |
|
897 |
* See parse_url()'s return values. |
|
898 |
*/ |
|
899 |
protected static function parse_url( $url ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
900 |
_deprecated_function( __METHOD__, '4.4.0', 'wp_parse_url()' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
return wp_parse_url( $url ); |
5 | 902 |
} |
903 |
||
904 |
/** |
|
905 |
* Converts a relative URL to an absolute URL relative to a given URL. |
|
906 |
* |
|
907 |
* If an Absolute URL is provided, no processing of that URL is done. |
|
908 |
* |
|
909 |
* @since 3.4.0 |
|
910 |
* |
|
911 |
* @param string $maybe_relative_path The URL which might be relative |
|
912 |
* @param string $url The URL which $maybe_relative_path is relative to |
|
913 |
* @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned. |
|
914 |
*/ |
|
915 |
public static function make_absolute_url( $maybe_relative_path, $url ) { |
|
9 | 916 |
if ( empty( $url ) ) { |
0 | 917 |
return $maybe_relative_path; |
9 | 918 |
} |
0 | 919 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
if ( ! $url_parts = wp_parse_url( $url ) ) { |
5 | 921 |
return $maybe_relative_path; |
922 |
} |
|
923 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
if ( ! $relative_url_parts = wp_parse_url( $maybe_relative_path ) ) { |
0 | 925 |
return $maybe_relative_path; |
5 | 926 |
} |
0 | 927 |
|
5 | 928 |
// Check for a scheme on the 'relative' url |
929 |
if ( ! empty( $relative_url_parts['scheme'] ) ) { |
|
0 | 930 |
return $maybe_relative_path; |
5 | 931 |
} |
0 | 932 |
|
5 | 933 |
$absolute_path = $url_parts['scheme'] . '://'; |
0 | 934 |
|
5 | 935 |
// Schemeless URL's will make it this far, so we check for a host in the relative url and convert it to a protocol-url |
936 |
if ( isset( $relative_url_parts['host'] ) ) { |
|
937 |
$absolute_path .= $relative_url_parts['host']; |
|
9 | 938 |
if ( isset( $relative_url_parts['port'] ) ) { |
5 | 939 |
$absolute_path .= ':' . $relative_url_parts['port']; |
9 | 940 |
} |
5 | 941 |
} else { |
942 |
$absolute_path .= $url_parts['host']; |
|
9 | 943 |
if ( isset( $url_parts['port'] ) ) { |
5 | 944 |
$absolute_path .= ':' . $url_parts['port']; |
9 | 945 |
} |
5 | 946 |
} |
0 | 947 |
|
5 | 948 |
// Start off with the Absolute URL path. |
0 | 949 |
$path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/'; |
950 |
||
5 | 951 |
// If it's a root-relative path, then great. |
0 | 952 |
if ( ! empty( $relative_url_parts['path'] ) && '/' == $relative_url_parts['path'][0] ) { |
953 |
$path = $relative_url_parts['path']; |
|
954 |
||
9 | 955 |
// Else it's a relative path. |
0 | 956 |
} elseif ( ! empty( $relative_url_parts['path'] ) ) { |
5 | 957 |
// Strip off any file components from the absolute path. |
0 | 958 |
$path = substr( $path, 0, strrpos( $path, '/' ) + 1 ); |
959 |
||
5 | 960 |
// Build the new path. |
0 | 961 |
$path .= $relative_url_parts['path']; |
962 |
||
5 | 963 |
// Strip all /path/../ out of the path. |
0 | 964 |
while ( strpos( $path, '../' ) > 1 ) { |
965 |
$path = preg_replace( '![^/]+/\.\./!', '', $path ); |
|
966 |
} |
|
967 |
||
5 | 968 |
// Strip any final leading ../ from the path. |
0 | 969 |
$path = preg_replace( '!^/(\.\./)+!', '', $path ); |
970 |
} |
|
971 |
||
5 | 972 |
// Add the Query string. |
9 | 973 |
if ( ! empty( $relative_url_parts['query'] ) ) { |
0 | 974 |
$path .= '?' . $relative_url_parts['query']; |
9 | 975 |
} |
0 | 976 |
|
977 |
return $absolute_path . '/' . ltrim( $path, '/' ); |
|
978 |
} |
|
979 |
||
980 |
/** |
|
981 |
* Handles HTTP Redirects and follows them if appropriate. |
|
982 |
* |
|
983 |
* @since 3.7.0 |
|
984 |
* |
|
985 |
* @param string $url The URL which was requested. |
|
5 | 986 |
* @param array $args The Arguments which were used to make the request. |
0 | 987 |
* @param array $response The Response of the HTTP request. |
988 |
* @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise. |
|
989 |
*/ |
|
5 | 990 |
public static function handle_redirects( $url, $args, $response ) { |
0 | 991 |
// If no redirects are present, or, redirects were not requested, perform no action. |
9 | 992 |
if ( ! isset( $response['headers']['location'] ) || 0 === $args['_redirection'] ) { |
0 | 993 |
return false; |
9 | 994 |
} |
0 | 995 |
|
5 | 996 |
// Only perform redirections on redirection http codes. |
9 | 997 |
if ( $response['response']['code'] > 399 || $response['response']['code'] < 300 ) { |
0 | 998 |
return false; |
9 | 999 |
} |
0 | 1000 |
|
5 | 1001 |
// Don't redirect if we've run out of redirects. |
9 | 1002 |
if ( $args['redirection']-- <= 0 ) { |
1003 |
return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); |
|
1004 |
} |
|
0 | 1005 |
|
1006 |
$redirect_location = $response['headers']['location']; |
|
1007 |
||
5 | 1008 |
// If there were multiple Location headers, use the last header specified. |
9 | 1009 |
if ( is_array( $redirect_location ) ) { |
0 | 1010 |
$redirect_location = array_pop( $redirect_location ); |
9 | 1011 |
} |
0 | 1012 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1013 |
$redirect_location = WP_Http::make_absolute_url( $redirect_location, $url ); |
0 | 1014 |
|
5 | 1015 |
// POST requests should not POST to a redirected location. |
0 | 1016 |
if ( 'POST' == $args['method'] ) { |
9 | 1017 |
if ( in_array( $response['response']['code'], array( 302, 303 ) ) ) { |
0 | 1018 |
$args['method'] = 'GET'; |
9 | 1019 |
} |
0 | 1020 |
} |
1021 |
||
5 | 1022 |
// Include valid cookies in the redirect process. |
0 | 1023 |
if ( ! empty( $response['cookies'] ) ) { |
1024 |
foreach ( $response['cookies'] as $cookie ) { |
|
9 | 1025 |
if ( $cookie->test( $redirect_location ) ) { |
0 | 1026 |
$args['cookies'][] = $cookie; |
9 | 1027 |
} |
0 | 1028 |
} |
1029 |
} |
|
1030 |
||
1031 |
return wp_remote_request( $redirect_location, $args ); |
|
1032 |
} |
|
1033 |
||
1034 |
/** |
|
1035 |
* Determines if a specified string represents an IP address or not. |
|
1036 |
* |
|
1037 |
* This function also detects the type of the IP address, returning either |
|
1038 |
* '4' or '6' to represent a IPv4 and IPv6 address respectively. |
|
1039 |
* This does not verify if the IP is a valid IP, only that it appears to be |
|
1040 |
* an IP address. |
|
1041 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
* @link http://home.deds.nl/~aeron/regex/ for IPv6 regex |
0 | 1043 |
* |
1044 |
* @since 3.7.0 |
|
1045 |
* |
|
1046 |
* @param string $maybe_ip A suspected IP address |
|
1047 |
* @return integer|bool Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure |
|
1048 |
*/ |
|
5 | 1049 |
public static function is_ip_address( $maybe_ip ) { |
9 | 1050 |
if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $maybe_ip ) ) { |
0 | 1051 |
return 4; |
9 | 1052 |
} |
0 | 1053 |
|
9 | 1054 |
if ( false !== strpos( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) ) { |
0 | 1055 |
return 6; |
9 | 1056 |
} |
0 | 1057 |
|
1058 |
return false; |
|
1059 |
} |
|
1060 |
||
1061 |
} |