|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* hirak/prestissimo |
|
4
|
|
|
* @author Hiraku NAKANO |
|
5
|
|
|
* @license MIT https://github.com/hirak/prestissimo |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Hirak\Prestissimo\Aspects; |
|
8
|
|
|
|
|
9
|
|
|
use Composer\IO; |
|
10
|
|
|
use Composer\Composer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Simple Container for http-get request |
|
14
|
|
|
*/ |
|
15
|
|
|
class HttpGetRequest |
|
16
|
|
|
{ |
|
17
|
|
|
public $origin |
|
|
|
|
|
|
18
|
|
|
, $scheme = 'http' |
|
19
|
|
|
, $host = 'example.com' |
|
20
|
|
|
, $port = 80 |
|
21
|
|
|
, $path = '/' |
|
22
|
|
|
|
|
23
|
|
|
, $special = null |
|
24
|
|
|
|
|
25
|
|
|
, $query = array() |
|
26
|
|
|
, $headers = array() |
|
27
|
|
|
|
|
28
|
|
|
, $curlOpts = array() |
|
29
|
|
|
|
|
30
|
|
|
, $username = null |
|
31
|
|
|
, $password = null |
|
32
|
|
|
|
|
33
|
|
|
, $maybePublic = true |
|
34
|
|
|
, $verbose = false |
|
35
|
|
|
; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* normalize url and authentication info |
|
39
|
|
|
* @param string $origin domain text |
|
40
|
|
|
* @param string $url |
|
41
|
|
|
* @param IO/IOInterface $io |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function __construct($origin, $url, IO\IOInterface $io) |
|
44
|
|
|
{ |
|
45
|
|
|
// normalize github origin |
|
46
|
6 |
|
if (substr($origin, -10) === 'github.com') { |
|
47
|
2 |
|
$origin = 'github.com'; |
|
48
|
2 |
|
$this->special = 'github'; |
|
49
|
|
|
} |
|
50
|
6 |
|
$this->origin = $origin; |
|
51
|
|
|
|
|
52
|
6 |
|
$this->importURL($url); |
|
53
|
|
|
|
|
54
|
6 |
|
if ($this->username && $this->password) { |
|
55
|
1 |
|
$io->setAuthentication($origin, $this->username, $this->password); |
|
56
|
6 |
|
} elseif ($io->hasAuthentication($origin)) { |
|
57
|
1 |
|
$auth = $io->getAuthentication($origin); |
|
58
|
1 |
|
$this->username = $auth['username']; |
|
59
|
1 |
|
$this->password = $auth['password']; |
|
60
|
|
|
} |
|
61
|
6 |
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
public function importURL($url) |
|
64
|
|
|
{ |
|
65
|
6 |
|
$struct = parse_url($url); |
|
66
|
6 |
|
if (! $struct) { |
|
67
|
|
|
throw new \InvalidArgumentException("$url is not valid URL"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
$this->scheme = self::setOr($struct, 'scheme', $this->scheme); |
|
71
|
6 |
|
$this->host = self::setOr($struct, 'host', $this->host); |
|
72
|
6 |
|
$this->port = self::setOr($struct, 'port', null); |
|
73
|
6 |
|
$this->path = self::setOr($struct, 'path', ''); |
|
74
|
6 |
|
$this->username = self::setOr($struct, 'user', null); |
|
75
|
6 |
|
$this->password = self::setOr($struct, 'pass', null); |
|
76
|
|
|
|
|
77
|
6 |
|
if (! empty($struct['query'])) { |
|
78
|
1 |
|
parse_str($struct['query'], $this->query); |
|
79
|
|
|
} |
|
80
|
6 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
// utility for __construct |
|
83
|
6 |
|
private static function setOr(array $struct, $key, $default=null) |
|
84
|
|
|
{ |
|
85
|
6 |
|
if (!empty($struct[$key])) { |
|
86
|
6 |
|
return $struct[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
return $default; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function getCurlOpts() |
|
93
|
|
|
{ |
|
94
|
1 |
|
$curlOpts = $this->curlOpts + array( |
|
95
|
1 |
|
CURLOPT_HTTPGET => true, |
|
96
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
|
97
|
1 |
|
CURLOPT_MAXREDIRS => 20, |
|
98
|
1 |
|
CURLOPT_ENCODING => 'gzip', |
|
99
|
1 |
|
CURLOPT_HTTPHEADER => $this->headers, |
|
100
|
1 |
|
CURLOPT_USERAGENT => $this->genUA(), |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
1 |
|
$curlOpts[CURLOPT_VERBOSE] = (bool) $this->verbose; |
|
104
|
|
|
|
|
105
|
1 |
|
if ($this->username && $this->password) { |
|
106
|
1 |
|
$curlOpts[CURLOPT_USERPWD] = "$this->username:$this->password"; |
|
107
|
|
|
} else { |
|
108
|
1 |
|
$curlOpts[CURLOPT_USERPWD] = null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$curlOpts[CURLOPT_URL] = $this->getUrl(); |
|
112
|
|
|
|
|
113
|
1 |
|
return $curlOpts; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
public function getURL() |
|
117
|
|
|
{ |
|
118
|
2 |
|
if ($this->scheme) { |
|
119
|
2 |
|
$url = "$this->scheme://"; |
|
120
|
|
|
} else { |
|
121
|
1 |
|
$url = ''; |
|
122
|
|
|
} |
|
123
|
2 |
|
$url .= $this->host; |
|
124
|
|
|
|
|
125
|
2 |
|
if ($this->port) { |
|
126
|
1 |
|
$url .= ":$this->port"; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
$url .= $this->path; |
|
130
|
|
|
|
|
131
|
2 |
|
if ($this->query) { |
|
|
|
|
|
|
132
|
1 |
|
$url .= '?' . http_build_query($this->query); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
return $url; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* special domain special flag |
|
140
|
|
|
* @param array $map |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function setSpecial(array $map) |
|
143
|
|
|
{ |
|
144
|
1 |
|
foreach ($map as $key => $domains) { |
|
145
|
1 |
|
if (in_array($this->origin, $domains)) { |
|
146
|
1 |
|
$this->special = $key; |
|
147
|
1 |
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
public static function genUA() |
|
153
|
|
|
{ |
|
154
|
1 |
|
static $ua; |
|
155
|
1 |
|
if ($ua) { |
|
156
|
1 |
|
return $ua; |
|
157
|
|
|
} |
|
158
|
1 |
|
$phpVersion = defined('HHVM_VERSION') ? 'HHVM ' . HHVM_VERSION : 'PHP ' . PHP_VERSION; |
|
159
|
|
|
|
|
160
|
1 |
|
return $ua = sprintf( |
|
161
|
1 |
|
'Composer/%s (%s; %s; %s)', |
|
162
|
1 |
|
str_replace('@package_version@', 'source', Composer::VERSION), |
|
163
|
1 |
|
php_uname('s'), |
|
164
|
1 |
|
php_uname('r'), |
|
165
|
|
|
$phpVersion |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.