Skip to content

Instantly share code, notes, and snippets.

@srgoogleguy
Created October 31, 2014 02:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srgoogleguy/f729053e3e88b2d2b3ec to your computer and use it in GitHub Desktop.
Save srgoogleguy/f729053e3e88b2d2b3ec to your computer and use it in GitHub Desktop.
PHP Http Interface Draft

Http Interface

The Http interface SHOULD define the standardized methods and constants upheld by implementing classes. All classes should be obligated to implement such methods and constants, but MAY NOT be limited to only these methods or constants.

The Interface Definition

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();
    
}

An example for an implementing class might be as follows...

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
    }
    
}

Example of implementing class for HttpResponse

class HttpResponse extends HttpMessage implements HttpMessageSend
{

    protected $statusCode, $statusLine;

    public function sendMessage($message)
    {
        // implement method here
    }
    public function composeMessage()
    {
        // implement method here
    }
    public function encodeBody()
    {
        // implement method here
    }
    public function onSend()
    {
        // implement method here
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment