ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CAS_ProxiedService_Http_Abstract Class Reference

This class implements common methods for ProxiedService implementations included with phpCAS. More...

+ Inheritance diagram for CAS_ProxiedService_Http_Abstract:
+ Collaboration diagram for CAS_ProxiedService_Http_Abstract:

Public Member Functions

 __construct (CAS_Request_RequestInterface $requestHandler, CAS_CookieJar $cookieJar)
 Constructor. More...
 
 getServiceUrl ()
 Answer a service identifier (URL) for whom we should fetch a proxy ticket. More...
 
 setUrl ($url)
 Set the URL of the Request. More...
 
 send ()
 Perform the request. More...
 
 getResponseHeaders ()
 Answer the headers of the response. More...
 
 getResponseStatusCode ()
 Answer HTTP status code of the response. More...
 
 getResponseBody ()
 Answer the body of response. More...
 
 getCookies ()
 Answer the cookies from the response. More...
 
- Public Member Functions inherited from CAS_ProxiedService_Abstract
 setProxyTicket ($proxyTicket)
 Register a proxy ticket with the Proxy that it can use when making requests. More...
 
 setCasClient (CAS_Client $casClient)
 Use a particular CAS_Client->initializeProxiedService() rather than the static phpCAS::initializeProxiedService(). More...
 
- Public Member Functions inherited from CAS_ProxiedService
 getServiceUrl ()
 Answer a service identifier (URL) for whom we should fetch a proxy ticket. More...
 
 setProxyTicket ($proxyTicket)
 Register a proxy ticket with the ProxiedService that it can use when making requests. More...
 
- Public Member Functions inherited from CAS_ProxiedService_Http
 setUrl ($url)
 Set the URL of the Request. More...
 
 send ()
 Perform the request. More...
 
 getResponseHeaders ()
 Answer the headers of the response. More...
 
 getResponseBody ()
 Answer the body of response. More...
 

Protected Member Functions

 makeRequest ($url)
 Build and perform a request, following redirects. More...
 
 populateRequest (CAS_Request_RequestInterface $request)
 Add any other parts of the request needed by concrete classes. More...
 
 getRedirectUrl (array $responseHeaders)
 Answer a redirect URL if a redirect header is found, otherwise null. More...
 
 hasBeenSent ()
 Answer true if our request has been sent yet. More...
 
- Protected Member Functions inherited from CAS_ProxiedService_Abstract
 getProxyTicket ()
 Answer the proxy ticket to be used when making requests. More...
 
 initializeProxyTicket ()
 Fetch our proxy ticket. More...
 

Protected Attributes

 $requestHandler
 The HTTP request mechanism talking to the target service. More...
 

