Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #71 (Content-Length injection into header fields) #72

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,25 @@ protected function prepRequest($opts, $url)
*/
protected function _isNumericallyIndexedArray($array)
{
return !(bool)count(array_filter(array_keys($array), 'is_string'));
return (bool)(count(array_filter(array_keys($array), 'is_string')) === 0);
}

/**
* Flatten headers from an associative array to a numerically indexed array of "Name: Value"
* style entries like CURLOPT_HTTPHEADER expects. Numerically indexed arrays are not modified.
* style entries like CURLOPT_HTTPHEADER expects. If $content_length is explicitly set,
* the function checks and adds 'Content-Length' value if needed.
* Other portions of numerically indexed arrays are not modified.
*
* @param array $headers
* @param int $content_length
* @return array
*/
protected function prepHeaders($headers)
protected function prepHeaders($headers, $content_length = null)
{
if ($this->_isNumericallyIndexedArray($headers)) {
if ($content_length && array_search('content-length', array_map('strtolower', $headers)) === false)
$headers['Content-Length'] = $content_length;

return $headers;
}

Expand All @@ -223,6 +229,9 @@ protected function prepHeaders($headers)
$flattened[] = $name . ': ' . $value;
}

if ($content_length && !array_key_exists('Content-Length', $headers))
$headers[] = 'Content-Length:' . $content_length;

return $flattened;
}

Expand Down Expand Up @@ -378,11 +387,12 @@ public function head($url)
public function post($url, $data, $headers = array())
{
$data = $this->prepData($data);
$length = null;

$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'POST';
if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers);
if (!is_array($data)) $length = strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers, $length);
$curl_opts[CURLOPT_POSTFIELDS] = $data;

$curl = $this->prepRequest($curl_opts, $url);
Expand Down Expand Up @@ -427,11 +437,12 @@ public function prepData($data)
public function put($url, $data, $headers = array())
{
$data = $this->prepData($data);
$length = null;

$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers);
if (!is_array($data)) $length = strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers, $length);
$curl_opts[CURLOPT_POSTFIELDS] = $data;

$curl = $this->prepRequest($curl_opts, $url);
Expand All @@ -456,8 +467,7 @@ public function patch($url, $data, $headers = array())

$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'PATCH';
$headers[] = 'Content-Length: ' . strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers);
$curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers, strlen($data));
$curl_opts[CURLOPT_POSTFIELDS] = $data;

$curl = $this->prepRequest($curl_opts, $url);
Expand Down