Http.php 6.1 KB

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