PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/service/lib/php/transport/THttpClient.php

#
PHP | 202 lines | 74 code | 23 blank | 105 comment | 11 complexity | 64fd95fc4bc036140646294f0b338511 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. * HTTP client for Thrift
  24. *
  25. * @package thrift.transport
  26. */
  27. class THttpClient extends TTransport {
  28. /**
  29. * The host to connect to
  30. *
  31. * @var string
  32. */
  33. protected $host_;
  34. /**
  35. * The port to connect on
  36. *
  37. * @var int
  38. */
  39. protected $port_;
  40. /**
  41. * The URI to request
  42. *
  43. * @var string
  44. */
  45. protected $uri_;
  46. /**
  47. * The scheme to use for the request, i.e. http, https
  48. *
  49. * @var string
  50. */
  51. protected $scheme_;
  52. /**
  53. * Buffer for the HTTP request data
  54. *
  55. * @var string
  56. */
  57. protected $buf_;
  58. /**
  59. * Input socket stream.
  60. *
  61. * @var resource
  62. */
  63. protected $handle_;
  64. /**
  65. * Read timeout
  66. *
  67. * @var float
  68. */
  69. protected $timeout_;
  70. /**
  71. * Make a new HTTP client.
  72. *
  73. * @param string $host
  74. * @param int $port
  75. * @param string $uri
  76. */
  77. public function __construct($host, $port=80, $uri='', $scheme = 'http') {
  78. if ((strlen($uri) > 0) && ($uri{0} != '/')) {
  79. $uri = '/'.$uri;
  80. }
  81. $this->scheme_ = $scheme;
  82. $this->host_ = $host;
  83. $this->port_ = $port;
  84. $this->uri_ = $uri;
  85. $this->buf_ = '';
  86. $this->handle_ = null;
  87. $this->timeout_ = null;
  88. }
  89. /**
  90. * Set read timeout
  91. *
  92. * @param float $timeout
  93. */
  94. public function setTimeoutSecs($timeout) {
  95. $this->timeout_ = $timeout;
  96. }
  97. /**
  98. * Whether this transport is open.
  99. *
  100. * @return boolean true if open
  101. */
  102. public function isOpen() {
  103. return true;
  104. }
  105. /**
  106. * Open the transport for reading/writing
  107. *
  108. * @throws TTransportException if cannot open
  109. */
  110. public function open() {}
  111. /**
  112. * Close the transport.
  113. */
  114. public function close() {
  115. if ($this->handle_) {
  116. @fclose($this->handle_);
  117. $this->handle_ = null;
  118. }
  119. }
  120. /**
  121. * Read some data into the array.
  122. *
  123. * @param int $len How much to read
  124. * @return string The data that has been read
  125. * @throws TTransportException if cannot read any more data
  126. */
  127. public function read($len) {
  128. $data = @fread($this->handle_, $len);
  129. if ($data === FALSE || $data === '') {
  130. $md = stream_get_meta_data($this->handle_);
  131. if ($md['timed_out']) {
  132. throw new TTransportException('THttpClient: timed out reading '.$len.' bytes from '.$this->host_.':'.$this->port_.'/'.$this->uri_, TTransportException::TIMED_OUT);
  133. } else {
  134. throw new TTransportException('THttpClient: Could not read '.$len.' bytes from '.$this->host_.':'.$this->port_.'/'.$this->uri_, TTransportException::UNKNOWN);
  135. }
  136. }
  137. return $data;
  138. }
  139. /**
  140. * Writes some data into the pending buffer
  141. *
  142. * @param string $buf The data to write
  143. * @throws TTransportException if writing fails
  144. */
  145. public function write($buf) {
  146. $this->buf_ .= $buf;
  147. }
  148. /**
  149. * Opens and sends the actual request over the HTTP connection
  150. *
  151. * @throws TTransportException if a writing error occurs
  152. */
  153. public function flush() {
  154. // God, PHP really has some esoteric ways of doing simple things.
  155. $host = $this->host_.($this->port_ != 80 ? ':'.$this->port_ : '');
  156. $headers = array('Host: '.$host,
  157. 'Accept: application/x-thrift',
  158. 'User-Agent: PHP/THttpClient',
  159. 'Content-Type: application/x-thrift',
  160. 'Content-Length: '.strlen($this->buf_));
  161. $options = array('method' => 'POST',
  162. 'header' => implode("\r\n", $headers),
  163. 'max_redirects' => 1,
  164. 'content' => $this->buf_);
  165. if ($this->timeout_ > 0) {
  166. $options['timeout'] = $this->timeout_;
  167. }
  168. $this->buf_ = '';
  169. $contextid = stream_context_create(array('http' => $options));
  170. $this->handle_ = @fopen($this->scheme_.'://'.$host.$this->uri_, 'r', false, $contextid);
  171. // Connect failed?
  172. if ($this->handle_ === FALSE) {
  173. $this->handle_ = null;
  174. $error = 'THttpClient: Could not connect to '.$host.$this->uri_;
  175. throw new TTransportException($error, TTransportException::NOT_OPEN);
  176. }
  177. }
  178. }
  179. ?>