TSSLSocket.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. namespace Thrift\Transport;
  23. use Thrift\Exception\TException;
  24. use Thrift\Exception\TTransportException;
  25. use Thrift\Factory\TStringFuncFactory;
  26. /**
  27. * Sockets implementation of the TTransport interface.
  28. *
  29. * @package thrift.transport
  30. */
  31. class TSSLSocket extends TSocket
  32. {
  33. /**
  34. * Remote port
  35. *
  36. * @var resource
  37. */
  38. protected $context_ = null;
  39. /**
  40. * Socket constructor
  41. *
  42. * @param string $host Remote hostname
  43. * @param int $port Remote port
  44. * @param resource $context Stream context
  45. * @param bool $persist Whether to use a persistent socket
  46. * @param string $debugHandler Function to call for error logging
  47. */
  48. public function __construct($host='localhost',
  49. $port=9090,
  50. $context=null,
  51. $debugHandler=null) {
  52. $this->host_ = $this->getSSLHost($host);
  53. $this->port_ = $port;
  54. $this->context_ = $context;
  55. $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
  56. }
  57. /**
  58. * Creates a host name with SSL transport protocol
  59. * if no transport protocol already specified in
  60. * the host name.
  61. *
  62. * @param string $host Host to listen on
  63. * @return string $host Host name with transport protocol
  64. */
  65. private function getSSLHost($host)
  66. {
  67. $transport_protocol_loc = strpos($host, "://");
  68. if ($transport_protocol_loc === false) {
  69. $host = 'ssl://'.$host;
  70. }
  71. return $host;
  72. }
  73. /**
  74. * Connects the socket.
  75. */
  76. public function open()
  77. {
  78. if ($this->isOpen()) {
  79. throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN);
  80. }
  81. if (empty($this->host_)) {
  82. throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN);
  83. }
  84. if ($this->port_ <= 0) {
  85. throw new TTransportException('Cannot open without port', TTransportException::NOT_OPEN);
  86. }
  87. $this->handle_ = @stream_socket_client($this->host_.':'.$this->port_,
  88. $errno,
  89. $errstr,
  90. $this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
  91. STREAM_CLIENT_CONNECT,
  92. $this->context_);
  93. // Connect failed?
  94. if ($this->handle_ === FALSE) {
  95. $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])';
  96. if ($this->debug_) {
  97. call_user_func($this->debugHandler_, $error);
  98. }
  99. throw new TException($error);
  100. }
  101. }
  102. }