Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Last active March 22, 2016 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noodlehaus/e2d00d631ee95173a082 to your computer and use it in GitHub Desktop.
Save noodlehaus/e2d00d631ee95173a082 to your computer and use it in GitHub Desktop.
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