From c970eee4bbacd866216cbc79d1f222969c508b89 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Tue, 4 Dec 2012 14:27:52 -0500 Subject: [PATCH] Add protocol and protocolVersion members and their accessors. --- Message.inc.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++-- Request.inc.php | 12 ++-------- Response.inc.php | 14 ++++------- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/Message.inc.php b/Message.inc.php index 1a40ea3..3f91e35 100644 --- a/Message.inc.php +++ b/Message.inc.php @@ -24,6 +24,18 @@ abstract class Message { */ protected $headers; + /** + * Name of the protocol to use. + * @var string + */ + protected $protocol = 'HTTP'; + + /** + * Version of the protocol to use. + * @var string + */ + protected $protocolVersion = '1.1'; + // ------------------------------------------------------------------------- // !Accessors @@ -39,6 +51,10 @@ abstract class Message { return $this->getBody(); case 'headers': return $this->getHeaders(); + case 'protocol': + return $this->getProtocol(); + case 'protocolVersion': + return $this->getProtocolVersion(); default: throw new \Exception('Property ' . $name . ' does not exist.'); } @@ -47,7 +63,7 @@ abstract class Message { /** * @param string $name - * @param mixed $value + * @param $value * @throws \Exception */ public function __set($name, $value) { @@ -56,6 +72,12 @@ abstract class Message { case 'body': $this->setBody($value); return; + case 'protocol': + $this->setProtocol($value); + return; + case 'protocolVersion': + $this->setProtocolVersion($value); + return; default: throw new \Exception('Property ' . $name . 'does not exist or is read-only.'); } @@ -109,6 +131,7 @@ abstract class Message { * Add or update a header to a given value * * @param string $header + * @param $value * @param string $value */ public function setHeader($header, $value) { @@ -118,7 +141,7 @@ abstract class Message { /** * Return if the response contains a header with the given key. * - * @param string $header + * @param $header * @return bool */ public function hasHeader($header) { @@ -136,6 +159,40 @@ abstract class Message { } } + /** + * @return string + */ + public function getProtocol() { + return $this->protocol; + } + + /** + * @param $protocol + */ + public function setProtocol($protocol) { + + if (strpos($protocol, '/') === false) { + list($this->protocol, $this->protocolVersion) = explode('/', $protocol, 2); + } else { + $this->protocol = $protocol; + } + + } + + /** + * @return string + */ + public function getProtocolVersion() { + return $this->protocolVersion; + } + + /** + * @param string $protocolVersion + */ + public function setProtocolVersion($protocolVersion) { + $this->protocolVersion = $protocolVersion; + } + } ?> diff --git a/Request.inc.php b/Request.inc.php index f110216..151f832 100644 --- a/Request.inc.php +++ b/Request.inc.php @@ -6,8 +6,7 @@ require_once(dirname(__FILE__) . '/Message.inc.php'); require_once(dirname(__FILE__) . '/Response.inc.php'); require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php'); -// !TODO: Include port in the URI -// !TODO: This and Response both have protocol members, but they are different. +// TODO: Include port in the URI /** * A Request instance represents an HTTP request. This class has two main uses: @@ -73,13 +72,6 @@ class Request extends Message { */ static protected $theRequest; - /** - * Protocal for the request (e.g., http, https) - * - * @var string - */ - public $protocol = 'http'; - // ------------------------------------------------------------------------- // !Accessors @@ -232,7 +224,7 @@ class Request extends Message { */ public function getUri() { - $uri = $this->protocol . '://' . $this->hostname . $this->path; + $uri = strtolower($this->protocol) . '://' . $this->hostname . $this->path; if ($this->query) { $uri .= '?' . http_build_query($this->query); diff --git a/Response.inc.php b/Response.inc.php index 5d86faf..ccd7e83 100644 --- a/Response.inc.php +++ b/Response.inc.php @@ -21,14 +21,6 @@ require_once(dirname(__FILE__) . '/Message.inc.php'); */ class Response extends Message { - /** - * The protocol. Set this to the protocol you wish to use, such as - * "HTTP/1.1". The default is $_SERVER['SERVER_PROTOCOL'] or HTTP/1.1 - * - * @var string - */ - public $protocol; - /** * Text explanation of the HTTP Status Code. You only need to set this if * you are using nonstandard status codes. Otherwise, the instance will @@ -231,7 +223,11 @@ class Response extends Message { * @return string */ protected function getStatusLine() { - return $this->protocol . ' ' . $this->statusCode . ' ' . $this->reasonPhrase; + return sprintf('%s/%s %s %s', + strtoupper($this->protocol), + $this->protocolVersion, + $this->statusCode, + $this->reasonPhrase); } // -------------------------------------------------------------------------