jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 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 | { |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 36 | /** |
| 37 | * Remote port |
| 38 | * |
| 39 | * @var resource |
| 40 | */ |
| 41 | protected $context_ = null; |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 42 | |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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'; |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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; |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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 | } |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 89 | |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 90 | if (empty($this->host_)) { |
| 91 | throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN); |
| 92 | } |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 93 | |
Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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 | } |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 116 | } |
jfarrell | 311c984 | 2016-09-20 13:30:16 -0400 | [diff] [blame] | 117 | } |