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

Public Member Functions

 __construct (Socket\Client $client)
 
 send (Array $headers, $content=null)
 
 getResponseContent ()
 
 getResponseHeaders ()
 
 setClient (Socket\Client $client)
 
 getClient ()
 
- Public Member Functions inherited from Hoa\Fastcgi\Connection
 pack ($type, $content, $id=1)
 
 packPairs (Array $pairs)
 
 unpackPairs ($pack)
 

Public Attributes

const REQUEST_BEGIN = 1
 
const REQUEST_ABORD = 2
 
const REQUEST_END = 3
 
const REQUEST_PARAMETERS = 4
 
const STREAM_STDIN = 5
 
const STREAM_STDOUT = 6
 
const STREAM_STDERR = 7
 
const STATUS_COMPLETE = 0
 
const STATUS_CANNOT_MULTIPLEX = 1
 
const STATUS_OVERLOADED = 2
 
const STATUS_UNKNOWN_ROLE = 3
 
- Public Attributes inherited from Hoa\Fastcgi\Connection
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

 read ($length)
 
- Protected Member Functions inherited from Hoa\Fastcgi\Connection
 readPack ()
 
 read ($length)
 

Protected Attributes

 $_client = null
 
 $_content = null
 
 $_headers = []
 

Detailed Description

Class .

A FastCGI client with a responder role. Inspired by PHP SAPI code: php://sapi/cgi/fastcgi.*.

Definition at line 50 of file Responder.php.

Constructor & Destructor Documentation

Hoa\Fastcgi\Responder::__construct ( Socket\Client  $client)

Constructor.

Parameters
\Hoa\Socket\Client$clientClient connection.
Returns
void

Definition at line 162 of file Responder.php.

163  {
164  $this->setClient($client);
165 
166  return;
167  }
setClient(Socket\Client $client)
Definition: Responder.php:314

Here is the call graph for this function:

Member Function Documentation

Hoa\Fastcgi\Responder::getClient ( )

Get client.

Returns

Definition at line 327 of file Responder.php.

328  {
329  return $this->_client;
330  }

Here is the caller graph for this function:

Hoa\Fastcgi\Responder::getResponseContent ( )

Get response content.

Returns
string

Definition at line 282 of file Responder.php.

283  {
284  return $this->_content;
285  }
Hoa\Fastcgi\Responder::getResponseHeaders ( )

Get response headers.

Returns
array

Definition at line 292 of file Responder.php.

293  {
294  return $this->_headers;
295  }
Hoa\Fastcgi\Responder::read (   $length)
protected

Read data.

Parameters
int$lengthLength of data to read.
Returns
string

Definition at line 303 of file Responder.php.

304  {
305  return $this->getClient()->read($length);
306  }

Here is the call graph for this function:

Hoa\Fastcgi\Responder::send ( Array  $headers,
  $content = null 
)

Send data on a FastCGI.

Parameters
array$headersHeaders.
string$contentContent (e.g. key=value for POST).
Returns
string
Exceptions

default: // self::STATUS_COMPLETE break;

Definition at line 181 of file Responder.php.

