Esempio n. 1
0
 /**
  * Perform a request
  *
  * @throws Requests_Exception On failure to connect to socket (`fsockopenerror`)
  * @throws Requests_Exception On socket timeout (`timeout`)
  *
  * @param string $url URL to request
  * @param array $headers Associative array of request headers
  * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  * @param array $options Request options, see {@see Requests::response()} for documentation
  * @return string Raw HTTP result
  */
 public function request($url, $headers = array(), $data = array(), $options = array())
 {
     $options['hooks']->dispatch('fsockopen.before_request');
     $url_parts = parse_url($url);
     $host = $url_parts['host'];
     $context = stream_context_create();
     $verifyname = false;
     // HTTPS support
     if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') {
         $remote_socket = 'ssl://' . $host;
         $url_parts['port'] = 443;
         $context_options = array('verify_peer' => true, 'capture_peer_cert' => true);
         $verifyname = true;
         // SNI, if enabled (OpenSSL >=0.9.8j)
         if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) {
             $context_options['SNI_enabled'] = true;
             if (isset($options['verifyname']) && $options['verifyname'] === false) {
                 $context_options['SNI_enabled'] = false;
             }
         }
         if (isset($options['verify'])) {
             if ($options['verify'] === false) {
                 $context_options['verify_peer'] = false;
             } elseif (is_string($options['verify'])) {
                 $context_options['cafile'] = $options['verify'];
             }
         }
         if (isset($options['verifyname']) && $options['verifyname'] === false) {
             $verifyname = false;
         }
         stream_context_set_option($context, array('ssl' => $context_options));
     } else {
         $remote_socket = 'tcp://' . $host;
     }
     $this->max_bytes = $options['max_bytes'];
     $proxy = isset($options['proxy']);
     $proxy_auth = $proxy && isset($options['proxy_username']) && isset($options['proxy_password']);
     if (!isset($url_parts['port'])) {
         $url_parts['port'] = 80;
     }
     $remote_socket .= ':' . $url_parts['port'];
     set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE);
     $options['hooks']->dispatch('fsockopen.remote_socket', array(&$remote_socket));
     $fp = stream_socket_client($remote_socket, $errno, $errstr, ceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context);
     restore_error_handler();
     if ($verifyname) {
         if (!$this->verify_certificate_from_context($host, $context)) {
             throw new Requests_Exception('SSL certificate did not match the requested domain name', 'ssl.no_match');
         }
     }
     if (!$fp) {
         if ($errno === 0) {
             // Connection issue
             throw new Requests_Exception(rtrim($this->connect_error), 'fsockopen.connect_error');
         } else {
             throw new Requests_Exception($errstr, 'fsockopenerror', NULL, $errno);
             return;
         }
     }
     $request_body = '';
     $out = '';
     switch ($options['type']) {
         case Requests::POST:
         case Requests::PUT:
         case Requests::PATCH:
             if (isset($url_parts['path'])) {
                 $path = $url_parts['path'];
                 if (isset($url_parts['query'])) {
                     $path .= '?' . $url_parts['query'];
                 }
             } else {
                 $path = '/';
             }
             $options['hooks']->dispatch('fsockopen.remote_host_path', array(&$path, $url));
             $out = $options['type'] . " {$path} HTTP/1.0\r\n";
             if (is_array($data)) {
                 $request_body = http_build_query($data, null, '&');
             } else {
                 $request_body = $data;
             }
             if (empty($headers['Content-Length'])) {
                 $headers['Content-Length'] = strlen($request_body);
             }
             if (empty($headers['Content-Type'])) {
                 $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
             }
             break;
         case Requests::HEAD:
         case Requests::GET:
         case Requests::DELETE:
             $path = self::format_get($url_parts, $data);
             $options['hooks']->dispatch('fsockopen.remote_host_path', array(&$path, $url));
             $out = $options['type'] . " {$path} HTTP/1.0\r\n";
             break;
     }
     $out .= "Host: {$url_parts['host']}";
     if ($url_parts['port'] !== 80) {
         $out .= ":{$url_parts['port']}";
     }
     $out .= "\r\n";
     $out .= "User-Agent: {$options['useragent']}\r\n";
     $accept_encoding = $this->accept_encoding();
     if (!empty($accept_encoding)) {
         $out .= "Accept-Encoding: {$accept_encoding}\r\n";
     }
     $headers = Requests::flatten($headers);
     if (!empty($headers)) {
         $out .= implode($headers, "\r\n") . "\r\n";
     }
     $options['hooks']->dispatch('fsockopen.after_headers', array(&$out));
     if (substr($out, -2) !== "\r\n") {
         $out .= "\r\n";
     }
     $out .= "Connection: Close\r\n\r\n" . $request_body;
     $options['hooks']->dispatch('fsockopen.before_send', array(&$out));
     fwrite($fp, $out);
     $options['hooks']->dispatch('fsockopen.after_send', array(&$fake_headers));
     if (!$options['blocking']) {
         fclose($fp);
         $fake_headers = '';
         $options['hooks']->dispatch('fsockopen.after_request', array(&$fake_headers));
         return '';
     }
     $timeout_sec = (int) floor($options['timeout']);
     $timeout_msec = $timeout_sec == $options['timeout'] ? 0 : self::SECOND_IN_MICROSECONDS * $options['timeout'] % self::SECOND_IN_MICROSECONDS;
     stream_set_timeout($fp, $timeout_sec, $timeout_msec);
     $response = $body = $headers = '';
     $this->info = stream_get_meta_data($fp);
     $size = 0;
     $doingbody = false;
     $download = false;
     if ($options['filename']) {
         $download = fopen($options['filename'], 'wb');
     }
     while (!feof($fp)) {
         $this->info = stream_get_meta_data($fp);
         if ($this->info['timed_out']) {
             throw new Requests_Exception('fsocket timed out', 'timeout');
         }
         $block = fread($fp, Requests::BUFFER_SIZE);
         if (!$doingbody) {
             $response .= $block;
             if (strpos($response, "\r\n\r\n")) {
                 list($headers, $block) = explode("\r\n\r\n", $response, 2);
                 $doingbody = true;
             }
         }
         // Are we in body mode now?
         if ($doingbody) {
             $options['hooks']->dispatch('request.progress', array($block, $size, $this->max_bytes));
             $data_length = strlen($block);
             if ($this->max_bytes) {
                 // Have we already hit a limit?
                 if ($size === $this->max_bytes) {
                     continue;
                 }
                 if ($size + $data_length > $this->max_bytes) {
                     // Limit the length
                     $limited_length = $this->max_bytes - $size;
                     $block = substr($block, 0, $limited_length);
                 }
             }
             $size += strlen($block);
             if ($download) {
                 fwrite($download, $block);
             } else {
                 $body .= $block;
             }
         }
     }
     $this->headers = $headers;
     if ($download) {
         fclose($download);
     } else {
         $this->headers .= "\r\n\r\n" . $body;
     }
     fclose($fp);
     $options['hooks']->dispatch('fsockopen.after_request', array(&$this->headers));
     return $this->headers;
 }
