diff --git a/remote_stream_wrapper.inc b/remote_stream_wrapper.inc index 4f8358a..3659c21 100644 --- a/remote_stream_wrapper.inc +++ b/remote_stream_wrapper.inc @@ -572,7 +572,9 @@ class DrupalRemoteStreamWrapperHttp extends DrupalRemoteStreamWrapper { stream_wrapper_unregister($scheme); stream_wrapper_restore($scheme); - $this->handle = @fopen($this->uri, $mode, FALSE); + $this->updateStreamContext(); + + $this->handle = @fopen($this->uri, $mode, FALSE, $this->context); stream_wrapper_unregister($scheme); stream_wrapper_register($scheme, get_class($this)); @@ -581,6 +583,46 @@ class DrupalRemoteStreamWrapperHttp extends DrupalRemoteStreamWrapper { } /** + * Updates the stream context to match drupal_http_request(). + */ + protected function updateStreamContext() { + $options = stream_context_get_options($this->context); + + // Set options according to drupal_http_request(). + $options['http']['user_agent'] = 'Drupal (+http://drupal.org/)'; + $options['http']['timeout'] = 30.0; + $options['http']['max_redirects'] = 3; + + // Don't allow user-agent overriding. + unset($options['http']['header']['User-Agent']); + + // Use a proxy if one is defined and the host is not on the excluded list. + $proxy_server = variable_get('proxy_server', ''); + if ($proxy_server && _drupal_http_use_proxy(parse_url($this->uri, PHP_URL_HOST))) { + $options['http']['request_fulluri'] = TRUE; + $options['http']['proxy'] = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080); + + // Add in username and password to Proxy-Authorization header if needed. + if ($proxy_username = variable_get('proxy_username', '')) { + $proxy_password = variable_get('proxy_password', ''); + $options['http']['header']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ':' . $proxy_password : '')); + } + // Some proxies reject requests with any User-Agent headers, while others + // require a specific one. + $proxy_user_agent = variable_get('proxy_user_agent', ''); + // The default value matches neither condition. + if ($proxy_user_agent === NULL) { + unset($options['http']['user_agent']); + } + elseif ($proxy_user_agent) { + $options['http']['user_agent'] = $proxy_user_agent; + } + } + + stream_context_set_option($this->context, $options); + } + + /** * {@inheritdoc} */ public function stream_read($count) {