phpCAS version 1.3.6

Abstract.php
Go to the documentation of this file.
1 <?php
2
41 abstract class CAS_ProxiedService_Http_Abstract extends
42 CAS_ProxiedService_Abstract implements CAS_ProxiedService_Http
43 {
49 protected $requestHandler;
50
56 private $_cookieJar;
57
66 public function __construct(CAS_Request_RequestInterface $requestHandler,
67 CAS_CookieJar $cookieJar
68 ) {
69 $this->requestHandler = $requestHandler;
70 $this->_cookieJar = $cookieJar;
71 }
72
77 private $_url;
78
85 public function getServiceUrl()
86 {
87 if (empty($this->_url)) {
88 throw new CAS_ProxiedService_Exception(
89 'No URL set via ' . get_class($this) . '->setUrl($url).'
90 );
91 }
92
93 return $this->_url;
94 }
95
96 /*********************************************************
97 * Configure the Request
98 *********************************************************/
99
108 public function setUrl($url)
109 {
110 if ($this->hasBeenSent()) {
111 throw new CAS_OutOfSequenceException(
112 'Cannot set the URL, request already sent.'
113 );
114 }
115 if (!is_string($url)) {
116 throw new CAS_InvalidArgumentException('$url must be a string.');
117 }
118
119 $this->_url = $url;
120 }
121
122 /*********************************************************
123 * 2. Send the Request
124 *********************************************************/
125
139 public function send()
140 {
141 if ($this->hasBeenSent()) {
142 throw new CAS_OutOfSequenceException(
143 'Cannot send, request already sent.'
144 );
145 }
146
147 phpCAS::traceBegin();
148
149 // Get our proxy ticket and append it to our URL.
150 $this->initializeProxyTicket();
151 $url = $this->getServiceUrl();
152 if (strstr($url, '?') === false) {
153 $url = $url . '?ticket=' . $this->getProxyTicket();
154 } else {
155 $url = $url . '&ticket=' . $this->getProxyTicket();
156 }
157
158 try {
159 $this->makeRequest($url);
160 } catch (Exception $e) {
161 phpCAS::traceEnd();
162 throw $e;
163 }
164 }
165
171 private $_numRequests = 0;
172
178 private $_responseHeaders = array();
179
185 private $_responseStatusCode = '';
186
192 private $_responseBody = '';
193
208 protected function makeRequest($url)
209 {
210 // Verify that we are not in a redirect loop
211 $this->_numRequests++;
212 if ($this->_numRequests > 4) {
213 $message = 'Exceeded the maximum number of redirects (3) in proxied service request.';
214 phpCAS::trace($message);
215 throw new CAS_ProxiedService_Exception($message);
216 }
217
218 // Create a new request.
219 $request = clone $this->requestHandler;
220 $request->setUrl($url);
221
222 // Add any cookies to the request.
223 $request->addCookies($this->_cookieJar->getCookies($url));
224
225 // Add any other parts of the request needed by concrete classes
226 $this->populateRequest($request);
227
228 // Perform the request.
229 phpCAS::trace('Performing proxied service request to \'' . $url . '\'');
230 if (!$request->send()) {
231 $message = 'Could not perform proxied service request to URL`'
232 . $url . '\'. ' . $request->getErrorMessage();
233 phpCAS::trace($message);
234 throw new CAS_ProxiedService_Exception($message);
235 }
236
237 // Store any cookies from the response;
238 $this->_cookieJar->storeCookies($url, $request->getResponseHeaders());
239
240 // Follow any redirects
241 if ($redirectUrl = $this->getRedirectUrl($request->getResponseHeaders())
242 ) {
243 phpCAS::trace('Found redirect:' . $redirectUrl);
244 $this->makeRequest($redirectUrl);
245 } else {
246
247 $this->_responseHeaders = $request->getResponseHeaders();
248 $this->_responseBody = $request->getResponseBody();
249 $this->_responseStatusCode = $request->getResponseStatusCode();
250 }
251 }
252
260 abstract protected function populateRequest(
261 CAS_Request_RequestInterface $request
262 );
263
271 protected function getRedirectUrl(array $responseHeaders)
272 {
273 // Check for the redirect after authentication
274 foreach ($responseHeaders as $header) {
275 if ( preg_match('/^(Location:|URI:)\s*([^\s]+.*)$/', $header, $matches)
276 ) {
277 return trim(array_pop($matches));
278 }
279 }
280 return null;
281 }
282
283 /*********************************************************
284 * 3. Access the response
285 *********************************************************/
286
292 protected function hasBeenSent()
293 {
294 return ($this->_numRequests > 0);
295 }
296
303 public function getResponseHeaders()
304 {
305 if (!$this->hasBeenSent()) {
306 throw new CAS_OutOfSequenceException(
307 'Cannot access response, request not sent yet.'
308 );
309 }
310
311 return $this->_responseHeaders;
312 }
313
320 public function getResponseStatusCode()
321 {
322 if (!$this->hasBeenSent()) {
323 throw new CAS_OutOfSequenceException(
324 'Cannot access response, request not sent yet.'
325 );
326 }
327
328 return $this->_responseStatusCode;
329 }
330
337 public function getResponseBody()
338 {
339 if (!$this->hasBeenSent()) {
340 throw new CAS_OutOfSequenceException(
341 'Cannot access response, request not sent yet.'
342 );
343 }
344
345 return $this->_responseBody;
346 }
347
354 public function getCookies()
355 {
356 return $this->_cookieJar->getCookies($this->getServiceUrl());
357 }
358
359 }
360 ?>
CAS_ProxiedService_Http_Abstract\$_url
$_url
Definition: Abstract.php:77
CAS_ProxiedService_Abstract\initializeProxyTicket
initializeProxyTicket()
Definition: Abstract.php:133
CAS_ProxiedService_Http_Abstract\send
send()
Definition: Abstract.php:139
CAS_ProxiedService_Http_Abstract\$_numRequests
$_numRequests
Definition: Abstract.php:171
CAS_ProxiedService_Http_Abstract\getServiceUrl
getServiceUrl()
Definition: Abstract.php:85
CAS_ProxiedService_Exception
Definition: Exception.php:40
CAS_Request_RequestInterface
Definition: RequestInterface.php:40
CAS_ProxiedService_Abstract
Definition: Abstract.php:41
phpCAS\traceEnd
static traceEnd($res='')
Definition: CAS.php:638
phpCAS\trace
static trace($str)
Definition: CAS.php:579
CAS_ProxiedService_Http_Abstract\makeRequest
makeRequest($url)
Definition: Abstract.php:208
CAS_ProxiedService_Http_Abstract\$requestHandler
$requestHandler
Definition: Abstract.php:49
CAS_OutOfSequenceException
Definition: OutOfSequenceException.php:44
CAS_ProxiedService_Http_Abstract\hasBeenSent
hasBeenSent()
Definition: Abstract.php:292
CAS_InvalidArgumentException
Definition: InvalidArgumentException.php:40
CAS_ProxiedService_Http_Abstract\$_responseHeaders
$_responseHeaders
Definition: Abstract.php:178
CAS_ProxiedService_Http
Definition: Http.php:41
CAS_ProxiedService_Http_Abstract\__construct
__construct(CAS_Request_RequestInterface $requestHandler, CAS_CookieJar $cookieJar)
Definition: Abstract.php:66
CAS_ProxiedService_Http_Abstract
Definition: Abstract.php:41
CAS_ProxiedService_Http_Abstract\setUrl
setUrl($url)
Definition: Abstract.php:108
phpCAS\traceBegin
static traceBegin()
Definition: CAS.php:591
CAS_ProxiedService_Abstract\getProxyTicket
getProxyTicket()
Definition: Abstract.php:83
CAS_ProxiedService_Http_Abstract\$_responseBody
$_responseBody
Definition: Abstract.php:192
CAS_ProxiedService_Http_Abstract\$_responseStatusCode
$_responseStatusCode
Definition: Abstract.php:185
CAS_ProxiedService_Http_Abstract\populateRequest
populateRequest(CAS_Request_RequestInterface $request)
CAS_ProxiedService_Http_Abstract\$_cookieJar
$_cookieJar
Definition: Abstract.php:56
CAS_CookieJar
Definition: CookieJar.php:41