Esempio n. 2
0
 /**
  * Setup the cURL handle for the given data
  *
  * @param string $url URL to request
  * @param array $headers Associative array of request headers
  * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  * @param array $options Request options, see {@see Requests::response()} for documentation
  */
 protected function setup_handle($url, $headers, $data, $options)
 {
     $options['hooks']->dispatch('curl.before_request', array(&$this->handle));
     // Force closing the connection for old versions of cURL (<7.22).
     if (!isset($headers['Connection'])) {
         $headers['Connection'] = 'close';
     }
     $headers = Requests::flatten($headers);
     if (!empty($data)) {
         $data_format = $options['data_format'];
         if ($data_format === 'query') {
             $url = self::format_get($url, $data);
             $data = '';
         } elseif (!is_string($data)) {
             $data = http_build_query($data, null, '&');
         }
     }
     switch ($options['type']) {
         case Requests::POST:
             curl_setopt($this->handle, CURLOPT_POST, true);
             curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
             break;
         case Requests::HEAD:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             curl_setopt($this->handle, CURLOPT_NOBODY, true);
             break;
         case Requests::TRACE:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             break;
         case Requests::PATCH:
         case Requests::PUT:
         case Requests::DELETE:
         case Requests::OPTIONS:
         default:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             if (!empty($data)) {
                 curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
             }
     }
     // cURL requires a minimum timeout of 1 second when using the system
     // DNS resolver, as it uses `alarm()`, which is second resolution only.
     // There's no way to detect which DNS resolver is being used from our
     // end, so we need to round up regardless of the supplied timeout.
     //
     // https://github.com/curl/curl/blob/4f45240bc84a9aa648c8f7243be7b79e9f9323a5/lib/hostip.c#L606-L609
     $timeout = max($options['timeout'], 1);
     if (is_int($timeout) || $this->version < self::CURL_7_16_2) {
         curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
     } else {
         curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
     }
     if (is_int($options['connect_timeout']) || $this->version < self::CURL_7_16_2) {
         curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
     } else {
         curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
     }
     curl_setopt($this->handle, CURLOPT_URL, $url);
     curl_setopt($this->handle, CURLOPT_REFERER, $url);
     curl_setopt($this->handle, CURLOPT_USERAGENT, $options['useragent']);
     curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
     if ($options['protocol_version'] === 1.1) {
         curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     } else {
         curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     }
     if (true === $options['blocking']) {
         curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
         curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
         curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
     }
 }
