@@ -8,169 +8,169 @@ |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | namespace XoopsModules\Wggithub\Github\ { |
11 | - /** |
|
12 | - * Marker interface. |
|
13 | - */ |
|
14 | - interface IException |
|
15 | - {} |
|
11 | + /** |
|
12 | + * Marker interface. |
|
13 | + */ |
|
14 | + interface IException |
|
15 | + {} |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Wrong algorithm. API is used in wrong way. Application code should be changed. |
|
20 | - */ |
|
21 | - class LogicException extends \LogicException implements IException |
|
22 | - {} |
|
18 | + /** |
|
19 | + * Wrong algorithm. API is used in wrong way. Application code should be changed. |
|
20 | + */ |
|
21 | + class LogicException extends \LogicException implements IException |
|
22 | + {} |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Substitution is used in URL path but corresponding parameter is missing. |
|
27 | - */ |
|
28 | - class MissingParameterException extends LogicException |
|
29 | - {} |
|
25 | + /** |
|
26 | + * Substitution is used in URL path but corresponding parameter is missing. |
|
27 | + */ |
|
28 | + class MissingParameterException extends LogicException |
|
29 | + {} |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * Unpredictable situation occurred. |
|
34 | - */ |
|
35 | - abstract class RuntimeException extends \RuntimeException implements IException |
|
36 | - {} |
|
32 | + /** |
|
33 | + * Unpredictable situation occurred. |
|
34 | + */ |
|
35 | + abstract class RuntimeException extends \RuntimeException implements IException |
|
36 | + {} |
|
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * Github API returned a non-success HTTP code or data are somehow wrong. See all following descendants. |
|
41 | - * |
|
42 | - * @see Api::decode() |
|
43 | - * @see https://developer.github.com/v3/#client-errors |
|
44 | - */ |
|
45 | - abstract class ApiException extends RuntimeException |
|
46 | - { |
|
47 | - /** @var Http\Response|NULL */ |
|
48 | - private $response; |
|
39 | + /** |
|
40 | + * Github API returned a non-success HTTP code or data are somehow wrong. See all following descendants. |
|
41 | + * |
|
42 | + * @see Api::decode() |
|
43 | + * @see https://developer.github.com/v3/#client-errors |
|
44 | + */ |
|
45 | + abstract class ApiException extends RuntimeException |
|
46 | + { |
|
47 | + /** @var Http\Response|NULL */ |
|
48 | + private $response; |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @param string |
|
53 | - * @param int |
|
54 | - */ |
|
55 | - public function __construct($message = '', $code = 0, \Exception $previous = NULL, Http\Response $response = NULL) |
|
56 | - { |
|
57 | - parent::__construct($message, $code, $previous); |
|
58 | - $this->response = clone $response; |
|
59 | - } |
|
51 | + /** |
|
52 | + * @param string |
|
53 | + * @param int |
|
54 | + */ |
|
55 | + public function __construct($message = '', $code = 0, \Exception $previous = NULL, Http\Response $response = NULL) |
|
56 | + { |
|
57 | + parent::__construct($message, $code, $previous); |
|
58 | + $this->response = clone $response; |
|
59 | + } |
|
60 | 60 | |
61 | 61 | |
62 | - /** |
|
63 | - * @return Http\Response|NULL |
|
64 | - */ |
|
65 | - final public function getResponse() |
|
66 | - { |
|
67 | - return $this->response; |
|
68 | - } |
|
62 | + /** |
|
63 | + * @return Http\Response|NULL |
|
64 | + */ |
|
65 | + final public function getResponse() |
|
66 | + { |
|
67 | + return $this->response; |
|
68 | + } |
|
69 | 69 | |
70 | - } |
|
70 | + } |
|
71 | 71 | |
72 | 72 | |
73 | - /** |
|
74 | - * Invalid credentials (e.g. revoked token). |
|
75 | - */ |
|
76 | - class UnauthorizedException extends ApiException |
|
77 | - {} |
|
73 | + /** |
|
74 | + * Invalid credentials (e.g. revoked token). |
|
75 | + */ |
|
76 | + class UnauthorizedException extends ApiException |
|
77 | + {} |
|
78 | 78 | |
79 | 79 | |
80 | - /** |
|
81 | - * Invalid JSON sent to Github API. |
|
82 | - */ |
|
83 | - class BadRequestException extends ApiException |
|
84 | - {} |
|
80 | + /** |
|
81 | + * Invalid JSON sent to Github API. |
|
82 | + */ |
|
83 | + class BadRequestException extends ApiException |
|
84 | + {} |
|
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * Invalid structure sent to Github API (e.g. some required fields are missing). |
|
89 | - */ |
|
90 | - class UnprocessableEntityException extends ApiException |
|
91 | - {} |
|
87 | + /** |
|
88 | + * Invalid structure sent to Github API (e.g. some required fields are missing). |
|
89 | + */ |
|
90 | + class UnprocessableEntityException extends ApiException |
|
91 | + {} |
|
92 | 92 | |
93 | 93 | |
94 | - /** |
|
95 | - * Access denied. |
|
96 | - * @see https://developer.github.com/v3/#authentication |
|
97 | - */ |
|
98 | - class ForbiddenException extends ApiException |
|
99 | - {} |
|
94 | + /** |
|
95 | + * Access denied. |
|
96 | + * @see https://developer.github.com/v3/#authentication |
|
97 | + */ |
|
98 | + class ForbiddenException extends ApiException |
|
99 | + {} |
|
100 | 100 | |
101 | 101 | |
102 | - /** |
|
103 | - * Rate limit exceed. |
|
104 | - * @see https://developer.github.com/v3/#rate-limiting |
|
105 | - */ |
|
106 | - class RateLimitExceedException extends ForbiddenException |
|
107 | - {} |
|
102 | + /** |
|
103 | + * Rate limit exceed. |
|
104 | + * @see https://developer.github.com/v3/#rate-limiting |
|
105 | + */ |
|
106 | + class RateLimitExceedException extends ForbiddenException |
|
107 | + {} |
|
108 | 108 | |
109 | 109 | |
110 | - /** |
|
111 | - * Resource not found. |
|
112 | - * @see https://developer.github.com/v3/#authentication |
|
113 | - */ |
|
114 | - class NotFoundException extends ApiException |
|
115 | - {} |
|
110 | + /** |
|
111 | + * Resource not found. |
|
112 | + * @see https://developer.github.com/v3/#authentication |
|
113 | + */ |
|
114 | + class NotFoundException extends ApiException |
|
115 | + {} |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * Response cannot be classified. |
|
120 | - */ |
|
121 | - class UnexpectedResponseException extends ApiException |
|
122 | - {} |
|
118 | + /** |
|
119 | + * Response cannot be classified. |
|
120 | + */ |
|
121 | + class UnexpectedResponseException extends ApiException |
|
122 | + {} |
|
123 | 123 | |
124 | 124 | |
125 | - /** |
|
126 | - * Response from Github is somehow wrong (e.g. invalid JSON decoding). |
|
127 | - */ |
|
128 | - class InvalidResponseException extends ApiException |
|
129 | - {} |
|
125 | + /** |
|
126 | + * Response from Github is somehow wrong (e.g. invalid JSON decoding). |
|
127 | + */ |
|
128 | + class InvalidResponseException extends ApiException |
|
129 | + {} |
|
130 | 130 | |
131 | 131 | |
132 | - /** |
|
133 | - * JSON cannot be decoded, or value cannot be encoded to JSON. |
|
134 | - */ |
|
135 | - class JsonException extends RuntimeException |
|
136 | - { |
|
137 | - } |
|
132 | + /** |
|
133 | + * JSON cannot be decoded, or value cannot be encoded to JSON. |
|
134 | + */ |
|
135 | + class JsonException extends RuntimeException |
|
136 | + { |
|
137 | + } |
|
138 | 138 | |
139 | 139 | } |
140 | 140 | |
141 | 141 | |
142 | 142 | namespace XoopsModules\Wggithub\Github\Http { |
143 | - use XoopsModules\Wggithub\Github; |
|
143 | + use XoopsModules\Wggithub\Github; |
|
144 | 144 | |
145 | 145 | |
146 | - /** |
|
147 | - * HTTP response is somehow wrong and cannot be processed. |
|
148 | - */ |
|
149 | - class BadResponseException extends Github\RuntimeException |
|
150 | - {} |
|
146 | + /** |
|
147 | + * HTTP response is somehow wrong and cannot be processed. |
|
148 | + */ |
|
149 | + class BadResponseException extends Github\RuntimeException |
|
150 | + {} |
|
151 | 151 | |
152 | 152 | } |
153 | 153 | |
154 | 154 | |
155 | 155 | namespace XoopsModules\Wggithub\Github\OAuth { |
156 | - use XoopsModules\Wggithub\Github; |
|
156 | + use XoopsModules\Wggithub\Github; |
|
157 | 157 | |
158 | - /** |
|
159 | - * Something fails during the token obtaining. |
|
160 | - */ |
|
161 | - class LoginException extends Github\RuntimeException |
|
162 | - {} |
|
158 | + /** |
|
159 | + * Something fails during the token obtaining. |
|
160 | + */ |
|
161 | + class LoginException extends Github\RuntimeException |
|
162 | + {} |
|
163 | 163 | |
164 | 164 | } |
165 | 165 | |
166 | 166 | |
167 | 167 | namespace XoopsModules\Wggithub\Github\Storages { |
168 | - use XoopsModules\Wggithub\Github; |
|
168 | + use XoopsModules\Wggithub\Github; |
|
169 | 169 | |
170 | - /** |
|
171 | - * Directory is missing and/or cannot be created. |
|
172 | - */ |
|
173 | - class MissingDirectoryException extends Github\RuntimeException |
|
174 | - {} |
|
170 | + /** |
|
171 | + * Directory is missing and/or cannot be created. |
|
172 | + */ |
|
173 | + class MissingDirectoryException extends Github\RuntimeException |
|
174 | + {} |
|
175 | 175 | |
176 | 176 | } |
@@ -13,131 +13,131 @@ |
||
13 | 13 | */ |
14 | 14 | class CachedClient extends Github\Sanity implements IClient |
15 | 15 | { |
16 | - /** @var Storages\ICache|NULL */ |
|
17 | - private $cache; |
|
18 | - |
|
19 | - /** @var IClient */ |
|
20 | - private $client; |
|
21 | - |
|
22 | - /** @var bool */ |
|
23 | - private $forbidRecheck; |
|
24 | - |
|
25 | - /** @var callable|NULL */ |
|
26 | - private $onResponse; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * @param Storages\ICache |
|
31 | - * @param IClient |
|
32 | - * @param bool forbid checking Github for new data; more or less development purpose only |
|
33 | - */ |
|
34 | - public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE) |
|
35 | - { |
|
36 | - $this->cache = $cache; |
|
37 | - $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
38 | - $this->forbidRecheck = (bool) $forbidRecheck; |
|
39 | - } |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * @return IClient |
|
44 | - */ |
|
45 | - public function getInnerClient() |
|
46 | - { |
|
47 | - return $this->client; |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * @return Response |
|
53 | - * |
|
54 | - * @throws BadResponseException |
|
55 | - */ |
|
56 | - public function request(Request $request) |
|
57 | - { |
|
58 | - $request = clone $request; |
|
59 | - |
|
60 | - $cacheKey = implode('.', [ |
|
61 | - $request->getMethod(), |
|
62 | - $request->getUrl(), |
|
63 | - |
|
64 | - /** @todo This should depend on Vary: header */ |
|
65 | - $request->getHeader('Accept'), |
|
66 | - $request->getHeader('Accept-Encoding'), |
|
67 | - $request->getHeader('Authorization') |
|
68 | - ]); |
|
69 | - |
|
70 | - if ($cached = $this->cache->load($cacheKey)) { |
|
71 | - if ($this->forbidRecheck) { |
|
72 | - $cached = clone $cached; |
|
73 | - $this->onResponse && call_user_func($this->onResponse, $cached); |
|
74 | - return $cached; |
|
75 | - } |
|
76 | - |
|
77 | - /** @var $cached Response */ |
|
78 | - if ($cached->hasHeader('Last-Modified')) { |
|
79 | - $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified')); |
|
80 | - } |
|
81 | - if ($cached->hasHeader('ETag')) { |
|
82 | - $request->addHeader('If-None-Match', $cached->getHeader('ETag')); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - $response = $this->client->request($request); |
|
87 | - |
|
88 | - if ($this->isCacheable($response)) { |
|
89 | - $this->cache->save($cacheKey, clone $response); |
|
90 | - } |
|
91 | - |
|
92 | - if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) { |
|
93 | - $cached = clone $cached; |
|
94 | - |
|
95 | - /** @todo Should be responses somehow combined into one? */ |
|
96 | - $response = $cached->setPrevious($response); |
|
97 | - } |
|
98 | - |
|
99 | - $this->onResponse && call_user_func($this->onResponse, $response); |
|
100 | - |
|
101 | - return $response; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * @param callable|NULL function(Request $request) |
|
107 | - * @return self |
|
108 | - */ |
|
109 | - public function onRequest($callback) |
|
110 | - { |
|
111 | - $this->client->onRequest($callback); |
|
112 | - return $this; |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * @param callable|NULL function(Response $response) |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public function onResponse($callback) |
|
121 | - { |
|
122 | - $this->client->onResponse(NULL); |
|
123 | - $this->onResponse = $callback; |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * @return bool |
|
130 | - */ |
|
131 | - protected function isCacheable(Response $response) |
|
132 | - { |
|
133 | - /** @todo Do it properly. Vary:, Pragma:, TTL... */ |
|
134 | - if (!$response->isCode(200)) { |
|
135 | - return FALSE; |
|
136 | - } elseif (preg_match('#max-age=0|must-revalidate#i', $response->getHeader('Cache-Control', ''))) { |
|
137 | - return FALSE; |
|
138 | - } |
|
139 | - |
|
140 | - return $response->hasHeader('ETag') || $response->hasHeader('Last-Modified'); |
|
141 | - } |
|
16 | + /** @var Storages\ICache|NULL */ |
|
17 | + private $cache; |
|
18 | + |
|
19 | + /** @var IClient */ |
|
20 | + private $client; |
|
21 | + |
|
22 | + /** @var bool */ |
|
23 | + private $forbidRecheck; |
|
24 | + |
|
25 | + /** @var callable|NULL */ |
|
26 | + private $onResponse; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * @param Storages\ICache |
|
31 | + * @param IClient |
|
32 | + * @param bool forbid checking Github for new data; more or less development purpose only |
|
33 | + */ |
|
34 | + public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE) |
|
35 | + { |
|
36 | + $this->cache = $cache; |
|
37 | + $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
38 | + $this->forbidRecheck = (bool) $forbidRecheck; |
|
39 | + } |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * @return IClient |
|
44 | + */ |
|
45 | + public function getInnerClient() |
|
46 | + { |
|
47 | + return $this->client; |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * @return Response |
|
53 | + * |
|
54 | + * @throws BadResponseException |
|
55 | + */ |
|
56 | + public function request(Request $request) |
|
57 | + { |
|
58 | + $request = clone $request; |
|
59 | + |
|
60 | + $cacheKey = implode('.', [ |
|
61 | + $request->getMethod(), |
|
62 | + $request->getUrl(), |
|
63 | + |
|
64 | + /** @todo This should depend on Vary: header */ |
|
65 | + $request->getHeader('Accept'), |
|
66 | + $request->getHeader('Accept-Encoding'), |
|
67 | + $request->getHeader('Authorization') |
|
68 | + ]); |
|
69 | + |
|
70 | + if ($cached = $this->cache->load($cacheKey)) { |
|
71 | + if ($this->forbidRecheck) { |
|
72 | + $cached = clone $cached; |
|
73 | + $this->onResponse && call_user_func($this->onResponse, $cached); |
|
74 | + return $cached; |
|
75 | + } |
|
76 | + |
|
77 | + /** @var $cached Response */ |
|
78 | + if ($cached->hasHeader('Last-Modified')) { |
|
79 | + $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified')); |
|
80 | + } |
|
81 | + if ($cached->hasHeader('ETag')) { |
|
82 | + $request->addHeader('If-None-Match', $cached->getHeader('ETag')); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + $response = $this->client->request($request); |
|
87 | + |
|
88 | + if ($this->isCacheable($response)) { |
|
89 | + $this->cache->save($cacheKey, clone $response); |
|
90 | + } |
|
91 | + |
|
92 | + if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) { |
|
93 | + $cached = clone $cached; |
|
94 | + |
|
95 | + /** @todo Should be responses somehow combined into one? */ |
|
96 | + $response = $cached->setPrevious($response); |
|
97 | + } |
|
98 | + |
|
99 | + $this->onResponse && call_user_func($this->onResponse, $response); |
|
100 | + |
|
101 | + return $response; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * @param callable|NULL function(Request $request) |
|
107 | + * @return self |
|
108 | + */ |
|
109 | + public function onRequest($callback) |
|
110 | + { |
|
111 | + $this->client->onRequest($callback); |
|
112 | + return $this; |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * @param callable|NULL function(Response $response) |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public function onResponse($callback) |
|
121 | + { |
|
122 | + $this->client->onResponse(NULL); |
|
123 | + $this->onResponse = $callback; |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * @return bool |
|
130 | + */ |
|
131 | + protected function isCacheable(Response $response) |
|
132 | + { |
|
133 | + /** @todo Do it properly. Vary:, Pragma:, TTL... */ |
|
134 | + if (!$response->isCode(200)) { |
|
135 | + return FALSE; |
|
136 | + } elseif (preg_match('#max-age=0|must-revalidate#i', $response->getHeader('Cache-Control', ''))) { |
|
137 | + return FALSE; |
|
138 | + } |
|
139 | + |
|
140 | + return $response->hasHeader('ETag') || $response->hasHeader('Last-Modified'); |
|
141 | + } |
|
142 | 142 | |
143 | 143 | } |
@@ -10,108 +10,108 @@ |
||
10 | 10 | */ |
11 | 11 | class StreamClient extends AbstractClient |
12 | 12 | { |
13 | - /** @var array|NULL */ |
|
14 | - private $sslOptions; |
|
15 | - |
|
16 | - |
|
17 | - /** |
|
18 | - * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
19 | - */ |
|
20 | - public function __construct(array $sslOptions = NULL) |
|
21 | - { |
|
22 | - $this->sslOptions = $sslOptions; |
|
23 | - } |
|
24 | - |
|
25 | - |
|
26 | - protected function setupRequest(Request $request) |
|
27 | - { |
|
28 | - parent::setupRequest($request); |
|
29 | - $request->setHeader('Connection', 'close'); |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @return Response |
|
35 | - * |
|
36 | - * @throws BadResponseException |
|
37 | - */ |
|
38 | - protected function process(Request $request) |
|
39 | - { |
|
40 | - $headerStr = []; |
|
41 | - foreach ($request->getHeaders() as $name => $value) { |
|
42 | - foreach ((array) $value as $v) { |
|
43 | - $headerStr[] = "$name: $v"; |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - $options = [ |
|
48 | - 'http' => [ |
|
49 | - 'method' => $request->getMethod(), |
|
50 | - 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
51 | - 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
52 | - 'protocol_version' => 1.1, |
|
53 | - 'ignore_errors' => TRUE, |
|
54 | - ], |
|
55 | - 'ssl' => [ |
|
56 | - 'verify_peer' => TRUE, |
|
57 | - 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
58 | - 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
59 | - ], |
|
60 | - ]; |
|
61 | - |
|
62 | - if (($content = $request->getContent()) !== NULL) { |
|
63 | - $options['http']['content'] = $content; |
|
64 | - } |
|
65 | - |
|
66 | - if ($this->sslOptions) { |
|
67 | - $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
68 | - } |
|
69 | - |
|
70 | - list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
71 | - return new Response($code, $headers, $content); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @internal |
|
77 | - * @param string |
|
78 | - * @param array |
|
79 | - * @return array |
|
80 | - * |
|
81 | - * @throws BadResponseException |
|
82 | - */ |
|
83 | - protected function fileGetContents($url, array $contextOptions) |
|
84 | - { |
|
85 | - $context = stream_context_create($contextOptions); |
|
86 | - |
|
87 | - $e = NULL; |
|
88 | - set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
89 | - $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
90 | - }, E_WARNING); |
|
91 | - |
|
92 | - $content = file_get_contents($url, FALSE, $context); |
|
93 | - restore_error_handler(); |
|
94 | - |
|
95 | - if (!isset($http_response_header)) { |
|
96 | - throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
97 | - } |
|
98 | - |
|
99 | - if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
100 | - throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
101 | - } |
|
102 | - unset($http_response_header[0]); |
|
103 | - |
|
104 | - $headers = []; |
|
105 | - foreach ($http_response_header as $header) { |
|
106 | - if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
107 | - $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
108 | - } else { |
|
109 | - list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
110 | - $headers[$last = trim($name)] = trim($value); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return [$m[1], $headers, $content]; |
|
115 | - } |
|
13 | + /** @var array|NULL */ |
|
14 | + private $sslOptions; |
|
15 | + |
|
16 | + |
|
17 | + /** |
|
18 | + * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
19 | + */ |
|
20 | + public function __construct(array $sslOptions = NULL) |
|
21 | + { |
|
22 | + $this->sslOptions = $sslOptions; |
|
23 | + } |
|
24 | + |
|
25 | + |
|
26 | + protected function setupRequest(Request $request) |
|
27 | + { |
|
28 | + parent::setupRequest($request); |
|
29 | + $request->setHeader('Connection', 'close'); |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @return Response |
|
35 | + * |
|
36 | + * @throws BadResponseException |
|
37 | + */ |
|
38 | + protected function process(Request $request) |
|
39 | + { |
|
40 | + $headerStr = []; |
|
41 | + foreach ($request->getHeaders() as $name => $value) { |
|
42 | + foreach ((array) $value as $v) { |
|
43 | + $headerStr[] = "$name: $v"; |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + $options = [ |
|
48 | + 'http' => [ |
|
49 | + 'method' => $request->getMethod(), |
|
50 | + 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
51 | + 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
52 | + 'protocol_version' => 1.1, |
|
53 | + 'ignore_errors' => TRUE, |
|
54 | + ], |
|
55 | + 'ssl' => [ |
|
56 | + 'verify_peer' => TRUE, |
|
57 | + 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
58 | + 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
59 | + ], |
|
60 | + ]; |
|
61 | + |
|
62 | + if (($content = $request->getContent()) !== NULL) { |
|
63 | + $options['http']['content'] = $content; |
|
64 | + } |
|
65 | + |
|
66 | + if ($this->sslOptions) { |
|
67 | + $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
68 | + } |
|
69 | + |
|
70 | + list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
71 | + return new Response($code, $headers, $content); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @internal |
|
77 | + * @param string |
|
78 | + * @param array |
|
79 | + * @return array |
|
80 | + * |
|
81 | + * @throws BadResponseException |
|
82 | + */ |
|
83 | + protected function fileGetContents($url, array $contextOptions) |
|
84 | + { |
|
85 | + $context = stream_context_create($contextOptions); |
|
86 | + |
|
87 | + $e = NULL; |
|
88 | + set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
89 | + $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
90 | + }, E_WARNING); |
|
91 | + |
|
92 | + $content = file_get_contents($url, FALSE, $context); |
|
93 | + restore_error_handler(); |
|
94 | + |
|
95 | + if (!isset($http_response_header)) { |
|
96 | + throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
97 | + } |
|
98 | + |
|
99 | + if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
100 | + throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
101 | + } |
|
102 | + unset($http_response_header[0]); |
|
103 | + |
|
104 | + $headers = []; |
|
105 | + foreach ($http_response_header as $header) { |
|
106 | + if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
107 | + $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
108 | + } else { |
|
109 | + list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
110 | + $headers[$last = trim($name)] = trim($value); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return [$m[1], $headers, $content]; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | } |
@@ -47,15 +47,15 @@ discard block |
||
47 | 47 | $options = [ |
48 | 48 | 'http' => [ |
49 | 49 | 'method' => $request->getMethod(), |
50 | - 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
51 | - 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
50 | + 'header' => implode("\r\n", $headerStr)."\r\n", |
|
51 | + 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
52 | 52 | 'protocol_version' => 1.1, |
53 | 53 | 'ignore_errors' => TRUE, |
54 | 54 | ], |
55 | 55 | 'ssl' => [ |
56 | 56 | 'verify_peer' => TRUE, |
57 | - 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
58 | - 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
57 | + 'cafile' => realpath(__DIR__.'/../../ca-chain.crt'), |
|
58 | + 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
59 | 59 | ], |
60 | 60 | ]; |
61 | 61 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | $headers = []; |
105 | 105 | foreach ($http_response_header as $header) { |
106 | 106 | if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
107 | - $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
107 | + $headers[$last] .= ' '.trim($header); # RFC2616, 2.2 |
|
108 | 108 | } else { |
109 | 109 | list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
110 | 110 | $headers[$last = trim($name)] = trim($value); |
@@ -12,84 +12,84 @@ |
||
12 | 12 | */ |
13 | 13 | class Request extends Message |
14 | 14 | { |
15 | - /** HTTP request method */ |
|
16 | - const |
|
17 | - DELETE = 'DELETE', |
|
18 | - GET = 'GET', |
|
19 | - HEAD = 'HEAD', |
|
20 | - PATCH = 'PATCH', |
|
21 | - POST = 'POST', |
|
22 | - PUT = 'PUT'; |
|
23 | - |
|
24 | - |
|
25 | - /** @var string */ |
|
26 | - private $method; |
|
27 | - |
|
28 | - /** @var string */ |
|
29 | - private $url; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @param string |
|
34 | - * @param string |
|
35 | - * @param array |
|
36 | - * @param string|NULL |
|
37 | - */ |
|
38 | - public function __construct($method, $url, array $headers = [], $content = NULL) |
|
39 | - { |
|
40 | - $this->method = $method; |
|
41 | - $this->url = $url; |
|
42 | - parent::__construct($headers, $content); |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string |
|
48 | - * @return bool |
|
49 | - */ |
|
50 | - public function isMethod($method) |
|
51 | - { |
|
52 | - return strcasecmp($this->method, $method) === 0; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getMethod() |
|
60 | - { |
|
61 | - return $this->method; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function getUrl() |
|
69 | - { |
|
70 | - return $this->url; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param string |
|
76 | - * @param string |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public function addHeader($name, $value) |
|
80 | - { |
|
81 | - return parent::addHeader($name, $value); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string |
|
87 | - * @param string|NULL |
|
88 | - * @return self |
|
89 | - */ |
|
90 | - public function setHeader($name, $value) |
|
91 | - { |
|
92 | - return parent::setHeader($name, $value); |
|
93 | - } |
|
15 | + /** HTTP request method */ |
|
16 | + const |
|
17 | + DELETE = 'DELETE', |
|
18 | + GET = 'GET', |
|
19 | + HEAD = 'HEAD', |
|
20 | + PATCH = 'PATCH', |
|
21 | + POST = 'POST', |
|
22 | + PUT = 'PUT'; |
|
23 | + |
|
24 | + |
|
25 | + /** @var string */ |
|
26 | + private $method; |
|
27 | + |
|
28 | + /** @var string */ |
|
29 | + private $url; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @param string |
|
34 | + * @param string |
|
35 | + * @param array |
|
36 | + * @param string|NULL |
|
37 | + */ |
|
38 | + public function __construct($method, $url, array $headers = [], $content = NULL) |
|
39 | + { |
|
40 | + $this->method = $method; |
|
41 | + $this->url = $url; |
|
42 | + parent::__construct($headers, $content); |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string |
|
48 | + * @return bool |
|
49 | + */ |
|
50 | + public function isMethod($method) |
|
51 | + { |
|
52 | + return strcasecmp($this->method, $method) === 0; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getMethod() |
|
60 | + { |
|
61 | + return $this->method; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function getUrl() |
|
69 | + { |
|
70 | + return $this->url; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param string |
|
76 | + * @param string |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public function addHeader($name, $value) |
|
80 | + { |
|
81 | + return parent::addHeader($name, $value); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string |
|
87 | + * @param string|NULL |
|
88 | + * @return self |
|
89 | + */ |
|
90 | + public function setHeader($name, $value) |
|
91 | + { |
|
92 | + return parent::setHeader($name, $value); |
|
93 | + } |
|
94 | 94 | |
95 | 95 | } |
@@ -12,97 +12,97 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class Message extends Github\Sanity |
14 | 14 | { |
15 | - /** @var array[name => value] */ |
|
16 | - private $headers = []; |
|
17 | - |
|
18 | - /** @var string|NULL */ |
|
19 | - private $content; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array |
|
24 | - * @param string|NULL |
|
25 | - */ |
|
26 | - public function __construct(array $headers = [], $content = NULL) |
|
27 | - { |
|
28 | - $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | - $this->content = $content; |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @param string |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function hasHeader($name) |
|
38 | - { |
|
39 | - return array_key_exists(strtolower($name), $this->headers); |
|
40 | - } |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string |
|
45 | - * @param mixed |
|
46 | - * @return mixed |
|
47 | - */ |
|
48 | - public function getHeader($name, $default = NULL) |
|
49 | - { |
|
50 | - $name = strtolower($name); |
|
51 | - return array_key_exists($name, $this->headers) |
|
52 | - ? $this->headers[$name] |
|
53 | - : $default; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string |
|
59 | - * @param string |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - protected function addHeader($name, $value) |
|
63 | - { |
|
64 | - $name = strtolower($name); |
|
65 | - if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | - $this->headers[$name] = $value; |
|
67 | - } |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @param string |
|
75 | - * @param string|NULL |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - protected function setHeader($name, $value) |
|
79 | - { |
|
80 | - $name = strtolower($name); |
|
81 | - if ($value === NULL) { |
|
82 | - unset($this->headers[$name]); |
|
83 | - } else { |
|
84 | - $this->headers[$name] = $value; |
|
85 | - } |
|
86 | - |
|
87 | - return $this; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function getHeaders() |
|
95 | - { |
|
96 | - return $this->headers; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string|NULL |
|
102 | - */ |
|
103 | - public function getContent() |
|
104 | - { |
|
105 | - return $this->content; |
|
106 | - } |
|
15 | + /** @var array[name => value] */ |
|
16 | + private $headers = []; |
|
17 | + |
|
18 | + /** @var string|NULL */ |
|
19 | + private $content; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array |
|
24 | + * @param string|NULL |
|
25 | + */ |
|
26 | + public function __construct(array $headers = [], $content = NULL) |
|
27 | + { |
|
28 | + $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | + $this->content = $content; |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @param string |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function hasHeader($name) |
|
38 | + { |
|
39 | + return array_key_exists(strtolower($name), $this->headers); |
|
40 | + } |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string |
|
45 | + * @param mixed |
|
46 | + * @return mixed |
|
47 | + */ |
|
48 | + public function getHeader($name, $default = NULL) |
|
49 | + { |
|
50 | + $name = strtolower($name); |
|
51 | + return array_key_exists($name, $this->headers) |
|
52 | + ? $this->headers[$name] |
|
53 | + : $default; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string |
|
59 | + * @param string |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + protected function addHeader($name, $value) |
|
63 | + { |
|
64 | + $name = strtolower($name); |
|
65 | + if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | + $this->headers[$name] = $value; |
|
67 | + } |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @param string |
|
75 | + * @param string|NULL |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + protected function setHeader($name, $value) |
|
79 | + { |
|
80 | + $name = strtolower($name); |
|
81 | + if ($value === NULL) { |
|
82 | + unset($this->headers[$name]); |
|
83 | + } else { |
|
84 | + $this->headers[$name] = $value; |
|
85 | + } |
|
86 | + |
|
87 | + return $this; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function getHeaders() |
|
95 | + { |
|
96 | + return $this->headers; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string|NULL |
|
102 | + */ |
|
103 | + public function getContent() |
|
104 | + { |
|
105 | + return $this->content; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,110 +12,110 @@ |
||
12 | 12 | */ |
13 | 13 | class CurlClient extends AbstractClient |
14 | 14 | { |
15 | - /** @var array|NULL */ |
|
16 | - private $options; |
|
17 | - |
|
18 | - /** @var resource */ |
|
19 | - private $curl; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | - * |
|
25 | - * @throws Github\LogicException |
|
26 | - */ |
|
27 | - public function __construct(array $options = NULL) |
|
28 | - { |
|
29 | - if (!extension_loaded('curl')) { |
|
30 | - throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | - } |
|
32 | - |
|
33 | - $this->options = $options; |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - protected function setupRequest(Request $request) |
|
38 | - { |
|
39 | - parent::setupRequest($request); |
|
40 | - $request->addHeader('Connection', 'keep-alive'); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * @return Response |
|
46 | - * |
|
47 | - * @throws BadResponseException |
|
48 | - */ |
|
49 | - protected function process(Request $request) |
|
50 | - { |
|
51 | - $headers = []; |
|
52 | - foreach ($request->getHeaders() as $name => $value) { |
|
53 | - $headers[] = "$name: $value"; |
|
54 | - } |
|
55 | - |
|
56 | - $responseHeaders = []; |
|
57 | - $softOptions = [ |
|
58 | - CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | - CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | - CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | - CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | - ]; |
|
63 | - |
|
64 | - $hardOptions = [ |
|
65 | - CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | - CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | - CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | - CURLOPT_URL => $request->getUrl(), |
|
70 | - CURLOPT_HTTPHEADER => $headers, |
|
71 | - CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | - CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | - CURLOPT_HEADER => FALSE, |
|
74 | - CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | - if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | - /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | - # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | - $responseHeaders = []; |
|
79 | - |
|
80 | - } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | - $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | - |
|
83 | - } elseif ($line !== "\r\n") { |
|
84 | - list($name, $value) = explode(':', $line, 2); |
|
85 | - $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | - } |
|
87 | - |
|
88 | - return strlen($line); |
|
89 | - }, |
|
90 | - ]; |
|
91 | - |
|
92 | - if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | - $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | - } |
|
95 | - |
|
96 | - if (!$this->curl) { |
|
97 | - $this->curl = curl_init(); |
|
98 | - if ($this->curl === FALSE) { |
|
99 | - throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | - if ($result === FALSE) { |
|
105 | - throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | - } |
|
107 | - |
|
108 | - $content = curl_exec($this->curl); |
|
109 | - if ($content === FALSE) { |
|
110 | - throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | - } |
|
112 | - |
|
113 | - $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | - if ($code === FALSE) { |
|
115 | - throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | - } |
|
117 | - |
|
118 | - return new Response($code, $responseHeaders, $content); |
|
119 | - } |
|
15 | + /** @var array|NULL */ |
|
16 | + private $options; |
|
17 | + |
|
18 | + /** @var resource */ |
|
19 | + private $curl; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | + * |
|
25 | + * @throws Github\LogicException |
|
26 | + */ |
|
27 | + public function __construct(array $options = NULL) |
|
28 | + { |
|
29 | + if (!extension_loaded('curl')) { |
|
30 | + throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | + } |
|
32 | + |
|
33 | + $this->options = $options; |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + protected function setupRequest(Request $request) |
|
38 | + { |
|
39 | + parent::setupRequest($request); |
|
40 | + $request->addHeader('Connection', 'keep-alive'); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * @return Response |
|
46 | + * |
|
47 | + * @throws BadResponseException |
|
48 | + */ |
|
49 | + protected function process(Request $request) |
|
50 | + { |
|
51 | + $headers = []; |
|
52 | + foreach ($request->getHeaders() as $name => $value) { |
|
53 | + $headers[] = "$name: $value"; |
|
54 | + } |
|
55 | + |
|
56 | + $responseHeaders = []; |
|
57 | + $softOptions = [ |
|
58 | + CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | + CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | + CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | + CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | + ]; |
|
63 | + |
|
64 | + $hardOptions = [ |
|
65 | + CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | + CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | + CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | + CURLOPT_URL => $request->getUrl(), |
|
70 | + CURLOPT_HTTPHEADER => $headers, |
|
71 | + CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | + CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | + CURLOPT_HEADER => FALSE, |
|
74 | + CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | + if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | + /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | + # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | + $responseHeaders = []; |
|
79 | + |
|
80 | + } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | + $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | + |
|
83 | + } elseif ($line !== "\r\n") { |
|
84 | + list($name, $value) = explode(':', $line, 2); |
|
85 | + $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | + } |
|
87 | + |
|
88 | + return strlen($line); |
|
89 | + }, |
|
90 | + ]; |
|
91 | + |
|
92 | + if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | + $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | + } |
|
95 | + |
|
96 | + if (!$this->curl) { |
|
97 | + $this->curl = curl_init(); |
|
98 | + if ($this->curl === FALSE) { |
|
99 | + throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | + if ($result === FALSE) { |
|
105 | + throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | + } |
|
107 | + |
|
108 | + $content = curl_exec($this->curl); |
|
109 | + if ($content === FALSE) { |
|
110 | + throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | + } |
|
112 | + |
|
113 | + $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | + if ($code === FALSE) { |
|
115 | + throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | + } |
|
117 | + |
|
118 | + return new Response($code, $responseHeaders, $content); |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | CURLOPT_CONNECTTIMEOUT => 10, |
59 | 59 | CURLOPT_SSL_VERIFYHOST => 2, |
60 | 60 | CURLOPT_SSL_VERIFYPEER => 1, |
61 | - CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
61 | + CURLOPT_CAINFO => realpath(__DIR__.'/../ca-chain.crt'), |
|
62 | 62 | ]; |
63 | 63 | |
64 | 64 | $hardOptions = [ |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | $responseHeaders = []; |
79 | 79 | |
80 | 80 | } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
81 | - $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
81 | + $responseHeaders[$last] .= ' '.trim($line); # RFC2616, 2.2 |
|
82 | 82 | |
83 | 83 | } elseif ($line !== "\r\n") { |
84 | 84 | list($name, $value) = explode(':', $line, 2); |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | |
103 | 103 | $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
104 | 104 | if ($result === FALSE) { |
105 | - throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
105 | + throw new BadResponseException('Setting cURL options failed: '.curl_error($this->curl), curl_errno($this->curl)); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | $content = curl_exec($this->curl); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | |
113 | 113 | $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
114 | 114 | if ($code === FALSE) { |
115 | - throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
115 | + throw new BadResponseException('HTTP status code is missing:'.curl_error($this->curl), curl_errno($this->curl)); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | return new Response($code, $responseHeaders, $content); |
@@ -12,80 +12,80 @@ |
||
12 | 12 | */ |
13 | 13 | class Response extends Message |
14 | 14 | { |
15 | - /** HTTP 1.1 code */ |
|
16 | - const |
|
17 | - S200_OK = 200, |
|
18 | - S301_MOVED_PERMANENTLY = 301, |
|
19 | - S302_FOUND = 302, |
|
20 | - S304_NOT_MODIFIED = 304, |
|
21 | - S307_TEMPORARY_REDIRECT = 307, |
|
22 | - S400_BAD_REQUEST = 400, |
|
23 | - S401_UNAUTHORIZED = 401, |
|
24 | - S403_FORBIDDEN = 403, |
|
25 | - S404_NOT_FOUND = 404, |
|
26 | - S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | - |
|
28 | - /** @var int */ |
|
29 | - private $code; |
|
30 | - |
|
31 | - /** @var Response */ |
|
32 | - private $previous; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * @param int |
|
37 | - * @param array |
|
38 | - * @param string |
|
39 | - */ |
|
40 | - public function __construct($code, array $headers, $content) |
|
41 | - { |
|
42 | - $this->code = (int) $code; |
|
43 | - parent::__construct($headers, $content); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * HTTP code. |
|
49 | - * @return int |
|
50 | - */ |
|
51 | - public function getCode() |
|
52 | - { |
|
53 | - return $this->code; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param int |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function isCode($code) |
|
62 | - { |
|
63 | - return $this->code === (int) $code; |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @return Response|NULL |
|
69 | - */ |
|
70 | - public function getPrevious() |
|
71 | - { |
|
72 | - return $this->previous; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * @return self |
|
78 | - * |
|
79 | - * @throws Github\LogicException |
|
80 | - */ |
|
81 | - public function setPrevious(Response $previous = NULL) |
|
82 | - { |
|
83 | - if ($this->previous) { |
|
84 | - throw new Github\LogicException('Previous response is already set.'); |
|
85 | - } |
|
86 | - $this->previous = $previous; |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
15 | + /** HTTP 1.1 code */ |
|
16 | + const |
|
17 | + S200_OK = 200, |
|
18 | + S301_MOVED_PERMANENTLY = 301, |
|
19 | + S302_FOUND = 302, |
|
20 | + S304_NOT_MODIFIED = 304, |
|
21 | + S307_TEMPORARY_REDIRECT = 307, |
|
22 | + S400_BAD_REQUEST = 400, |
|
23 | + S401_UNAUTHORIZED = 401, |
|
24 | + S403_FORBIDDEN = 403, |
|
25 | + S404_NOT_FOUND = 404, |
|
26 | + S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | + |
|
28 | + /** @var int */ |
|
29 | + private $code; |
|
30 | + |
|
31 | + /** @var Response */ |
|
32 | + private $previous; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * @param int |
|
37 | + * @param array |
|
38 | + * @param string |
|
39 | + */ |
|
40 | + public function __construct($code, array $headers, $content) |
|
41 | + { |
|
42 | + $this->code = (int) $code; |
|
43 | + parent::__construct($headers, $content); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * HTTP code. |
|
49 | + * @return int |
|
50 | + */ |
|
51 | + public function getCode() |
|
52 | + { |
|
53 | + return $this->code; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param int |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function isCode($code) |
|
62 | + { |
|
63 | + return $this->code === (int) $code; |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @return Response|NULL |
|
69 | + */ |
|
70 | + public function getPrevious() |
|
71 | + { |
|
72 | + return $this->previous; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * @return self |
|
78 | + * |
|
79 | + * @throws Github\LogicException |
|
80 | + */ |
|
81 | + public function setPrevious(Response $previous = NULL) |
|
82 | + { |
|
83 | + if ($this->previous) { |
|
84 | + throw new Github\LogicException('Previous response is already set.'); |
|
85 | + } |
|
86 | + $this->previous = $previous; |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | 90 | |
91 | 91 | } |
@@ -12,98 +12,98 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class AbstractClient extends Github\Sanity implements IClient |
14 | 14 | { |
15 | - /** @var int[] will follow Location header on these response codes */ |
|
16 | - public $redirectCodes = [ |
|
17 | - Response::S301_MOVED_PERMANENTLY, |
|
18 | - Response::S302_FOUND, |
|
19 | - Response::S307_TEMPORARY_REDIRECT, |
|
20 | - ]; |
|
21 | - |
|
22 | - /** @var int maximum redirects per request*/ |
|
23 | - public $maxRedirects = 5; |
|
24 | - |
|
25 | - /** @var callable|NULL */ |
|
26 | - private $onRequest; |
|
27 | - |
|
28 | - /** @var callable|NULL */ |
|
29 | - private $onResponse; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @see https://developer.github.com/v3/#http-redirects |
|
34 | - * |
|
35 | - * @return Response |
|
36 | - * |
|
37 | - * @throws BadResponseException |
|
38 | - */ |
|
39 | - public function request(Request $request) |
|
40 | - { |
|
41 | - $request = clone $request; |
|
42 | - |
|
43 | - $counter = $this->maxRedirects; |
|
44 | - $previous = NULL; |
|
45 | - do { |
|
46 | - $this->setupRequest($request); |
|
47 | - |
|
48 | - $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | - $response = $this->process($request); |
|
50 | - $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | - |
|
52 | - $previous = $response->setPrevious($previous); |
|
53 | - |
|
54 | - if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | - /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | - $request = new Request( |
|
57 | - $request->getMethod(), |
|
58 | - $response->getHeader('Location'), |
|
59 | - $request->getHeaders(), |
|
60 | - $request->getContent() |
|
61 | - ); |
|
62 | - |
|
63 | - $counter--; |
|
64 | - continue; |
|
65 | - } |
|
66 | - break; |
|
67 | - |
|
68 | - } while (TRUE); |
|
69 | - |
|
70 | - return $response; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param callable|NULL function(Request $request) |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function onRequest($callback) |
|
79 | - { |
|
80 | - $this->onRequest = $callback; |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @param callable|NULL function(Response $response) |
|
87 | - * @return self |
|
88 | - */ |
|
89 | - public function onResponse($callback) |
|
90 | - { |
|
91 | - $this->onResponse = $callback; |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - protected function setupRequest(Request $request) |
|
97 | - { |
|
98 | - $request->addHeader('Expect', ''); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @return Response |
|
104 | - * |
|
105 | - * @throws BadResponseException |
|
106 | - */ |
|
107 | - abstract protected function process(Request $request); |
|
15 | + /** @var int[] will follow Location header on these response codes */ |
|
16 | + public $redirectCodes = [ |
|
17 | + Response::S301_MOVED_PERMANENTLY, |
|
18 | + Response::S302_FOUND, |
|
19 | + Response::S307_TEMPORARY_REDIRECT, |
|
20 | + ]; |
|
21 | + |
|
22 | + /** @var int maximum redirects per request*/ |
|
23 | + public $maxRedirects = 5; |
|
24 | + |
|
25 | + /** @var callable|NULL */ |
|
26 | + private $onRequest; |
|
27 | + |
|
28 | + /** @var callable|NULL */ |
|
29 | + private $onResponse; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @see https://developer.github.com/v3/#http-redirects |
|
34 | + * |
|
35 | + * @return Response |
|
36 | + * |
|
37 | + * @throws BadResponseException |
|
38 | + */ |
|
39 | + public function request(Request $request) |
|
40 | + { |
|
41 | + $request = clone $request; |
|
42 | + |
|
43 | + $counter = $this->maxRedirects; |
|
44 | + $previous = NULL; |
|
45 | + do { |
|
46 | + $this->setupRequest($request); |
|
47 | + |
|
48 | + $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | + $response = $this->process($request); |
|
50 | + $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | + |
|
52 | + $previous = $response->setPrevious($previous); |
|
53 | + |
|
54 | + if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | + /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | + $request = new Request( |
|
57 | + $request->getMethod(), |
|
58 | + $response->getHeader('Location'), |
|
59 | + $request->getHeaders(), |
|
60 | + $request->getContent() |
|
61 | + ); |
|
62 | + |
|
63 | + $counter--; |
|
64 | + continue; |
|
65 | + } |
|
66 | + break; |
|
67 | + |
|
68 | + } while (TRUE); |
|
69 | + |
|
70 | + return $response; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param callable|NULL function(Request $request) |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function onRequest($callback) |
|
79 | + { |
|
80 | + $this->onRequest = $callback; |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @param callable|NULL function(Response $response) |
|
87 | + * @return self |
|
88 | + */ |
|
89 | + public function onResponse($callback) |
|
90 | + { |
|
91 | + $this->onResponse = $callback; |
|
92 | + return $this; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + protected function setupRequest(Request $request) |
|
97 | + { |
|
98 | + $request->addHeader('Expect', ''); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @return Response |
|
104 | + * |
|
105 | + * @throws BadResponseException |
|
106 | + */ |
|
107 | + abstract protected function process(Request $request); |
|
108 | 108 | |
109 | 109 | } |
@@ -10,21 +10,21 @@ |
||
10 | 10 | */ |
11 | 11 | interface IClient |
12 | 12 | { |
13 | - /** |
|
14 | - * @return Response |
|
15 | - */ |
|
16 | - function request(Request $request); |
|
13 | + /** |
|
14 | + * @return Response |
|
15 | + */ |
|
16 | + function request(Request $request); |
|
17 | 17 | |
18 | - /** |
|
19 | - * @param callable|NULL |
|
20 | - * @return self |
|
21 | - */ |
|
22 | - function onRequest($callback); |
|
18 | + /** |
|
19 | + * @param callable|NULL |
|
20 | + * @return self |
|
21 | + */ |
|
22 | + function onRequest($callback); |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param callable|NULL |
|
26 | - * @return self |
|
27 | - */ |
|
28 | - function onResponse($callback); |
|
24 | + /** |
|
25 | + * @param callable|NULL |
|
26 | + * @return self |
|
27 | + */ |
|
28 | + function onResponse($callback); |
|
29 | 29 | |
30 | 30 | } |