http.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt, Thomas Tanghus, Bart Visscher
  6. * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework;
  23. use OCP\AppFramework\Http as BaseHttp;
  24. class Http extends BaseHttp {
  25. private $server;
  26. private $protocolVersion;
  27. protected $headers;
  28. /**
  29. * @param $_SERVER $server
  30. * @param string $protocolVersion the http version to use defaults to HTTP/1.1
  31. */
  32. public function __construct($server, $protocolVersion='HTTP/1.1') {
  33. $this->server = $server;
  34. $this->protocolVersion = $protocolVersion;
  35. $this->headers = array(
  36. self::STATUS_CONTINUE => 'Continue',
  37. self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols',
  38. self::STATUS_PROCESSING => 'Processing',
  39. self::STATUS_OK => 'OK',
  40. self::STATUS_CREATED => 'Created',
  41. self::STATUS_ACCEPTED => 'Accepted',
  42. self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information',
  43. self::STATUS_NO_CONTENT => 'No Content',
  44. self::STATUS_RESET_CONTENT => 'Reset Content',
  45. self::STATUS_PARTIAL_CONTENT => 'Partial Content',
  46. self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918
  47. self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842
  48. self::STATUS_IM_USED => 'IM Used', // RFC 3229
  49. self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices',
  50. self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently',
  51. self::STATUS_FOUND => 'Found',
  52. self::STATUS_SEE_OTHER => 'See Other',
  53. self::STATUS_NOT_MODIFIED => 'Not Modified',
  54. self::STATUS_USE_PROXY => 'Use Proxy',
  55. self::STATUS_RESERVED => 'Reserved',
  56. self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect',
  57. self::STATUS_BAD_REQUEST => 'Bad request',
  58. self::STATUS_UNAUTHORIZED => 'Unauthorized',
  59. self::STATUS_PAYMENT_REQUIRED => 'Payment Required',
  60. self::STATUS_FORBIDDEN => 'Forbidden',
  61. self::STATUS_NOT_FOUND => 'Not Found',
  62. self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed',
  63. self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable',
  64. self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
  65. self::STATUS_REQUEST_TIMEOUT => 'Request Timeout',
  66. self::STATUS_CONFLICT => 'Conflict',
  67. self::STATUS_GONE => 'Gone',
  68. self::STATUS_LENGTH_REQUIRED => 'Length Required',
  69. self::STATUS_PRECONDITION_FAILED => 'Precondition failed',
  70. self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
  71. self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
  72. self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
  73. self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
  74. self::STATUS_EXPECTATION_FAILED => 'Expectation Failed',
  75. self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324
  76. self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918
  77. self::STATUS_LOCKED => 'Locked', // RFC 4918
  78. self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918
  79. self::STATUS_UPGRADE_REQUIRED => 'Upgrade required',
  80. self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status
  81. self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status
  82. self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status
  83. self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error',
  84. self::STATUS_NOT_IMPLEMENTED => 'Not Implemented',
  85. self::STATUS_BAD_GATEWAY => 'Bad Gateway',
  86. self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable',
  87. self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout',
  88. self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported',
  89. self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
  90. self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918
  91. self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842
  92. self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard
  93. self::STATUS_NOT_EXTENDED => 'Not extended',
  94. self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status
  95. );
  96. }
  97. /**
  98. * Gets the correct header
  99. * @param Http::CONSTANT $status the constant from the Http class
  100. * @param \DateTime $lastModified formatted last modified date
  101. * @param string $Etag the etag
  102. */
  103. public function getStatusHeader($status, \DateTime $lastModified=null,
  104. $ETag=null) {
  105. if(!is_null($lastModified)) {
  106. $lastModified = $lastModified->format(\DateTime::RFC2822);
  107. }
  108. // if etag or lastmodified have not changed, return a not modified
  109. if ((isset($this->server['HTTP_IF_NONE_MATCH'])
  110. && trim($this->server['HTTP_IF_NONE_MATCH']) === $ETag)
  111. ||
  112. (isset($this->server['HTTP_IF_MODIFIED_SINCE'])
  113. && trim($this->server['HTTP_IF_MODIFIED_SINCE']) ===
  114. $lastModified)) {
  115. $status = self::STATUS_NOT_MODIFIED;
  116. }
  117. // we have one change currently for the http 1.0 header that differs
  118. // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND
  119. // if this differs any more, we want to create childclasses for this
  120. if($status === self::STATUS_TEMPORARY_REDIRECT
  121. && $this->protocolVersion === 'HTTP/1.0') {
  122. $status = self::STATUS_FOUND;
  123. }
  124. return $this->protocolVersion . ' ' . $status . ' ' .
  125. $this->headers[$status];
  126. }
  127. }