Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"require": {
"php": ">=5.6.0",
"nette/utils": "^2.4"
"nette/utils": "^2.4",
"nette/tokenizer": "^2.2"
},
"require-dev": {
"nette/di": "^2.3",
Expand Down
56 changes: 56 additions & 0 deletions src/Http/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,62 @@ public static function ipMatch($ip, $mask)
}


public static function parseHeader($value)
{
$tokenizer = new Nette\Utils\Tokenizer([
'word' => '[\\w!#$%&\'*+\\-.\\^`|\\~/]+', // "/" manually allowed

'quoted' => '"(?:\\\\[\x01-\x7F]|[^"\\\\\x00\x80-\xFF])*+"',
'equal' => '=',
'semicolon' => ';',
'ows' => '\h+',
]);

$tokens = $tokenizer->tokenize("$value;");
$parsed = [];
$k = NULL;
$v = NULL;
$e = FALSE;

foreach ($tokens as list($value, $offset, $type)) {
if ($type === 'word') {
if ($e && $v === NULL) {
$v = $value;
} elseif ($k === NULL) {
$k = $value;
} else {
throw new \Exception();
}

} elseif ($type === 'quoted') {
$value = stripslashes(substr($value, 1, -1));
if ($e && $v === NULL) {
$v = $value;
} else {
throw new \Exception();
}

} elseif ($type === 'semicolon') {
if ($k === NULL) {
throw new \Exception();
}
$parsed[$k] = $v;
$k = $v = NULL;
$e = FALSE;

} elseif ($type === 'equal') {
if ($e || $k === NULL) {
throw new \Exception();
}

$e = TRUE;
}
}

return $parsed;
}


/**
* Removes duplicate cookies from response.
* @return void
Expand Down
44 changes: 44 additions & 0 deletions tests/Http/Helpers.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,47 @@ test(function () {
Assert::same('Tue, 15 Nov 1994 08:12:31 GMT', Helpers::formatDate(new DateTime('1994-11-15T06:12:31-0200')));
Assert::same('Tue, 15 Nov 1994 08:12:31 GMT', Helpers::formatDate(784887151));
});



test(function () {
Assert::same(
['text/html' => NULL],
Helpers::parseHeader('text/html')
);

Assert::same(
['text/html' => NULL, 'charset' => 'iso-8859-1'],
Helpers::parseHeader('text/html; charset="iso-8859-1"')
);

Assert::same(
['text/html' => NULL, 'charset' => 'ISO-8859-4'],
Helpers::parseHeader('text/html; charset=ISO-8859-4')
);

Assert::same(
['text/html' => NULL, 'charset' => 'utf-8'],
Helpers::parseHeader('text/html;charset=utf-8')
);

Assert::same(
['INLINE' => NULL, 'FILENAME' => 'an example.html'],
Helpers::parseHeader('INLINE; FILENAME= "an example.html"')
);

// SUPPORT THIS?
Assert::same(
['attachment' => NULL, 'filename' => '€ rates'],
Helpers::parseHeader('attachment; filename*= UTF-8\'\'%e2%82%ac%20rates')
);

/*

Content-Type: token "/" token params

params = *( OWS ";" OWS parameter )
param = token "=" ( token / quoted-string )

*/
});