1
0

Http.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\AppFramework;
  8. use OCP\AppFramework\Http as BaseHttp;
  9. class Http extends BaseHttp {
  10. private $server;
  11. private $protocolVersion;
  12. protected $headers;
  13. /**
  14. * @param array $server $_SERVER
  15. * @param string $protocolVersion the http version to use defaults to HTTP/1.1
  16. */
  17. public function __construct($server, $protocolVersion = 'HTTP/1.1') {
  18. $this->server = $server;
  19. $this->protocolVersion = $protocolVersion;
  20. $this->headers = [
  21. self::STATUS_CONTINUE => 'Continue',
  22. self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols',
  23. self::STATUS_PROCESSING => 'Processing',
  24. self::STATUS_OK => 'OK',
  25. self::STATUS_CREATED => 'Created',
  26. self::STATUS_ACCEPTED => 'Accepted',
  27. self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information',
  28. self::STATUS_NO_CONTENT => 'No Content',
  29. self::STATUS_RESET_CONTENT => 'Reset Content',
  30. self::STATUS_PARTIAL_CONTENT => 'Partial Content',
  31. self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918
  32. self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842
  33. self::STATUS_IM_USED => 'IM Used', // RFC 3229
  34. self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices',
  35. self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently',
  36. self::STATUS_FOUND => 'Found',
  37. self::STATUS_SEE_OTHER => 'See Other',
  38. self::STATUS_NOT_MODIFIED => 'Not Modified',
  39. self::STATUS_USE_PROXY => 'Use Proxy',
  40. self::STATUS_RESERVED => 'Reserved',
  41. self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect',
  42. self::STATUS_BAD_REQUEST => 'Bad request',
  43. self::STATUS_UNAUTHORIZED => 'Unauthorized',
  44. self::STATUS_PAYMENT_REQUIRED => 'Payment Required',
  45. self::STATUS_FORBIDDEN => 'Forbidden',
  46. self::STATUS_NOT_FOUND => 'Not Found',
  47. self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed',
  48. self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable',
  49. self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
  50. self::STATUS_REQUEST_TIMEOUT => 'Request Timeout',
  51. self::STATUS_CONFLICT => 'Conflict',
  52. self::STATUS_GONE => 'Gone',
  53. self::STATUS_LENGTH_REQUIRED => 'Length Required',
  54. self::STATUS_PRECONDITION_FAILED => 'Precondition failed',
  55. self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
  56. self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
  57. self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
  58. self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
  59. self::STATUS_EXPECTATION_FAILED => 'Expectation Failed',
  60. self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324
  61. self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918
  62. self::STATUS_LOCKED => 'Locked', // RFC 4918
  63. self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918
  64. self::STATUS_UPGRADE_REQUIRED => 'Upgrade required',
  65. self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status
  66. self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status
  67. self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status
  68. self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error',
  69. self::STATUS_NOT_IMPLEMENTED => 'Not Implemented',
  70. self::STATUS_BAD_GATEWAY => 'Bad Gateway',
  71. self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable',
  72. self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout',
  73. self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported',
  74. self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
  75. self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918
  76. self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842
  77. self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard
  78. self::STATUS_NOT_EXTENDED => 'Not extended',
  79. self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status
  80. ];
  81. }
  82. /**
  83. * Gets the correct header
  84. * @param int Http::CONSTANT $status the constant from the Http class
  85. * @param \DateTime $lastModified formatted last modified date
  86. * @param string $ETag the etag
  87. * @return string
  88. */
  89. public function getStatusHeader($status) {
  90. // we have one change currently for the http 1.0 header that differs
  91. // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND
  92. // if this differs any more, we want to create childclasses for this
  93. if ($status === self::STATUS_TEMPORARY_REDIRECT
  94. && $this->protocolVersion === 'HTTP/1.0') {
  95. $status = self::STATUS_FOUND;
  96. }
  97. return $this->protocolVersion . ' ' . $status . ' ' .
  98. $this->headers[$status];
  99. }
  100. }