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

Pest->post() method inserts "Content-Length" parameter to header incorrectly #71

Open
geldrin opened this issue Mar 17, 2016 · 0 comments

Comments

@geldrin
Copy link

geldrin commented Mar 17, 2016

There's a hardware device I wanted to control via HTTP requests. The machine expects the authentication data to be passed as an XML string in the CURLOPT_POSTFIELDS parameter.

After several failed attempts I discovered a bug in the post() method call:

    public function post($url, $data, $headers = array())
    {
        (...)
        if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data);
        $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers);
        (...)
    }

On line 384, when the code inspects the $data variable, it inserts the Content-Length value incorrectly into the header array if $data is not array.

My expected array was:

array(4) {
  [0]=>
  string(29) "content-type: application/xml"
  [1]=>
  string(23) "cache-control: no-cache"
  [2]=>
  string(25) "authorization: Basic Og=="
  [3]=>
  string(21) "Content-Length: 86"
}

but I've got the following data:

array(4) {
  [0]=>
  string(29) "content-type: application/xml"
  [1]=>
  string(23) "cache-control: no-cache"
  [2]=>
  string(25) "authorization: Basic Og=="
  [3]=>
  string(21) "0: Content-Length: 86"
}

As you can see, an array index has been inserted in the last string element.

The main problem that the code doesn't examine the header array's format before adding the new Content-Length element (is it numerically indexed, or associative). The code just places a numerically indexed indice, so if your $headers array is associative, the new array's indexing becomes mixed.

And this produces another error, since prepHeaders() method calls _isNumericallyIndexedArray() (line 203), which fails to properly determine how the array was indexed, because it will instantly return true on the first numeric index it encounters.

Because of the false result, the prepHeaders() method returns the array as it was... incorrectly formatted.

I would advise to handle the header array format consistently througout the code, so actions with the header data would be less error prone... but that's just my two cents on the topic.

Thanks,
Gergely Lukacsy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant