5 * Inspired by Requests for Python.
7 * Based on concepts from SimplePie_File, RequestCore and WP_Http.
15 * Inspired by Requests for Python.
17 * Based on concepts from SimplePie_File, RequestCore and WP_Http.
55 const DELETE = 'DELETE';
62 const OPTIONS = 'OPTIONS';
69 const TRACE = 'TRACE';
74 * @link https://tools.ietf.org/html/rfc5789
77 const PATCH = 'PATCH';
80 * Default size of buffer size to read streams
84 const BUFFER_SIZE = 1160;
87 * Current version of Requests
91 const VERSION = '1.7';
94 * Registered transport classes
98 protected static $transports = array();
101 * Selected transport name
103 * Use {@see get_transport()} instead
107 public static $transport = array();
110 * Default certificate path.
112 * @see Requests::get_certificate_path()
113 * @see Requests::set_certificate_path()
117 protected static $certificate_path;
120 * This is a static class, do not instantiate it
122 * @codeCoverageIgnore
124 private function __construct() {}
127 * Autoloader for Requests
129 * Register this with {@see register_autoloader()} if you'd like to avoid
130 * having to create your own.
132 * (You can also use `spl_autoload_register` directly if you'd prefer.)
134 * @codeCoverageIgnore
136 * @param string $class Class name to load
138 public static function autoloader($class) {
139 // Check that the class starts with "Requests"
140 if (strpos($class, 'Requests') !== 0) {
144 $file = str_replace('_', '/', $class);
145 if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
146 require_once(dirname(__FILE__) . '/' . $file . '.php');
151 * Register the built-in autoloader
153 * @codeCoverageIgnore
155 public static function register_autoloader() {
156 spl_autoload_register(array('Requests', 'autoloader'));
160 * Register a transport
162 * @param string $transport Transport class to add, must support the Requests_Transport interface
164 public static function add_transport($transport) {
165 if (empty(self::$transports)) {
166 self::$transports = array(
167 'Requests_Transport_cURL',
168 'Requests_Transport_fsockopen',
172 self::$transports = array_merge(self::$transports, array($transport));
176 * Get a working transport
178 * @throws Requests_Exception If no valid transport is found (`notransport`)
179 * @return Requests_Transport
181 protected static function get_transport($capabilities = array()) {
182 // Caching code, don't bother testing coverage
183 // @codeCoverageIgnoreStart
184 // array of capabilities as a string to be used as an array key
185 ksort($capabilities);
186 $cap_string = serialize($capabilities);
188 // Don't search for a transport if it's already been done for these $capabilities
189 if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) {
190 return new self::$transport[$cap_string]();
192 // @codeCoverageIgnoreEnd
194 if (empty(self::$transports)) {
195 self::$transports = array(
196 'Requests_Transport_cURL',
197 'Requests_Transport_fsockopen',
201 // Find us a working transport
202 foreach (self::$transports as $class) {
203 if (!class_exists($class)) {
207 $result = call_user_func(array($class, 'test'), $capabilities);
209 self::$transport[$cap_string] = $class;
213 if (self::$transport[$cap_string] === null) {
214 throw new Requests_Exception('No working transports found', 'notransport', self::$transports);
217 return new self::$transport[$cap_string]();
223 * @param array $headers
224 * @param array $options
225 * @return Requests_Response
230 public static function get($url, $headers = array(), $options = array()) {
231 return self::request($url, $headers, null, self::GET, $options);
235 * Send a HEAD request
237 public static function head($url, $headers = array(), $options = array()) {
238 return self::request($url, $headers, null, self::HEAD, $options);
242 * Send a DELETE request
244 public static function delete($url, $headers = array(), $options = array()) {
245 return self::request($url, $headers, null, self::DELETE, $options);
249 * Send a TRACE request
251 public static function trace($url, $headers = array(), $options = array()) {
252 return self::request($url, $headers, null, self::TRACE, $options);
259 * @param array $headers
261 * @param array $options
262 * @return Requests_Response
265 * Send a POST request
267 public static function post($url, $headers = array(), $data = array(), $options = array()) {
268 return self::request($url, $headers, $data, self::POST, $options);
273 public static function put($url, $headers = array(), $data = array(), $options = array()) {
274 return self::request($url, $headers, $data, self::PUT, $options);
278 * Send an OPTIONS request
280 public static function options($url, $headers = array(), $data = array(), $options = array()) {
281 return self::request($url, $headers, $data, self::OPTIONS, $options);
285 * Send a PATCH request
287 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
288 * specification recommends that should send an ETag
290 * @link https://tools.ietf.org/html/rfc5789
292 public static function patch($url, $headers, $data = array(), $options = array()) {
293 return self::request($url, $headers, $data, self::PATCH, $options);
298 * Main interface for HTTP requests
300 * This method initiates a request and sends it via a transport before
303 * The `$options` parameter takes an associative array with the following
306 * - `timeout`: How long should we wait for a response?
307 * Note: for cURL, a minimum of 1 second applies, as DNS resolution
308 * operates at second-resolution only.
309 * (float, seconds with a millisecond precision, default: 10, example: 0.01)
310 * - `connect_timeout`: How long should we wait while trying to connect?
311 * (float, seconds with a millisecond precision, default: 10, example: 0.01)
312 * - `useragent`: Useragent to send to the server
313 * (string, default: php-requests/$version)
314 * - `follow_redirects`: Should we follow 3xx redirects?
315 * (boolean, default: true)
316 * - `redirects`: How many times should we redirect before erroring?
317 * (integer, default: 10)
318 * - `blocking`: Should we block processing on this request?
319 * (boolean, default: true)
320 * - `filename`: File to stream the body to instead.
321 * (string|boolean, default: false)
322 * - `auth`: Authentication handler or array of user/password details to use
323 * for Basic authentication
324 * (Requests_Auth|array|boolean, default: false)
325 * - `proxy`: Proxy details to use for proxy by-passing and authentication
326 * (Requests_Proxy|array|string|boolean, default: false)
327 * - `max_bytes`: Limit for the response body size.
328 * (integer|boolean, default: false)
329 * - `idn`: Enable IDN parsing
330 * (boolean, default: true)
331 * - `transport`: Custom transport. Either a class name, or a
332 * transport object. Defaults to the first working transport from
333 * {@see getTransport()}
334 * (string|Requests_Transport, default: {@see getTransport()})
335 * - `hooks`: Hooks handler.
336 * (Requests_Hooker, default: new Requests_Hooks())
337 * - `verify`: Should we verify SSL certificates? Allows passing in a custom
338 * certificate file as a string. (Using true uses the system-wide root
339 * certificate store instead, but this may have different behaviour
340 * across transports.)
341 * (string|boolean, default: library/Requests/Transport/cacert.pem)
342 * - `verifyname`: Should we verify the common name in the SSL certificate?
343 * (boolean: default, true)
344 * - `data_format`: How should we send the `$data` parameter?
345 * (string, one of 'query' or 'body', default: 'query' for
346 * HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH)
348 * @throws Requests_Exception On invalid URLs (`nonhttp`)
350 * @param string $url URL to request
351 * @param array $headers Extra headers to send with the request
352 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
353 * @param string $type HTTP request type (use Requests constants)
354 * @param array $options Options for the request (see description for more information)
355 * @return Requests_Response
357 public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) {
358 if (empty($options['type'])) {
359 $options['type'] = $type;
361 $options = array_merge(self::get_default_options(), $options);
363 self::set_defaults($url, $headers, $data, $type, $options);
365 $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options));
367 if (!empty($options['transport'])) {
368 $transport = $options['transport'];
370 if (is_string($options['transport'])) {
371 $transport = new $transport();
375 $need_ssl = (0 === stripos($url, 'https://'));
376 $capabilities = array('ssl' => $need_ssl);
377 $transport = self::get_transport($capabilities);
379 $response = $transport->request($url, $headers, $data, $options);
381 $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options));
383 return self::parse_response($response, $url, $headers, $data, $options);
387 * Send multiple HTTP requests simultaneously
389 * The `$requests` parameter takes an associative or indexed array of
390 * request fields. The key of each request can be used to match up the
391 * request with the returned data, or with the request passed into your
392 * `multiple.request.complete` callback.
394 * The request fields value is an associative array with the following keys:
396 * - `url`: Request URL Same as the `$url` parameter to
397 * {@see Requests::request}
399 * - `headers`: Associative array of header fields. Same as the `$headers`
400 * parameter to {@see Requests::request}
401 * (array, default: `array()`)
402 * - `data`: Associative array of data fields or a string. Same as the
403 * `$data` parameter to {@see Requests::request}
404 * (array|string, default: `array()`)
405 * - `type`: HTTP request type (use Requests constants). Same as the `$type`
406 * parameter to {@see Requests::request}
407 * (string, default: `Requests::GET`)
408 * - `cookies`: Associative array of cookie name to value, or cookie jar.
409 * (array|Requests_Cookie_Jar)
411 * If the `$options` parameter is specified, individual requests will
412 * inherit options from it. This can be used to use a single hooking system,
413 * or set all the types to `Requests::POST`, for example.
415 * In addition, the `$options` parameter takes the following global options:
417 * - `complete`: A callback for when a request is complete. Takes two
418 * parameters, a Requests_Response/Requests_Exception reference, and the
419 * ID from the request array (Note: this can also be overridden on a
420 * per-request basis, although that's a little silly)
423 * @param array $requests Requests data (see description for more information)
424 * @param array $options Global and default options (see {@see Requests::request})
425 * @return array Responses (either Requests_Response or a Requests_Exception object)
427 public static function request_multiple($requests, $options = array()) {
428 $options = array_merge(self::get_default_options(true), $options);
430 if (!empty($options['hooks'])) {
431 $options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
432 if (!empty($options['complete'])) {
433 $options['hooks']->register('multiple.request.complete', $options['complete']);
437 foreach ($requests as $id => &$request) {
438 if (!isset($request['headers'])) {
439 $request['headers'] = array();
441 if (!isset($request['data'])) {
442 $request['data'] = array();
444 if (!isset($request['type'])) {
445 $request['type'] = self::GET;
447 if (!isset($request['options'])) {
448 $request['options'] = $options;
449 $request['options']['type'] = $request['type'];
452 if (empty($request['options']['type'])) {
453 $request['options']['type'] = $request['type'];
455 $request['options'] = array_merge($options, $request['options']);
458 self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']);
460 // Ensure we only hook in once
461 if ($request['options']['hooks'] !== $options['hooks']) {
462 $request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
463 if (!empty($request['options']['complete'])) {
464 $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']);
470 if (!empty($options['transport'])) {
471 $transport = $options['transport'];
473 if (is_string($options['transport'])) {
474 $transport = new $transport();
478 $transport = self::get_transport();
480 $responses = $transport->request_multiple($requests, $options);
482 foreach ($responses as $id => &$response) {
483 // If our hook got messed with somehow, ensure we end up with the
485 if (is_string($response)) {
486 $request = $requests[$id];
487 self::parse_multiple($response, $request);
488 $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id));
496 * Get the default options
498 * @see Requests::request() for values returned by this method
499 * @param boolean $multirequest Is this a multirequest?
500 * @return array Default option values
502 protected static function get_default_options($multirequest = false) {
505 'connect_timeout' => 10,
506 'useragent' => 'php-requests/' . self::VERSION,
507 'protocol_version' => 1.1,
510 'follow_redirects' => true,
517 'max_bytes' => false,
521 'verify' => Requests::get_certificate_path(),
522 'verifyname' => true,
524 if ($multirequest !== false) {
525 $defaults['complete'] = null;
531 * Get default certificate path.
533 * @return string Default certificate path.
535 public static function get_certificate_path() {
536 if ( ! empty( Requests::$certificate_path ) ) {
537 return Requests::$certificate_path;
540 return dirname(__FILE__) . '/Requests/Transport/cacert.pem';
544 * Set default certificate path.
546 * @param string $path Certificate path, pointing to a PEM file.
548 public static function set_certificate_path( $path ) {
549 Requests::$certificate_path = $path;
553 * Set the default values
555 * @param string $url URL to request
556 * @param array $headers Extra headers to send with the request
557 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
558 * @param string $type HTTP request type
559 * @param array $options Options for the request
560 * @return array $options
562 protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {
563 if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {
564 throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);
567 if (empty($options['hooks'])) {
568 $options['hooks'] = new Requests_Hooks();
571 if (is_array($options['auth'])) {
572 $options['auth'] = new Requests_Auth_Basic($options['auth']);
574 if ($options['auth'] !== false) {
575 $options['auth']->register($options['hooks']);
578 if (is_string($options['proxy']) || is_array($options['proxy'])) {
579 $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']);
581 if ($options['proxy'] !== false) {
582 $options['proxy']->register($options['hooks']);
585 if (is_array($options['cookies'])) {
586 $options['cookies'] = new Requests_Cookie_Jar($options['cookies']);
588 elseif (empty($options['cookies'])) {
589 $options['cookies'] = new Requests_Cookie_Jar();
591 if ($options['cookies'] !== false) {
592 $options['cookies']->register($options['hooks']);
595 if ($options['idn'] !== false) {
596 $iri = new Requests_IRI($url);
597 $iri->host = Requests_IDNAEncoder::encode($iri->ihost);
601 // Massage the type to ensure we support it.
602 $type = strtoupper($type);
604 if (!isset($options['data_format'])) {
605 if (in_array($type, array(self::HEAD, self::GET, self::DELETE))) {
606 $options['data_format'] = 'query';
609 $options['data_format'] = 'body';
615 * HTTP response parser
617 * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`)
618 * @throws Requests_Exception On missing head/body separator (`noversion`)
619 * @throws Requests_Exception On missing head/body separator (`toomanyredirects`)
621 * @param string $headers Full response text including headers and body
622 * @param string $url Original request URL
623 * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects
624 * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects
625 * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects
626 * @return Requests_Response
628 protected static function parse_response($headers, $url, $req_headers, $req_data, $options) {
629 $return = new Requests_Response();
630 if (!$options['blocking']) {
634 $return->raw = $headers;
637 if (!$options['filename']) {
638 if (($pos = strpos($headers, "\r\n\r\n")) === false) {
640 throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
643 $headers = substr($return->raw, 0, $pos);
644 $return->body = substr($return->raw, $pos + strlen("\n\r\n\r"));
649 // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3)
650 $headers = str_replace("\r\n", "\n", $headers);
651 // Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2)
652 $headers = preg_replace('/\n[ \t]/', ' ', $headers);
653 $headers = explode("\n", $headers);
654 preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches);
655 if (empty($matches)) {
656 throw new Requests_Exception('Response could not be parsed', 'noversion', $headers);
658 $return->protocol_version = (float) $matches[1];
659 $return->status_code = (int) $matches[2];
660 if ($return->status_code >= 200 && $return->status_code < 300) {
661 $return->success = true;
664 foreach ($headers as $header) {
665 list($key, $value) = explode(':', $header, 2);
666 $value = trim($value);
667 preg_replace('#(\s+)#i', ' ', $value);
668 $return->headers[$key] = $value;
670 if (isset($return->headers['transfer-encoding'])) {
671 $return->body = self::decode_chunked($return->body);
672 unset($return->headers['transfer-encoding']);
674 if (isset($return->headers['content-encoding'])) {
675 $return->body = self::decompress($return->body);
678 //fsockopen and cURL compatibility
679 if (isset($return->headers['connection'])) {
680 unset($return->headers['connection']);
683 $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options));
685 if ($return->is_redirect() && $options['follow_redirects'] === true) {
686 if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) {
687 if ($return->status_code === 303) {
688 $options['type'] = self::GET;
690 $options['redirected']++;
691 $location = $return->headers['location'];
692 if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) {
693 // relative redirect, for compatibility make it absolute
694 $location = Requests_IRI::absolutize($url, $location);
695 $location = $location->uri;
705 $options['hooks']->dispatch('requests.before_redirect', $hook_args);
706 $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options);
707 $redirected->history[] = $return;
710 elseif ($options['redirected'] >= $options['redirects']) {
711 throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return);
715 $return->redirects = $options['redirected'];
717 $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options));
722 * Callback for `transport.internal.parse_response`
724 * Internal use only. Converts a raw HTTP response to a Requests_Response
725 * while still executing a multiple request.
727 * @param string $response Full response text including headers and body (will be overwritten with Response instance)
728 * @param array $request Request data as passed into {@see Requests::request_multiple()}
729 * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object
731 public static function parse_multiple(&$response, $request) {
733 $url = $request['url'];
734 $headers = $request['headers'];
735 $data = $request['data'];
736 $options = $request['options'];
737 $response = self::parse_response($response, $url, $headers, $data, $options);
739 catch (Requests_Exception $e) {
745 * Decoded a chunked body as per RFC 2616
747 * @see https://tools.ietf.org/html/rfc2616#section-3.6.1
748 * @param string $data Chunked body
749 * @return string Decoded body
751 protected static function decode_chunked($data) {
752 if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) {
762 $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches);
764 // Looks like it's not chunked after all
768 $length = hexdec(trim($matches[1]));
770 // Ignore trailer headers
774 $chunk_length = strlen($matches[0]);
775 $decoded .= substr($encoded, $chunk_length, $length);
776 $encoded = substr($encoded, $chunk_length + $length + 2);
778 if (trim($encoded) === '0' || empty($encoded)) {
783 // We'll never actually get down here
784 // @codeCoverageIgnoreStart
786 // @codeCoverageIgnoreEnd
789 * Convert a key => value array to a 'key: value' array for headers
791 * @param array $array Dictionary of header values
792 * @return array List of headers
794 public static function flatten($array) {
796 foreach ($array as $key => $value) {
797 $return[] = sprintf('%s: %s', $key, $value);
803 * Convert a key => value array to a 'key: value' array for headers
805 * @codeCoverageIgnore
806 * @deprecated Misspelling of {@see Requests::flatten}
807 * @param array $array Dictionary of header values
808 * @return array List of headers
810 public static function flattern($array) {
811 return self::flatten($array);
815 * Decompress an encoded body
817 * Implements gzip, compress and deflate. Guesses which it is by attempting
820 * @param string $data Compressed data in one of the above formats
821 * @return string Decompressed string
823 public static function decompress($data) {
824 if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") {
825 // Not actually compressed. Probably cURL ruining this for us.
829 if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) {
832 elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) {
835 elseif (($decoded = self::compatible_gzinflate($data)) !== false) {
838 elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) {
846 * Decompression of deflated string while staying compatible with the majority of servers.
848 * Certain Servers will return deflated data with headers which PHP's gzinflate()
849 * function cannot handle out of the box. The following function has been created from
850 * various snippets on the gzinflate() PHP documentation.
852 * Warning: Magic numbers within. Due to the potential different formats that the compressed
853 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
854 * takes place. For a simple progmatic way to determine the magic offset in use, see:
855 * https://core.trac.wordpress.org/ticket/18273
858 * @link https://core.trac.wordpress.org/ticket/18273
859 * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
860 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
862 * @param string $gzData String to decompress.
863 * @return string|bool False on failure.
865 public static function compatible_gzinflate($gzData) {
866 // Compressed data might contain a full zlib header, if so strip it for
868 if (substr($gzData, 0, 3) == "\x1f\x8b\x08") {
870 $flg = ord(substr($gzData, 3, 1));
873 list($xlen) = unpack('v', substr($gzData, $i, 2));
877 $i = strpos($gzData, "\0", $i) + 1;
880 $i = strpos($gzData, "\0", $i) + 1;
886 $decompressed = self::compatible_gzinflate(substr($gzData, $i));
887 if (false !== $decompressed) {
888 return $decompressed;
892 // If the data is Huffman Encoded, we must first strip the leading 2
893 // byte Huffman marker for gzinflate()
894 // The response is Huffman coded by many compressors such as
895 // java.util.zip.Deflater, Ruby’s Zlib::Deflate, and .NET's
896 // System.IO.Compression.DeflateStream.
898 // See https://decompres.blogspot.com/ for a quick explanation of this
900 $huffman_encoded = false;
902 // low nibble of first byte should be 0x08
903 list(, $first_nibble) = unpack('h', $gzData);
905 // First 2 bytes should be divisible by 0x1F
906 list(, $first_two_bytes) = unpack('n', $gzData);
908 if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) {
909 $huffman_encoded = true;
912 if ($huffman_encoded) {
913 if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
914 return $decompressed;
918 if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) {
919 // ZIP file format header
920 // Offset 6: 2 bytes, General-purpose field
921 // Offset 26: 2 bytes, filename length
922 // Offset 28: 2 bytes, optional field length
923 // Offset 30: Filename field, followed by optional field, followed
924 // immediately by data
925 list(, $general_purpose_flag) = unpack('v', substr($gzData, 6, 2));
927 // If the file has been compressed on the fly, 0x08 bit is set of
928 // the general purpose field. We can use this to differentiate
929 // between a compressed document, and a ZIP file
930 $zip_compressed_on_the_fly = (0x08 == (0x08 & $general_purpose_flag));
932 if (!$zip_compressed_on_the_fly) {
933 // Don't attempt to decode a compressed zip file
937 // Determine the first byte of data, based on the above ZIP header
939 $first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4)));
940 if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) {
941 return $decompressed;
946 // Finally fall back to straight gzinflate
947 if (false !== ($decompressed = @gzinflate($gzData))) {
948 return $decompressed;
951 // Fallback for all above failing, not expected, but included for
952 // debugging and preventing regressions and to track stats
953 if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
954 return $decompressed;
960 public static function match_domain($host, $reference) {
961 // Check for a direct match
962 if ($host === $reference) {
966 // Calculate the valid wildcard match if the host is not an IP address
967 // Also validates that the host has 3 parts or more, as per Firefox's
969 $parts = explode('.', $host);
970 if (ip2long($host) === false && count($parts) >= 3) {
972 $wildcard = implode('.', $parts);
973 if ($wildcard === $reference) {