Hoa central
Request.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Http;
38 
47 class Request extends Http
48 {
54  const METHOD_CONNECT = 'connect';
55 
61  const METHOD_DELETE = 'delete';
62 
68  const METHOD_GET = 'get';
69 
75  const METHOD_HEAD = 'head';
76 
82  const METHOD_OPTIONS = 'options';
83 
89  const METHOD_PATCH = 'patch';
90 
96  const METHOD_POST = 'post';
97 
103  const METHOD_PUT = 'put';
104 
110  const METHOD_TRACE = 'trace';
111 
117  const METHOD_EXTENDED = 'extended';
118 
124  protected $_method = null;
125 
131  protected $_url = null;
132 
133 
134 
142  public function parse($packet)
143  {
144  $headers = explode("\r\n", $packet);
145  $http = array_shift($headers);
146  $this->setBody(null);
147 
148  foreach ($headers as $i => $header) {
149  if ('' == trim($header)) {
150  unset($headers[$i]);
151  $this->setBody(
152  trim(
153  implode("\r\n", array_splice($headers, $i))
154  )
155  );
156 
157  break;
158  }
159  }
160 
161  if (0 === preg_match('#^([^\s]+)\s+([^\s]+)\s+HTTP/(1\.(?:0|1))$#i', $http, $matches)) {
162  throw new Exception(
163  'HTTP headers are not well-formed: %s',
164  0,
165  $http
166  );
167  }
168 
169  switch ($method = strtolower($matches[1])) {
170  case self::METHOD_CONNECT:
171  case self::METHOD_DELETE:
172  case self::METHOD_GET:
173  case self::METHOD_HEAD:
174  case self::METHOD_OPTIONS:
175  case self::METHOD_PATCH:
176  case self::METHOD_POST:
177  case self::METHOD_PUT:
178  case self::METHOD_TRACE:
179  $this->_method = $method;
180 
181  break;
182 
183  default:
184  $this->_method = self::METHOD_EXTENDED;
185  }
186 
187  $this->setUrl($matches[2]);
188  $this->setHttpVersion((float) $matches[3]);
189 
190  $this->_parse($headers);
191 
192  return;
193  }
194 
202  public function setMethod($method)
203  {
204  $old = $this->_method;
205  $this->_method = $method;
206 
207  return $old;
208  }
209 
215  public function getMethod()
216  {
217  return $this->_method;
218  }
219 
226  public function setUrl($url)
227  {
228  $old = $this->_url;
229  $this->_url = $url;
230 
231  return $old;
232  }
233 
239  public function getUrl()
240  {
241  return $this->_url;
242  }
243 
249  public function __toString()
250  {
251  return
252  strtoupper($this->getMethod()) . ' ' .
253  $this->getUrl() . ' ' .
254  'HTTP/' . $this->getHttpVersion() . CRLF .
255  parent::__toString() . CRLF;
256  }
257 }
setBody($body)
Definition: Http.php:256
const METHOD_EXTENDED
Definition: Request.php:117
const METHOD_PUT
Definition: Request.php:103
const METHOD_CONNECT
Definition: Request.php:54
setHttpVersion($version)
Definition: Http.php:101
const METHOD_PATCH
Definition: Request.php:89
setMethod($method)
Definition: Request.php:202
const METHOD_TRACE
Definition: Request.php:110
_parse(Array $headers)
Definition: Http.php:134
getHttpVersion()
Definition: Http.php:114
parse($packet)
Definition: Request.php:142
const METHOD_POST
Definition: Request.php:96
const METHOD_GET
Definition: Request.php:68
const METHOD_DELETE
Definition: Request.php:61
const METHOD_OPTIONS
Definition: Request.php:82
const METHOD_HEAD
Definition: Request.php:75