/** * Opens and sends the actual request over the HTTP connection * * @throws TTransportException if a writing error occurs */ public function flush() { // God, PHP really has some esoteric ways of doing simple things. if (!self::$curlHandle) { register_shutdown_function(array('Thrift\\Transport\\TCurlClient', 'closeCurlHandle')); self::$curlHandle = curl_init(); curl_setopt(self::$curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$curlHandle, CURLOPT_BINARYTRANSFER, true); curl_setopt(self::$curlHandle, CURLOPT_USERAGENT, 'PHP/TCurlClient'); curl_setopt(self::$curlHandle, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt(self::$curlHandle, CURLOPT_SSL_VERIFYPEER, false); // FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set @curl_setopt(self::$curlHandle, CURLOPT_FOLLOWLOCATION, true); curl_setopt(self::$curlHandle, CURLOPT_MAXREDIRS, 1); } $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : ''); $fullUrl = $this->scheme_ . "://" . $host . $this->uri_; $headers = array('Host: ' . $host, 'Accept: application/x-thrift', 'User-Agent: PHP/THttpClient', 'Content-Type: application/x-thrift', 'Content-Length: ' . TStringFuncFactory::create()->strlen($this->request_), 'Authorization: Basic ' . $this->authorizationString); curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers); if ($this->timeout_ > 0) { curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_); } curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_); $this->request_ = ''; curl_setopt(self::$curlHandle, CURLOPT_URL, $fullUrl); $this->response_ = curl_exec(self::$curlHandle); // Connect failed? if (!$this->response_) { curl_close(self::$curlHandle); self::$curlHandle = null; $error = 'TCurlClient: Could not connect to ' . $fullUrl; throw new TTransportException($error, TTransportException::NOT_OPEN); } }
/** * Guarantees that the full amount of data is read. * * @return string The data, of exact length * @throws TTransportException if cannot read data */ public function readAll($len) { // return $this->read($len); $data = ''; $got = 0; while (($got = TStringFuncFactory::create()->strlen($data)) < $len) { $data .= $this->read($len - $got); } return $data; }
public static function readContextSerialize($buf) { // 判断是否是TBinaryProtocol $sz = unpack('N', $buf); if ((int) ($sz[1] & \Thrift\Protocol\TBinaryProtocol::VERSION_MASK) != (int) \Thrift\Protocol\TBinaryProtocol::VERSION_1) { return 0; } $totallength = \Thrift\Factory\TStringFuncFactory::create()->strlen($buf); global $context; $obj = new \Thrift\ContextSeralize($context); $transObj = new \Thrift\Transport\TMemoryBuffer($buf); $protocol = new \Thrift\Protocol\TBinaryProtocol($transObj); $flag = $obj->read($protocol); if ($flag) { return $totallength - \Thrift\Factory\TStringFuncFactory::create()->strlen($transObj->getBuffer()); } else { return 0; } }
/** * Opens and sends the actual request over the HTTP connection * * @throws TTransportException if a writing error occurs */ public function flush() { // God, PHP really has some esoteric ways of doing simple things. $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : ''); $headers = array('Host: ' . $host, 'Accept: application/x-thrift', 'User-Agent: PHP/THttpClient', 'Content-Type: application/x-thrift', 'Content-Length: ' . TStringFuncFactory::create()->strlen($this->buf_), 'Authorization: Basic ' . $this->authorizationString); $options = array('method' => 'POST', 'header' => implode("\r\n", $headers), 'max_redirects' => 1, 'content' => $this->buf_); if ($this->timeout_ > 0) { $options['timeout'] = $this->timeout_; } $this->buf_ = ''; $contextid = stream_context_create(array('http' => $options)); $this->handle_ = @fopen($this->scheme_ . '://' . $host . $this->uri_, 'r', false, $contextid); // Connect failed? if ($this->handle_ === FALSE) { $this->handle_ = null; $error = 'P13nTHttpClient: Could not connect to ' . $host . $this->uri_; throw new TTransportException($error, TTransportException::NOT_OPEN); } }
public function read($len) { if (!$this->read_) { return $this->_read($len); } if (Thrift\Factory\TStringFuncFactory::create()->strlen($this->rBuf_) === 0) { $this->readFrame(); } // Just return full buff if ($len >= Thrift\Factory\TStringFuncFactory::create()->strlen($this->rBuf_)) { $out = $this->rBuf_; $this->rBuf_ = null; return $out; } // Return TStringFuncFactory::create()->substr $out = Thrift\Factory\TStringFuncFactory::create()->substr($this->rBuf_, 0, $len); $this->rBuf_ = Thrift\Factory\TStringFuncFactory::create()->substr($this->rBuf_, $len); return $out; }
public function available() { return TStringFuncFactory::create()->strlen($this->buf_); }
/** * Write to the socket. * * @param string $buf The data to write */ public function write($buf) { $null = null; $write = array($this->handle_); // keep writing until all the data has been written while (TStringFuncFactory::create()->strlen($buf) > 0) { // wait for stream to become available for writing $writable = @stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_); if ($writable > 0) { // write buffer to stream $written = @stream_socket_sendto($this->handle_, $buf); if ($written === -1 || $written === false) { throw new TTransportException('TSocket: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes ' . $this->host_ . ':' . $this->port_); } // determine how much of the buffer is left to write $buf = TStringFuncFactory::create()->substr($buf, $written); } else { if ($writable === 0) { throw new TTransportException('TSocket: timed out writing ' . TStringFuncFactory::create()->strlen($buf) . ' bytes from ' . $this->host_ . ':' . $this->port_); } else { throw new TTransportException('TSocket: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes ' . $this->host_ . ':' . $this->port_); } } } }
public function writeString($value) { $len = TStringFuncFactory::create()->strlen($value); $result = $this->writeI32($len); if ($len) { $this->trans_->write($value, $len); } return $result + $len; }
/** * Writes the output buffer to the stream in the format of a 4-byte length * followed by the actual data. */ public function flush() { if (!$this->write_ || TStringFuncFactory::create()->strlen($this->wBuf_) == 0) { return $this->transport_->flush(); } $out = pack('N', TStringFuncFactory::create()->strlen($this->wBuf_)); $out .= $this->wBuf_; // Note that we clear the internal wBuf_ prior to the underlying write // to ensure we're in a sane state (i.e. internal buffer cleaned) // if the underlying write throws up an exception $this->wBuf_ = ''; $this->transport_->write($out); $this->transport_->flush(); }
/** * Writes the output buffer to the stream in the format of a 4-byte length * followed by the actual data. */ public function flush() { if (!$this->write_ || TStringFuncFactory::create()->strlen($this->wBuf_) == 0) { return $this->transport_->flush(); } if ($contextBuf = $this->writeContextSerialize()) { $this->wBuf_ = $contextBuf . $this->wBuf_; } // Note that we clear the internal wBuf_ prior to the underlying write // to ensure we're in a sane state (i.e. internal buffer cleaned) // if the underlying write throws up an exception $this->transport_->write(pack('N', TStringFuncFactory::create()->strlen($this->wBuf_)) . $this->wBuf_); $this->wBuf_ = ''; $this->transport_->flush(); }
public function writeI64($value) { // If we are in an I32 range, use the easy method below. if ($value > 4294967296 || $value < -4294967296) { // Convert $value to $hi and $lo $neg = $value < 0; if ($neg) { $value *= -1; } $hi = (int) $value >> 32; $lo = (int) $value & 0xffffffff; if ($neg) { $hi = ~$hi; $lo = ~$lo; if (($lo & (int) 0xffffffff) == (int) 0xffffffff) { $lo = 0; $hi++; } else { $lo++; } } // Now do the zigging and zagging. $xorer = 0; if ($neg) { $xorer = 0xffffffff; } $lowbit = $lo >> 31 & 1; $hi = $hi << 1 | $lowbit; $lo = $lo << 1; $lo = ($lo ^ $xorer) & 0xffffffff; $hi = ($hi ^ $xorer) & 0xffffffff; // now write out the varint, ensuring we shift both hi and lo $out = ""; while (true) { if (($lo & ~0x7f) === 0 && $hi === 0) { $out .= chr($lo); break; } else { $out .= chr($lo & 0xff | 0x80); $lo = $lo >> 7; $lo = $lo | $hi << 25; $hi = $hi >> 7; // Right shift carries sign, but we don't want it to. $hi = $hi & 127 << 25; } } $ret = TStringFuncFactory::create()->strlen($out); $this->trans_->write($out, $ret); return $ret; } else { return $this->writeVarint($this->toZigZag($value, 64)); } }
public function write($buf) { while (TStringFuncFactory::create()->strlen($buf) > 0) { $got = @fwrite($this->outStream_, $buf); if ($got === 0 || $got === FALSE) { throw new TException('TPhpStream: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes'); } $buf = TStringFuncFactory::create()->substr($buf, $got); } }
private function getFromBuffer_($len) { if (TStringFuncFactory::create()->strlen($this->readBuffer_) <= $len) { $return = $this->readBuffer_; $this->readBuffer_ = ''; } else { $return = TStringFuncFactory::create()->substr($this->readBuffer_, 0, $len); $this->readBuffer_ = TStringFuncFactory::create()->substr($this->readBuffer_, $len); } return $return; }
public function flush() { if (TStringFuncFactory::create()->strlen($this->wBuf_) > 0) { $this->transport_->write($this->wBuf_); $this->wBuf_ = ''; } $this->transport_->flush(); }
/** * Opens and sends the actual request over the HTTP connection * * @throws TTransportException if a writing error occurs */ public function flush() { $error = ''; if (!self::$curlHandle) { register_shutdown_function(array('Thrift\\Transport\\TCurlClient', 'closeCurlHandle')); self::$curlHandle = curl_init(); curl_setopt_array(self::$curlHandle, [CURLOPT_RETURNTRANSFER => true, CURLOPT_BINARYTRANSFER => true, CURLOPT_USERAGENT => 'PHP/TCurlClient', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 1, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0]); } // God, PHP really has some esoteric ways of doing simple things. $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : ''); $fullUrl = $this->scheme_ . "://" . $host . $this->uri_; $headers = array('Accept: application/x-thrift', 'Content-Type: application/x-thrift', 'Content-Length: ' . TStringFuncFactory::create()->strlen($this->request_)); curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers); if ($this->timeout_ > 0) { curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_); } curl_setopt_array(self::$curlHandle, [CURLOPT_POSTFIELDS => $this->request_, CURLOPT_URL => $fullUrl]); $this->request_ = ''; $this->response_ = curl_exec(self::$curlHandle); if ($this->response_) { $http_code = curl_getinfo(self::$curlHandle, CURLINFO_HTTP_CODE); if ($http_code >= 400) { $error = 'TCurlClient: Could not connect to ' . $fullUrl . ' : http_code : ' . $http_code; } } else { $error = 'TCurlClient: Could not connect to ' . $fullUrl . ' : ' . curl_error(self::$curlHandle); } if (!empty($error)) { curl_close(self::$curlHandle); self::$curlHandle = null; throw new TTransportException($error, TTransportException::NOT_OPEN); } }