Skip to content

Commit

Permalink
Merge pull request #16 from Laragear/feat/http3
Browse files Browse the repository at this point in the history
[2.x] Use HTTP/3 if possible.
  • Loading branch information
DarkGhostHunter authored Apr 10, 2024
2 parents 8905257 + a77f8aa commit 5360eab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Laragear_ReCaptcha&metric=alert_status)](https://sonarcloud.io/dashboard?id=Laragear_ReCaptcha)
[![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/11.x/octane#introduction)

Integrate reCAPTCHA using **async HTTP/2**, making your app **fast** with a few lines.
Integrate reCAPTCHA using **async HTTP/3**, making your app **fast** with a few lines.

```php
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -449,14 +449,14 @@ This also control how many minutes to set the "remember". You can set `INF` cons
```php
return [
'client' => [
'version' => 2.0,
'version' => 3.0,
],
];
```

This array sets the options for the outgoing request to reCAPTCHA servers. [This is handled by Guzzle](https://docs.guzzlephp.org/en/stable/request-options.html), which in turn will pass it to the underlying transport. Depending on your system, it will probably be cURL.

By default, it instructs Guzzle to use HTTP/2 whenever possible.
By default, it instructs Guzzle to use HTTP/3 whenever possible.

### Credentials

Expand Down Expand Up @@ -554,9 +554,11 @@ The file gets published into the `.stubs` folder of your project, while the meta

There should be no problems using this package with Laravel Octane as intended.

## HTTP/3
## HTTP/3 and cURL

To use HTTP/3, [ensure you're using PHP 8.2 or later](https://php.watch/articles/php-curl-http3). cURL version [7.66](https://curl.se/changes.html#7_66_0) supports HTTP/3, and latest PHP 8.2 uses version 7.85.

Currently, HTTP/3 is [still on draft state](https://datatracker.ietf.org/doc/draft-ietf-quic-http/). Until it's [Internet Standard](https://en.wikipedia.org/wiki/Internet_Standard), cURL and Guzzle and reCAPTCHA servers must implement the finished state of the protocol, which as of today is still a moving target.
For more information about checking if your platform can make HTTP/3 requests, check this [PHP Watch article](https://php.watch/articles/php-curl-http3).

## Security

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "laragear/recaptcha",
"description": "Integrate reCAPTCHA using async HTTP/2, making your app fast with a few lines.",
"description": "Integrate reCAPTCHA using async HTTP/3, making your app fast with a few lines.",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
Expand All @@ -9,7 +9,9 @@
"laragear",
"captcha",
"recaptcha",
"google"
"google",
"http2",
"http3"
],
"authors": [
{
Expand Down
5 changes: 3 additions & 2 deletions config/recaptcha.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use GuzzleHttp\RequestOptions;
use Laragear\ReCaptcha\ReCaptcha;

return [
Expand Down Expand Up @@ -83,14 +84,14 @@
|--------------------------------------------------------------------------
|
| This array is passed down to the underlying HTTP Client which will make
| the request to reCAPTCHA servers. By default, is set to use HTTP/2 for
| the request to reCAPTCHA servers. By default, is set to use HTTP/3 for
| the request. You can change, remove or add more options in the array.
|
| @see https://docs.guzzlephp.org/en/stable/request-options.html
*/

'client' => [
'version' => 2.0,
RequestOptions::VERSION => 3.0,
],

/*
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Middleware/ScoreMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public function test_checks_for_human_score(): void

$mock->expects('async')->withNoArgs()->times(4)->andReturnSelf();
$mock->expects('asForm')->withNoArgs()->times(4)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 2.0])->times(4)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 3.0])->times(4)->andReturnSelf();
$mock->expects('post')
->with(
ReCaptcha::SERVER_ENDPOINT,
Expand Down
8 changes: 4 additions & 4 deletions tests/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_returns_response(): void

$mock->expects('asForm')->withNoArgs()->once()->andReturnSelf();
$mock->expects('async')->withNoArgs()->once()->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 2.0])->once()->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 3.0])->once()->andReturnSelf();
$mock->expects('post')
->with(
ReCaptcha::SERVER_ENDPOINT,
Expand Down Expand Up @@ -62,7 +62,7 @@ public function test_uses_v2_test_credentials_by_default(): void

$mock->expects('asForm')->withNoArgs()->times(3)->andReturnSelf();
$mock->expects('async')->withNoArgs()->times(3)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 2.0])->times(3)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 3.0])->times(3)->andReturnSelf();
$mock->expects('post')
->with(
ReCaptcha::SERVER_ENDPOINT,
Expand Down Expand Up @@ -114,7 +114,7 @@ public function test_uses_v2_custom_credentials(): void

$mock->expects('asForm')->withNoArgs()->times(3)->andReturnSelf();
$mock->expects('async')->withNoArgs()->times(3)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 2.0])->times(3)->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 3.0])->times(3)->andReturnSelf();

$mock->expects('post')
->with(
Expand Down Expand Up @@ -207,7 +207,7 @@ public function test_receives_v3_secret(): void

$mock->expects('asForm')->withNoArgs()->once()->andReturnSelf();
$mock->expects('async')->withNoArgs()->once()->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 2.0])->once()->andReturnSelf();
$mock->expects('withOptions')->with(['version' => 3.0])->once()->andReturnSelf();
$mock->expects('post')
->with(ReCaptcha::SERVER_ENDPOINT, [
'secret' => 'secret',
Expand Down

0 comments on commit 5360eab

Please sign in to comment.