Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 require_once dirname(__FILE__).'/RequestInterface.php';
00032 include_once(dirname(__FILE__).'/Exception.php');
00033 include_once(dirname(__FILE__).'/../InvalidArgumentException.php');
00034 include_once(dirname(__FILE__).'/../OutOfSequenceException.php');
00035
00039 abstract class CAS_AbstractRequest
00040 implements CAS_RequestInterface
00041 {
00042
00043 protected $url = null;
00044 protected $cookies = array();
00045 protected $headers = array();
00046 protected $isPost = FALSE;
00047 protected $postBody = null;
00048 protected $caCertPath = null;
00049 private $sent = FALSE;
00050 private $responseHeaders = array();
00051 private $responseBody = null;
00052 private $errorMessage = '';
00053
00054
00055
00056
00057
00065 public function setUrl ($url) {
00066 if ($this->sent)
00067 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00068
00069 $this->url = $url;
00070 }
00071
00080 public function addCookie ($name, $value) {
00081 if ($this->sent)
00082 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00083
00084 $this->cookies[$name] = $value;
00085 }
00086
00096 public function addCookies (array $cookies) {
00097 if ($this->sent)
00098 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00099
00100 $this->cookies = array_merge($this->cookies, $cookies);
00101 }
00102
00110 public function addHeader ($header) {
00111 if ($this->sent)
00112 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00113
00114 $this->headers[] = $header;
00115 }
00116
00124 public function addHeaders (array $headers) {
00125 if ($this->sent)
00126 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00127
00128 $this->headers = array_merge($this->headers, $headers);
00129 }
00130
00137 public function makePost () {
00138 if ($this->sent)
00139 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00140
00141 $this->isPost = TRUE;
00142 }
00143
00151 public function setPostBody ($body) {
00152 if ($this->sent)
00153 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00154 if (!$this->isPost)
00155 throw new CAS_OutOfSequenceException('Cannot add a POST body to a GET request, use makePost() first.');
00156
00157 $this->postBody = $body;
00158 }
00159
00167 public function setSslCaCert ($caCertPath) {
00168 if ($this->sent)
00169 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00170
00171 $this->caCertPath = $caCertPath;
00172 }
00173
00174
00175
00176
00177
00184 public function send () {
00185 if ($this->sent)
00186 throw new CAS_OutOfSequenceException('Request has already been sent cannot send again.');
00187 if (is_null($this->url) || !$this->url)
00188 throw new CAS_OutOfSequenceException('A url must be specified via setUrl() before the request can be sent.');
00189
00190 $this->sent = true;
00191 return $this->_sendRequest();
00192 }
00193
00199 abstract protected function _sendRequest ();
00200
00207 protected function storeResponseHeaders (array $headers) {
00208 $this->responseHeaders = array_merge($this->responseHeaders, $headers);
00209 }
00210
00217 protected function storeResponseHeader ($header) {
00218 $this->responseHeaders[] = $header;
00219 }
00220
00227 protected function storeResponseBody ($body) {
00228 $this->responseBody = $body;
00229 }
00230
00237 protected function storeErrorMessage ($message) {
00238 $this->errorMessage .= $message;
00239 }
00240
00241
00242
00243
00244
00251 public function getResponseHeaders () {
00252 if (!$this->sent)
00253 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00254
00255 return $this->responseHeaders;
00256 }
00257
00264 public function getResponseStatusCode () {
00265 if (!$this->sent)
00266 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00267
00268 if (!preg_match('/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/', $this->responseHeaders[0], $matches))
00269 throw new CAS_Request_Exception("Bad response, no status code was found in the first line.");
00270
00271 return intval($matches[1]);
00272 }
00273
00280 public function getResponseBody () {
00281 if (!$this->sent)
00282 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00283
00284 return $this->responseBody;
00285 }
00286
00293 public function getErrorMessage () {
00294 if (!$this->sent)
00295 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00296
00297 return $this->errorMessage;
00298 }
00299 }