Skip to content

Commit be9b740

Browse files
authored
Support -E, --cert and --key options (#36)
1 parent 918cec7 commit be9b740

File tree

10 files changed

+50
-1
lines changed

10 files changed

+50
-1
lines changed

src/Console/Commands/CurlCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class CurlCommand extends Command
99
{
10-
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|curl-silent} {--u|user=} {--L|location} {--compressed} {--k|insecure} {url}';
10+
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|curl-silent} {--u|user=} {--L|location} {--compressed} {--k|insecure} {--E|cert=} {--key=} {url}';
1111

1212
protected $description = 'Convert a UNIX curl request to an HTTP Client request';
1313

@@ -42,6 +42,8 @@ private function gatherOptions()
4242
'user' => $this->option('user'),
4343
'compressed' => $this->option('compressed'),
4444
'insecure' => $this->option('insecure'),
45+
'cert' => $this->option('cert'),
46+
'key' => $this->option('key'),
4547
];
4648
}
4749
}

src/Models/Request.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Request
2727

2828
private ?int $connectTimeout = null;
2929

30+
private array $options = [];
31+
3032
private bool $insecure = false;
3133

3234
private function __construct($url, $method)
@@ -108,6 +110,20 @@ public static function create(array $data): self
108110
$request->connectTimeout = $data['connectTimeout'];
109111
}
110112

113+
if (isset($data['cert'])) {
114+
@[$certificate, $password] = explode(':', $data['cert'], 2);
115+
116+
if (isset($password)) {
117+
$request->options['cert'] = [$certificate, $password];
118+
} else {
119+
$request->options['cert'] = $certificate;
120+
}
121+
}
122+
123+
if (isset($data['key'])) {
124+
$request->options['ssl_key'] = $data['key'];
125+
}
126+
111127
if ($data['insecure']) {
112128
$request->insecure = true;
113129
}
@@ -180,6 +196,11 @@ public function connectTimeout(): int
180196
return $this->connectTimeout;
181197
}
182198

199+
public function options(): array
200+
{
201+
return $this->options;
202+
}
203+
183204
public function isInsecure(): bool
184205
{
185206
return $this->insecure;

src/Support/HttpCall.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ private static function generateOptions(Request $request): string
9393
$options[] = 'connectTimeout(' . $request->connectTimeout() . ')';
9494
}
9595

96+
if (!empty($request->options())) {
97+
$options[] = 'withOptions(' . self::prettyPrintArray($request->options()) . ')';
98+
}
99+
96100
if ($request->isInsecure()) {
97101
$options[] = 'withoutVerifying()';
98102
}

tests/Feature/Console/Commands/CurlCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public static function curlCommandFixtures(): array
6464
'GET request with short k flag' => ['with-insecure-k-option'],
6565
'Request with raw data' => ['with-raw-data'],
6666
'POST request with mixed data' => ['raw-data-mixed'],
67+
'Request with cert (path only)' => ['with-cert-path-only'],
68+
'Request with cert (path and password)' => ['with-cert-path-and-password'],
69+
'Request with cert and key' => ['with-cert-and-key'],
6770
];
6871
}
6972

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl --cert /path/to/cert --key /path/to/key https://example.com
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Http::withOptions([
2+
'cert' => '/path/to/cert',
3+
'ssl_key' => '/path/to/key',
4+
])
5+
->get('https://example.com');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -E /path/to/cert:password https://example.com
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Http::withOptions([
2+
'cert' => [
3+
'/path/to/cert',
4+
'password',
5+
],
6+
])
7+
->get('https://example.com');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl --cert /path/to/cert https://example.com
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Http::withOptions([
2+
'cert' => '/path/to/cert',
3+
])
4+
->get('https://example.com');

0 commit comments

Comments
 (0)