You are here

function httprl_set_socket in HTTP Parallel Request & Threading Library 6

Same name and namespace in other branches
  1. 7 httprl.module \httprl_set_socket()

Create the TCP/SSL socket connection string.

Parameters

array $uri: Array from parse_url().

array &$options: Array containing options.

string $proxy_server: String containing the proxy servers host name if one is to be used.

object &$result: Result object; used only for error handling in this function.

Return value

string String containing the TCP/SSL socket connection URI.

1 call to httprl_set_socket()
httprl_request in ./httprl.module
Queue up a HTTP request in httprl_send_request.

File

./httprl.module, line 640
HTTP Parallel Request Library module.

Code

function httprl_set_socket($uri, &$options, $proxy_server, &$result) {
  $socket = '';
  switch ($uri['scheme']) {
    case 'proxy':

      // Make the socket connection to a proxy server.
      $socket = 'tcp://' . $proxy_server . ':' . httprl_variable_get('proxy_port', 8080);

      // The Host header still needs to match the real request.
      $options['headers']['Host'] = $uri['host'];
      $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
      break;
    case 'http':
    case 'feed':
      $port = isset($uri['port']) ? $uri['port'] : 80;
      $socket = 'tcp://' . $uri['host'] . ':' . $port;

      // RFC 2616: "non-standard ports MUST, default ports MAY be included".
      // We don't add the standard port to prevent from breaking rewrite rules
      // checking the host that do not take into account the port number.
      if (empty($options['headers']['Host'])) {
        $options['headers']['Host'] = $uri['host'];
      }
      if ($port != 80) {
        $options['headers']['Host'] .= ':' . $port;
      }
      break;
    case 'https':

      // Note: Only works when PHP is compiled with OpenSSL support.
      $port = isset($uri['port']) ? $uri['port'] : 443;
      $socket = $options['secure_socket_transport'] . '://' . $uri['host'] . ':' . $port;
      if (empty($options['headers']['Host'])) {
        $options['headers']['Host'] = $uri['host'];
      }
      if ($port != 443) {
        $options['headers']['Host'] .= ':' . $port;
      }

      // Disable SNI support as this causes issues with old versions of OpenSSL.
      // By default httprl doesn't validate the SSL certificate, so this is OK.
      if (empty($options['context'])) {
        $drupal_ssl_context_options = variable_get('drupal_ssl_context_options', array(
          'verify_peer' => TRUE,
        ));

        // Affected versions of openssl are 1.0.0i to 1.0.1b.
        if (!defined('OPENSSL_VERSION_NUMBER') || OPENSSL_VERSION_NUMBER >= 0x1000009f && OPENSSL_VERSION_NUMBER <= 0x1000102f) {
          $drupal_ssl_context_options += array(
            'SNI_enabled' => FALSE,
          );
        }
        $options['context'] = stream_context_create(array(
          'ssl' => $drupal_ssl_context_options,
        ));
      }
      break;
    default:

      // If the t function is not available use httprl_pr.
      $t = function_exists('t') ? 't' : 'httprl_pr';
      $result->error = $t('Invalid schema @scheme.', array(
        '@scheme' => $uri['scheme'],
      ));
      $result->code = HTTPRL_URL_INVALID_SCHEMA;
  }
  return $socket;
}