Repository navigation
Expand file tree
/
Copy pathCurlHttpClient.php
More file actions
127 lines (96 loc) · 3.12 KB
/
Copy pathCurlHttpClient.php
File metadata and controls
127 lines (96 loc) · 3.12 KB
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
<?php
namespace Mpdf\Http;
use Mpdf\Log\Context as LogContext;
use Mpdf\Mpdf;
use Mpdf\PsrHttpMessageShim\Response;
use Mpdf\PsrHttpMessageShim\Stream;
use Mpdf\PsrLogAwareTrait\PsrLogAwareTrait;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
class CurlHttpClient implements \Mpdf\Http\ClientInterface, \Psr\Log\LoggerAwareInterface
{
use PsrLogAwareTrait;
private $mpdf;
public function __construct(Mpdf $mpdf, LoggerInterface $logger)
{
$this->mpdf = $mpdf;
$this->logger = $logger;
}
public function sendRequest(RequestInterface $request)
{
if (null === $request->getUri()) {
return (new Response());
}
$url = $request->getUri();
$this->logger->debug(sprintf('Fetching (cURL) content of remote URL "%s"', $url), ['context' => LogContext::REMOTE_CONTENT]);
$response = new Response();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->mpdf->curlUserAgent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->mpdf->curlTimeout);
if ($this->mpdf->curlExecutionTimeout) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->mpdf->curlExecutionTimeout);
}
if ($this->mpdf->curlFollowLocation) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
if ($this->mpdf->curlAllowUnsafeSslRequests) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
if ($this->mpdf->curlCaCertificate && is_file($this->mpdf->curlCaCertificate)) {
curl_setopt($ch, CURLOPT_CAINFO, $this->mpdf->curlCaCertificate);
}
if ($this->mpdf->curlProxy) {
curl_setopt($ch, CURLOPT_PROXY, $this->mpdf->curlProxy);
if ($this->mpdf->curlProxyAuth) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->mpdf->curlProxyAuth);
}
}
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
static function ($curl, $header) use (&$response) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) { // ignore invalid headers
return $len;
}
$response = $response->withHeader(trim($header[0]), trim($header[1]));
return $len;
}
);
$data = curl_exec($ch);
if (curl_error($ch)) {
$message = sprintf('cURL error: "%s"', curl_error($ch));
$this->logger->error($message, ['context' => LogContext::REMOTE_CONTENT]);
if ($this->mpdf->debug) {
throw new \Mpdf\MpdfException($message);
}
$this->closeCurl($ch);
return $response;
}
$info = curl_getinfo($ch);
if (isset($info['http_code']) && !str_starts_with((string) $info['http_code'], '2')) {
$message = sprintf('HTTP error: %d', $info['http_code']);
$this->logger->error($message, ['context' => LogContext::REMOTE_CONTENT]);
if ($this->mpdf->debug) {
throw new \Mpdf\MpdfException($message);
}
$this->closeCurl($ch);
return $response->withStatus($info['http_code']);
}
$this->closeCurl($ch);
return $response
->withStatus($info['http_code'])
->withBody(Stream::create($data));
}
private function closeCurl($ch)
{
if (PHP_VERSION_ID < 80000) {
curl_close($ch);
}
}
}