Request.php 7.2 KB

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