Http.php 6.2 KB

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