Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 330a4c8

Browse files
committed
Merge pull request zendframework#567 from weierophinney/hotfix/less-strict-headers
Cast int and float to string when creating headers
2 parents d925ec1 + b07f4ea commit 330a4c8

File tree

10 files changed

+102
-44
lines changed

10 files changed

+102
-44
lines changed

library/Zend/Http/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,11 @@ protected function _validateHeaderValue($value, $recurse = true)
15921592
return;
15931593
}
15941594

1595+
// Cast integers and floats to strings for purposes of header representation.
1596+
if (is_int($value) || is_float($value)) {
1597+
$value = (string) $value;
1598+
}
1599+
15951600
if (! is_string($value) && (! is_object($value) || ! method_exists($value, '__toString'))) {
15961601
require_once 'Zend/Http/Exception.php';
15971602
throw new Zend_Http_Exception('Invalid header value detected');

tests/Zend/Cloud/Infrastructure/Adapter/Ec2Test.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ public function setUp()
9191
*/
9292
protected function loadResponse($name)
9393
{
94-
return file_get_contents($name);
94+
$response = file_get_contents($name);
95+
96+
// Line endings are sometimes an issue inside the canned responses; the
97+
// following is a negative lookbehind assertion, and replaces any \n
98+
// not preceded by \r with the sequence \r\n, ensuring that the message
99+
// is well-formed.
100+
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
95101
}
96102
/**
97103
* Get Config Array

tests/Zend/Cloud/Infrastructure/Adapter/RackspaceTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function setUp()
7474

7575
if (file_exists($filename)) {
7676
// authentication (from file)
77-
$content = file_get_contents(dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response');
78-
$this->httpClientAdapterTest->setResponse($content);
77+
$content = dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response';
78+
$this->httpClientAdapterTest->setResponse($this->loadResponse($content));
7979
$this->assertTrue($this->infrastructure->getAdapter()->authenticate(),'Authentication failed');
8080

8181
$this->httpClientAdapterTest->setResponse($this->loadResponse($filename));
@@ -91,7 +91,13 @@ public function setUp()
9191
*/
9292
protected function loadResponse($name)
9393
{
94-
return file_get_contents($name);
94+
$response = file_get_contents($name);
95+
96+
// Line endings are sometimes an issue inside the canned responses; the
97+
// following is a negative lookbehind assertion, and replaces any \n
98+
// not preceded by \r with the sequence \r\n, ensuring that the message
99+
// is well-formed.
100+
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
95101
}
96102
/**
97103
* Get Config Array

tests/Zend/Gdata/App/EntryTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class Zend_Gdata_App_EntryTest extends PHPUnit_Framework_TestCase
3939

4040
public function setUp()
4141
{
42-
$this->enryText = file_get_contents(
43-
'Zend/Gdata/App/_files/EntrySample1.xml',
44-
true);
45-
$this->httpEntrySample = file_get_contents(
46-
'Zend/Gdata/App/_files/EntrySampleHttp1.txt',
47-
true);
42+
$this->enryText = $this->loadResponse(
43+
dirname(__FILE__) . '/../App/_files/EntrySample1.xml'
44+
);
45+
$this->httpEntrySample = $this->loadResponse(
46+
dirname(__FILE__) . '/../App/_files/EntrySampleHttp1.txt'
47+
);
4848
$this->enry = new Zend_Gdata_App_Entry();
4949

5050
$this->adapter = new Test_Zend_Gdata_MockHttpClient();
@@ -53,6 +53,17 @@ public function setUp()
5353
$this->service = new Zend_Gdata_App($this->client);
5454
}
5555

56+
public function loadResponse($filename)
57+
{
58+
$response = file_get_contents($filename);
59+
60+
// Line endings are sometimes an issue inside the canned responses; the
61+
// following is a negative lookbehind assertion, and replaces any \n
62+
// not preceded by \r with the sequence \r\n, ensuring that the message
63+
// is well-formed.
64+
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
65+
}
66+
5667
public function testEmptyEntryShouldHaveEmptyExtensionsList()
5768
{
5869
$this->assertTrue(is_array($this->enry->extensionElements));

tests/Zend/Gdata/AppTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,36 @@ public function setUp()
4141
$this->expectedEtag = 'W/"CkcHQH8_fCp7ImA9WxRTGEw."';
4242
$this->expectedMajorProtocolVersion = 1;
4343
$this->expectedMinorProtocolVersion = 2;
44-
$this->httpEntrySample = file_get_contents(
45-
'Zend/Gdata/_files/AppSample1.txt',
46-
true);
47-
$this->httpEntrySampleWithoutVersion = file_get_contents(
48-
'Zend/Gdata/_files/AppSample2.txt',
49-
true);
50-
$this->httpFeedSample = file_get_contents(
51-
'Zend/Gdata/_files/AppSample3.txt',
52-
true);
53-
$this->httpFeedSampleWithoutVersion = file_get_contents(
54-
'Zend/Gdata/_files/AppSample4.txt',
55-
true);
44+
$this->httpEntrySample = $this->loadResponse(
45+
dirname(__FILE__) . '/_files/AppSample1.txt'
46+
);
47+
$this->httpEntrySampleWithoutVersion = $this->loadResponse(
48+
dirname(__FILE__) . '/_files/AppSample2.txt'
49+
);
50+
$this->httpFeedSample = $this->loadResponse(
51+
dirname(__FILE__) . '/_files/AppSample3.txt'
52+
);
53+
$this->httpFeedSampleWithoutVersion = $this->loadResponse(
54+
dirname(__FILE__) . '/_files/AppSample4.txt'
55+
);
5656

5757
$this->adapter = new Test_Zend_Gdata_MockHttpClient();
5858
$this->client = new Zend_Gdata_HttpClient();
5959
$this->client->setAdapter($this->adapter);
6060
$this->service = new Zend_Gdata_App($this->client);
6161
}
6262

63+
public function loadResponse($filename)
64+
{
65+
$response = file_get_contents($filename);
66+
67+
// Line endings are sometimes an issue inside the canned responses; the
68+
// following is a negative lookbehind assertion, and replaces any \n
69+
// not preceded by \r with the sequence \r\n, ensuring that the message
70+
// is well-formed.
71+
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
72+
}
73+
6374
public function testImportFile()
6475
{
6576
$feed = Zend_Gdata_App::importFile($this->fileName,

tests/Zend/Gdata/AuthSubTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,8 @@ public function testAuthSubRevokeTokenCatchesHttpClientException()
221221
public function testGetAuthSubTokenInfoReceivesSuccessfulResult()
222222
{
223223
$adapter = new Zend_Http_Client_Adapter_Test();
224-
$adapter->setResponse("HTTP/1.1 200 OK
225-
226-
Target=http://example.com
227-
Scope=http://example.com
228-
Secure=false");
224+
$response = "HTTP/1.1 200 OK\r\n\r\nTarget=http://example.com\nScope=http://example.com\nSecure=false";
225+
$adapter->setResponse($response);
229226

230227
$client = new Zend_Gdata_HttpClient();
231228
$client->setUri('http://example.com/AuthSub');

tests/Zend/Http/Client/StaticTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,14 @@ public function testRedirectWithTrailingSpaceInLocationHeaderZF11283()
689689

690690
$adapter = $this->_client->getAdapter(); /* @var $adapter Zend_Http_Client_Adapter_Test */
691691

692-
$adapter->setResponse(<<<RESPONSE
693-
HTTP/1.1 302 Redirect
694-
Content-Type: text/html; charset=UTF-8
695-
Location: /test
696-
Server: Microsoft-IIS/7.0
697-
Date: Tue, 19 Apr 2011 11:23:48 GMT
698-
699-
RESPONSE
700-
);
692+
$response = "HTTP/1.1 302 Redirect\r\n"
693+
. "Content-Type: text/html; charset=UTF-8\r\n"
694+
. "Location: /test\r\n"
695+
. "Server: Microsoft-IIS/7.0\r\n"
696+
. "Date: Tue, 19 Apr 2011 11:23:48 GMT\r\n\r\n"
697+
. "RESPONSE";
698+
699+
$adapter->setResponse($response);
701700

702701
$res = $this->_client->request('GET');
703702

tests/Zend/Http/CookieJarTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
*/
3636
class Zend_Http_CookieJarTest extends PHPUnit_Framework_TestCase
3737
{
38+
public function loadResponse($filename)
39+
{
40+
$message = file_get_contents($filename);
41+
// Line endings are sometimes an issue inside the canned responses; the
42+
// following is a negative lookbehind assertion, and replaces any \n
43+
// not preceded by \r with the sequence \r\n, ensuring that the message
44+
// is well-formed.
45+
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
46+
}
47+
3848
/**
3949
* Test we can add cookies to the jar
4050
*
@@ -83,8 +93,9 @@ public function testExceptAddInvalidCookie()
8393
public function testAddCookiesFromResponse()
8494
{
8595
$jar = new Zend_Http_Cookiejar();
86-
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
87-
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies');
96+
$res_str = $this->loadResponse(
97+
dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
98+
);
8899
$response = Zend_Http_Response::fromString($res_str);
89100

90101
$jar->addCookiesFromResponse($response, 'http://www.example.com');
@@ -442,8 +453,9 @@ public function testExceptGetMatchingCookiesInvalidUri()
442453
*/
443454
public function testFromResponse()
444455
{
445-
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
446-
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_single_cookie');
456+
$res_str = $this->loadResponse(
457+
dirname(realpath(__FILE__)) . '/_files/response_with_single_cookie'
458+
);
447459
$response = Zend_Http_Response::fromString($res_str);
448460

449461
$jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
@@ -457,8 +469,9 @@ public function testFromResponse()
457469
*/
458470
public function testFromResponseMultiHeader()
459471
{
460-
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
461-
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies');
472+
$res_str = $this->loadResponse(
473+
dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
474+
);
462475
$response = Zend_Http_Response::fromString($res_str);
463476

464477
$jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');

tests/Zend/Service/Audioscrobbler/AudioscrobblerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public function testCallInterceptMethodsRequireExactlyOneParameterAndThrowExcept
102102

103103
public static function readTestResponse($file)
104104
{
105-
return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $file);
105+
$message = file_get_contents(sprintf('%s/_files/%s', dirname(__FILE__), $file));
106+
// Line endings are sometimes an issue inside the canned responses; the
107+
// following is a negative lookbehind assertion, and replaces any \n
108+
// not preceded by \r with the sequence \r\n, ensuring that the message
109+
// is well-formed.
110+
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
106111
}
107112
}

tests/Zend/Service/Flickr/OfflineTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,12 @@ protected function _saveResponse($name)
534534
*/
535535
protected function _loadResponse($name)
536536
{
537-
return file_get_contents("$this->_filesPath/$name.response");
537+
$message = file_get_contents(sprintf('%s/%s.response', $this->_filesPath, $name));
538+
// Line endings are sometimes an issue inside the canned responses; the
539+
// following is a negative lookbehind assertion, and replaces any \n
540+
// not preceded by \r with the sequence \r\n, ensuring that the message
541+
// is well-formed.
542+
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
538543
}
539544
}
540545

0 commit comments

Comments
 (0)