Private Attributes

 $_cookieJar
 The storage mechanism for cookies set by the target service. More...
 
 $_url
 The target service url. More...
 
 $_numRequests = 0
 Indicator of the number of requests (including redirects performed. More...
 
 $_responseHeaders = array()
 The response headers. More...
 
 $_responseStatusCode = ''
 The response status code. More...
 
 $_responseBody = ''
 The response headers. More...
 

Detailed Description

This class implements common methods for ProxiedService implementations included with phpCAS.

Definition at line 41 of file Abstract.php.

Constructor & Destructor Documentation

◆ __construct()

CAS_ProxiedService_Http_Abstract::__construct ( CAS_Request_RequestInterface  $requestHandler,
CAS_CookieJar  $cookieJar 
)

Constructor.

Parameters
CAS_Request_RequestInterface$requestHandlerrequest handler object
CAS_CookieJar$cookieJarcookieJar object
Returns
void

Definition at line 65 of file Abstract.php.

References $requestHandler.

68  {
69  $this->requestHandler = $requestHandler;
70  $this->_cookieJar = $cookieJar;
71  }
$requestHandler
The HTTP request mechanism talking to the target service.
Definition: Abstract.php:48

Member Function Documentation

◆ getCookies()

CAS_ProxiedService_Http_Abstract::getCookies ( )

Answer the cookies from the response.

This may include cookies set during redirect responses.

Returns
array An array containing cookies. E.g. array('name' => 'val');

Definition at line 353 of file Abstract.php.

354  {
355  return $this->_cookieJar->getCookies($this->getServiceUrl());
356  }
getServiceUrl()
Answer a service identifier (URL) for whom we should fetch a proxy ticket.
Definition: Abstract.php:85

◆ getRedirectUrl()

CAS_ProxiedService_Http_Abstract::getRedirectUrl ( array  $responseHeaders)
protected

Answer a redirect URL if a redirect header is found, otherwise null.

Parameters
array$responseHeadersresponse header to extract a redirect from
Returns
string or null

Definition at line 270 of file Abstract.php.

271  {
272  // Check for the redirect after authentication
273  foreach ($responseHeaders as $header) {
274  if (preg_match('/^(Location:|URI:)\s*([^\s]+.*)$/', $header, $matches)
275  ) {
276  return trim(array_pop($matches));
277  }
278  }
279  return null;
280  }

◆ getResponseBody()

CAS_ProxiedService_Http_Abstract::getResponseBody ( )

Answer the body of response.

Returns
string
Exceptions
CAS_OutOfSequenceExceptionIf called before the Request has been sent.

Definition at line 336 of file Abstract.php.

337  {
338  if (!$this->hasBeenSent()) {
339  throw new CAS_OutOfSequenceException(
340  'Cannot access response, request not sent yet.'
341  );
342  }
343 
344  return $this->_responseBody;
345  }
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
$_responseBody
The response headers.
Definition: Abstract.php:192

◆ getResponseHeaders()

CAS_ProxiedService_Http_Abstract::getResponseHeaders ( )

Answer the headers of the response.

Returns
array An array of header strings.
Exceptions
CAS_OutOfSequenceExceptionIf called before the Request has been sent.

Definition at line 302 of file Abstract.php.

303  {
304  if (!$this->hasBeenSent()) {
305  throw new CAS_OutOfSequenceException(
306  'Cannot access response, request not sent yet.'
307  );
308  }
309 
311  }
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
$_responseHeaders
The response headers.
Definition: Abstract.php:178

◆ getResponseStatusCode()

CAS_ProxiedService_Http_Abstract::getResponseStatusCode ( )

Answer HTTP status code of the response.

Returns
int
Exceptions
CAS_OutOfSequenceExceptionIf called before the Request has been sent.

Definition at line 319 of file Abstract.php.

320  {
321  if (!$this->hasBeenSent()) {
322  throw new CAS_OutOfSequenceException(
323  'Cannot access response, request not sent yet.'
324  );
325  }
326 
328  }
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
$_responseStatusCode
The response status code.
Definition: Abstract.php:185

◆ getServiceUrl()

CAS_ProxiedService_Http_Abstract::getServiceUrl ( )

Answer a service identifier (URL) for whom we should fetch a proxy ticket.

Returns
string
Exceptions
ExceptionIf no service url is available.

Definition at line 85 of file Abstract.php.

References $_url.

Referenced by send().

86  {
87  if (empty($this->_url)) {
89  'No URL set via ' . get_class($this) . '->setUrl($url).'
90  );
91  }
92 
93  return $this->_url;
94  }
$_url
The target service url.
Definition: Abstract.php:77
An Exception for problems communicating with a proxied service.
Definition: Exception.php:40
+ Here is the caller graph for this function:

◆ hasBeenSent()

CAS_ProxiedService_Http_Abstract::hasBeenSent ( )
protected

Answer true if our request has been sent yet.

Returns
bool

Definition at line 291 of file Abstract.php.

Referenced by send(), CAS_ProxiedService_Http_Post\setBody(), CAS_ProxiedService_Http_Post\setContentType(), and setUrl().

292  {
293  return ($this->_numRequests > 0);
294  }
+ Here is the caller graph for this function:

◆ makeRequest()

CAS_ProxiedService_Http_Abstract::makeRequest (   $url)
protected

Build and perform a request, following redirects.

Parameters
string$urlurl for the request
Returns
void
Exceptions
CAS_ProxyTicketExceptionIf there is a proxy-ticket failure. The code of the Exception will be one of: PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE PHPCAS_SERVICE_PT_FAILURE
CAS_ProxiedService_ExceptionIf there is a failure sending the request to the target service.

Definition at line 208 of file Abstract.php.

References $message, $request, $requestHandler, $url, populateRequest(), and phpCAS\trace().

