4 * HTTP Request information
6 * This object can be used to easily access information about an HTTP request.
7 * It can additionally be used to create 'mock' requests.
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'.
15 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
16 * @author Evert Pot (http://www.rooftopsolutions.nl/)
17 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
19 class Sabre_HTTP_Request {
36 * The request body, if any.
38 * This is stored in the form of a stream resource.
42 protected $body = null;
45 * This will be set as the 'default' inputStream for a specific HTTP request
46 * We sometimes need to retain, or rebuild this if we need multiple runs
47 * of parsing the original HTTP request.
51 static $defaultInputStream=null;
56 * The serverData and postData array can be used to override usage of PHP's
57 * global _SERVER and _POST variable respectively.
59 * @param array $serverData
60 * @param array $postData
62 public function __construct(array $serverData = null, array $postData = null) {
64 if ($serverData) $this->_SERVER = $serverData;
65 else $this->_SERVER =& $_SERVER;
67 if ($postData) $this->_POST = $postData;
68 else $this->_POST =& $_POST;
73 * Returns the value for a specific http header.
75 * This method returns null if the header did not exist.
80 public function getHeader($name) {
82 $name = strtoupper(str_replace(array('-'),array('_'),$name));
83 if (isset($this->_SERVER['HTTP_' . $name])) {
84 return $this->_SERVER['HTTP_' . $name];
87 // There's a few headers that seem to end up in the top-level
91 case 'CONTENT_LENGTH' :
92 if (isset($this->_SERVER[$name])) {
93 return $this->_SERVER[$name];
103 * Returns all (known) HTTP headers.
105 * All headers are converted to lower-case, and additionally all underscores
106 * are automatically converted to dashes
110 public function getHeaders() {
113 foreach($this->_SERVER as $key=>$value) {
116 case 'CONTENT_LENGTH' :
117 case 'CONTENT_TYPE' :
118 $hdrs[strtolower(str_replace('_','-',$key))] = $value;
121 if (strpos($key,'HTTP_')===0) {
122 $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
134 * Returns the HTTP request method
136 * This is for example POST or GET
140 public function getMethod() {
142 return $this->_SERVER['REQUEST_METHOD'];
147 * Returns the requested uri
151 public function getUri() {
153 return $this->_SERVER['REQUEST_URI'];
158 * Will return protocol + the hostname + the uri
162 public function getAbsoluteUri() {
164 // Checking if the request was made through HTTPS. The last in line is for IIS
165 $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
166 return ($protocol?'https':'http') . '://' . $this->getHeader('Host') . $this->getUri();
171 * Returns everything after the ? from the current url
175 public function getQueryString() {
177 return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
182 * Returns the HTTP request body body
184 * This method returns a readable stream resource.
185 * If the asString parameter is set to true, a string is sent instead.
187 * @param bool $asString
190 public function getBody($asString = false) {
192 if (is_null($this->body)) {
193 if (!is_null(self::$defaultInputStream)) {
194 $this->body = self::$defaultInputStream;
196 $this->body = fopen('php://input','r');
197 self::$defaultInputStream = $this->body;
201 $body = stream_get_contents($this->body);
210 * Sets the contents of the HTTP request body
212 * This method can either accept a string, or a readable stream resource.
214 * If the setAsDefaultInputStream is set to true, it means for this run of the
215 * script the supplied body will be used instead of php://input.
218 * @param bool $setAsDefaultInputStream
221 public function setBody($body,$setAsDefaultInputStream = false) {
223 if(is_resource($body)) {
227 $stream = fopen('php://temp','r+');
228 fputs($stream,$body);
231 $this->body = $stream;
233 if ($setAsDefaultInputStream) {
234 self::$defaultInputStream = $this->body;
240 * Returns PHP's _POST variable.
242 * The reason this is in a method is so it can be subclassed and
247 public function getPostVars() {
254 * Returns a specific item from the _SERVER array.
256 * Do not rely on this feature, it is for internal use only.
258 * @param string $field
261 public function getRawServerValue($field) {
263 return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;