rfc:http-interface

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:http-interface [2014/10/31 03:12] googleguyrfc:http-interface [2014/10/31 03:19] 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>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 59: Line 148:
  
 ===== 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 by 127.0.0.1