-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
77 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
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Util; | ||
|
||
use Sentry\Client; | ||
use Sentry\Dsn; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Http | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public static function getRequestHeaders(Dsn $dsn, string $sdkIdentifier, string $sdkVersion): array | ||
{ | ||
$headers = [ | ||
'Content-Type' => 'application/x-sentry-envelope', | ||
]; | ||
|
||
$data = [ | ||
'sentry_version' => Client::PROTOCOL_VERSION, | ||
'sentry_client' => $sdkIdentifier . '/' . $sdkVersion, | ||
'sentry_key' => $dsn->getPublicKey(), | ||
]; | ||
|
||
if (null !== $dsn->getSecretKey()) { | ||
$data['sentry_secret'] = $dsn->getSecretKey(); | ||
} | ||
|
||
$authHeader = []; | ||
foreach ($data as $headerKey => $headerValue) { | ||
$authHeader[] = $headerKey . '=' . $headerValue; | ||
} | ||
|
||
return array_merge($headers, [ | ||
'X-Sentry-Auth' => 'Sentry ' . implode(', ', $authHeader), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public static function getResponseHeaders(?int $headerSize, string $body): array | ||
{ | ||
$headers = []; | ||
$rawHeaders = explode("\r\n", trim(substr($body, 0, $headerSize))); | ||
|
||
foreach ($rawHeaders as $value) { | ||
if (!str_contains($value, ':')) { | ||
continue; | ||
} | ||
[$name, $value] = explode(':', $value, 2); | ||
$value = trim($value); | ||
$name = trim($name); | ||
|
||
if (isset($headers[$name])) { | ||
$headers[$name][] = $value; | ||
} else { | ||
$headers[$name] = (array) $value; | ||
} | ||
} | ||
|
||
return $headers; | ||
} | ||
} |
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
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Tests\Util; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sentry\Dsn; | ||
use Sentry\Util\Http; | ||
|
||
final class HttpTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getRequestHeadersDataProvider | ||
*/ | ||
public function testGetRequestHeaders(Dsn $dsn, string $sdkIdentifier, string $sdkVersion, array $expectedResult): void | ||
{ | ||
$this->assertSame($expectedResult, Http::getRequestHeaders($dsn, $sdkIdentifier, $sdkVersion)); | ||
} | ||
|
||
public static function getRequestHeadersDataProvider(): \Generator | ||
{ | ||
yield [ | ||
Dsn::createFromString('http://[email protected]/1'), | ||
'sentry.sdk.identifier', | ||
'1.2.3', | ||
[ | ||
'Content-Type' => 'application/x-sentry-envelope', | ||
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.sdk.identifier/1.2.3, sentry_key=public', | ||
], | ||
]; | ||
|
||
yield [ | ||
Dsn::createFromString('http://public:[email protected]/1'), | ||
'sentry.sdk.identifier', | ||
'1.2.3', | ||
[ | ||
'Content-Type' => 'application/x-sentry-envelope', | ||
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.sdk.identifier/1.2.3, sentry_key=public, sentry_secret=secret', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getResponseHeadersDataProvider | ||
*/ | ||
public function testGetResponseHeaders(?int $headerSize, string $body, array $expectedResult): void | ||
{ | ||
$this->assertSame($expectedResult, Http::getResponseHeaders($headerSize, $body)); | ||
} | ||
|
||
public static function getResponseHeadersDataProvider(): \Generator | ||
{ | ||
yield [ | ||
128, | ||
<<<TEXT | ||
HTTP/1.1 200 OK\r\n | ||
Server: nginx\r\n | ||
Date: Tue, 10 Oct 2023 10:00:00 GMT\r\n | ||
Content-Type: application/json\r\n | ||
Content-Length: 41\r\n | ||
\r\n | ||
{"id":"2beb84919c3b4c92855206dd7f911a56"} | ||
TEXT | ||
, | ||
[ | ||
'Server' => ['nginx'], | ||
'Date' => ['Tue, 10 Oct 2023 10:00:00 GMT'], | ||
'Content-Type' => ['application/json'], | ||
'Content-Length' => ['41'], | ||
], | ||
]; | ||
} | ||
} |