Skip to content
jrbasso edited this page Dec 13, 2010 · 11 revisions

The HttpSocket class has undergone some changes.

HttpSocket doesn't change the header keys

Following others places in core, the HttpSocket do not change the headers. RFC 2616 says that headers are case insensitive, and HttpSocket preserves the values the remote host sends.

So, if your server sends the headers like

HTTP/1.1 200 OK
Date: Mon, 16 Apr 2007 04:14:16 GMT
server: CakeHttp Server
content-tyPe: text/html

Your $http->response['header'] will contain the exact keys sent.

array(
    'Date' => 'Mon, 16 Apr 2007 04:14:16 GMT',
    'server' => 'CakeHttp Server',
    'content-tyPe' => 'text/html'
)

Response values returned as a reference

The get(), post(), put(), delete() and request() methods now returns the response body as reference. This is useful when the response is large, as a big response could consume a large amount of memory when returned by value. The return value of the previously named methods is a reference to $http->response['body']. To use this feature, you'll need receive the result with reference, for example:

$response =& $http->get('http://www.cakephp.org');

Getting result using a stream (or simply download the result)

HttpSocket has a new method called setContentResource(). By setting a resource with this method, the content will be written to this resource, using fwrite(). To you download a file, you can do:

$http = new HttpSocket();
$f = fopen('bakery.xml', 'w');
$http->setContentResource($f);
$http->get('http://bakery.cakephp.org/comments.rss');
fclose($f);

Attention: The header is not included in file, you will only get the body content written to your resource. To disable saving into the resource, use $http->setContentResource(false).

Independent cookie by host

Cookies are stored internally by host, not per instance. This means that, if you make two requests to different servers, cookies from domain1 won't be sent to domain2. This was done to avoid possible security problems.

Extensible Authentication methods

You can now create your own authentication method to use with HttpSocket. You could create the file `app/libs/http/your_method_authentication.php' with the content:

<?php

class YourMethodAuthentication {

/**
 * Authentication
 *
 * @param HttpSocket $http
 * @param array $authInfo
 * @return void
 */
    public static function authentication(HttpSocket $http, &$authInfo) {
        // Do something, for example set $http->request['header']['Authentication'] value
    }

}

To set the auth config, you can use the new method configAuth():

$http->configAuth('YourMethod', array('config1' => 'value1', 'config2' => 'value2'));
$http->get('http://secure.your-site.com');

The Basic authentication is still supported by CakePHP, support for Digest authentication has also been added. To use this feature you will need to set $http->configAuth('Basic', 'user', 'password');

Support to Proxy

As part of auth configuration, you can configure proxy authentication. You can create your customized method to proxy authentication in the same class of authentication. For example:

<?php

class YourMethodAuthentication {

/**
 * Authentication
 *
 * @param HttpSocket $http
 * @param array $authInfo
 * @return void
 */
    public static function authentication(HttpSocket $http, &$authInfo) {
        // Do something, for example set $http->request['header']['Authentication'] value
    }

/**
 * Proxy Authentication
 *
 * @param HttpSocket $http
 * @param array $proxyInfo
 * @return void
 */
    public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
        // Do something, for example set $http->request['header']['Proxy-Authentication'] value
    }

}

To use, you must call the HttpSocket::configProxy() as same the HttpSocket::configAuth().

Clone this wiki locally