Permalink
Browse files

BREAK: Fixes #387 on dev.lithify.me. Changes `net\http\Request::$para…

…ms` to `query`. See change in test cases.
  • Loading branch information...
1 parent 2dddf3e commit f453b39b1ef13684ce2e78f33adc98f50e0a77be @gwoo gwoo committed Jul 21, 2011
View
@@ -71,18 +71,24 @@ class Message extends \lithium\core\Object {
/**
* Adds config values to the public properties when a new object is created.
*
- * @param array $config
+ * @param array $config Configuration options : default value
+ * - `scheme`: tcp
+ * - `host`: localhost
+ * - `port`: null
+ * - `username`: null
+ * - `password`: null
+ * - `path`: null
+ * - `body`: null
*/
public function __construct(array $config = array()) {
$defaults = array(
'scheme' => 'tcp',
'host' => 'localhost',
'port' => null,
- 'path' => null,
'username' => null,
'password' => null,
- 'body' => null,
- 'message' => null
+ 'path' => null,
+ 'body' => null
);
$config += $defaults;
View
@@ -19,7 +19,7 @@ class Message extends \lithium\net\Message {
*
* @var string
*/
- public $protocol = 'HTTP/1.1';
+ public $protocol = null;
/**
* Specification version number
@@ -35,13 +35,6 @@ class Message extends \lithium\net\Message {
*/
public $headers = array();
- /**
- * body
- *
- * @var array
- */
- public $body = array();
-
/**
* Content-Type
*
@@ -56,6 +49,43 @@ class Message extends \lithium\net\Message {
*/
protected $_classes = array('media' => 'lithium\net\http\Media');
+ /**
+ * Adds config values to the public properties when a new object is created.
+ *
+ * @param array $config Configuration options : default value
+ * - `scheme`: http
+ * - `host`: localhost
+ * - `port`: null
+ * - `username`: null
+ * - `password`: null
+ * - `path`: null
+ * - `version`: 1.1
+ * - `headers`: array
+ * - `body`: null
+ */
+ public function __construct(array $config = array()) {
+ $defaults = array(
+ 'scheme' => 'http',
+ 'host' => 'localhost',
+ 'port' => null,
+ 'username' => null,
+ 'password' => null,
+ 'path' => null,
+ 'protocol' => null,
+ 'version' => '1.1',
+ 'headers' => array(),
+ 'body' => null
+ );
+ $config += $defaults;
+ parent::__construct($config);
+
+ if (strpos($this->host, '/') !== false) {
+ list($this->host, $this->path) = explode('/', $this->host, 2);
+ }
+ $this->path = str_replace('//', '/', "/{$this->path}/");
+ $this->protocol = $this->protocol ?: "HTTP/{$this->version}";
+ }
+
/**
* Add a header to rendered output, or return a single header or full header list.
*
View
@@ -17,33 +17,26 @@
class Request extends \lithium\net\http\Message {
/**
- * The method of the request, typically one of the following: `GET`, `POST`, `PUT`, `DELETE`,
- * `OPTIONS`, `HEAD`, `TRACE` or `CONNECT`.
+ * Key=value pairs after the ?.
*
- * @var string
+ * @var array
*/
- public $method = 'GET';
+ public $query = array();
/**
- * Used to build query string.
+ * The Authorization type: Basic or Digest
*
- * @var array
+ * @var string
*/
- public $params = array();
+ public $auth = null;
/**
- * Headers.
+ * The method of the request, typically one of the following: `GET`, `POST`, `PUT`, `DELETE`,
+ * `OPTIONS`, `HEAD`, `TRACE` or `CONNECT`.
*
- * For example:
- * {{{
- * array(
- * 'Host' => $this->host . ":" . $this->port,
- * 'Connection' => 'Close', 'User-Agent' => 'Mozilla/5.0'
- * )
- * }}}
- * @var array
+ * @var string
*/
- public $headers = array();
+ public $method = 'GET';
/**
* Cookies.
@@ -53,69 +46,68 @@ class Request extends \lithium\net\http\Message {
public $cookies = array();
/**
- * Constructor
+ * Adds config values to the public properties when a new object is created.
*
- * @param array $config
- * - auth: the Authorization method (Basic|Digest)
- * - username: the username for auth
- * - password: the password for auth
+ * @param array $config Configuration options : default value
+ * - `scheme`: http
+ * - `host`: localhost
+ * - `port`: null
+ * - `username`: null
+ * - `password`: null
+ * - `path`: null
+ * - `query`: array - after the question mark ?
+ * - `fragment`: null - after the hashmark #
+ * - `auth` - the Authorization method (Basic|Digest)
+ * - `method` - GET
+ * - `version`: 1.1
+ * - `headers`: array
+ * - `body`: null
*/
public function __construct(array $config = array()) {
$defaults = array(
'scheme' => 'http',
'host' => 'localhost',
'port' => null,
- 'method' => 'GET',
- 'path' => '/',
- 'auth' => null,
+ 'username' => null,
+ 'password' => null,
+ 'path' => null,
+ 'query' => array(),
+ 'fragment' => null,
'headers' => array(),
- 'body' => array(),
- 'params' => array()
+ 'body' => null,
+ 'auth' => null,
+ 'method' => 'GET'
);
$config += $defaults;
parent::__construct($config);
- $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 (strpos($this->host, '/') !== false) {
- $parts = explode('/', $this->host, 2);
- $this->host = $parts[0];
- $this->path = str_replace('//', '/', "/{$parts[1]}/");
- }
}
/**
- * Set queryString.
+ * Get the queryString.
*
* @param array $params
* @param string $format
* @return array
*/
public function queryString($params = array(), $format = "{:key}={:value}&") {
- if (!$params) {
- if (is_string($this->params)) {
- return "?" . $this->params;
- }
- $params = $this->params;
- } elseif (is_array($this->params)) {
- $params = array_merge($this->params, $params);
- }
+ $params = empty($params) ? (array) $this->query : (array) $this->query + (array) $params;
$query = null;
- foreach ($params as $key => $value) {
+ foreach (array_filter($params) as $key => $value) {
@m4rcsch

m4rcsch Aug 31, 2011

Contributor

The transition from params to query i a quite good idea.

But changing to a "normal" key=value array wouldnt accept multiple equivalent keys with different values. Which are sometimes used.
Example: A Solr Filter query, with multiple "fq" keys and different values for a multi filter request..

see: #103

$values = array('key' => urlencode($key), 'value' => urlencode($value));
$query .= String::insert($format, $values);
}
if (!$query) {
return null;
}
- return "?" . $this->params = substr($query, 0, -1);
+ return "?" . substr($query, 0, -1);
}
/**
@@ -153,30 +145,29 @@ public function to($format, array $options = array()) {
'host' => $this->host,
'port' => $this->port ? ":{$this->port}" : '',
'path' => $this->path,
- 'query' => $this->queryString(),
+ 'query' => null,
'auth' => $this->_config['auth'],
- 'content' => $this->body(),
+ 'headers' => array(),
+ 'body' => null,
'version' => $this->version
);
$options += $defaults;
- if ($options['query'] && is_array($options['query'])) {
- $options['query'] = $this->queryString($options['query']);
- }
-
switch ($format) {
case 'url':
+ $options['query'] = $this->queryString($options['query']);
return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options);
case 'context':
if ($options['auth']) {
$auth = base64_encode("{$this->username}:{$this->password}");
$this->headers('Authorization', "{$options['auth']} {$auth}");
}
- $this->headers('Content-Length', strlen($options['content']));
+ $body = $this->body($options['body']);
+ $this->headers('Content-Length', strlen($body));
$base = array(
- 'content' => $options['content'],
+ 'content' => $body,
'method' => $options['method'],
- 'header' => $this->headers(),
+ 'header' => $this->headers($options['headers']),
'protocol_version' => $options['version'],
'ignore_errors' => true
);
View
@@ -20,13 +20,6 @@ class Response extends \lithium\net\http\Message {
*/
public $status = array('code' => 200, 'message' => 'OK');
- /**
- * Headers.
- *
- * @var array
- */
- public $headers = array();
-
/**
* Content Type.
*
@@ -41,13 +34,6 @@ class Response extends \lithium\net\http\Message {
*/
public $encoding = 'UTF-8';
- /**
- * The body.
- *
- * @var array
- */
- public $body = array();
-
/**
* Status codes.
*
@@ -95,16 +81,28 @@ class Response extends \lithium\net\http\Message {
504 => 'Gateway Time-out'
);
+ /**
+ * Adds config values to the public properties when a new object is created.
+ *
+ * @param array $config
+ */
+ public function __construct(array $config = array()) {
+ $defaults = array('message' => null);
+ $config += $defaults;
+ parent::__construct($config);
+ }
+
+ /**
+ * Initialize the Response
+ *
+ * @return void
+ */
protected function _init() {
parent::_init();
- $body = $this->_config['body'];
- if ($this->_config['body'] && !$this->_config['message']) {
- $this->body = $this->_config['body'];
- } elseif (($body = $this->_config['message']) && !$this->_config['body']) {
- $body = $this->_parseMessage($body);
+ if ($this->_config['message']) {
+ $this->body = $this->_parseMessage($this->_config['message']);
}
-
if (isset($this->headers['Content-Type'])) {
preg_match('/^(.*?);\s*?charset=(.+)/i', $this->headers['Content-Type'], $match);
@@ -114,9 +112,8 @@ protected function _init() {
}
}
if (isset($this->headers['Transfer-Encoding'])) {
- $body = $this->_decode($body);
+ $this->body = $this->_decode($this->body);
}
- $this->body = $this->body ?: $body;
}
/**
View
@@ -170,7 +170,7 @@ public function send($method, $path = null, $data = array(), array $options = ar
$request = $this->_request($method, $path, $data, $options);
$options += array('message' => $request);
- if (!($conn =& $this->connection($options)) || !$conn->open()) {
+ if (!($conn = $this->connection($options)) || !$conn->open()) {
return;
}
@@ -199,18 +199,19 @@ protected function _request($method, $path, $data, $options) {
$request = $this->_instance('request', $options);
$request->path = str_replace('//', '/', "{$request->path}{$path}");
$request->method = $method = strtoupper($method);
+ $hasBody = in_array($method, array('POST', 'PUT'));
$media = $this->_classes['media'];
$type = null;
- if (in_array($options['type'], $media::types()) && $data && !is_string($data)) {
+ if ($data && in_array($options['type'], $media::types())) {
$type = $media::type($options['type']);
$contentType = (array) $type['content'];
$request->headers(array('Content-Type' => current($contentType)));
- $data = Media::encode($options['type'], $data, $options);
+ $data = $hasBody && !is_string($data) ?
+ Media::encode($options['type'], $data, $options) : $data;
}
- in_array($method, array('POST', 'PUT'))
- ? $request->body($data) : $request->params = $data;
+ $hasBody ? $request->body($data) : $request->query = $data;
return $request;
}
}
Oops, something went wrong.

0 comments on commit f453b39

Please sign in to comment.