Hoa central
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
Hoa\Fastcgi\Connection Class Reference
Inheritance diagram for Hoa\Fastcgi\Connection:

Public Member Functions

 pack ($type, $content, $id=1)
 
 packPairs (Array $pairs)
 
 unpackPairs ($pack)
 

Public Attributes

const HEADER_VERSION = 0
 
const HEADER_TYPE = 1
 
const HEADER_REQUEST_ID = 2
 
const HEADER_CONTENT_LENGTH = 3
 
const HEADER_PADDING_LENGTH = 4
 
const HEADER_RESERVED = 5
 
const HEADER_CONTENT = 6
 

Protected Member Functions

 readPack ()
 
 read ($length)
 

Detailed Description

Class .

A FastCGI connection; mainly pack & unpack methods. Specification can be found here: http://fastcgi.com/devkit/doc/fcgi-spec.html. Inspired by PHP SAPI code: php://sapi/cgi/fastcgi.*.

Definition at line 50 of file Connection.php.

Member Function Documentation

Hoa\Fastcgi\Connection::pack (   $type,
  $content,
  $id = 1 
)

Pack data to a packet.

Parameters
int$typePacket's type.
string$contentContent.
id$idPacket's ID.
Returns
string

Definition at line 111 of file Connection.php.

112  {
113  $length = strlen($content);
114 
115  return
116  chr(1) . // version
117  chr($type) . // type
118  chr(($id >> 8) & 0xff) . // ID B1
119  chr($id & 0xff) . // ID B0
120  chr(($length >> 8) & 0xff) . // length B1
121  chr($length & 0xff) . // length b0
122  chr(0) . // padding length
123  chr(0) . // reserved
124  $content;
125  }
$content
Definition: Hoa.php:119

Here is the caller graph for this function:

Hoa\Fastcgi\Connection::packPairs ( Array  $pairs)

Pack pairs (key/value).

Parameters
array$pairsKeys/values array.
Returns
string

Definition at line 133 of file Connection.php.

134  {
135  $out = null;
136 
137  foreach ($pairs as $key => $value) {
138  foreach ([$key, $value] as $handle) {
139  $length = strlen($handle);
140 
141  // B0
142  if ($length < 0x80) {
143  $out .= chr($length);
144  }
145  // B3 & B2 & B1 & B0
146  else {
147  $out .=
148  chr(($length >> 24) | 0x80) .
149  chr(($length >> 16) & 0xff) .
150  chr(($length >> 8) & 0xff) .
151  chr($length & 0xff);
152  }
153  }
154 
155  $out .= $key . $value;
156  }
157 
158  return $out;
159  }

Here is the caller graph for this function:

Hoa\Fastcgi\Connection::read (   $length)
abstractprotected

Read data.

Parameters
int$lengthLength of data to read.
Returns
string

Here is the caller graph for this function:

Hoa\Fastcgi\Connection::readPack ( )
protected

Read a packet.

Returns
array

Definition at line 209 of file Connection.php.

210  {
211  if ((null === $pack = $this->read(8)) ||
212  empty($pack)) {
213  return false;
214  }
215 
216  $headers = [
217  self::HEADER_VERSION => ord($pack[0]),
218  self::HEADER_TYPE => ord($pack[1]),
219  self::HEADER_REQUEST_ID => (ord($pack[2]) << 8) +
220  ord($pack[3]),
221  self::HEADER_CONTENT_LENGTH => (ord($pack[4]) << 8) +
222  ord($pack[5]),
223  self::HEADER_PADDING_LENGTH => ord($pack[6]),
224  self::HEADER_RESERVED => ord($pack[7]),
225  self::HEADER_CONTENT => null
226  ];
227  $length =
228  $headers[self::HEADER_CONTENT_LENGTH] +
229  $headers[self::HEADER_PADDING_LENGTH];
230 
231  if (0 === $length) {
232  return $headers;
233  }
234 
235  $headers[self::HEADER_CONTENT] = substr(
236  $this->read($length),
237  0,
238  $headers[self::HEADER_CONTENT_LENGTH]
239  );
240 
241  return $headers;
242  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Fastcgi\Connection::unpackPairs (   $pack)

Unpack pairs (key/value).

Parameters
string$packPacket to unpack.
Returns
string

Definition at line 167 of file Connection.php.

168  {
169  if (null === $length) {
170  $length = strlen($pack);
171  }
172 
173  $out = [];
174  $i = 0;
175 
176  for ($i = 0; $length >= $i; $i += $keyLength + $valueLength) {
177  $keyLength = ord($pack[$i++]);
178 
179  if ($keyLength >= 0x80) {
180  $keyLength =
181  ($keyLength & 0x7f << 24)
182  | (ord($pack[$i++]) << 16)
183  | (ord($pack[$i++]) << 8)
184  | ord($pack[$i++]);
185  }
186 
187  $valueLength = ord($pack[$i++]);
188 
189  if ($valueLength >= 0x80) {
190  $valueLength =
191  ($valueLength & 0x7f << 24)
192  | (ord($pack[$i++]) << 16)
193  | (ord($pack[$i++]) << 8)
194  | ord($pack[$i++]);
195  }
196 
197  $out[substr($pack, $i, $keyLength)]
198  = substr($pack, $i + $keyLength, $valueLength);
199  }
200 
201  return $out;
202  }

Member Data Documentation

const Hoa\Fastcgi\Connection::HEADER_CONTENT = 6

Header: content.

int

Definition at line 99 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_CONTENT_LENGTH = 3

Header: content length.

int

Definition at line 78 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_PADDING_LENGTH = 4

Header: padding length.

int

Definition at line 85 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_REQUEST_ID = 2

Header: request ID.

int

Definition at line 71 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_RESERVED = 5

Header: reserved.

int

Definition at line 92 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_TYPE = 1

Header: type.

int

Definition at line 64 of file Connection.php.

const Hoa\Fastcgi\Connection::HEADER_VERSION = 0

Header: version.

int

Definition at line 57 of file Connection.php.


The documentation for this class was generated from the following file: