http.php 6.0 KB

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