Esempio n. 3
0
 /**
  * Setup the cURL handle for the given data
  *
  * @param string $url URL to request
  * @param array $headers Associative array of request headers
  * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  * @param array $options Request options, see {@see Requests::response()} for documentation
  */
 protected function setup_handle($url, $headers, $data, $options)
 {
     $options['hooks']->dispatch('curl.before_request', array(&$this->fp));
     $headers = Requests::flatten($headers);
     if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
         $url = self::format_get($url, $data);
     } elseif (!empty($data) && !is_string($data)) {
         $data = http_build_query($data, null, '&');
     }
     switch ($options['type']) {
         case Requests::POST:
             curl_setopt($this->fp, CURLOPT_POST, true);
             curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
             break;
         case Requests::PATCH:
         case Requests::PUT:
             curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
             curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
             break;
         case Requests::DELETE:
             curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         case Requests::HEAD:
             curl_setopt($this->fp, CURLOPT_NOBODY, true);
             break;
     }
     if (is_int($options['timeout']) or $this->version < self::CURL_7_16_2) {
         curl_setopt($this->fp, CURLOPT_TIMEOUT, ceil($options['timeout']));
     } else {
         curl_setopt($this->fp, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000));
     }
     if (is_int($options['connect_timeout']) or $this->version < self::CURL_7_16_2) {
         curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
     } else {
         curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
     }
     curl_setopt($this->fp, CURLOPT_URL, $url);
     curl_setopt($this->fp, CURLOPT_REFERER, $url);
     curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
     curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
     if (true === $options['blocking']) {
         curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
         curl_setopt($this->fp, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
         curl_setopt($this->fp, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
     }
 }
Esempio n. 4
0
 /**
  * Setup the cURL handle for the given data
  *
  * @param string $url URL to request
  * @param array $headers Associative array of request headers
  * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  * @param array $options Request options, see {@see Requests::response()} for documentation
  */
 protected function setup_handle($url, $headers, $data, $options)
 {
     $options['hooks']->dispatch('curl.before_request', array(&$this->handle));
     // Force closing the connection for old versions of cURL (<7.22).
     if (!isset($headers['Connection'])) {
         $headers['Connection'] = 'close';
     }
     $headers = Requests::flatten($headers);
     if (!empty($data)) {
         $data_format = $options['data_format'];
         if ($data_format === 'query') {
             $url = self::format_get($url, $data);
             $data = '';
         } elseif (!is_string($data)) {
             $data = http_build_query($data, null, '&');
         }
     }
     switch ($options['type']) {
         case Requests::POST:
             curl_setopt($this->handle, CURLOPT_POST, true);
             curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
             break;
         case Requests::PATCH:
         case Requests::PUT:
         case Requests::DELETE:
         case Requests::OPTIONS:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
             break;
         case Requests::HEAD:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             curl_setopt($this->handle, CURLOPT_NOBODY, true);
             break;
         case Requests::TRACE:
             curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
             break;
     }
     if (is_int($options['timeout']) || $this->version < self::CURL_7_16_2) {
         curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($options['timeout']));
     } else {
         curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000));
     }
     if (is_int($options['connect_timeout']) || $this->version < self::CURL_7_16_2) {
         curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
     } else {
         curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
     }
     curl_setopt($this->handle, CURLOPT_URL, $url);
     curl_setopt($this->handle, CURLOPT_REFERER, $url);
     curl_setopt($this->handle, CURLOPT_USERAGENT, $options['useragent']);
     curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
     if ($options['protocol_version'] === 1.1) {
         curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     } else {
         curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     }
     if (true === $options['blocking']) {
         curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
         curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
         curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
     }
 }