Add protocol and protocolVersion members and their accessors.

This commit is contained in:
PJ Dietz 2012-12-04 14:27:52 -05:00
parent b3f997136f
commit c970eee4bb
3 changed files with 66 additions and 21 deletions

View File

@ -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;
}
}
?>

View File

@ -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);

View File

@ -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);
}
// -------------------------------------------------------------------------