Skip to content

Commit 3267c60

Browse files
committed
:octocat: fix for #1
1 parent f7f137f commit 3267c60

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/CurlHandle.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public function __construct(RequestInterface $request, ResponseInterface $respon
5252
$this->options = $options;
5353
$this->curl = curl_init();
5454

55-
if(is_array($this->options->curl_options)){
56-
curl_setopt_array($this->curl, $this->options->curl_options);
57-
}
55+
curl_setopt_array($this->curl, $this->options->curl_options);
5856
}
5957

6058
/**
@@ -118,8 +116,8 @@ public function init(){
118116
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
119117
CURLOPT_SSL_VERIFYPEER => true,
120118
CURLOPT_SSL_VERIFYHOST => 2,
121-
CURLOPT_CAINFO => is_file($this->options->ca_info) ? $this->options->ca_info : null,
122-
CURLOPT_TIMEOUT => (int)$this->options->timeout,
119+
CURLOPT_CAINFO => $this->options->ca_info,
120+
CURLOPT_TIMEOUT => 10,
123121
CURLOPT_CONNECTTIMEOUT => 30,
124122
CURLOPT_WRITEFUNCTION => [$this, 'writefunction'],
125123
CURLOPT_HEADERFUNCTION => [$this, 'headerfunction'],
@@ -223,7 +221,9 @@ public function init(){
223221
$options[CURLOPT_HTTPHEADER][] = 'Content-Type:';
224222
}
225223

226-
curl_setopt_array($this->curl, $this->options->curl_options + $options);
224+
// overwrite the default values with $curl_options
225+
// @todo: callback/middleware for the cURL options array?
226+
curl_setopt_array($this->curl, $options + $this->options->curl_options);
227227

228228
return $this->curl;
229229
}

src/HTTPOptions.php

-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
/**
1818
* @property string $user_agent
19-
* @property int $timeout
2019
* @property array $curl_options
2120
* @property string $ca_info
22-
* @property int $max_redirects
2321
*/
2422
class HTTPOptions extends SettingsContainerAbstract{
2523
use HTTPOptionsTrait;

src/HTTPOptionsTrait.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ trait HTTPOptionsTrait{
1919
*/
2020
public $user_agent = 'chillerlanHttpInterface/2.0 +https://github.com/chillerlan/php-httpinterface';
2121

22-
/**
23-
* @var int
24-
*/
25-
public $timeout = 10;
26-
2722
/**
2823
* options for each curl instance
2924
*
25+
* this array is being merged into the default options as the last thing before curl_exec().
26+
* none of the values (except existence of the CA file) will be checked - that's up to the implementation.
27+
*
3028
* @var array
3129
*/
3230
public $curl_options = [];
@@ -41,8 +39,29 @@ trait HTTPOptionsTrait{
4139
public $ca_info = null;
4240

4341
/**
44-
* @var int
42+
* HTTPOptionsTrait constructor
43+
*
44+
* @throws \chillerlan\HTTP\ClientException
4545
*/
46-
public $max_redirects = 0;
46+
protected function HTTPOptionsTrait():void{
47+
48+
if(!is_array($this->curl_options)){
49+
$this->curl_options = [];
50+
}
51+
52+
// we cannot verify a peer against a non-existent ca file, so turn it off in that case
53+
if(!$this->ca_info || !is_file($this->ca_info)
54+
|| (isset($this->curl_options[CURLOPT_CAINFO]) && !is_file($this->curl_options[CURLOPT_CAINFO]))){
55+
56+
$this->curl_options += [
57+
CURLOPT_SSL_VERIFYPEER => false,
58+
CURLOPT_CAINFO => null,
59+
];
60+
}
61+
62+
if(!is_string($this->user_agent) || empty(trim($this->user_agent))){
63+
throw new ClientException('invalid user agent');
64+
}
65+
}
4766

4867
}

0 commit comments

Comments
 (0)