3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
20 * @package thrift.transport
23 namespace Thrift\Transport
;
25 use Thrift\Exception\TException
;
26 use Thrift\Exception\TTransportException
;
27 use Thrift\Factory\TStringFuncFactory
;
30 * Sockets implementation of the TTransport interface.
32 * @package thrift.transport
34 class TSSLSocket
extends TSocket
41 protected $context_ = null;
46 * @param string $host Remote hostname
47 * @param int $port Remote port
48 * @param resource $context Stream context
49 * @param bool $persist Whether to use a persistent socket
50 * @param string $debugHandler Function to call for error logging
52 public function __construct(
58 $this->host_
= $this->getSSLHost($host);
60 $this->context_
= $context;
61 $this->debugHandler_
= $debugHandler ?
$debugHandler : 'error_log';
65 * Creates a host name with SSL transport protocol
66 * if no transport protocol already specified in
69 * @param string $host Host to listen on
70 * @return string $host Host name with transport protocol
72 private function getSSLHost($host)
74 $transport_protocol_loc = strpos($host, "://");
75 if ($transport_protocol_loc === false) {
76 $host = 'ssl://' . $host;
82 * Connects the socket.
84 public function open()
86 if ($this->isOpen()) {
87 throw new TTransportException('Socket already connected', TTransportException
::ALREADY_OPEN
);
90 if (empty($this->host_
)) {
91 throw new TTransportException('Cannot open null host', TTransportException
::NOT_OPEN
);
94 if ($this->port_
<= 0) {
95 throw new TTransportException('Cannot open without port', TTransportException
::NOT_OPEN
);
98 $this->handle_
= @stream_socket_client
(
99 $this->host_
. ':' . $this->port_
,
102 $this->sendTimeoutSec_ +
($this->sendTimeoutUsec_
/ 1000000),
103 STREAM_CLIENT_CONNECT
,
108 if ($this->handle_
=== false) {
109 $error = 'TSocket: Could not connect to ' .
110 $this->host_
. ':' . $this->port_
. ' (' . $errstr . ' [' . $errno . '])';
112 call_user_func($this->debugHandler_
, $error);
114 throw new TException($error);