UnionOfRAD/lithium — pull request #902 by pixelcog (2013-04-23 02:07:00+00:00)
Title: Improve Li3 Request/Response Classes

There's quite a few changes in this PR, so I'll go through them one at a time:

#### 1) Added Cookie header compiling to `\net\http\Request`.

You can now do stuff like this:

$request = new Request(array(
'host' => 'lithify.me',
'path' => '/test',
'cookies' => array(
'foo' => 'bar',
'bin' => 'baz'
)
));

$curl = new Curl();
$curl->open();
$curl->write($request);

and it will send the cookies along to the server. This is useful for maintaining session ids or other stateful data between multiple remote server requests/responses.

#### 2. Refactored `\net\http\Message::header()` and allow for multiple headers of the same name when parsing a `Response` message.

The `header()` method now has an option to specify whether headers are to be appended or overwritten when being set. Appending is now the default action when reading headers through `\net\http\Response::_parseMessage()`.

$message = join("\r\n", array(
'HTTP/1.1 200 OK',
'Connection: close',
'Content-Type: text/plain;charset=UTF8',
'Header: value1',
'Header: value2',
'',
'Test!'
));
$header = array('value1', 'value2');

$response = new Response(compact('message'));
$this->assertEqual($header, $response->headers('Header'));
$this->assertEqual($message, (string) $response);

The `header` method now also calls itself recursively when setting a `$key => $value` pair so it will be far more useful to any subclass which overloads it.

#### 3. Added Set-Cookie header parsing and compiling to `\net\http\Response`

Cookies in response messages are now parsed from:

Set-Cookie: Name=Marty%20McFly; Domain=.hillvalley.us; Secure
Set-Cookie: Destination=The%20Future; Expires=Wed, 21-Oct-2015 23:29:00 GMT

Into an associative array of the form:

array(
'Name' => array('value' => 'Marty McFly', 'domain' => '.hillvalley.us', 'secure' => true),
'Destination' => array('value' => 'The Future', 'expires' => 'Oct 21 2015 4:29 PM PDT')
)

#### 4. Made `\action\Request::header()` and `to('string')` useful again.

You can now call `$request->header('Foo-Bar')` to access any arbitrary header without the far less intuitive `$request->env('HTTP_FOO_BAR')` that it gets translated to in the webserver environment.

The entire request (sans attachments) can be serialized with $request->to('string') consistant with its parent class. This can be useful when debugging if you want to capture an approximation of the actual http request message for your logs along with a stack trace when an exception occurs.

Also moved request data parsing within `\action\Request::_init()` back into `body()` to be consistant with parent classes.