aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-15 09:45:15 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-15 09:45:15 +0200
commit9c228ff0724177c3f5529b41e96e4c35fa2cc31c (patch)
treee8437f9ce271984844a8dda13286d803d171b907
parentc88544ce5dfdcd718d47439d4d2a10ea5af43a1b (diff)
downloadFastcgi-9c228ff0724177c3f5529b41e96e4c35fa2cc31c.zip
Fastcgi-9c228ff0724177c3f5529b41e96e4c35fa2cc31c.tar.gz
Fastcgi-9c228ff0724177c3f5529b41e96e4c35fa2cc31c.tar.bz2
Move to PSR-1 and PSR-2.
-rw-r--r--Connection.php126
-rw-r--r--Documentation/En/Index.xyl5
-rw-r--r--Documentation/Fr/Index.xyl5
-rw-r--r--Exception/CannotMultiplex.php10
-rw-r--r--Exception/Exception.php10
-rw-r--r--Exception/Overloaded.php10
-rw-r--r--Exception/UnknownRole.php10
-rw-r--r--Responder.php110
8 files changed, 142 insertions, 144 deletions
diff --git a/Connection.php b/Connection.php
index ac18d3a..6fd597f 100644
--- a/Connection.php
+++ b/Connection.php
@@ -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);
}
diff --git a/Documentation/En/Index.xyl b/Documentation/En/Index.xyl
index cf18cfa..3ef1c46 100644
--- a/Documentation/En/Index.xyl
+++ b/Documentation/En/Index.xyl
@@ -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
diff --git a/Documentation/Fr/Index.xyl b/Documentation/Fr/Index.xyl
index 12ef3a2..fde5807 100644
--- a/Documentation/Fr/Index.xyl
+++ b/Documentation/Fr/Index.xyl
@@ -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
diff --git a/Exception/CannotMultiplex.php b/Exception/CannotMultiplex.php
index 4be3bb0..1f89194 100644
--- a/Exception/CannotMultiplex.php
+++ b/Exception/CannotMultiplex.php
@@ -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
+{
+}
diff --git a/Exception/Exception.php b/Exception/Exception.php
index 2cffd43..39c5e06 100644
--- a/Exception/Exception.php
+++ b/Exception/Exception.php
@@ -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.
diff --git a/Exception/Overloaded.php b/Exception/Overloaded.php
index d6fd70b..c7a4b5b 100644
--- a/Exception/Overloaded.php
+++ b/Exception/Overloaded.php
@@ -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
+{
+}
diff --git a/Exception/UnknownRole.php b/Exception/UnknownRole.php
index 8239b91..68dfc82 100644
--- a/Exception/UnknownRole.php
+++ b/Exception/UnknownRole.php
@@ -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
+{
+}
diff --git a/Responder.php b/Responder.php
index f65bf1d..4745935 100644
--- a/Responder.php
+++ b/Responder.php
@@ -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;
}
}