/
Curl.php
165 lines (149 loc) · 5.24 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.7.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http\Client\Adapter;
use Cake\Http\Client\AdapterInterface;
use Cake\Http\Client\Request;
use Cake\Http\Client\Response;
use Cake\Http\Exception\HttpException;
/**
* Implements sending Cake\Http\Client\Request via ext/curl.
*
* In addition to the standard options documented in Cake\Http\Client,
* this adapter supports all available curl options. Additional curl options
* can be set via the `curl` option key when making requests or configuring
* a client.
*/
class Curl implements AdapterInterface
{
/**
* {@inheritDoc}
*/
public function send(Request $request, array $options)
{
$ch = curl_init();
$options = $this->buildOptions($request, $options);
curl_setopt_array($ch, $options);
$body = $this->exec($ch);
if ($body === false) {
$errorCode = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
$status = 500;
if ($error === 28) {
$status = 504;
}
throw new HttpException("cURL Error ({$errorCode}) {$error}", $status);
}
$responses = $this->createResponse($ch, $body);
curl_close($ch);
return $responses;
}
/**
* Convert client options into curl options.
*
* @param \Cake\Http\Client\Request $request The request.
* @param array $options The client options
* @return array
*/
public function buildOptions(Request $request, array $options)
{
$headers = [];
foreach ($request->getHeaders() as $key => $values) {
$headers[] = $key . ': ' . implode(', ', $values);
}
$out = [
CURLOPT_URL => (string)$request->getUri(),
CURLOPT_HTTP_VERSION => $request->getProtocolVersion(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $headers
];
switch ($request->getMethod()) {
case Request::METHOD_GET:
$out[CURLOPT_HTTPGET] = true;
break;
case Request::METHOD_POST:
$out[CURLOPT_POST] = true;
break;
default:
$out[CURLOPT_POST] = true;
$out[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
break;
}
$body = $request->getBody();
if ($body) {
$body->rewind();
$out[CURLOPT_POSTFIELDS] = $body->getContents();
}
if (empty($options['ssl_cafile'])) {
$options['ssl_cafile'] = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem';
}
if (!empty($options['ssl_verify_host'])) {
// Value of 1 or true is deprecated. Only 2 or 0 should be used now.
$options['ssl_verify_host'] = 2;
}
$optionMap = [
'timeout' => CURLOPT_TIMEOUT,
'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER,
'ssl_verify_host' => CURLOPT_SSL_VERIFYHOST,
'ssl_cafile' => CURLOPT_CAINFO,
'ssl_local_cert' => CURLOPT_SSLCERT,
'ssl_passphrase' => CURLOPT_SSLCERTPASSWD,
];
foreach ($optionMap as $option => $curlOpt) {
if (isset($options[$option])) {
$out[$curlOpt] = $options[$option];
}
}
if (isset($options['proxy']['proxy'])) {
$out[CURLOPT_PROXY] = $options['proxy']['proxy'];
}
if (isset($options['proxy']['username'])) {
$password = !empty($options['proxy']['password']) ? $options['proxy']['password'] : '';
$out[CURLOPT_PROXYUSERPWD] = $options['proxy']['username'] . ':' . $password;
}
if (isset($options['curl']) && is_array($options['curl'])) {
// Can't use array_merge() because keys will be re-ordered.
foreach ($options['curl'] as $key => $value) {
$out[$key] = $value;
}
}
return $out;
}
/**
* Convert the raw curl response into an Http\Client\Response
*
* @param resource $handle Curl handle
* @param string $responseData string The response data from curl_exec
* @return \Cake\Http\Client\Response
*/
protected function createResponse($handle, $responseData)
{
$meta = curl_getinfo($handle);
$headers = trim(substr($responseData, 0, $meta['header_size']));
$body = substr($responseData, $meta['header_size']);
$response = new Response(explode("\r\n", $headers), $body);
return [$response];
}
/**
* Execute the curl handle.
*
* @param resource $ch Curl Resource handle
* @return string
*/
protected function exec($ch)
{
return curl_exec($ch);
}
}