Skip to content

Commit

Permalink
[Network][Http] Fix ssl options
Browse files Browse the repository at this point in the history
  • Loading branch information
GeLoLabs committed Apr 22, 2015
1 parent 8a83521 commit f60632f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
23 changes: 16 additions & 7 deletions src/Network/Http/Adapter/Stream.php
Expand Up @@ -35,12 +35,19 @@ class Stream
protected $_context;

/**
* Array of options/content for the stream context.
* Array of options/content for the http stream context.
*
* @var array
*/
protected $_contextOptions;

/**
* Array of options/content for the ssl stream context.
*
* @var array
*/
protected $_sslContextOptions;

/**
* The stream resource.
*
Expand All @@ -67,6 +74,7 @@ public function send(Request $request, array $options)
$this->_stream = null;
$this->_context = [];
$this->_contextOptions = [];
$this->_sslContextOptions = [];
$this->_connectionErrors = [];

$this->_buildContext($request, $options);
Expand Down Expand Up @@ -119,9 +127,7 @@ protected function _buildContext(Request $request, $options)
if ($scheme === 'https') {
$this->_buildSslContext($request, $options);
}
$this->_context = stream_context_create([
'http' => $this->_contextOptions
]);
$this->_context = stream_context_create($this->contextOptions());
}

/**
Expand Down Expand Up @@ -225,12 +231,12 @@ protected function _buildSslContext(Request $request, $options)
if (!empty($options['ssl_verify_host'])) {
$url = $request->url();
$host = parse_url($url, PHP_URL_HOST);
$this->_contextOptions['CN_match'] = $host;
$this->_sslContextOptions['CN_match'] = $host;
}
foreach ($sslOptions as $key) {
if (isset($options[$key])) {
$name = substr($key, 4);
$this->_contextOptions[$name] = $options[$key];
$this->_sslContextOptions[$name] = $options[$key];
}
}
}
Expand Down Expand Up @@ -303,6 +309,9 @@ protected function _connectionErrorHandler($code, $message)
*/
public function contextOptions()
{
return $this->_contextOptions;
return [
'http' => $this->_contextOptions,
'ssl' => $this->_sslContextOptions,
];
}
}
18 changes: 9 additions & 9 deletions tests/TestCase/Network/Http/Adapter/StreamTest.php
Expand Up @@ -84,9 +84,9 @@ public function testBuildingContextHeader()
'Content-Type: application/json',
'Cookie: testing=value; utm_src=awesome',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertEquals($options['redirect'], $result['max_redirects']);
$this->assertTrue($result['ignore_errors']);
$this->assertEquals(implode("\r\n", $expected), $result['http']['header']);
$this->assertEquals($options['redirect'], $result['http']['max_redirects']);
$this->assertTrue($result['http']['ignore_errors']);
}

/**
Expand Down Expand Up @@ -114,8 +114,8 @@ public function testSendContextContentString()
'User-Agent: CakePHP',
'Content-Type: application/json',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertEquals($content, $result['content']);
$this->assertEquals(implode("\r\n", $expected), $result['http']['header']);
$this->assertEquals($content, $result['http']['content']);
}

/**
Expand All @@ -139,9 +139,9 @@ public function testSendContextContentArray()
'User-Agent: CakePHP',
'Content-Type: multipart/form-data; boundary="',
];
$this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
$this->assertContains('Content-Disposition: form-data; name="a"', $result['content']);
$this->assertContains('my value', $result['content']);
$this->assertStringStartsWith(implode("\r\n", $expected), $result['http']['header']);
$this->assertContains('Content-Disposition: form-data; name="a"', $result['http']['content']);
$this->assertContains('my value', $result['http']['content']);
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ public function testSendContextSsl()
'allow_self_signed' => false,
];
foreach ($expected as $k => $v) {
$this->assertEquals($v, $result[$k]);
$this->assertEquals($v, $result['ssl'][$k]);
}
}

Expand Down

0 comments on commit f60632f

Please sign in to comment.