rfc:http-interface

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:http-interface [2014/10/31 03:12]
googleguy
rfc:http-interface [2014/11/01 02:43]
googleguy
Line 17: Line 17:
  
 This greatly reduces the need to change core PHP for any form of edge cases that may rely on processing HTTP messages differently and improves the flexibility and extensibility of PHP's built-in message processing facilities. Further more, this removes the inefficiencies of reusing/reinitializing GPC variables in custom HttpRequest or HttpResponse classes. Additional benefits can be found in the filtering or validating of data through custom callbacks defined in the implementing class. This greatly reduces the need to change core PHP for any form of edge cases that may rely on processing HTTP messages differently and improves the flexibility and extensibility of PHP's built-in message processing facilities. Further more, this removes the inefficiencies of reusing/reinitializing GPC variables in custom HttpRequest or HttpResponse classes. Additional benefits can be found in the filtering or validating of data through custom callbacks defined in the implementing class.
 +
 +===== Interface Definitions =====
 +
 +The following are the interface definitions required by the implementing classes...
 +
 +<code php>
 +abstract class HttpMessage
 +{
 +    protected $headers = array();
 +    protected $body = "";
 +    protected $version = 1.0;
 +    protected $message;
 +}
 +
 +interface HttpMessageReceive
 +{
 +
 +    public function receiveMessage($message);
 +    public function parseMessage();
 +    public function parseParameters();
 +    public function decodeBody();
 +    public function onReceive();
 +
 +}
 +
 +interface HttpMessageSend
 +{
 +
 +    public function sendMessage($message);
 +    public function composeMessage();
 +    public function encodeBody();
 +    public function onSend();
 +
 +}
 +</code>
 +
 +So an implementing class could look something like this...
 +
 +<code php>
 +class HttpRequest extends HttpMessage implements HttpMessageReceive
 +{
 +
 +    protected $parameters = array();
 +    protected $method, $requestURI, $host;
 +
 +    public function receiveMessage($message)
 +    {
 +        $this->message = $message;
 +        $this->onReceive();
 +        $this->parseMessage();
 +        $this->parseParameters();
 +        $this->decodeBody();
 +    }
 +
 +    public function parseMessage()
 +    {
 +        // implement message parsing here
 +        list($header, $body) = explode("\r\n\r\n", $this->message, 2) + array(null, null);
 +        $this->body = $body;
 +        $headers = explode("\r\n", $header);
 +        $header = array_shift($headers);
 +        list($this->method, $this->requestURI, $this->version) = explode(" ", $header, 3) + array(null, null, $this->version);
 +        $this->version = (float) ltrim(substr($this->version, strpos($this->version, "/")), "/");
 +        foreach($headers as $h) {
 +            list($key, $value) = explode(":", $h, 2) + array(null, null);
 +            if ($key === null) {
 +                throw new Exception("Empty header field in message!");
 +            }
 +            $this->headers[$key] = trim($value);
 +        }
 +    }
 +
 +    public function parseParameters()        
 +    {
 +        // implement parameter parsing method here
 +    }
 +
 +    public function decodeBody()        
 +    {
 +        // implement message body decoding here
 +    }
 +
 +    public function onReceive()        
 +    {
 +        // implement on receive hook here
 +    }
 +
 +}
 +</code>
 +
 +===== Implementation =====
 +
 +It appears the SAPI_API to the incoming HTTP request to PHP is already capable of allowing things such as register callbacks for the header, setting different content_type handlers/readers, etc... However, it is not currently exposed directly to userland.
 +
 +    * http://lxr.php.net/xref/PHP_5_6/main/SAPI.c#122
 +    * http://lxr.php.net/xref/PHP_5_6/main/php_content_types.c#49
 +
 +
 +This can be easily achieved by providing a direct interface to the internal API through a new class that will then be called along with the SAPI API on RINIT stage.
 +
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 53: Line 153:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
- 
- 
-===== Implementation ===== 
  
  
 ===== References ===== ===== References =====
-https://bugs.php.net/bug.php?id=55815 +    * https://bugs.php.net/bug.php?id=55815 
-https://gist.github.com/srgoogleguy/f729053e3e88b2d2b3ec - //Sample interface and implementation//+    https://gist.github.com/srgoogleguy/f729053e3e88b2d2b3ec - //Sample interface and implementation//
  
  
 ===== Rejected Features ===== ===== Rejected Features =====
rfc/http-interface.txt · Last modified: 2017/09/22 13:28 (external edit)