2
0

Request.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Amf_Parse_InputStream */
  21. require_once 'Zend/Amf/Parse/InputStream.php';
  22. /** Zend_Amf_Parse_Amf0_Deserializer */
  23. require_once 'Zend/Amf/Parse/Amf0/Deserializer.php';
  24. /** Zend_Amf_Constants */
  25. require_once 'Zend/Amf/Constants.php';
  26. /** Zend_Amf_Value_MessageHeader */
  27. require_once 'Zend/Amf/Value/MessageHeader.php';
  28. /** Zend_Amf_Value_MessageBody */
  29. require_once 'Zend/Amf/Value/MessageBody.php';
  30. /**
  31. * Handle the incoming AMF request by deserializing the data to php object
  32. * types and storing the data for Zend_Amf_Server to handle for processing.
  33. *
  34. * @todo Currently not checking if the object needs to be Type Mapped to a server object.
  35. * @package Zend_Amf
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Amf_Request
  40. {
  41. /**
  42. * @var int AMF client type (AMF0, AMF3)
  43. */
  44. protected $_clientType = 0; // default AMF0
  45. /**
  46. * @var array Message bodies
  47. */
  48. protected $_bodies = array();
  49. /**
  50. * @var array Message headers
  51. */
  52. protected $_headers = array();
  53. /**
  54. * @var int Message encoding to use for objects in response
  55. */
  56. protected $_objectEncoding = 0;
  57. /**
  58. * @var Zend_Amf_Parse_InputStream
  59. */
  60. protected $_inputStream;
  61. /**
  62. * @var Zend_Amf_Parse_AMF0_Deserializer
  63. */
  64. protected $_deserializer;
  65. /**
  66. * Time of the request
  67. * @var mixed
  68. */
  69. protected $_time;
  70. /**
  71. * Prepare the AMF InputStream for parsing.
  72. *
  73. * @param string $request
  74. * @return Zend_Amf_Request
  75. */
  76. public function initialize($request)
  77. {
  78. $this->_inputStream = new Zend_Amf_Parse_InputStream($request);
  79. $this->_deserializer = new Zend_Amf_Parse_Amf0_Deserializer($this->_inputStream);
  80. $this->readMessage($this->_inputStream);
  81. return $this;
  82. }
  83. /**
  84. * Takes the raw AMF input stream and converts it into valid PHP objects
  85. *
  86. * @param Zend_Amf_Parse_InputStream
  87. * @return Zend_Amf_Request
  88. */
  89. public function readMessage(Zend_Amf_Parse_InputStream $stream)
  90. {
  91. $clientVersion = $stream->readUnsignedShort();
  92. if (($clientVersion != Zend_Amf_Constants::AMF0_OBJECT_ENCODING)
  93. && ($clientVersion != Zend_Amf_Constants::AMF3_OBJECT_ENCODING)
  94. && ($clientVersion != Zend_Amf_Constants::FMS_OBJECT_ENCODING)
  95. ) {
  96. require_once 'Zend/Amf/Exception.php';
  97. throw new Zend_Amf_Exception('Unknown Player Version ' . $clientVersion);
  98. }
  99. $this->_bodies = array();
  100. $this->_headers = array();
  101. $headerCount = $stream->readInt();
  102. // Iterate through the AMF envelope header
  103. while ($headerCount--) {
  104. $this->_headers[] = $this->readHeader();
  105. }
  106. // Iterate through the AMF envelope body
  107. $bodyCount = $stream->readInt();
  108. while ($bodyCount--) {
  109. $this->_bodies[] = $this->readBody();
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Deserialize a message header from the input stream.
  115. *
  116. * A message header is structured as:
  117. * - NAME String
  118. * - MUST UNDERSTAND Boolean
  119. * - LENGTH Int
  120. * - DATA Object
  121. *
  122. * @return Zend_Amf_Value_MessageHeader
  123. */
  124. public function readHeader()
  125. {
  126. $name = $this->_inputStream->readUTF();
  127. $mustRead = (bool)$this->_inputStream->readByte();
  128. $length = $this->_inputStream->readLong();
  129. try {
  130. $data = $this->_deserializer->readTypeMarker();
  131. } catch (Exception $e) {
  132. require_once 'Zend/Amf/Exception.php';
  133. throw new Zend_Amf_Exception('Unable to parse ' . $name . ' header data: ' . $e->getMessage() . ' '. $e->getLine());
  134. }
  135. $header = new Zend_Amf_Value_MessageHeader($name, $mustRead, $data, $length);
  136. return $header;
  137. }
  138. /**
  139. * Deserialize a message body from the input stream
  140. *
  141. * @return Zend_Amf_Value_MessageBody
  142. */
  143. public function readBody()
  144. {
  145. $targetURI = $this->_inputStream->readUTF();
  146. $responseURI = $this->_inputStream->readUTF();
  147. $length = $this->_inputStream->readLong();
  148. try {
  149. $data = $this->_deserializer->readTypeMarker();
  150. } catch (Exception $e) {
  151. require_once 'Zend/Amf/Exception.php';
  152. throw new Zend_Amf_Exception('Unable to parse ' . $targetURI . ' body data ' . $e->getMessage());
  153. }
  154. // Check for AMF3 objectEncoding
  155. if ($this->_deserializer->getObjectEncoding() == Zend_Amf_Constants::AMF3_OBJECT_ENCODING) {
  156. /*
  157. * When and AMF3 message is sent to the server it is nested inside
  158. * an AMF0 array called Content. The following code gets the object
  159. * out of the content array and sets it as the message data.
  160. */
  161. if(is_array($data) && $data[0] instanceof Zend_Amf_Value_Messaging_AbstractMessage){
  162. $data = $data[0];
  163. }
  164. // set the encoding so we return our message in AMF3
  165. $this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  166. }
  167. $body = new Zend_Amf_Value_MessageBody($targetURI, $responseURI, $data);
  168. return $body;
  169. }
  170. /**
  171. * Return an array of the body objects that were found in the amf request.
  172. *
  173. * @return array {target, response, length, content}
  174. */
  175. public function getAmfBodies()
  176. {
  177. return $this->_bodies;
  178. }
  179. /**
  180. * Accessor to private array of message bodies.
  181. *
  182. * @param Zend_Amf_Value_MessageBody $message
  183. * @return Zend_Amf_Request
  184. */
  185. public function addAmfBody(Zend_Amf_Value_MessageBody $message)
  186. {
  187. $this->_bodies[] = $message;
  188. return $this;
  189. }
  190. /**
  191. * Return an array of headers that were found in the amf request.
  192. *
  193. * @return array {operation, mustUnderstand, length, param}
  194. */
  195. public function getAmfHeaders()
  196. {
  197. return $this->_headers;
  198. }
  199. /**
  200. * Return the either 0 or 3 for respect AMF version
  201. *
  202. * @return int
  203. */
  204. public function getObjectEncoding()
  205. {
  206. return $this->_objectEncoding;
  207. }
  208. /**
  209. * Set the object response encoding
  210. *
  211. * @param mixed $int
  212. * @return Zend_Amf_Request
  213. */
  214. public function setObjectEncoding($int)
  215. {
  216. $this->_objectEncoding = $int;
  217. return $this;
  218. }
  219. }