You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Connection.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. 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. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. abstract class Connection
  48. {
  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. * @param int $type Packet's type.
  95. * @param string $content Content.
  96. * @param id $id Packet's ID.
  97. * @return string
  98. */
  99. public function pack($type, $content, $id = 1)
  100. {
  101. $length = strlen($content);
  102. return
  103. chr(1) . // version
  104. chr($type) . // type
  105. chr(($id >> 8) & 0xff) . // ID B1
  106. chr($id & 0xff) . // ID B0
  107. chr(($length >> 8) & 0xff) . // length B1
  108. chr($length & 0xff) . // length b0
  109. chr(0) . // padding length
  110. chr(0) . // reserved
  111. $content;
  112. }
  113. /**
  114. * Pack pairs (key/value).
  115. *
  116. * @param array $pairs Keys/values array.
  117. * @return string
  118. */
  119. public function packPairs(array $pairs)
  120. {
  121. $out = null;
  122. foreach ($pairs as $key => $value) {
  123. foreach ([$key, $value] as $handle) {
  124. $length = strlen($handle);
  125. // B0
  126. if ($length < 0x80) {
  127. $out .= chr($length);
  128. }
  129. // B3 & B2 & B1 & B0
  130. else {
  131. $out .=
  132. chr(($length >> 24) | 0x80) .
  133. chr(($length >> 16) & 0xff) .
  134. chr(($length >> 8) & 0xff) .
  135. chr($length & 0xff);
  136. }
  137. }
  138. $out .= $key . $value;
  139. }
  140. return $out;
  141. }
  142. /**
  143. * Unpack pairs (key/value).
  144. *
  145. * @param string $pack Packet to unpack.
  146. * @return string
  147. */
  148. public function unpackPairs($pack)
  149. {
  150. if (null === $length) {
  151. $length = strlen($pack);
  152. }
  153. $out = [];
  154. $i = 0;
  155. for ($i = 0; $length >= $i; $i += $keyLength + $valueLength) {
  156. $keyLength = ord($pack[$i++]);
  157. if ($keyLength >= 0x80) {
  158. $keyLength =
  159. ($keyLength & 0x7f << 24)
  160. | (ord($pack[$i++]) << 16)
  161. | (ord($pack[$i++]) << 8)
  162. | ord($pack[$i++]);
  163. }
  164. $valueLength = ord($pack[$i++]);
  165. if ($valueLength >= 0x80) {
  166. $valueLength =
  167. ($valueLength & 0x7f << 24)
  168. | (ord($pack[$i++]) << 16)
  169. | (ord($pack[$i++]) << 8)
  170. | ord($pack[$i++]);
  171. }
  172. $out[substr($pack, $i, $keyLength)]
  173. = substr($pack, $i + $keyLength, $valueLength);
  174. }
  175. return $out;
  176. }
  177. /**
  178. * Read a packet.
  179. *
  180. * @return array
  181. */
  182. protected function readPack()
  183. {
  184. if ((null === $pack = $this->read(8)) ||
  185. empty($pack)) {
  186. return false;
  187. }
  188. $headers = [
  189. self::HEADER_VERSION => ord($pack[0]),
  190. self::HEADER_TYPE => ord($pack[1]),
  191. self::HEADER_REQUEST_ID => (ord($pack[2]) << 8) +
  192. ord($pack[3]),
  193. self::HEADER_CONTENT_LENGTH => (ord($pack[4]) << 8) +
  194. ord($pack[5]),
  195. self::HEADER_PADDING_LENGTH => ord($pack[6]),
  196. self::HEADER_RESERVED => ord($pack[7]),
  197. self::HEADER_CONTENT => null
  198. ];
  199. $length =
  200. $headers[self::HEADER_CONTENT_LENGTH] +
  201. $headers[self::HEADER_PADDING_LENGTH];
  202. if (0 === $length) {
  203. return $headers;
  204. }
  205. $headers[self::HEADER_CONTENT] = substr(
  206. $this->read($length),
  207. 0,
  208. $headers[self::HEADER_CONTENT_LENGTH]
  209. );
  210. return $headers;
  211. }
  212. /**
  213. * Read data.
  214. *
  215. * @param int $length Length of data to read.
  216. * @return string
  217. */
  218. abstract protected function read($length);
  219. }