HeaderValue.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Header
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Http
  25. * @subpackage Header
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. final class Zend_Http_Header_HeaderValue
  30. {
  31. /**
  32. * Private constructor; non-instantiable.
  33. */
  34. private function __construct()
  35. {
  36. }
  37. /**
  38. * Filter a header value
  39. *
  40. * Ensures CRLF header injection vectors are filtered.
  41. *
  42. * Per RFC 7230, only VISIBLE ASCII characters, spaces, and horizontal
  43. * tabs are allowed in values; only one whitespace character is allowed
  44. * between visible characters.
  45. *
  46. * @see http://en.wikipedia.org/wiki/HTTP_response_splitting
  47. * @param string $value
  48. * @return string
  49. */
  50. public static function filter($value)
  51. {
  52. $value = (string) $value;
  53. $length = strlen($value);
  54. $string = '';
  55. for ($i = 0; $i < $length; $i += 1) {
  56. $ascii = ord($value[$i]);
  57. // Non-visible, non-whitespace characters
  58. // 9 === horizontal tab
  59. // 32-126, 128-254 === visible
  60. // 127 === DEL
  61. // 255 === null byte
  62. if (($ascii < 32 && $ascii !== 9)
  63. || $ascii === 127
  64. || $ascii > 254
  65. ) {
  66. continue;
  67. }
  68. $string .= $value[$i];
  69. }
  70. return $string;
  71. }
  72. /**
  73. * Validate a header value.
  74. *
  75. * Per RFC 7230, only VISIBLE ASCII characters, spaces, and horizontal
  76. * tabs are allowed in values; only one whitespace character is allowed
  77. * between visible characters.
  78. *
  79. * @see http://en.wikipedia.org/wiki/HTTP_response_splitting
  80. * @param string $value
  81. * @return bool
  82. */
  83. public static function isValid($value)
  84. {
  85. $value = (string) $value;
  86. $length = strlen($value);
  87. for ($i = 0; $i < $length; $i += 1) {
  88. $ascii = ord($value[$i]);
  89. // Non-visible, non-whitespace characters
  90. // 9 === horizontal tab
  91. // 32-126, 128-254 === visible
  92. // 127 === DEL
  93. // 255 === null byte
  94. if (($ascii < 32 && $ascii !== 9)
  95. || $ascii === 127
  96. || $ascii > 254
  97. ) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * Assert a header value is valid.
  105. *
  106. * @param string $value
  107. * @throws Exception\RuntimeException for invalid values
  108. * @return void
  109. */
  110. public static function assertValid($value)
  111. {
  112. if (! self::isValid($value)) {
  113. #require_once 'Zend/Http/Header/Exception/InvalidArgumentException.php';
  114. throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid header value');
  115. }
  116. }
  117. }