-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHTTPOptionsTrait.php
More file actions
167 lines (136 loc) · 3.94 KB
/
HTTPOptionsTrait.php
File metadata and controls
167 lines (136 loc) · 3.94 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
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
166
167
<?php
/**
* Trait HTTPOptionsTrait
*
* @created 28.08.2018
* @author Smiley <[email protected]>
* @copyright 2018 Smiley
* @license MIT
*
* @phan-file-suppress PhanTypeInvalidThrowsIsInterface
*/
declare(strict_types=1);
namespace chillerlan\HTTP;
use function parse_url, sprintf, strtolower, trim;
use const CURLOPT_CAINFO, CURLOPT_CAPATH;
/**
*
*/
trait HTTPOptionsTrait{
/**
* A custom user agent string
*/
protected string $user_agent = 'chillerlanHttpInterface/6.0 +https://github.com/chillerlan/php-httpinterface';
/**
* options for each curl instance
*
* this array is being merged into the default options as the last thing before curl_exec().
* none of the values (except existence of the CA file) will be checked - that's up to the implementation.
*/
protected array $curl_options = [];
/**
* CA Root Certificates for use with CURL/SSL
*
* (if not configured in php.ini or available in a default path via the `ca-certificates` package)
*
* @see https://curl.se/docs/caextract.html
* @see https://curl.se/ca/cacert.pem
* @see https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt
*/
protected string|null $ca_info = null;
/**
* see CURLOPT_SSL_VERIFYPEER
* requires either HTTPOptions::$ca_info or a properly working system CA file
*
* @see https://php.net/manual/function.curl-setopt.php
*/
protected bool $ssl_verifypeer = true;
/**
* options for the curl multi instance
*
* @see https://www.php.net/manual/function.curl-multi-setopt.php
*/
protected array $curl_multi_options = [];
/**
* cURL extra hardening
*
* When set to true, cURL validates that the server staples an OCSP response during the TLS handshake.
*
* Use with caution as cURL will refuse a connection if it doesn't receive a valid OCSP response -
* this does not necessarily mean that the TLS connection is insecure.
*
* @see \CURLOPT_SSL_VERIFYSTATUS
*/
protected bool $curl_check_OCSP = false;
/**
* maximum of concurrent requests for curl_multi
*/
protected int $window_size = 5;
/**
* sleep timer (microseconds) between each fired multi request on startup
*/
protected int $sleep = 0;
/**
* Timeout value
*
* @see \CURLOPT_TIMEOUT
*/
protected int $timeout = 10;
/**
* Number of retries (multi fetch)
*/
protected int $retries = 3;
/**
* Sets a DNS-over-HTTPS provider URL
*
* e.g.
*
* - https://cloudflare-dns.com/dns-query
* - https://dns.google/dns-query
* - https://dns.nextdns.io
*
* @see https://en.wikipedia.org/wiki/DNS_over_HTTPS
* @see https://github.com/curl/curl/wiki/DNS-over-HTTPS
*/
protected string|null $dns_over_https = null;
/**
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
protected function set_user_agent(string $user_agent):void{
$user_agent = trim($user_agent);
if(empty($user_agent)){
throw new ClientException('invalid user agent');
}
$this->user_agent = $user_agent;
}
/**
*
*/
protected function set_curl_options(array $curl_options):void{
// let's check if there's a CA bundle given via the cURL options and move it to the ca_info option instead
foreach([CURLOPT_CAINFO, CURLOPT_CAPATH] as $opt){
if(!empty($curl_options[$opt])){
if($this->ca_info === null){
$this->ca_info = $curl_options[$opt];
}
unset($curl_options[$opt]);
}
}
$this->curl_options = $curl_options;
}
/**
* @throws \chillerlan\HTTP\ClientException
*/
protected function set_dns_over_https(string|null $dns_over_https):void{
if($dns_over_https === null){
$this->dns_over_https = null;
return;
}
$dns_over_https = trim($dns_over_https);
$parsed = parse_url($dns_over_https);
if(empty($dns_over_https) || !isset($parsed['scheme'], $parsed['host']) || strtolower($parsed['scheme']) !== 'https'){
throw new ClientException(sprintf('invalid DNS-over-HTTPS URL: "%s"', $dns_over_https));
}
$this->dns_over_https = $dns_over_https;
}
}