|
9 | 9 | use Psr\Http\Message\UriInterface; |
10 | 10 | use function base64_encode; |
11 | 11 | use function count; |
| 12 | +use function file_exists; |
12 | 13 | use function http_build_query; |
13 | 14 | use function is_array; |
14 | 15 | use function is_bool; |
15 | 16 | use function is_int; |
16 | 17 | use function is_string; |
| 18 | +use function json_encode; |
| 19 | +use function var_dump; |
17 | 20 |
|
18 | 21 | class HttpRequest extends Request |
19 | 22 | { |
@@ -106,6 +109,18 @@ private static function preProcessHeaders(array $options, &$body): array |
106 | 109 | $data['verify'] = (bool) $value; |
107 | 110 | } |
108 | 111 |
|
| 112 | + if (isset($options['cert'])) { |
| 113 | + $value = $options['cert']; |
| 114 | + static::validateCertOption($value); |
| 115 | + $data['cert'] = $value; |
| 116 | + } |
| 117 | + |
| 118 | + if (isset($options['ssl_key'])) { |
| 119 | + $value = $options['ssl_key']; |
| 120 | + static::validateSslKeyOption($value); |
| 121 | + $data['ssl_key'] = $value; |
| 122 | + } |
| 123 | + |
109 | 124 | return $data; |
110 | 125 | } |
111 | 126 |
|
@@ -223,4 +238,76 @@ private static function validateVerify($verify): void |
223 | 238 | } |
224 | 239 | } |
225 | 240 |
|
| 241 | + /** |
| 242 | + * @param mixed $value |
| 243 | + */ |
| 244 | + private static function validateCertOption($value): void |
| 245 | + { |
| 246 | + if (!is_string($value) && !is_array($value)) { |
| 247 | + throw new InvalidArgumentException('Option cert must be a string.'); |
| 248 | + } |
| 249 | + |
| 250 | + if (is_array($value)) { |
| 251 | + if (!isset($value[0]) || !isset($value[1])) { |
| 252 | + throw new InvalidArgumentException( |
| 253 | + 'Option cert must be an array of two elements (cert and key). Provided array: ' . json_encode( |
| 254 | + $value |
| 255 | + ) |
| 256 | + ); |
| 257 | + } |
| 258 | + |
| 259 | + if (!is_string($value[0]) || !is_string($value[1])) { |
| 260 | + throw new InvalidArgumentException( |
| 261 | + 'Option cert must be an array of two strings (cert and key). Provided array: ' . json_encode($value) |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + $file = $value[0]; |
| 266 | + } else { |
| 267 | + $file = $value; |
| 268 | + } |
| 269 | + |
| 270 | + var_dump(file_exists($file)); |
| 271 | + |
| 272 | + if (!file_exists($file)) { |
| 273 | + throw new InvalidArgumentException('Option cert file not found. Provided file: ' . $file); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * @param mixed $value |
| 279 | + */ |
| 280 | + private static function validateSslKeyOption($value): void |
| 281 | + { |
| 282 | + if (!is_string($value) && !is_array($value)) { |
| 283 | + throw new InvalidArgumentException('Option ssl_key must be a string.'); |
| 284 | + } |
| 285 | + |
| 286 | + if (is_array($value)) { |
| 287 | + if (!isset($value[0]) || !isset($value[1])) { |
| 288 | + throw new InvalidArgumentException( |
| 289 | + 'Option ssl_key must be an array of two elements (key and password). Provided array: ' . json_encode( |
| 290 | + $value |
| 291 | + ) . '.' |
| 292 | + ); |
| 293 | + } |
| 294 | + |
| 295 | + if (!is_string($value[0]) || !is_string($value[1])) { |
| 296 | + throw new InvalidArgumentException( |
| 297 | + 'Option ssl_key must be an array of two strings (key and password). Provided array: ' . json_encode( |
| 298 | + $value |
| 299 | + ) . '.' |
| 300 | + ); |
| 301 | + } |
| 302 | + |
| 303 | + $file = $value[0]; |
| 304 | + } else { |
| 305 | + $file = $value; |
| 306 | + } |
| 307 | + |
| 308 | + if (!file_exists($file)) { |
| 309 | + throw new InvalidArgumentException('Option ssl_key file not found. Provided file: ' . $file); |
| 310 | + } |
| 311 | + } |
| 312 | + |
226 | 313 | } |
0 commit comments