PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/HTTP/Request.php

https://github.com/KOLANICH/SabreDAV
PHP | 284 lines | 102 code | 51 blank | 131 comment | 19 complexity | a713be53ce2bf9a0d68f159fefce4609 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\HTTP;
  3. /**
  4. * HTTP Request information
  5. *
  6. * This object can be used to easily access information about an HTTP request.
  7. * It can additionally be used to create 'mock' requests.
  8. *
  9. * This class mostly operates independent, but because of the nature of a single
  10. * request per run it can operate as a singleton. For more information check out
  11. * the behaviour around 'defaultInputStream'.
  12. *
  13. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  14. * @author Evert Pot (http://evertpot.com/)
  15. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  16. */
  17. class Request {
  18. /**
  19. * PHP's $_SERVER data
  20. *
  21. * @var array
  22. */
  23. protected $_SERVER;
  24. /**
  25. * PHP's $_POST data
  26. *
  27. * @var array
  28. */
  29. protected $_POST;
  30. /**
  31. * The request body, if any.
  32. *
  33. * This is stored in the form of a stream resource.
  34. *
  35. * @var resource
  36. */
  37. protected $body = null;
  38. /**
  39. * This will be set as the 'default' inputStream for a specific HTTP request
  40. * We sometimes need to retain, or rebuild this if we need multiple runs
  41. * of parsing the original HTTP request.
  42. *
  43. * @var resource
  44. */
  45. static $defaultInputStream=null;
  46. /**
  47. * Sets up the object
  48. *
  49. * The serverData and postData array can be used to override usage of PHP's
  50. * global _SERVER and _POST variable respectively.
  51. *
  52. * @param array $serverData
  53. * @param array $postData
  54. */
  55. public function __construct(array $serverData = null, array $postData = null) {
  56. if ($serverData) $this->_SERVER = $serverData;
  57. else $this->_SERVER =& $_SERVER;
  58. if ($postData) $this->_POST = $postData;
  59. else $this->_POST =& $_POST;
  60. }
  61. /**
  62. * Returns the value for a specific http header.
  63. *
  64. * This method returns null if the header did not exist.
  65. *
  66. * @param string $name
  67. * @return string
  68. */
  69. public function getHeader($name) {
  70. $name = strtoupper(str_replace(array('-'),array('_'),$name));
  71. if (isset($this->_SERVER['HTTP_' . $name])) {
  72. return $this->_SERVER['HTTP_' . $name];
  73. }
  74. // There's a few headers that seem to end up in the top-level
  75. // server array.
  76. switch($name) {
  77. case 'CONTENT_TYPE' :
  78. case 'CONTENT_LENGTH' :
  79. if (isset($this->_SERVER[$name])) {
  80. return $this->_SERVER[$name];
  81. }
  82. break;
  83. }
  84. return;
  85. }
  86. /**
  87. * Returns all (known) HTTP headers.
  88. *
  89. * All headers are converted to lower-case, and additionally all underscores
  90. * are automatically converted to dashes
  91. *
  92. * @return array
  93. */
  94. public function getHeaders() {
  95. $hdrs = array();
  96. foreach($this->_SERVER as $key=>$value) {
  97. switch($key) {
  98. case 'CONTENT_LENGTH' :
  99. case 'CONTENT_TYPE' :
  100. $hdrs[strtolower(str_replace('_','-',$key))] = $value;
  101. break;
  102. default :
  103. if (strpos($key,'HTTP_')===0) {
  104. $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
  105. }
  106. break;
  107. }
  108. }
  109. return $hdrs;
  110. }
  111. /**
  112. * Returns the HTTP request method
  113. *
  114. * This is for example POST or GET
  115. *
  116. * @return string
  117. */
  118. public function getMethod() {
  119. return $this->_SERVER['REQUEST_METHOD'];
  120. }
  121. /**
  122. * Returns the requested uri
  123. *
  124. * @return string
  125. */
  126. public function getUri() {
  127. return $this->_SERVER['REQUEST_URI'];
  128. }
  129. /**
  130. * Will return protocol + the hostname + the uri
  131. *
  132. * @return string
  133. */
  134. public function getAbsoluteUri() {
  135. // Checking if the request was made through HTTPS. The last in line is for IIS
  136. $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
  137. return ($protocol?'https':'http') . '://' . $this->getHeader('Host') . $this->getUri();
  138. }
  139. /**
  140. * Returns everything after the ? from the current url
  141. *
  142. * @return string
  143. */
  144. public function getQueryString() {
  145. return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
  146. }
  147. /**
  148. * Returns the HTTP request body body
  149. *
  150. * This method returns a readable stream resource.
  151. * If the asString parameter is set to true, a string is sent instead.
  152. *
  153. * @param bool $asString
  154. * @return resource
  155. */
  156. public function getBody($asString = false) {
  157. if (is_null($this->body)) {
  158. if (!is_null(self::$defaultInputStream)) {
  159. $this->body = self::$defaultInputStream;
  160. } else {
  161. $this->body = fopen('php://input','r');
  162. self::$defaultInputStream = $this->body;
  163. }
  164. }
  165. if ($asString) {
  166. $body = stream_get_contents($this->body);
  167. return $body;
  168. } else {
  169. return $this->body;
  170. }
  171. }
  172. /**
  173. * Sets the contents of the HTTP request body
  174. *
  175. * This method can either accept a string, or a readable stream resource.
  176. *
  177. * If the setAsDefaultInputStream is set to true, it means for this run of the
  178. * script the supplied body will be used instead of php://input.
  179. *
  180. * @param mixed $body
  181. * @param bool $setAsDefaultInputStream
  182. * @return void
  183. */
  184. public function setBody($body,$setAsDefaultInputStream = false) {
  185. if(is_resource($body)) {
  186. $this->body = $body;
  187. } else {
  188. $stream = fopen('php://temp','r+');
  189. fputs($stream,$body);
  190. rewind($stream);
  191. // String is assumed
  192. $this->body = $stream;
  193. }
  194. if ($setAsDefaultInputStream) {
  195. self::$defaultInputStream = $this->body;
  196. }
  197. }
  198. /**
  199. * Returns PHP's _POST variable.
  200. *
  201. * The reason this is in a method is so it can be subclassed and
  202. * overridden.
  203. *
  204. * @return array
  205. */
  206. public function getPostVars() {
  207. return $this->_POST;
  208. }
  209. /**
  210. * Returns a specific item from the _SERVER array.
  211. *
  212. * Do not rely on this feature, it is for internal use only.
  213. *
  214. * @param string $field
  215. * @return string
  216. */
  217. public function getRawServerValue($field) {
  218. return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;
  219. }
  220. /**
  221. * Returns the HTTP version specified within the request.
  222. *
  223. * @return string
  224. */
  225. public function getHTTPVersion() {
  226. $protocol = $this->getRawServerValue('SERVER_PROTOCOL');
  227. if ($protocol==='HTTP/1.0') {
  228. return '1.0';
  229. } else {
  230. return '1.1';
  231. }
  232. }
  233. }