182  {
183  $client = $this->getClient();
184  $client->connect();
185  $client->setStreamBlocking(true);
186 
187  $parameters = null;
188  $response = null;
189  $request = $this->pack(
190  self::REQUEST_BEGIN,
191  // ┌───────────────────┐
192  // │ “I'm a responder” │
193  // └─────────⌵─────────┘
194  chr(0) . chr(1) . chr((int) $client->isPersistent()) .
195  chr(0) . chr(0) . chr(0) . chr(0) . chr(0)
196  );
197 
198  $parameters .= $this->packPairs($headers);
199 
200  if (null !== $parameters) {
201  $request .= $this->pack(self::REQUEST_PARAMETERS, $parameters);
202  }
203 
204  $request .= $this->pack(self::REQUEST_PARAMETERS, '');
205 
206  if (null !== $content) {
207  $request .= $this->pack(self::STREAM_STDIN, $content);
208  }
209 
210  $request .= $this->pack(self::STREAM_STDIN, '');
211  $client->writeAll($request);
212  $handle = null;
213 
214  do {
215  if (false === $handle = $this->readPack()) {
216  throw new Exception(
217  'Bad request (not a well-formed FastCGI request).',
218  0
219  );
220  }
221 
222  if (self::STREAM_STDOUT === $handle[parent::HEADER_TYPE] ||
223  self::STREAM_STDERR === $handle[parent::HEADER_TYPE]) {
224  $response .= $handle[parent::HEADER_CONTENT];
225  }
226  } while (self::REQUEST_END !== $handle[parent::HEADER_TYPE]);
227 
228  $client->disconnect();
229 
230  switch (ord($handle[parent::HEADER_CONTENT][4])) {
231  case self::STATUS_CANNOT_MULTIPLEX:
232  throw new Exception\CannotMultiplex(
233  'Application %s that you are trying to reach does not ' .
234  'support multiplexing.',
235  0,
236  $this->getClient()->getSocket()->__toString()
237  );
238 
239  break;
240 
241  case self::STATUS_OVERLOADED:
242  throw new Exception\Overloaded(
243  'Application %s is too busy and rejects your request.',
244  1,
245  $this->getClient()->getSocket()->__toString()
246  );
247 
248  break;
249 
250  case self::STATUS_UNKNOWN_ROLE:
251  throw new Exception\UnknownRole(
252  'Server for the application %s returns an unknown role.',
253  2,
254  $this->getClient()->getSocket()->__toString()
255  );
256 
257  break;
258  }
259 
265  $pos = strpos($response, "\r\n\r\n");
266  $headers = substr($response, 0, $pos);
267 
268  foreach (explode("\r\n", $headers) as $header) {
269  $semicolon = strpos($header, ':');
270  $this->_headers[strtolower(trim(substr($header, 0, $semicolon)))]
271  = trim(substr($header, $semicolon + 1));
272  }
273 
274  return $this->_content = substr($response, $pos + 4);
275  }
pack($type, $content, $id=1)
Definition: Connection.php:111
$content
Definition: Hoa.php:119
packPairs(Array $pairs)
Definition: Connection.php:133

Here is the call graph for this function:

Hoa\Fastcgi\Responder::setClient ( Socket\Client  $client)

Set client.

Parameters
\Hoa\Socket\Client$clientClient.
Returns

Definition at line 314 of file Responder.php.

315  {
316  $old = $this->_client;
317  $this->_client = $client;
318 
319  return $old;
320  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Fastcgi\Responder::$_client = null
protected

Definition at line 138 of file Responder.php.

Hoa\Fastcgi\Responder::$_content = null
protected

Definition at line 145 of file Responder.php.

Hoa\Fastcgi\Responder::$_headers = []
protected

Definition at line 152 of file Responder.php.

const Hoa\Fastcgi\Responder::REQUEST_ABORD = 2

Request: abord.

int

Definition at line 64 of file Responder.php.

const Hoa\Fastcgi\Responder::REQUEST_BEGIN = 1

Request: begin.

int

Definition at line 57 of file Responder.php.

const Hoa\Fastcgi\Responder::REQUEST_END = 3

Request: end.

int

Definition at line 71 of file Responder.php.

const Hoa\Fastcgi\Responder::REQUEST_PARAMETERS = 4

Request: parameters.

int

Definition at line 78 of file Responder.php.

const Hoa\Fastcgi\Responder::STATUS_CANNOT_MULTIPLEX = 1

Request status: rejecting a new request; this happens when a Web server sends concurrent requests over one connection to an application that is designed to process one request at a time per connection.

int

Definition at line 115 of file Responder.php.

const Hoa\Fastcgi\Responder::STATUS_COMPLETE = 0

Request status: normal en of request.

int

Definition at line 106 of file Responder.php.

const Hoa\Fastcgi\Responder::STATUS_OVERLOADED = 2

Request status: rejecting a new request; this happens when the application runs out of some resource, e.g. database connections.

int

Definition at line 123 of file Responder.php.

const Hoa\Fastcgi\Responder::STATUS_UNKNOWN_ROLE = 3

Request status: rejecting a new request; this happens when the Web server has specificied a role that is unknown to the application.

int

Definition at line 131 of file Responder.php.

const Hoa\Fastcgi\Responder::STREAM_STDERR = 7

Stream: stderr.

int

Definition at line 99 of file Responder.php.

const Hoa\Fastcgi\Responder::STREAM_STDIN = 5

Stream: stdin.

int

Definition at line 85 of file Responder.php.

const Hoa\Fastcgi\Responder::STREAM_STDOUT = 6

Stream: stdout.

int

Definition at line 92 of file Responder.php.


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