hwi/HWIOAuthBundle — pull request #657 by ninsuo (2014-10-01 22:19:19+00:00)
Title: Removed the Content-Length header

The `SensioConnectResourceOwner.doGetTokenRequest()` method was returning an error:

```json
{"error":"invalid_client","message":"Please provide your client id and secret"}
```

So I edited it to debug:

```php
protected function doGetTokenRequest($url, array $parameters = array())
{
$this->options['access_token_url'] = 'http://localhost:1234/test.php';
// ...
```

And got the following:

```
ninsuo:developpement alain$ nc -l localhost 1234
POST /test.php HTTP/1.0
Host: localhost:1234
Accept: */*
User-Agent: HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle)
Content-Length: 295
Content-Type: multipart/form-data; boundary=------------------------c73a7ff3f41a6b4e

--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="code"

c4afe5899956f76bfdb0c156534980f70975555755554f002435cb26062bd499
--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="grant_type"

authorization_code
--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="client_id"

abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef
--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="client_secret"

abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef
--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="redirect_uri"

http://twigfiddle.com/twigfiddle.com/front/web/login/check-sensio-connect
--------------------------c73a7ff3f41a6b4e
Content-Disposition: form-data; name="scope"

SCOPE_PUBLIC
--------------------------c73a7ff3f41a6b4e--
```

No need to count to see that the content length is wrong.
So I looked for, found and removed this header to try again, the Content-type was valid ( 936 ).

Trying to investigate in depth, I found 2 problems:

1) in buzz/lib/Buzz/Client/AbstractCurl.php, CURLOPT_POSTFIELDS is given as an array:

```php
$options[CURLOPT_POSTFIELDS] = $fields = static::getPostFields($request);
```

From the PHP documentation ( http://php.net/manual/en/function.curl-setopt.php ) : "If value is an array, the Content-Type header will be set to multipart/form-data.".

2) I think there were an issue in the way the Content-Length was calculated:

```php
} elseif (is_array($content)) {
$contentLength = strlen(implode('', $content));
}
```
From the rfc2616: "The Content-Length entity-header field indicates the size of the entity-body,
in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
method, the size of the entity-body that would have been sent had the request
been a GET."

So, even if the form wasn't multipart, the raw body of the request should be a valid query string-like format (urlencoded, and in the form key1=value1&key2=value2...), this should be larger than the concatenation of all contents.

Ok, I have a token now, but I still can't connect to SensioLabs Connect for other reasons, will investigate later :-).

Thanks for your great work anyway.