Skip to content

Commit e1c847d

Browse files
committed
🚿
1 parent ce7ead3 commit e1c847d

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

src/CurlClient.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,35 @@
2121
*/
2222
class CurlClient extends HTTPClientAbstract{
2323

24-
protected CurlHandle $handle;
25-
2624
/**
2725
* @inheritDoc
2826
*/
2927
public function sendRequest(RequestInterface $request):ResponseInterface{
30-
$stream = $this->streamFactory?->createStream();
31-
$this->handle = new CurlHandle($request, $this->responseFactory->createResponse(), $this->options, $stream);
32-
$errno = $this->handle->exec();
28+
29+
$handle = new CurlHandle(
30+
$request,
31+
$this->responseFactory->createResponse(),
32+
$this->options,
33+
$this->streamFactory?->createStream(),
34+
);
35+
36+
$errno = $handle->exec();
3337

3438
if($errno !== CURLE_OK){
35-
$error = $this->handle->getError();
39+
$error = $handle->getError();
3640

3741
$this->logger->error(sprintf('cURL error #%s: %s', $errno, $error));
3842

39-
if(in_array($errno, $this->handle::CURL_NETWORK_ERRORS, true)){
43+
if(in_array($errno, $handle::CURL_NETWORK_ERRORS, true)){
4044
throw new NetworkException($error, $request);
4145
}
4246

4347
throw new RequestException($error, $request);
4448
}
4549

46-
$this->handle->close();
50+
$handle->close();
4751

48-
return $this->handle->getResponse();
52+
return $handle->getResponse();
4953
}
5054

5155
}

src/StreamClient.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,52 @@
1414

1515
use chillerlan\HTTP\Utils\HeaderUtil;
1616
use Psr\Http\Message\{RequestInterface, ResponseInterface};
17-
use function explode, file_get_contents, get_headers, in_array, intval, restore_error_handler,
18-
set_error_handler, stream_context_create, strtolower, str_starts_with, trim;
17+
use Exception, Throwable;
18+
use function explode, file_get_contents, get_headers, in_array, intval, is_file, restore_error_handler,
19+
set_error_handler, sprintf, stream_context_create, strtolower, str_starts_with, trim;
1920

2021
/**
22+
* A http client via PHP streams
2123
*
24+
* (I'm not exactly sure why I'm keeping this - use CurlClient in production)
25+
*
26+
* @see \file_get_contents()
27+
* @see \stream_context_create()
2228
*/
2329
class StreamClient extends HTTPClientAbstract{
2430

2531
/**
2632
* @inheritDoc
33+
* @throws \Exception|\chillerlan\HTTP\ClientException
2734
*/
2835
public function sendRequest(RequestInterface $request):ResponseInterface{
2936

3037
$errorHandler = function(int $errno, string $errstr):bool{
31-
$this->logger->error('StreamClient error #'.$errno.': '.$errstr);
38+
$this->logger->error(sprintf('StreamClient error #%s: %s', $errno, $errstr));
3239

33-
throw new ClientException($errstr, $errno);
40+
throw new Exception($errstr, $errno);
3441
};
3542

3643
set_error_handler($errorHandler);
3744

38-
$context = stream_context_create($this->getContextOptions($request));
39-
$requestUri = (string)$request->getUri()->withFragment('');
40-
$responseBody = file_get_contents($requestUri, false, $context);
41-
$response = $this->createResponse(get_headers($requestUri, true, $context));
45+
$exception = null;
46+
47+
try{
48+
$context = stream_context_create($this->getContextOptions($request));
49+
$requestUri = (string)$request->getUri()->withFragment('');
50+
$responseBody = file_get_contents($requestUri, false, $context);
51+
$response = $this->createResponse(get_headers($requestUri, true, $context));
52+
}
53+
catch(Throwable $e){
54+
$exception = $e;
55+
}
4256

4357
restore_error_handler();
4458

59+
if($exception !== null){
60+
throw new ClientException($exception->getMessage());
61+
}
62+
4563
$body = $this->streamFactory !== null
4664
? $this->streamFactory->createStream()
4765
: $response->getBody()
@@ -58,9 +76,11 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
5876
*/
5977
protected function getContextOptions(RequestInterface $request):array{
6078
$method = $request->getMethod();
61-
$body = in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true)
62-
? $request->getBody()->getContents()
63-
: null;
79+
$body = null;
80+
81+
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true)){
82+
$body = $request->getBody()->getContents();
83+
}
6484

6585
$options = [
6686
'http' => [
@@ -81,8 +101,10 @@ protected function getContextOptions(RequestInterface $request):array{
81101
],
82102
];
83103

84-
$ca = ($this->options->ca_info_is_path) ? 'capath' : 'cafile';
85-
$options['ssl'][$ca] = $this->options->ca_info;
104+
if($this->options->ca_info){
105+
$ca = (is_file($this->options->ca_info)) ? 'capath' : 'cafile';
106+
$options['ssl'][$ca] = $this->options->ca_info;
107+
}
86108

87109
return $options;
88110
}

0 commit comments

Comments
 (0)