Skip to content

Commit

Permalink
Merge branch 'hotfix/ZF2-78' of https://github.com/sasezaki/zf2 into …
Browse files Browse the repository at this point in the history
…hotfix/zf2-78
  • Loading branch information
weierophinney committed Oct 26, 2011
2 parents f821000 + b29614c commit a886f80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
56 changes: 24 additions & 32 deletions library/Zend/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,40 +1134,32 @@ protected function prepareBody()
// If we have POST parameters or files, encode and add them to the body
if (count($this->getRequest()->post()->toArray()) > 0 || $totalFiles > 0) {

switch($this->getEncType()) {
case self::ENC_FORMDATA:
// Encode body as multipart/form-data
$boundary = '---ZENDHTTPCLIENT-' . md5(microtime());
$this->setEncType(self::ENC_FORMDATA, $boundary);

// Get POST parameters and encode them
$params = self::flattenParametersArray($this->getRequest()->post()->toArray());
foreach ($params as $pp) {
$body .= $this->encodeFormData($boundary, $pp[0], $pp[1]);
}

// Encode files
foreach ($this->getRequest()->file()->toArray() as $key => $file) {
$fhead = array('Content-Type' => $file['ctype']);
$body .= $this->encodeFormData($boundary, $file['formname'], $file['data'], $file['filename'], $fhead);
}

$body .= "--{$boundary}--\r\n";
break;

case self::ENC_URLENCODED:
// Encode body as application/x-www-form-urlencoded
$body = http_build_query($this->getRequest()->post()->toArray());
break;

default:
if (isset($mbIntEnc)) {
mb_internal_encoding($mbIntEnc);
}
if (stripos($this->getEncType(), self::ENC_FORMDATA) === 0) {
$boundary = '---ZENDHTTPCLIENT-' . md5(microtime());
$this->setEncType(self::ENC_FORMDATA, $boundary);

// Get POST parameters and encode them
$params = self::flattenParametersArray($this->getRequest()->post()->toArray());
foreach ($params as $pp) {
$body .= $this->encodeFormData($boundary, $pp[0], $pp[1]);
}

throw new Exception\RuntimeException("Cannot handle content type '{$this->encType}' automatically");
break;
// Encode files
foreach ($this->getRequest()->file()->toArray() as $key => $file) {
$fhead = array('Content-Type' => $file['ctype']);
$body .= $this->encodeFormData($boundary, $file['formname'], $file['data'], $file['filename'], $fhead);
}
$body .= "--{$boundary}--\r\n";
} else if(stripos($this->getEncType(), self::ENC_URLENCODED) === 0) {
// Encode body as application/x-www-form-urlencoded
$body = http_build_query($this->getRequest()->post()->toArray());
} else {
if (isset($mbIntEnc)) {
mb_internal_encoding($mbIntEnc);
}
throw new Exception\RuntimeException("Cannot handle content type '{$this->encType}' automatically");
}

}

if (isset($mbIntEnc)) {
Expand Down
25 changes: 23 additions & 2 deletions tests/Zend/Http/Client/CommonHttpTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ public function testAbsolutePathRedirect()
$this->client->setConfig(array('maxredirects' => 1));

// Get the host and port part of our baseuri
$uri = $this->client->getUri()->getScheme() . '://' . $this->client->getUri()->getHost() . ':' .
$this->client->getUri()->getPort();
$port = ($this->client->getUri()->getPort() == 80) ? '' : ':' .$this->client->getUri()->getPort();
$uri = $this->client->getUri()->getScheme() . '://' . $this->client->getUri()->getHost() . $port;

$res = $this->client->send();

Expand Down Expand Up @@ -916,6 +916,27 @@ public function testZF9404DoubleContentLengthHeader()
$this->assertEquals($expect, strlen($response->getBody()));
}


/**
* @group ZF2-78
* @dataProvider parameterArrayProvider
*/
public function testContentTypeAdditionlInfo($params)
{
$this->client->setUri($this->baseuri . 'testPostData.php');
$this->client->setHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
));
$this->client->setMethod(\Zend\Http\Request::METHOD_POST);

$this->client->setParameterPost($params);

$this->client->send();

$res = $this->client->send();
$this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
}

/**
* Internal helpder function to get the contents of test files
*
Expand Down

0 comments on commit a886f80

Please sign in to comment.