Browse Source

Move to PSR-1 and PSR-2.

tags/2.15.05.29
Ivan Enderlin 4 years ago
parent
commit
9c228ff072

+ 64
- 62
Connection.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -44,13 +44,11 @@ namespace Hoa\Fastcgi;
* http://fastcgi.com/devkit/doc/fcgi-spec.html.
* Inspired by PHP SAPI code: php://sapi/cgi/fastcgi.*.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

abstract class Connection {

abstract class Connection
{
/**
* Header: version.
*
@@ -105,53 +103,53 @@ abstract class Connection {
/**
* Pack data to a packet.
*
* @access public
* @param int $type Packet's type.
* @param string $content Content.
* @param id $id Packet's ID.
* @return string
*/
public function pack ( $type, $content, $id = 1 ) {
public function pack($type, $content, $id = 1)
{
$length = strlen($content);

return chr(1) . // version
chr($type) . // type
chr(($id >> 8) & 0xff) . // ID B1
chr( $id & 0xff) . // ID B0
chr(($length >> 8) & 0xff) . // length B1
chr( $length & 0xff) . // length b0
chr(0) . // padding length
chr(0) . // reserved
$content;
return
chr(1) . // version
chr($type) . // type
chr(($id >> 8) & 0xff) . // ID B1
chr($id & 0xff) . // ID B0
chr(($length >> 8) & 0xff) . // length B1
chr($length & 0xff) . // length b0
chr(0) . // padding length
chr(0) . // reserved
$content;
}

/**
* Pack pairs (key/value).
*
* @access public
* @param array $pairs Keys/values array.
* @return string
*/
public function packPairs ( Array $pairs ) {
public function packPairs(Array $pairs)
{
$out = null;

foreach($pairs as $key => $value) {

foreach([$key, $value] as $handle) {

foreach ($pairs as $key => $value) {
foreach ([$key, $value] as $handle) {
$length = strlen($handle);

// B0
if($length < 0x80)
if ($length < 0x80) {
$out .= chr($length);
}
// B3 & B2 & B1 & B0
else
$out .= chr(($length >> 24) | 0x80) .
chr(($length >> 16) & 0xff) .
chr(($length >> 8) & 0xff) .
chr( $length & 0xff);
else {
$out .=
chr(($length >> 24) | 0x80) .
chr(($length >> 16) & 0xff) .
chr(($length >> 8) & 0xff) .
chr($length & 0xff);
}
}

$out .= $key . $value;
@@ -163,35 +161,38 @@ abstract class Connection {
/**
* Unpack pairs (key/value).
*
* @access public
* @param string $pack Packet to unpack.
* @return string
*/
public function unpackPairs ( $pack ) {
if(null === $length)
public function unpackPairs($pack)
{
if (null === $length) {
$length = strlen($pack);
}

$out = [];
$i = 0;

for($i = 0; $length >= $i; $i += $keyLength + $valueLength) {

for ($i = 0; $length >= $i; $i += $keyLength + $valueLength) {
$keyLength = ord($pack[$i++]);

if($keyLength >= 0x80)
$keyLength = ($keyLength & 0x7f << 24)
| (ord($pack[$i++]) << 16)
| (ord($pack[$i++]) << 8)
| ord($pack[$i++]);
if ($keyLength >= 0x80) {
$keyLength =
($keyLength & 0x7f << 24)
| (ord($pack[$i++]) << 16)
| (ord($pack[$i++]) << 8)
| ord($pack[$i++]);
}

$valueLength = ord($pack[$i++]);

if($valueLength >= 0x80)
$valueLength = ($valueLength & 0x7f << 24)
| (ord($pack[$i++]) << 16)
| (ord($pack[$i++]) << 8)
| ord($pack[$i++]);
if ($valueLength >= 0x80) {
$valueLength =
($valueLength & 0x7f << 24)
| (ord($pack[$i++]) << 16)
| (ord($pack[$i++]) << 8)
| ord($pack[$i++]);
}

$out[substr($pack, $i, $keyLength)]
= substr($pack, $i + $keyLength, $valueLength);
@@ -203,31 +204,33 @@ abstract class Connection {
/**
* Read a packet.
*
* @access public
* @return array
*/
protected function readPack ( ) {
if( (null === $pack = $this->read(8))
|| empty($pack))
protected function readPack()
{
if ((null === $pack = $this->read(8)) ||
empty($pack)) {
return false;
}

$headers = [
self::HEADER_VERSION => ord($pack[0]),
self::HEADER_TYPE => ord($pack[1]),
self::HEADER_VERSION => ord($pack[0]),
self::HEADER_TYPE => ord($pack[1]),
self::HEADER_REQUEST_ID => (ord($pack[2]) << 8) +
ord($pack[3]),
self::HEADER_CONTENT_LENGTH => (ord($pack[4]) << 8) +
ord($pack[5]),
self::HEADER_PADDING_LENGTH => ord($pack[6]),
self::HEADER_RESERVED => ord($pack[7]),
self::HEADER_CONTENT => null
self::HEADER_PADDING_LENGTH => ord($pack[6]),
self::HEADER_RESERVED => ord($pack[7]),
self::HEADER_CONTENT => null
];
$length = $headers[self::HEADER_CONTENT_LENGTH] +
$headers[self::HEADER_PADDING_LENGTH];
$length =
$headers[self::HEADER_CONTENT_LENGTH] +
$headers[self::HEADER_PADDING_LENGTH];

if(0 === $length)
if (0 === $length) {
return $headers;
}

$headers[self::HEADER_CONTENT] = substr(
$this->read($length),
@@ -241,9 +244,8 @@ abstract class Connection {
/**
* Read data.
*
* @access protected
* @param int $length Length of data to read.
* @return string
*/
abstract protected function read ( $length );
abstract protected function read($length);
}

+ 1
- 4
Documentation/En/Index.xyl View File

@@ -157,11 +157,8 @@ var_dump($fastcgi->getResponseContent());
<p>The best way to catch all the exceptions is to catch
<code>Hoa\Fastcgi\Exception\Exception</code>:</p>
<pre><code class="language-php">try {

$fastcgi->send(…);
}
catch ( Hoa\Fastcgi\Exception\Exception $e ) {

} catch (Hoa\Fastcgi\Exception\Exception $e) {
// compute $e.
}</code></pre>
<p>So far, the most used CGI servers support the multiplexing, high load and

+ 1
- 4
Documentation/Fr/Index.xyl View File

@@ -162,11 +162,8 @@ var_dump($fastcgi->getResponseContent());
<p>Le meilleur moyen de capturer toutes les exceptions est de capturer
<code>Hoa\Fastcgi\Exception\Exception</code> :</p>
<pre><code class="language-php">try {

$fastcgi->send(…);
}
catch ( Hoa\Fastcgi\Exception\Exception $e ) {

} catch (Hoa\Fastcgi\Exception\Exception $e) {
// compute $e.
}</code></pre>
<p>Actuellement, les serveurs CGI les plus répandus supportent le

+ 5
- 5
Exception/CannotMultiplex.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -44,9 +44,9 @@ namespace Hoa\Fastcgi\Exception;
* to an application that is designed to process one request at a time per
* connection.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

class CannotMultiplex extends Exception { }
class CannotMultiplex extends Exception
{
}

+ 5
- 5
Exception/Exception.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,12 +43,12 @@ use Hoa\Core;
*
* Extending the \Hoa\Core\Exception class.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

class Exception extends Core\Exception { }
class Exception extends Core\Exception
{
}

/**
* Flex entity.

+ 5
- 5
Exception/Overloaded.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,9 +43,9 @@ namespace Hoa\Fastcgi\Exception;
* This happens when the application runs out of some resource, e.g. database
* connections.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

class Overloaded extends Exception { }
class Overloaded extends Exception
{
}

+ 5
- 5
Exception/UnknownRole.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,9 +43,9 @@ namespace Hoa\Fastcgi\Exception;
* This happens when the Web server has specified a role that is unknown to the
* application.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

class UnknownRole extends Exception { }
class UnknownRole extends Exception
{
}

+ 56
- 54
Responder.php View File

@@ -8,7 +8,7 @@
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
* Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -44,13 +44,11 @@ use Hoa\Socket;
* A FastCGI client with a responder role.
* Inspired by PHP SAPI code: php://sapi/cgi/fastcgi.*.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/

class Responder extends Connection {

class Responder extends Connection
{
/**
* Request: begin.
*
@@ -135,21 +133,21 @@ class Responder extends Connection {
/**
* Client socket connection.
*
* @var \Hoa\Socket\Client object
* @var \Hoa\Socket\Client
*/
protected $_client = null;

/**
* Response: content.
*
* @var \Hoa\Fastcgi\Responder string
* @var string
*/
protected $_content = null;

/**
* Response: headers.
*
* @var \Hoa\Fastcgi\Responder array
* @var array
*/
protected $_headers = [];

@@ -158,12 +156,11 @@ class Responder extends Connection {
/**
* Constructor.
*
* @access public
* @param \Hoa\Socket\Client $client Client connection.
* @return void
*/
public function __construct ( Socket\Client $client ) {
public function __construct(Socket\Client $client)
{
$this->setClient($client);

return;
@@ -172,18 +169,17 @@ class Responder extends Connection {
/**
* Send data on a FastCGI.
*
* @access public
* @param array $headers Headers.
* @param string $content Content (e.g. key=value for POST).
* @return string
* @throw \Hoa\Socket\Exception
* @throw \Hoa\Fastcgi\Exception
* @throw \Hoa\Fastcgi\Exception\CannotMultiplex
* @throw \Hoa\Fastcgi\Exception\Overloaded
* @throw \Hoa\Fastcgi\Exception\UnknownRole
* @throws \Hoa\Socket\Exception
* @throws \Hoa\Fastcgi\Exception
* @throws \Hoa\Fastcgi\Exception\CannotMultiplex
* @throws \Hoa\Fastcgi\Exception\Overloaded
* @throws \Hoa\Fastcgi\Exception\UnknownRole
*/
public function send ( Array $headers, $content = null ) {
public function send(Array $headers, $content = null)
{
$client = $this->getClient();
$client->connect();
$client->setStreamBlocking(true);
@@ -201,52 +197,64 @@ class Responder extends Connection {

$parameters .= $this->packPairs($headers);

if(null !== $parameters)
if (null !== $parameters) {
$request .= $this->pack(self::REQUEST_PARAMETERS, $parameters);
}

$request .= $this->pack(self::REQUEST_PARAMETERS, '');

if(null !== $content)
if (null !== $content) {
$request .= $this->pack(self::STREAM_STDIN, $content);
}

$request .= $this->pack(self::STREAM_STDIN, '');
$client->writeAll($request);
$handle = null;

do {

if(false === $handle = $this->readPack())
if (false === $handle = $this->readPack()) {
throw new Exception(
'Bad request (not a well-formed FastCGI request).', 0);
'Bad request (not a well-formed FastCGI request).',
0
);
}

if( self::STREAM_STDOUT === $handle[parent::HEADER_TYPE]
|| self::STREAM_STDERR === $handle[parent::HEADER_TYPE])
if (self::STREAM_STDOUT === $handle[parent::HEADER_TYPE] ||
self::STREAM_STDERR === $handle[parent::HEADER_TYPE]) {
$response .= $handle[parent::HEADER_CONTENT];
} while(self::REQUEST_END !== $handle[parent::HEADER_TYPE]);
}
} while (self::REQUEST_END !== $handle[parent::HEADER_TYPE]);

$client->disconnect();

switch(ord($handle[parent::HEADER_CONTENT][4])) {

switch (ord($handle[parent::HEADER_CONTENT][4])) {
case self::STATUS_CANNOT_MULTIPLEX:
throw new Exception\CannotMultiplex(
'Application %s that you are trying to reach does not ' .
'support multiplexing.',
0, $this->getClient()->getSocket()->__toString());
break;
0,
$this->getClient()->getSocket()->__toString()
);

break;

case self::STATUS_OVERLOADED:
throw new Exception\Overloaded(
'Application %s is too busy and rejects your request.',
1, $this->getClient()->getSocket()->__toString());
break;
1,
$this->getClient()->getSocket()->__toString()
);

break;

case self::STATUS_UNKNOWN_ROLE:
throw new Exception\UnknownRole(
'Server for the application %s returns an unknown role.',
2, $this->getClient()->getSocket()->__toString());
break;
2,
$this->getClient()->getSocket()->__toString()
);

break;
}

/**
@@ -257,8 +265,7 @@ class Responder extends Connection {
$pos = strpos($response, "\r\n\r\n");
$headers = substr($response, 0, $pos);

foreach(explode("\r\n", $headers) as $header) {

foreach (explode("\r\n", $headers) as $header) {
$semicolon = strpos($header, ':');
$this->_headers[strtolower(trim(substr($header, 0, $semicolon)))]
= trim(substr($header, $semicolon + 1));
@@ -270,46 +277,42 @@ class Responder extends Connection {
/**
* Get response content.
*
* @access public
* @return string
*/
public function getResponseContent ( ) {
public function getResponseContent()
{
return $this->_content;
}

/**
* Get response headers.
*
* @access public
* @return array
*/
public function getResponseHeaders ( ) {
public function getResponseHeaders()
{
return $this->_headers;
}

/**
* Read data.
*
* @access protected
* @param int $length Length of data to read.
* @return string
*/
protected function read ( $length ) {
protected function read($length)
{
return $this->getClient()->read($length);
}

/**
* Set client.
*
* @access public
* @param \Hoa\Socket\Client $client Client.
* @return \Hoa\Socket\Client
*/
public function setClient ( Socket\Client $client ) {
public function setClient(Socket\Client $client)
{
$old = $this->_client;
$this->_client = $client;

@@ -319,11 +322,10 @@ class Responder extends Connection {
/**
* Get client.
*
* @access public
* @return \Hoa\Socket\Client
*/
public function getClient ( ) {
public function getClient()
{
return $this->_client;
}
}

Loading…
Cancel
Save