1
0

Http.php 5.7 KB

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