forked from hyperf/tracer
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix Guzzle psr7 invalid header error (#20)
- Loading branch information
1 parent
3286019
commit de74ee4
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf + OpenCodeCo | ||
* | ||
* @link https://opencodeco.dev | ||
* @document https://hyperf.wiki | ||
* @contact leo@opencodeco.dev | ||
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Tracer\Support; | ||
|
||
final class GuzzleHeaderValidate | ||
{ | ||
public static function isValidHeader(string $headerName, string $headerValue): bool | ||
{ | ||
return self::isValidHeaderName($headerName) && self::isValidHeaderValue($headerValue); | ||
} | ||
|
||
/** | ||
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L229 | ||
*/ | ||
public static function isValidHeaderName(string $headerName): bool | ||
{ | ||
return (bool)preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $headerName); | ||
} | ||
|
||
/** | ||
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L259 | ||
*/ | ||
public static function isValidHeaderValue(string $value): bool | ||
{ | ||
return (bool)preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf + OpenCodeCo | ||
* | ||
* @link https://opencodeco.dev | ||
* @document https://hyperf.wiki | ||
* @contact leo@opencodeco.dev | ||
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE | ||
*/ | ||
|
||
namespace HyperfTest\Tracer\Support; | ||
|
||
use Hyperf\Tracer\Support\GuzzleHeaderValidate; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
final class JaegerDecoderTest extends TestCase | ||
{ | ||
public function testTraceIdDecoder(): void | ||
{ | ||
self::assertFalse(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c@dt', '1234')); | ||
self::assertTrue(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c', '1234')); | ||
} | ||
|
||
public function testSpanIdDecoder(): void | ||
{ | ||
self::assertSame('255d1b261e0fcbc3', JaegerDecoder::spanIdDecoder('2692338002764483523')); | ||
self::assertSame('21c2405de0e90c56', JaegerDecoder::spanIdDecoder('21c2405de0e90c56')); | ||
} | ||
} |