22 require_once
'HTTP/Request2/Exception.php';
78 if (!empty($contextOptions)
79 && !isset($contextOptions[
'socket']) && !isset($contextOptions[
'ssl'])
82 $contextOptions = array(
'ssl' => $contextOptions);
84 $context = stream_context_create();
85 foreach ($contextOptions as $wrapper => $options) {
86 foreach ($options as $name => $value) {
87 if (!stream_context_set_option($context, $wrapper, $name, $value)) {
89 "Error setting '{$wrapper}' wrapper context option '{$name}'"
94 set_error_handler(array($this,
'connectionWarningsHandler'));
95 $this->socket = stream_socket_client(
96 $address, $errno, $errstr,
$timeout, STREAM_CLIENT_CONNECT, $context
98 restore_error_handler();
103 if ($this->connectionWarnings) {
105 fclose($this->socket);
107 $error = $errstr ? $errstr : implode(
"\n", $this->connectionWarnings);
109 "Unable to connect to {$address}. Error: {$error}", 0, $errno
119 fclose($this->socket);
132 if ($this->deadline) {
133 stream_set_timeout($this->socket, max($this->deadline - time(), 1));
135 $data = fread($this->socket, $length);
153 public function readLine($bufferSize, $localTimeout = null)
156 while (!feof($this->socket)) {
157 if (null !== $localTimeout) {
158 stream_set_timeout($this->socket, $localTimeout);
159 }
elseif ($this->deadline) {
160 stream_set_timeout($this->socket, max($this->deadline - time(), 1));
163 $line .= @fgets($this->socket, $bufferSize);
165 if (null === $localTimeout) {
169 $info = stream_get_meta_data($this->socket);
172 if (!$this->deadline) {
173 $default = (int)@ini_get(
'default_socket_timeout');
174 stream_set_timeout($this->socket, $default > 0 ? $default : PHP_INT_MAX);
176 if ($info[
'timed_out']) {
182 if (substr($line, -1) ==
"\n") {
183 return rtrim($line,
"\r\n");
199 if ($this->deadline) {
200 stream_set_timeout($this->socket, max($this->deadline - time(), 1));
202 $written = fwrite($this->socket, $data);
205 if ($written < strlen($data)) {
218 return feof($this->socket);
243 STREAM_CRYPTO_METHOD_TLS_CLIENT,
244 STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
245 STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
246 STREAM_CRYPTO_METHOD_SSLv2_CLIENT
249 foreach ($modes as $mode) {
250 if (stream_socket_enable_crypto($this->socket,
true, $mode)) {
255 'Failed to enable secure connection when connecting through proxy'
266 $info = stream_get_meta_data($this->socket);
267 if ($info[
'timed_out'] || $this->deadline && time() > $this->deadline) {
268 $reason = $this->deadline
269 ?
"after {$this->timeout} second(s)"
270 :
'due to default_socket_timeout php.ini setting';
291 if ($errno & E_WARNING) {
292 array_unshift($this->connectionWarnings, $errstr);