PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Connection.php

https://github.com/jeremyFreeAgent/Fastcgi
PHP | 250 lines | 90 code | 42 blank | 118 comment | 7 complexity | bb2abf21c094bce45f96511aa0a6cf4f MD5 | raw file
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2013, Ivan Enderlin. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Fastcgi {
  36. /**
  37. * Class \Hoa\Fastcgi\Connection.
  38. *
  39. * A FastCGI connection; mainly pack & unpack methods.
  40. * Specification can be found here:
  41. * http://fastcgi.com/devkit/doc/fcgi-spec.html.
  42. * Inspired by PHP SAPI code: php://sapi/cgi/fastcgi.*.
  43. *
  44. * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
  45. * @copyright Copyright © 2007-2013 Ivan Enderlin.
  46. * @license New BSD License
  47. */
  48. abstract class Connection {
  49. /**
  50. * Header: version.
  51. *
  52. * @const int
  53. */
  54. const HEADER_VERSION = 0;
  55. /**
  56. * Header: type.
  57. *
  58. * @const int
  59. */
  60. const HEADER_TYPE = 1;
  61. /**
  62. * Header: request ID.
  63. *
  64. * @const int
  65. */
  66. const HEADER_REQUEST_ID = 2;
  67. /**
  68. * Header: content length.
  69. *
  70. * @const int
  71. */
  72. const HEADER_CONTENT_LENGTH = 3;
  73. /**
  74. * Header: padding length.
  75. *
  76. * @const int
  77. */
  78. const HEADER_PADDING_LENGTH = 4;
  79. /**
  80. * Header: reserved.
  81. *
  82. * @const int
  83. */
  84. const HEADER_RESERVED = 5;
  85. /**
  86. * Header: content.
  87. *
  88. * @const int
  89. */
  90. const HEADER_CONTENT = 6;
  91. /**
  92. * Pack data to a packet.
  93. *
  94. * @access public
  95. * @param int $type Packet's type.
  96. * @param string $content Content.
  97. * @param id $id Packet's ID.
  98. * @return string
  99. */
  100. public function pack ( $type, $content, $id = 1 ) {
  101. $length = strlen($content);
  102. return chr(1) . // version
  103. chr($type) . // type
  104. chr(($id >> 8) & 0xff) . // ID B1
  105. chr( $id & 0xff) . // ID B0
  106. chr(($length >> 8) & 0xff) . // length B1
  107. chr( $length & 0xff) . // length b0
  108. chr(0) . // padding length
  109. chr(0) . // reserved
  110. $content;
  111. }
  112. /**
  113. * Pack pairs (key/value).
  114. *
  115. * @access public
  116. * @param array $pairs Keys/values array.
  117. * @return string
  118. */
  119. public function packPairs ( Array $pairs ) {
  120. $out = null;
  121. foreach($pairs as $key => $value) {
  122. foreach(array($key, $value) as $handle) {
  123. $length = strlen($handle);
  124. // B0
  125. if($length < 0x80)
  126. $out .= chr($length);
  127. // B3 & B2 & B1 & B0
  128. else
  129. $out .= chr(($length >> 24) | 0x80) .
  130. chr(($length >> 16) & 0xff) .
  131. chr(($length >> 8) & 0xff) .
  132. chr( $length & 0xff);
  133. }
  134. $out .= $key . $value;
  135. }
  136. return $out;
  137. }
  138. /**
  139. * Unpack pairs (key/value).
  140. *
  141. * @access public
  142. * @param string $pack Packet to unpack.
  143. * @return string
  144. */
  145. public function unpackPairs ( $pack ) {
  146. if(null === $length)
  147. $length = strlen($pack);
  148. $out = array();
  149. $i = 0;
  150. for($i = 0; $length >= $i; $i += $keyLength + $valueLength) {
  151. $keyLength = ord($pack[$i++]);
  152. if($keyLength >= 0x80)
  153. $keyLength = ($keyLength & 0x7f << 24)
  154. | (ord($pack[$i++]) << 16)
  155. | (ord($pack[$i++]) << 8)
  156. | ord($pack[$i++]);
  157. $valueLength = ord($pack[$i++]);
  158. if($valueLength >= 0x80)
  159. $valueLength = ($valueLength & 0x7f << 24)
  160. | (ord($pack[$i++]) << 16)
  161. | (ord($pack[$i++]) << 8)
  162. | ord($pack[$i++]);
  163. $out[substr($pack, $i, $keyLength)]
  164. = substr($pack, $i + $keyLength, $valueLength);
  165. }
  166. return $out;
  167. }
  168. /**
  169. * Read a packet.
  170. *
  171. * @access public
  172. * @return array
  173. */
  174. protected function readPack ( ) {
  175. if(null === $pack = $this->read(8))
  176. return false;
  177. $headers = array(
  178. self::HEADER_VERSION => ord($pack[0]),
  179. self::HEADER_TYPE => ord($pack[1]),
  180. self::HEADER_REQUEST_ID => (ord($pack[2]) << 8) +
  181. ord($pack[3]),
  182. self::HEADER_CONTENT_LENGTH => (ord($pack[4]) << 8) +
  183. ord($pack[5]),
  184. self::HEADER_PADDING_LENGTH => ord($pack[6]),
  185. self::HEADER_RESERVED => ord($pack[7]),
  186. self::HEADER_CONTENT => null
  187. );
  188. $length = $headers[self::HEADER_CONTENT_LENGTH] +
  189. $headers[self::HEADER_PADDING_LENGTH];
  190. if(0 === $length)
  191. return $headers;
  192. $headers[self::HEADER_CONTENT] = substr(
  193. $this->read($length),
  194. 0,
  195. $headers[self::HEADER_CONTENT_LENGTH]
  196. );
  197. return $headers;
  198. }
  199. /**
  200. * Read data.
  201. *
  202. * @access protected
  203. * @param int $length Length of data to read.
  204. * @return string
  205. */
  206. abstract protected function read ( $length );
  207. }
  208. }