Response.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Sabre_HTTP_Response
  4. *
  5. * @package Sabre
  6. * @subpackage HTTP
  7. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  8. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  9. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  10. */
  11. class Sabre_HTTP_Response {
  12. /**
  13. * Returns a full HTTP status message for an HTTP status code
  14. *
  15. * @param int $code
  16. * @return string
  17. */
  18. public function getStatusMessage($code) {
  19. $msg = array(
  20. 100 => 'Continue',
  21. 101 => 'Switching Protocols',
  22. 102 => 'Processing',
  23. 200 => 'OK',
  24. 201 => 'Created',
  25. 202 => 'Accepted',
  26. 203 => 'Non-Authorative Information',
  27. 204 => 'No Content',
  28. 205 => 'Reset Content',
  29. 206 => 'Partial Content',
  30. 207 => 'Multi-Status', // RFC 4918
  31. 208 => 'Already Reported', // RFC 5842
  32. 226 => 'IM Used', // RFC 3229
  33. 300 => 'Multiple Choices',
  34. 301 => 'Moved Permanently',
  35. 302 => 'Found',
  36. 303 => 'See Other',
  37. 304 => 'Not Modified',
  38. 305 => 'Use Proxy',
  39. 306 => 'Reserved',
  40. 307 => 'Temporary Redirect',
  41. 400 => 'Bad request',
  42. 401 => 'Unauthorized',
  43. 402 => 'Payment Required',
  44. 403 => 'Forbidden',
  45. 404 => 'Not Found',
  46. 405 => 'Method Not Allowed',
  47. 406 => 'Not Acceptable',
  48. 407 => 'Proxy Authentication Required',
  49. 408 => 'Request Timeout',
  50. 409 => 'Conflict',
  51. 410 => 'Gone',
  52. 411 => 'Length Required',
  53. 412 => 'Precondition failed',
  54. 413 => 'Request Entity Too Large',
  55. 414 => 'Request-URI Too Long',
  56. 415 => 'Unsupported Media Type',
  57. 416 => 'Requested Range Not Satisfiable',
  58. 417 => 'Expectation Failed',
  59. 418 => 'I\'m a teapot', // RFC 2324
  60. 422 => 'Unprocessable Entity', // RFC 4918
  61. 423 => 'Locked', // RFC 4918
  62. 424 => 'Failed Dependency', // RFC 4918
  63. 426 => 'Upgrade required',
  64. 428 => 'Precondition required', // draft-nottingham-http-new-status
  65. 429 => 'Too Many Requests', // draft-nottingham-http-new-status
  66. 431 => 'Request Header Fields Too Large', // draft-nottingham-http-new-status
  67. 500 => 'Internal Server Error',
  68. 501 => 'Not Implemented',
  69. 502 => 'Bad Gateway',
  70. 503 => 'Service Unavailable',
  71. 504 => 'Gateway Timeout',
  72. 505 => 'HTTP Version not supported',
  73. 506 => 'Variant Also Negotiates',
  74. 507 => 'Insufficient Storage', // RFC 4918
  75. 508 => 'Loop Detected', // RFC 5842
  76. 509 => 'Bandwidth Limit Exceeded', // non-standard
  77. 510 => 'Not extended',
  78. 511 => 'Network Authentication Required', // draft-nottingham-http-new-status
  79. );
  80. return 'HTTP/1.1 ' . $code . ' ' . $msg[$code];
  81. }
  82. /**
  83. * Sends an HTTP status header to the client
  84. *
  85. * @param int $code HTTP status code
  86. * @return bool
  87. */
  88. public function sendStatus($code) {
  89. if (!headers_sent())
  90. return header($this->getStatusMessage($code));
  91. else return false;
  92. }
  93. /**
  94. * Sets an HTTP header for the response
  95. *
  96. * @param string $name
  97. * @param string $value
  98. * @param bool $replace
  99. * @return bool
  100. */
  101. public function setHeader($name, $value, $replace = true) {
  102. $value = str_replace(array("\r","\n"),array('\r','\n'),$value);
  103. if (!headers_sent())
  104. return header($name . ': ' . $value, $replace);
  105. else return false;
  106. }
  107. /**
  108. * Sets a bunch of HTTP Headers
  109. *
  110. * headersnames are specified as keys, value in the array value
  111. *
  112. * @param array $headers
  113. * @return void
  114. */
  115. public function setHeaders(array $headers) {
  116. foreach($headers as $key=>$value)
  117. $this->setHeader($key, $value);
  118. }
  119. /**
  120. * Sends the entire response body
  121. *
  122. * This method can accept either an open filestream, or a string.
  123. *
  124. * @param mixed $body
  125. * @return void
  126. */
  127. public function sendBody($body) {
  128. if (is_resource($body)) {
  129. fpassthru($body);
  130. } else {
  131. // We assume a string
  132. echo $body;
  133. }
  134. }
  135. }