Beispiel #1
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_ : '');
     $user_agent = 'PHP/THttpClient';
     $script = urlencode(basename($_SERVER['SCRIPT_FILENAME']));
     if ($script) {
         $user_agent .= ' (' . $script . ')';
     }
     $curl = curl_init($this->scheme_ . '://' . $host . $this->uri_);
     $headers = array('Host: ' . $host, 'Accept: application/x-thrift', 'User-Agent: ' . $user_agent, 'Content-Type: application/x-thrift', 'Content-Length: ' . strlen($this->buf_));
     if (is_array($this->custom_headers_)) {
         foreach ($this->custom_headers_ as $header => $value) {
             $headers[] = $header . ': ' . $value;
         }
     }
     curl_setopt($curl, CURLOPT_PROXY, '');
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_POST, true);
     curl_setopt($curl, CURLOPT_MAXREDIRS, 1);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $this->buf_);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout_);
     $this->buf_ = '';
     $t_start = microtime(true);
     $this->data_ = curl_exec($curl);
     $t_delta = microtime(true) - $t_start;
     ProfilingCounters::incrCount(IProfilingCounters::THRIFT_FLUSH_COUNT);
     ProfilingCounters::incrDuration(IProfilingCounters::THRIFT_FLUSH_DURATION, $t_delta);
     $this->errstr_ = curl_error($curl);
     curl_close($curl);
     // Connect failed?
     if ($this->data_ === false) {
         $error = 'THttpClient: Could not connect to ' . $host . $this->uri_;
         throw new TTransportException($error, TTransportException::NOT_OPEN);
     }
 }
 public function doWrite()
 {
     $t_start = microtime(true);
     $got = @socket_write($this->handle_, $this->wBuf_);
     $t_delta = microtime(true) - $t_start;
     ProfilingCounters::incrCount(IProfilingCounters::THRIFT_WRITE_COUNT);
     ProfilingCounters::incrCount(IProfilingCounters::THRIFT_WRITE_BYTES, (int) $got);
     ProfilingCounters::incrDuration(IProfilingCounters::THRIFT_WRITE_DURATION, $t_delta);
     if ($got === 0 || $got === FALSE) {
         // Could not write
         $errno = socket_last_error($this->handle_);
         $errstr = socket_strerror($errno);
         $error = "doWrite: write failed ({$errno}): {$errstr} " . $this->host_ . ':' . $this->port_;
         throw new TTransportException($error);
     }
     $this->wBuf_ = substr($this->wBuf_, $got);
 }