forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.php
137 lines (124 loc) · 4.09 KB
/
curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
class Curl
{
/** @var CurlRequest[] */
private $requests = array();
private $running = false;
private $debug = false;
private static $multiHandle;
public function add(CurlRequest $request)
{
$this->requests[$request->url] = $request;
if ($this->running) {
$this->addHandle($request);
}
}
public function execute()
{
$this->running = true;
if (!is_resource(self::$multiHandle)) {
self::$multiHandle = curl_multi_init();
}
foreach ($this->requests as $request) {
$this->addHandle($request);
}
$finish = false;
$running = true;
do {
$finish = !$running;
while (CURLM_CALL_MULTI_PERFORM == $execrun = curl_multi_exec(self::$multiHandle, $running));
if ($execrun != CURLM_OK) {
break;
}
while ($done = curl_multi_info_read(self::$multiHandle)) {
$ch = $done['handle'];
$info = curl_getinfo($ch);
$url = self::getHeader($info['request_header'], 'X-Url');
$request = $this->requests[$url];
$rawResponse = curl_multi_getcontent($ch);
if (preg_match("@^HTTP/\\d\\.\\d 200 Connection established\r\n\r\n@i", $rawResponse)) {
list(, $header, $body) = explode("\r\n\r\n", $rawResponse, 3);
} else {
list($header, $body) = explode("\r\n\r\n", $rawResponse, 2);
}
$response = new CurlResponse();
$response->request = $request;
$response->status = $info['http_code'];
$headerNames = array(
'etag' => 'ETag',
'contentType' => 'Content-Type',
'link' => 'Link',
);
foreach ($headerNames as $key => $name) {
$response->$key = Curl::getHeader($header, $name);
}
if (200 == $response->status) {
$response->content = $body;
}
$callback = $request->callback;
$callback($response);
curl_close($ch);
curl_multi_remove_handle(self::$multiHandle, $ch);
}
} while ($running || !$finish);
$this->running = false;
return true;
}
private function addHandle(CurlRequest $request)
{
$defaultOptions = array(
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'alfred-github-workflow',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLINFO_HEADER_OUT => true,
);
if ($this->debug) {
$defaultOptions[CURLOPT_PROXY] = 'localhost';
$defaultOptions[CURLOPT_PROXYPORT] = 8888;
$defaultOptions[CURLOPT_SSL_VERIFYPEER] = 0;
}
$ch = curl_init();
$options = $defaultOptions;
$options[CURLOPT_URL] = $request->url;
$header = array();
$header[] = 'X-Url: ' . $request->url;
$header[] = 'Authorization: token ' . Workflow::getAccessToken();
if ($request->etag) {
$header[] = 'If-None-Match: ' . $request->etag;
}
$options[CURLOPT_HTTPHEADER] = $header;
curl_setopt_array($ch, $options);
curl_multi_add_handle(self::$multiHandle, $ch);
}
public static function getHeader($header, $key)
{
if (preg_match('/^' . preg_quote($key, '/') . ': (\V*)/mi', $header, $match)) {
return $match[1];
}
return null;
}
}
class CurlRequest
{
public $url;
public $etag;
public $callback;
public function __construct($url, $etag, $callback)
{
$this->url = $url;
$this->etag = $etag;
$this->callback = $callback;
}
}
class CurlResponse
{
/** @var CurlRequest */
public $request;
public $status;
public $contentType;
public $etag;
public $link;
public $content;
}