blob: b4a0adb54305b12fe2f3fd42416c85b3ae39828a [file] [log] [blame]
jfarrell311c9842016-09-20 13:30:16 -04001<?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
23namespace Thrift\Transport;
24
25use Thrift\Exception\TException;
26use Thrift\Exception\TTransportException;
27use Thrift\Factory\TStringFuncFactory;
28
29/**
30 * Sockets implementation of the TTransport interface.
31 *
32 * @package thrift.transport
33 */
34class TSSLSocket extends TSocket
35{
Robert Lub03ca012018-01-18 19:06:39 +080036 /**
37 * Remote port
38 *
39 * @var resource
40 */
41 protected $context_ = null;
jfarrell311c9842016-09-20 13:30:16 -040042
Robert Lub03ca012018-01-18 19:06:39 +080043 /**
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';
jfarrell311c9842016-09-20 13:30:16 -040062 }
63
Robert Lub03ca012018-01-18 19:06:39 +080064 /**
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;
jfarrell311c9842016-09-20 13:30:16 -040079 }
80
Robert Lub03ca012018-01-18 19:06:39 +080081 /**
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 }
jfarrell311c9842016-09-20 13:30:16 -040089
Robert Lub03ca012018-01-18 19:06:39 +080090 if (empty($this->host_)) {
91 throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN);
92 }
jfarrell311c9842016-09-20 13:30:16 -040093
Robert Lub03ca012018-01-18 19:06:39 +080094 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 }
jfarrell311c9842016-09-20 13:30:16 -0400116 }
jfarrell311c9842016-09-20 13:30:16 -0400117}