Referenced by send().

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.';
216  }
217 
218  // Create a new request.
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  $this->_responseHeaders = $request->getResponseHeaders();
247  $this->_responseBody = $request->getResponseBody();
248  $this->_responseStatusCode = $request->getResponseStatusCode();
249  }
250  }
foreach($paths as $path) $request
Definition: asyncclient.php:32
An Exception for problems communicating with a proxied service.
Definition: Exception.php:40
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:599
$requestHandler
The HTTP request mechanism talking to the target service.
Definition: Abstract.php:48
catch(Exception $e) $message
$url
populateRequest(CAS_Request_RequestInterface $request)
Add any other parts of the request needed by concrete classes.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ populateRequest()

CAS_ProxiedService_Http_Abstract::populateRequest ( CAS_Request_RequestInterface  $request)
abstractprotected

Add any other parts of the request needed by concrete classes.

Parameters
CAS_Request_RequestInterface$requestrequest interface object
Returns
void

Referenced by makeRequest().

+ Here is the caller graph for this function:

◆ send()

CAS_ProxiedService_Http_Abstract::send ( )

Perform the request.

Returns
void
Exceptions
CAS_OutOfSequenceExceptionIf called multiple times.
CAS_ProxyTicketExceptionIf there is a proxy-ticket failure. The code of the Exception will be one of: PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE PHPCAS_SERVICE_PT_FAILURE
CAS_ProxiedService_ExceptionIf there is a failure sending the request to the target service.

Definition at line 139 of file Abstract.php.

References $url, CAS_ProxiedService_Abstract\getProxyTicket(), getServiceUrl(), hasBeenSent(), CAS_ProxiedService_Abstract\initializeProxyTicket(), makeRequest(), phpCAS\traceBegin(), and phpCAS\traceEnd().

140  {
141  if ($this->hasBeenSent()) {
142  throw new CAS_OutOfSequenceException(
143  'Cannot send, request already sent.'
144  );
145  }
146 
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) {
162  throw $e;
163  }
164  }
initializeProxyTicket()
Fetch our proxy ticket.
Definition: Abstract.php:132
getServiceUrl()
Answer a service identifier (URL) for whom we should fetch a proxy ticket.
Definition: Abstract.php:85
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:658
makeRequest($url)
Build and perform a request, following redirects.
Definition: Abstract.php:208
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode...
Definition: CAS.php:611
getProxyTicket()
Answer the proxy ticket to be used when making requests.
Definition: Abstract.php:82
$url
+ Here is the call graph for this function:

◆ setUrl()

CAS_ProxiedService_Http_Abstract::setUrl (   $url)

Set the URL of the Request.

Parameters
string$urlurl to set
Returns
void
Exceptions
CAS_OutOfSequenceExceptionIf called after the Request has been sent.

Definition at line 108 of file Abstract.php.

References $url, and hasBeenSent().

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  }
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
Exception that denotes invalid arguments were passed.
$url
+ Here is the call graph for this function:

Field Documentation

◆ $_cookieJar

CAS_CookieJar CAS_ProxiedService_Http_Abstract::$_cookieJar
private

The storage mechanism for cookies set by the target service.

Definition at line 55 of file Abstract.php.

◆ $_numRequests

int CAS_ProxiedService_Http_Abstract::$_numRequests = 0
private

Indicator of the number of requests (including redirects performed.

Definition at line 171 of file Abstract.php.

◆ $_responseBody

string CAS_ProxiedService_Http_Abstract::$_responseBody = ''
private

The response headers.

Definition at line 192 of file Abstract.php.

◆ $_responseHeaders

array CAS_ProxiedService_Http_Abstract::$_responseHeaders = array()
private

The response headers.

Definition at line 178 of file Abstract.php.

◆ $_responseStatusCode

string CAS_ProxiedService_Http_Abstract::$_responseStatusCode = ''
private

The response status code.

Definition at line 185 of file Abstract.php.

◆ $_url

string CAS_ProxiedService_Http_Abstract::$_url
private

The target service url.

Definition at line 77 of file Abstract.php.

Referenced by getServiceUrl().

◆ $requestHandler

CAS_Request_RequestInterface CAS_ProxiedService_Http_Abstract::$requestHandler
protected

The HTTP request mechanism talking to the target service.

Definition at line 48 of file Abstract.php.

Referenced by __construct(), and makeRequest().


The documentation for this class was generated from the following file: