Permalink
Browse files

adding '\net\Message` for the basic request/response from `\net\Socket`.

Refactored `\net\http\*` to use the new class.
  • Loading branch information...
1 parent 907efd2 commit 077ba19c2368ea0b55b91ee2ec80ce8d6d8b0582 @gwoo gwoo committed Jul 10, 2010
@@ -21,6 +21,7 @@
require LITHIUM_LIBRARY_PATH . '/lithium/util/String.php';
require LITHIUM_LIBRARY_PATH . '/lithium/core/Adaptable.php';
require LITHIUM_LIBRARY_PATH . '/lithium/core/Environment.php';
+require LITHIUM_LIBRARY_PATH . '/lithium/net/Message.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Message.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Media.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Request.php';
@@ -0,0 +1,146 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace lithium\net;
+
+/**
+ * Base message class for any URI based request/response.
+ * @see http://tools.ietf.org/html/rfc3986#section-1.1.1
+ * @see http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
+ *
+ */
+class Message extends \lithium\core\Object {
+
+ /**
+ * The uri scheme
+ *
+ * @var string
+ */
+ public $scheme = 'tcp';
+
+ /**
+ * The hostname for this endpoint.
+ *
+ * @var string
+ */
+ public $host = 'localhost';
+
+ /**
+ * The port
+ *
+ * @var string
+ */
+ public $port = null;
+
+ /**
+ * Absolute path of the request.
+ *
+ * @var string
+ */
+ public $path = null;
+
+ /**
+ * The username
+ *
+ * @var string
+ */
+ public $username = null;
+
+ /**
+ * Absolute path of the request.
+ *
+ * @var string
+ */
+ public $password = null;
+
+ /**
+ * The body of the message.
+ *
+ * @var array
+ */
+ public $body = array();
+
+ /**
+ * Adds config values to the public properties when a new object is created.
+ *
+ * @param array $config
+ */
+ public function __construct(array $config = array()) {
+ $defaults = array(
+ 'scheme' => 'tcp',
+ 'host' => 'localhost',
+ 'port' => null,
+ 'path' => null,
+ 'username' => null,
+ 'password' => null,
+ 'body' => array(),
+ );
+ $config += $defaults;
+
+ foreach ($config as $key => $value) {
+ $this->{$key} = $value;
+ }
+ parent::__construct($config);
+ }
+
+ /**
+ * Add body parts.
+ *
+ * @param mixed $data
+ * @param array $options
+ * - `'buffer'`: split the body string
+ * @return array
+ */
+ public function body($data = null, $options = array()) {
+ $default = array('buffer' => null);
+ $options += $default;
+ $this->body = array_merge((array) $this->body, (array) $data);
+ $body = trim(join("\r\n", $this->body));
+ return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
+ }
+
+ /**
+ * Converts the data in the record set to a different format, i.e. an array. Available
+ * options: array, url, context, or string.
+ *
+ * @param string $format Format to convert to.
+ * @param array $options
+ * @return mixed
+ */
+ public function to($format, array $options = array()) {
+ switch ($format) {
+ case 'array':
+ $array = array();
+ $r = new \ReflectionClass(get_class($this));
+ foreach ($r->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
+ $array[$prop->getName()] = $prop->getValue($this);
+ }
+ return $array;
+ case 'url':
+ $host = $this->host . ($this->port ? ":{$this->port}" : '');
+ return "{$this->scheme}://{$host}{$this->path}";
+ case 'context':
+ $defaults = array('content' => $this->body(), 'ignore_errors' => true);
+ return array($this->scheme => $options + $defaults);
+ case 'string':
+ default:
+ return (string) $this;
+ }
+ }
+
+ /**
+ * Magic method to convert object to string.
+ *
+ * @return string
+ */
+ public function __toString() {
+ return (string) $this->body();
+ }
+}
+
+?>
@@ -23,6 +23,21 @@
protected $_resource = null;
/**
+ * the classes for the socket
+ *
+ * @var array
+ */
+ protected $_classes = array(
+ 'response' => '\lithium\net\Message'
+ );
+
+ /**
+ * Auto config
+ *
+ */
+ protected $_autoConfig = array('classes' => 'merge');
+
+ /**
* Constructor.
*
* @param array $config Available configuration options are:
@@ -70,7 +85,7 @@ public function __construct(array $config = array()) {
/**
* Reads from the socket.
*
- * @return mixed The read contents, or `false` if reading failed.
+ * @return object `lithium\net\Message`
*/
abstract public function read();
@@ -12,7 +12,7 @@
* Base class for `lithium\net\http\Request` and `lithium\net\http\Response`. Implements basic
* protocol handling for HTTP-based transactions.
*/
-class Message extends \lithium\core\Object {
+class Message extends \lithium\net\Message {
/**
* The full protocol: HTTP/1.1
@@ -96,22 +96,6 @@ public function headers($key = null, $value = null) {
}
/**
- * Add body parts.
- *
- * @param mixed $data
- * @param array $options
- * - `'buffer'`: split the body string
- * @return array
- */
- public function body($data = null, $options = array()) {
- $default = array('buffer' => null);
- $options += $default;
- $this->body = array_merge((array) $this->body, (array) $data);
- $body = trim(join("\r\n", $this->body));
- return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
- }
-
- /**
* Sets/Gets the content type
*
* @param string $type a full content type i.e. `'application/json'` or simple name `'json'`
@@ -17,28 +17,6 @@
class Request extends \lithium\net\http\Message {
/**
- * The protocol scheme to be used in the request. Used when calculating the target URL of this
- * request's end point.
- *
- * @var string
- */
- public $scheme = 'http';
-
- /**
- * The Host header value and authority.
- *
- * @var string
- */
- public $host = 'localhost';
-
- /**
- * Port number.
- *
- * @var string
- */
- public $port = null;
-
- /**
* The method of the request, typically one of the following: `GET`, `POST`, `PUT`, `DELETE`,
* `OPTIONS`, `HEAD`, `TRACE` or `CONNECT`.
*
@@ -47,13 +25,6 @@ class Request extends \lithium\net\http\Message {
public $method = 'GET';
/**
- * Absolute path of the request.
- *
- * @var string
- */
- public $path = '/';
-
- /**
* Used to build query string.
*
* @var array
@@ -75,34 +46,19 @@ class Request extends \lithium\net\http\Message {
public $headers = array();
/**
- * The authentication/authorization information
- *
- * For example:
- * {{{
- * array('method' => 'Basic', 'username' => 'lithium', 'password' => 'rad')
- * }}}
- * @var array
- */
- public $auth = array();
-
- /**
* Cookies.
*
* @var array
*/
public $cookies = array();
/**
- * Body.
- *
- * @var array
- */
- public $body = array();
-
- /**
* Constructor
*
* @param array $config
+ * - auth: the Authorization method (Basic|Digest)
+ * - username: the username for auth
+ * - password: the password for auth
* @return object
*/
public function __construct(array $config = array()) {
@@ -112,29 +68,22 @@ public function __construct(array $config = array()) {
'port' => null,
'method' => 'GET',
'path' => '/',
+ 'auth' => null,
'headers' => array(),
'body' => array(),
'params' => array()
);
$config += $defaults;
+ parent::__construct($config);
- foreach ($config as $key => $value) {
- $this->{$key} = $value;
- }
$this->protocol = "HTTP/{$this->version}";
-
$this->headers = array(
'Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host,
'Connection' => 'Close',
'User-Agent' => 'Mozilla/5.0',
);
$this->headers($config['headers']);
- if (!empty($config['auth']['method'])) {
- $this->headers('Authorization', $config['auth']['method'] . ' ' . base64_encode(
- $config['auth']['username'] . ':' . $config['auth']['password']
- ));
- }
if (strpos($this->host, '/') !== false) {
$parts = explode('/', $this->host, 2);
$this->host = $parts[0];
@@ -181,8 +130,6 @@ public function queryString($params = array(), $format = "{:key}={:value}&") {
*/
public function to($format, array $options = array()) {
switch ($format) {
- case 'array':
- return get_object_vars($this);
case 'url':
$query = $this->queryString();
$host = $this->host . ($this->port ? ":{$this->port}" : '');
@@ -194,9 +141,8 @@ public function to($format, array $options = array()) {
'protocol_version' => $this->version, 'ignore_errors' => true
);
return array('http' => $options + $defaults);
- case 'string':
default:
- return (string) $this;
+ return parent::to($format, $options);
}
}
@@ -206,12 +152,14 @@ public function to($format, array $options = array()) {
* @return string
*/
public function __toString() {
- $query = $this->queryString();
- $path = str_replace('//', '/', $this->path) . $query;
-
+ if (!empty($this->_config['auth'])) {
+ $this->headers('Authorization', "{$this->_config['auth']} " . base64_encode(
+ "{$this->username}:{$this->password}"
+ ));
+ }
+ $path = str_replace('//', '/', $this->path) . $this->queryString();
$body = $this->body();
$this->headers('Content-Length', strlen($body));
-
$request = array(
"{$this->method} {$path} {$this->protocol}",
join("\r\n", $this->headers()),
Oops, something went wrong.

0 comments on commit 077ba19

Please sign in to comment.