Post.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Licensed to Jasig under one or more contributor license
  4. * agreements. See the NOTICE file distributed with this work for
  5. * additional information regarding copyright ownership.
  6. *
  7. * Jasig licenses this file to you under the Apache License,
  8. * Version 2.0 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at:
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * PHP Version 5
  20. *
  21. * @file CAS/ProxiedService/Http/Post.php
  22. * @category Authentication
  23. * @package PhpCAS
  24. * @author Adam Franco <afranco@middlebury.edu>
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  26. * @link https://wiki.jasig.org/display/CASC/phpCAS
  27. */
  28. /**
  29. * This class is used to make proxied service requests via the HTTP POST method.
  30. *
  31. * Usage Example:
  32. *
  33. * try {
  34. * $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
  35. * $service->setUrl('http://www.example.com/path/');
  36. * $service->setContentType('text/xml');
  37. * $service->setBody('<?xml version="1.0"?'.'><methodCall><methodName>example.search</methodName></methodCall>');
  38. * $service->send();
  39. * if ($service->getResponseStatusCode() == 200)
  40. * return $service->getResponseBody();
  41. * else
  42. * // The service responded with an error code 404, 500, etc.
  43. * throw new Exception('The service responded with an error.');
  44. *
  45. * } catch (CAS_ProxyTicketException $e) {
  46. * if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE)
  47. * return "Your login has timed out. You need to log in again.";
  48. * else
  49. * // Other proxy ticket errors are from bad request format
  50. * // (shouldn't happen) or CAS server failure (unlikely) so lets just
  51. * // stop if we hit those.
  52. * throw $e;
  53. * } catch (CAS_ProxiedService_Exception $e) {
  54. * // Something prevented the service request from being sent or received.
  55. * // We didn't even get a valid error response (404, 500, etc), so this
  56. * // might be caused by a network error or a DNS resolution failure.
  57. * // We could handle it in some way, but for now we will just stop.
  58. * throw $e;
  59. * }
  60. *
  61. * @class CAS_ProxiedService_Http_Post
  62. * @category Authentication
  63. * @package PhpCAS
  64. * @author Adam Franco <afranco@middlebury.edu>
  65. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  66. * @link https://wiki.jasig.org/display/CASC/phpCAS
  67. */
  68. class CAS_ProxiedService_Http_Post
  69. extends CAS_ProxiedService_Http_Abstract
  70. {
  71. /**
  72. * The content-type of this request
  73. *
  74. * @var string $_contentType
  75. */
  76. private $_contentType;
  77. /**
  78. * The body of the this request
  79. *
  80. * @var string $_body
  81. */
  82. private $_body;
  83. /**
  84. * Set the content type of this POST request.
  85. *
  86. * @param string $contentType content type
  87. *
  88. * @return void
  89. * @throws CAS_OutOfSequenceException If called after the Request has been sent.
  90. */
  91. public function setContentType ($contentType)
  92. {
  93. if ($this->hasBeenSent()) {
  94. throw new CAS_OutOfSequenceException(
  95. 'Cannot set the content type, request already sent.'
  96. );
  97. }
  98. $this->_contentType = $contentType;
  99. }
  100. /**
  101. * Set the body of this POST request.
  102. *
  103. * @param string $body body to set
  104. *
  105. * @return void
  106. * @throws CAS_OutOfSequenceException If called after the Request has been sent.
  107. */
  108. public function setBody ($body)
  109. {
  110. if ($this->hasBeenSent()) {
  111. throw new CAS_OutOfSequenceException(
  112. 'Cannot set the body, request already sent.'
  113. );
  114. }
  115. $this->_body = $body;
  116. }
  117. /**
  118. * Add any other parts of the request needed by concrete classes
  119. *
  120. * @param CAS_Request_RequestInterface $request request interface class
  121. *
  122. * @return void
  123. */
  124. protected function populateRequest (CAS_Request_RequestInterface $request)
  125. {
  126. if (empty($this->_contentType) && !empty($this->_body)) {
  127. throw new CAS_ProxiedService_Exception(
  128. "If you pass a POST body, you must specify a content type via "
  129. .get_class($this).'->setContentType($contentType).'
  130. );
  131. }
  132. $request->makePost();
  133. if (!empty($this->_body)) {
  134. $request->addHeader('Content-Type: '.$this->_contentType);
  135. $request->addHeader('Content-Length: '.strlen($this->_body));
  136. $request->setPostBody($this->_body);
  137. }
  138. }
  139. }
  140. ?>