- $http_code = $curl_info['http_code'];
-
- logger($url . ': ' . $http_code . " " . $s, LOGGER_DATA);
- $header = '';
-
- // Pull out multiple headers, e.g. proxy and continuation headers
- // allow for HTTP/2.x without fixing code
-
- while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
- $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
- $header .= $chunk;
- $base = substr($base, strlen($chunk));
- }
-
- $a->set_curl_code($http_code);
- $a->set_curl_content_type($curl_info['content_type']);
- $a->set_curl_headers($header);
-
- if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
- $new_location_info = @parse_url($curl_info['redirect_url']);
- $old_location_info = @parse_url($curl_info['url']);
-
- $newurl = $curl_info['redirect_url'];
-
- if (($new_location_info['path'] == '') && ($new_location_info['host'] != '')) {
- $newurl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
- }
-
- $matches = [];
-
- if (preg_match('/(Location:|URI:)(.*?)\n/i', $header, $matches)) {
- $newurl = trim(array_pop($matches));
- }
- if (strpos($newurl, '/') === 0) {
- $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
- }
- $old_location_query = @parse_url($url, PHP_URL_QUERY);
-
- if ($old_location_query != '') {
- $newurl .= '?' . $old_location_query;
- }
-
- if (filter_var($newurl, FILTER_VALIDATE_URL)) {
- $redirects++;
- @curl_close($ch);
- return self::curl($newurl, $binary, $redirects, $opts);
- }
- }
-
- $a->set_curl_code($http_code);
- $a->set_curl_content_type($curl_info['content_type']);
-
- $rc = intval($http_code);
- $ret['return_code'] = $rc;
- $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);
- $ret['redirect_url'] = $url;
-
- if (!$ret['success']) {
- $ret['error'] = curl_error($ch);
- $ret['debug'] = $curl_info;
- logger('error: '.$url.': '.$ret['return_code'].' - '.$ret['error'], LOGGER_DEBUG);
- logger('debug: '.print_r($curl_info, true), LOGGER_DATA);
- }
-
- $ret['body'] = substr($s, strlen($header));
- $ret['header'] = $header;
-
- if (x($opts, 'debug')) {
- $ret['debug'] = $curl_info;