]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/php/lib/Transport/TSSLSocket.php
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / php / lib / Transport / TSSLSocket.php
1 <?php
2 /*
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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
18 * under the License.
19 *
20 * @package thrift.transport
21 */
22
23 namespace Thrift\Transport;
24
25 use Thrift\Exception\TException;
26 use Thrift\Exception\TTransportException;
27 use Thrift\Factory\TStringFuncFactory;
28
29 /**
30 * Sockets implementation of the TTransport interface.
31 *
32 * @package thrift.transport
33 */
34 class TSSLSocket extends TSocket
35 {
36 /**
37 * Remote port
38 *
39 * @var resource
40 */
41 protected $context_ = null;
42
43 /**
44 * Socket constructor
45 *
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
51 */
52 public function __construct(
53 $host = 'localhost',
54 $port = 9090,
55 $context = null,
56 $debugHandler = null
57 ) {
58 $this->host_ = $this->getSSLHost($host);
59 $this->port_ = $port;
60 $this->context_ = $context;
61 $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
62 }
63
64 /**
65 * Creates a host name with SSL transport protocol
66 * if no transport protocol already specified in
67 * the host name.
68 *
69 * @param string $host Host to listen on
70 * @return string $host Host name with transport protocol
71 */
72 private function getSSLHost($host)
73 {
74 $transport_protocol_loc = strpos($host, "://");
75 if ($transport_protocol_loc === false) {
76 $host = 'ssl://' . $host;
77 }
78 return $host;
79 }
80
81 /**
82 * Connects the socket.
83 */
84 public function open()
85 {
86 if ($this->isOpen()) {
87 throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN);
88 }
89
90 if (empty($this->host_)) {
91 throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN);
92 }
93
94 if ($this->port_ <= 0) {
95 throw new TTransportException('Cannot open without port', TTransportException::NOT_OPEN);
96 }
97
98 $this->handle_ = @stream_socket_client(
99 $this->host_ . ':' . $this->port_,
100 $errno,
101 $errstr,
102 $this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
103 STREAM_CLIENT_CONNECT,
104 $this->context_
105 );
106
107 // Connect failed?
108 if ($this->handle_ === false) {
109 $error = 'TSocket: Could not connect to ' .
110 $this->host_ . ':' . $this->port_ . ' (' . $errstr . ' [' . $errno . '])';
111 if ($this->debug_) {
112 call_user_func($this->debugHandler_, $error);
113 }
114 throw new TException($error);
115 }
116 }
117 }