1
|
|
|
<?php |
2
|
|
|
namespace Hirak\Prestissimo; |
3
|
|
|
|
4
|
|
|
use Composer\IO; |
5
|
|
|
use Composer\Config; |
6
|
|
|
|
7
|
|
|
class FetchRequest extends BaseRequest |
8
|
|
|
{ |
9
|
|
|
protected static $defaultCurlOptions = array( |
10
|
|
|
CURLOPT_HTTPGET => true, |
11
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
12
|
|
|
CURLOPT_MAXREDIRS => 20, |
13
|
|
|
CURLOPT_ENCODING => '', |
14
|
|
|
CURLOPT_RETURNTRANSFER => true, |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
private $headers = array(); |
|
|
|
|
18
|
|
|
private $errno, $error, $info; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $url |
22
|
|
|
* @param IO\IOInterface $io |
23
|
|
|
* @param Config $config |
24
|
|
|
*/ |
25
|
|
|
public function __construct($url, IO\IOInterface $io, Config $config) |
26
|
|
|
{ |
27
|
|
|
$this->setURL($url); |
28
|
|
|
$this->setCA($config->get('capath'), $config->get('cafile')); |
29
|
|
|
$this->setupAuthentication($io, false, $config->get('github-domains'), $config->get('gitlab-domains')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getCurlOptions() |
33
|
|
|
{ |
34
|
|
|
$curlOpts = parent::getCurlOptions(); |
35
|
|
|
$curlOpts[CURLOPT_RETURNTRANSFER] = true; |
36
|
|
|
$curlOpts[CURLOPT_HEADERFUNCTION] = array($this, 'headerCallback'); |
37
|
|
|
return $curlOpts; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private static function getCurl($key) |
41
|
|
|
{ |
42
|
|
|
static $curlCache = array(); |
43
|
|
|
|
44
|
|
|
if (isset($curlCache[$key])) { |
45
|
|
|
return $curlCache[$key]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$ch = curl_init(); |
49
|
|
|
Share::setup($ch); |
50
|
|
|
|
51
|
|
|
return $curlCache[$key] = $ch; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string|false |
56
|
|
|
*/ |
57
|
|
|
public function fetch() |
58
|
|
|
{ |
59
|
|
|
$ch = self::getCurl($this->getOriginURL()); |
60
|
|
|
curl_setopt_array($ch, $this->getCurlOptions()); |
61
|
|
|
|
62
|
|
|
$result = curl_exec($ch); |
63
|
|
|
|
64
|
|
|
$this->errno = $errno = curl_errno($ch); |
65
|
|
|
$this->error = curl_error($ch); |
66
|
|
|
$this->info = $info = curl_getinfo($ch); |
67
|
|
|
|
68
|
|
|
if ($errno === CURLE_OK && $info['http_code'] === 200) { |
69
|
|
|
return $result; |
70
|
|
|
} else { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getLastError() |
76
|
|
|
{ |
77
|
|
|
if ($this->errno || $this->error) { |
78
|
|
|
return array($this->errno, $this->error); |
79
|
|
|
} else { |
80
|
|
|
return array(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getLastHeaders() |
85
|
|
|
{ |
86
|
|
|
return $this->headers; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function headerCallback($ch, $headerString) |
90
|
|
|
{ |
91
|
|
|
$len = strlen($headerString); |
92
|
|
|
$this->headers[] = $headerString; |
93
|
|
|
return $len; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|