4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * @file CAS/Request/AbstractRequest.php
23 * @category Authentication
25 * @author Adam Franco <afranco@middlebury.edu>
26 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
27 * @link https://wiki.jasig.org/display/CASC/phpCAS
31 * Provides support for performing web-requests via curl
33 * @class CAS_Request_AbstractRequest
34 * @category Authentication
36 * @author Adam Franco <afranco@middlebury.edu>
37 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
38 * @link https://wiki.jasig.org/display/CASC/phpCAS
40 abstract class CAS_Request_AbstractRequest
41 implements CAS_Request_RequestInterface
44 protected $url = null;
45 protected $cookies = array();
46 protected $headers = array();
47 protected $isPost = false;
48 protected $postBody = null;
49 protected $caCertPath = null;
50 protected $validateCN = true;
51 private $_sent = false;
52 private $_responseHeaders = array();
53 private $_responseBody = null;
54 private $_errorMessage = '';
56 /*********************************************************
57 * Configure the Request
58 *********************************************************/
61 * Set the URL of the Request
63 * @param string $url Url to set
66 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
68 public function setUrl ($url)
71 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
78 * Add a cookie to the request.
80 * @param string $name Name of entry
81 * @param string $value value of entry
84 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
86 public function addCookie ($name, $value)
89 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
92 $this->cookies[$name] = $value;
96 * Add an array of cookies to the request.
97 * The cookie array is of the form
98 * array('cookie_name' => 'cookie_value', 'cookie_name2' => cookie_value2')
100 * @param array $cookies cookies to add
103 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
105 public function addCookies (array $cookies)
108 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
111 $this->cookies = array_merge($this->cookies, $cookies);
115 * Add a header string to the request.
117 * @param string $header Header to add
120 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
122 public function addHeader ($header)
125 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
128 $this->headers[] = $header;
132 * Add an array of header strings to the request.
134 * @param array $headers headers to add
137 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
139 public function addHeaders (array $headers)
142 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
145 $this->headers = array_merge($this->headers, $headers);
149 * Make the request a POST request rather than the default GET request.
152 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
154 public function makePost ()
157 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
160 $this->isPost = true;
164 * Add a POST body to the request
166 * @param string $body body to add
169 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
171 public function setPostBody ($body)
174 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
176 if (!$this->isPost) {
177 throw new CAS_OutOfSequenceException('Cannot add a POST body to a GET request, use makePost() first.');
180 $this->postBody = $body;
184 * Specify the path to an SSL CA certificate to validate the server with.
186 * @param string $caCertPath path to cert
187 * @param bool $validate_cn valdiate CN of certificate
190 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
192 public function setSslCaCert ($caCertPath,$validate_cn=true)
195 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
197 $this->caCertPath = $caCertPath;
198 $this->validateCN = $validate_cn;
201 /*********************************************************
202 * 2. Send the Request
203 *********************************************************/
206 * Perform the request.
208 * @return bool TRUE on success, FALSE on failure.
209 * @throws CAS_OutOfSequenceException If called multiple times.
211 public function send ()
214 throw new CAS_OutOfSequenceException('Request has already been sent cannot send again.');
216 if (is_null($this->url) || !$this->url) {
217 throw new CAS_OutOfSequenceException('A url must be specified via setUrl() before the request can be sent.');
220 return $this->sendRequest();
224 * Send the request and store the results.
226 * @return bool TRUE on success, FALSE on failure.
228 abstract protected function sendRequest ();
231 * Store the response headers.
233 * @param array $headers headers to store
237 protected function storeResponseHeaders (array $headers)
239 $this->_responseHeaders = array_merge($this->_responseHeaders, $headers);
243 * Store a single response header to our array.
245 * @param string $header header to store
249 protected function storeResponseHeader ($header)
251 $this->_responseHeaders[] = $header;
255 * Store the response body.
257 * @param string $body body to store
261 protected function storeResponseBody ($body)
263 $this->_responseBody = $body;
267 * Add a string to our error message.
269 * @param string $message message to add
273 protected function storeErrorMessage ($message)
275 $this->_errorMessage .= $message;
278 /*********************************************************
279 * 3. Access the response
280 *********************************************************/
283 * Answer the headers of the response.
285 * @return array An array of header strings.
286 * @throws CAS_OutOfSequenceException If called before the Request has been sent.
288 public function getResponseHeaders ()
291 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
293 return $this->_responseHeaders;
297 * Answer HTTP status code of the response
300 * @throws CAS_OutOfSequenceException If called before the Request has been sent.
302 public function getResponseStatusCode ()
305 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
308 if (!preg_match('/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/', $this->_responseHeaders[0], $matches)) {
309 throw new CAS_Request_Exception("Bad response, no status code was found in the first line.");
312 return intval($matches[1]);
316 * Answer the body of response.
319 * @throws CAS_OutOfSequenceException If called before the Request has been sent.
321 public function getResponseBody ()
324 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
327 return $this->_responseBody;
331 * Answer a message describing any errors if the request failed.
334 * @throws CAS_OutOfSequenceException If called before the Request has been sent.
336 public function getErrorMessage ()
339 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
341 return $this->_errorMessage;