1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<?php
declare(strict_types=1);
namespace OpenTelemetry\API\Baggage\Propagation;
use function explode;
use OpenTelemetry\API\Baggage\BaggageBuilderInterface;
use OpenTelemetry\API\Baggage\Metadata;
use function str_replace;
use function trim;
use function urldecode;
final class Parser
{
private const EXCLUDED_KEY_CHARS = [' ', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}'];
private const EXCLUDED_VALUE_CHARS = [' ', '"', ',', ';', '\\'];
private const EQUALS = '=';
/** @readonly */
private string $baggageHeader;
public function __construct(string $baggageHeader)
{
$this->baggageHeader = $baggageHeader;
}
public function parseInto(BaggageBuilderInterface $baggageBuilder): void
{
foreach (explode(',', $this->baggageHeader) as $baggageString) {
if (empty(trim($baggageString))) {
continue;
}
$explodedString = explode(';', $baggageString, 2);
$keyValue = trim($explodedString[0]);
if (empty($keyValue) || mb_strpos($keyValue, self::EQUALS) === false) {
continue;
}
$metadataString = $explodedString[1] ?? null;
if ($metadataString && !empty(trim(($metadataString)))) {
$metadata = new Metadata(trim($metadataString));
} else {
$metadata = null;
}
[$key, $value] = explode(self::EQUALS, $keyValue, 2);
$key = urldecode($key);
$value = urldecode($value);
$key = str_replace(self::EXCLUDED_KEY_CHARS, '', trim($key), $invalidKeyCharacters);
if (empty($key) || $invalidKeyCharacters > 0) {
continue;
}
$value = str_replace(self::EXCLUDED_VALUE_CHARS, '', trim($value), $invalidValueCharacters);
if (empty($value) || $invalidValueCharacters > 0) {
continue;
}
$baggageBuilder->set($key, $value, $metadata);
}
}
}
|