Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
header flushing code from zend-diactoros
array_walk($headers, function ($value, $key) {
# validate header key (ref: zend-diactoros)
if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $key)) {
throw new InvalidArgumentException("Invalid header name - {$key}");
}
# validate header values (ref: zend-diactoros)
$values = is_array($value) ? $value : [$value];
foreach ($values as $val) {
if (
preg_match("#(?:(?:(?<!\r)\n)|(?:\r(?!\n))|(?:\r\n(?![ \t])))#", $val) ||
preg_match('/[^\x09\x0a\x0d\x20-\x7E\x80-\xFE]/', $val)
) {
throw new InvalidArgumentException("Invalid header value - {$val}");
}
}
header($key.': '.implode(',', $values));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment