background_process.http.inc in Background Process 7.2
This contains the HTTP functions for Background Process.
File
background_process.http.incView source
<?php
/**
* @file
* This contains the HTTP functions for Background Process.
*/
/**
* Build url and headers for http request
* @param $url
* Relative url for the request
* @param $service_hostname
* Name of service host, e.g. 'default'
* @return array
* array(url, headers)
*/
function background_process_build_request($url, $service_hostname = NULL, $options = array()) {
$service_hosts = background_process_get_service_hosts();
if (!$service_hostname || empty($service_hosts[$service_hostname])) {
$service_hostname = 'default';
}
$service_host = $service_hosts[$service_hostname];
$options += array(
'absolute' => TRUE,
'base_url' => $service_host['base_url'],
);
$url = url($url, $options);
$parsed = parse_url($url);
$host = !empty($service_host['http_host']) ? $service_host['http_host'] : (isset($parsed['host']) ? $parsed['host'] : NULL);
$headers = _background_process_request_headers();
$headers = _background_process_filter_headers($headers);
$headers['Host'] = $host;
$headers['Connection'] = 'Close';
if (isset($parsed['user'])) {
$headers['Authorization'] = 'Basic ' . base64_encode($parsed['user'] . ':' . $parsed['pass']);
}
return array(
$url,
$headers,
);
}
/**
* Transform header array from key/value to strings.
* @param $headers
* array of headers in key/value style
* @return array
* array of strings.
*/
function background_process_build_headers($headers) {
$header = array();
foreach ($headers as $key => $value) {
$header[] = "{$key}: {$value}";
}
return $header;
}
/**
* Perform an http request.
* @see drupal_http_request()
*/
function background_process_http_request($url, array $options = array()) {
// Parse the URL and make sure we can handle the schema.
$result = new stdClass();
$result->url = $url;
$result->options = $options;
$uri = @parse_url($url);
$result->uri = $uri;
if ($uri == FALSE) {
$result->error = 'unable to parse URL';
$result->code = -1001;
return _background_process_http_request_result($result);
}
if (!isset($uri['scheme'])) {
$result->error = 'missing schema';
$result->code = -1002;
return _background_process_http_request_result($result);
}
// Merge the default options.
$options += array(
'headers' => array(),
'method' => 'GET',
'data' => NULL,
'max_redirects' => 3,
'timeout' => variable_get('background_process_connection_timeout', BACKGROUND_PROCESS_CONNECTION_TIMEOUT),
'context' => NULL,
'blocking' => FALSE,
'postpone' => FALSE,
);
// stream_socket_client() requires timeout to be a float.
$options['timeout'] = (double) $options['timeout'];
$host = NULL;
switch ($uri['scheme']) {
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.
$host = $uri['host'] . ($port != 80 ? ':' . $port : '');
break;
case 'https':
// Note: Only works when PHP is compiled with OpenSSL support.
$port = isset($uri['port']) ? $uri['port'] : 443;
$socket = 'ssl://' . $uri['host'] . ':' . $port;
$host = $uri['host'] . ($port != 443 ? ':' . $port : '');
break;
default:
$result->error = 'invalid schema ' . $uri['scheme'];
$result->code = -1003;
return _background_process_http_request_result($result);
}
if (!empty($host) && empty($options['headers']['Host'])) {
$options['headers']['Host'] = $host;
}
$result->options = $options;
$result->socket = $socket;
$result->postponed = $options['postpone'];
if ($result->postponed) {
return $result;
}
else {
return background_process_http_request_initiate($result);
}
}
/**
* Initiate the http request.
*/
function background_process_http_request_initiate(&$result) {
timer_start(__FUNCTION__);
$options = $result->options;
$socket = $result->socket;
$uri = $result->uri;
$result->start = microtime(TRUE);
$result->data_ready = TRUE;
if (empty($options['context'])) {
$fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
}
else {
// Create a stream with context. Allows verification of a SSL certificate.
$fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
}
// Make sure the socket opened properly.
if (!$fp) {
// When a network error occurs, we use a negative number so it does not
// clash with the HTTP status codes.
$result->code = -$errno;
$result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array(
'@socket' => $socket,
));
// Mark that this request failed. This will trigger a check of the web
// server's ability to make outgoing HTTP requests the next time that
// requirements checking is performed.
// See system_requirements()
// @fixme Disabled for Background Process
// variable_set('drupal_http_request_fails', TRUE);
return _background_process_http_request_result($result);
}
$result->fp = $fp;
$fps =& drupal_static('background_process_fps', array());
if (empty($fps['shutdown registered'])) {
drupal_register_shutdown_function('background_process_http_shutdown');
$fps['shutdown registered'] = TRUE;
}
$fps[(string) $fp] = $fp;
// Construct the path to act on.
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
// Merge the default headers.
$options['headers'] += array(
'User-Agent' => 'Drupal (+http://drupal.org/)',
);
// Only add Content-Length if we actually have any content or if it is a POST
// or PUT request. Some non-standard servers get confused by Content-Length in
// at least HEAD/GET requests, and Squid always requires Content-Length in
// POST/PUT requests.
$content_length = strlen($options['data']);
if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
$options['headers']['Content-Length'] = $content_length;
}
// If the server URL has a user then attempt to use basic authentication.
if (isset($uri['user'])) {
$options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
}
// If the database prefix is being used by SimpleTest to run the tests in a copied
// database then set the user-agent header to the database prefix so that any
// calls to other Drupal pages will run the SimpleTest prefixed database. The
// user-agent is used to ensure that multiple testing sessions running at the
// same time won't interfere with each other as they would if the database
// prefix were stored statically in a file or database variable.
$test_info =& $GLOBALS['drupal_test_info'];
if (!empty($test_info['test_run_id'])) {
$options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
}
$request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
foreach ($options['headers'] as $name => $value) {
$request .= $name . ': ' . trim($value) . "\r\n";
}
$request .= "\r\n" . $options['data'];
$result->request = $request;
// Calculate how much time is left of the original timeout value.
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
if ($timeout > 0) {
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
fwrite($fp, $request);
stream_set_blocking($fp, 0);
}
if (!empty($options['blocking'])) {
return background_process_http_request_get_response($result);
}
return $result;
}
/**
* Get response for an http request
*/
function background_process_http_request_get_response(&$result) {
if ($result->postponed) {
$result->postponed = FALSE;
return background_process_http_request_initiate($result);
}
if (isset($result->code)) {
return $result;
}
$fp = $result->fp;
$options = $result->options;
timer_start(__FUNCTION__);
if (!empty($options['blocking'])) {
stream_set_blocking($fp, 1);
}
// Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
// and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
// instead must invoke stream_get_meta_data() each iteration.
$info = stream_get_meta_data($fp);
$alive = !$info['eof'] && !$info['timed_out'];
while ($alive) {
// Calculate how much time is left of the original timeout value.
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
if ($timeout <= 0) {
$info['timed_out'] = TRUE;
break;
}
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
$chunk = fread($fp, 1024);
$result->response .= $chunk;
$result->data_ready = empty($chunk) ? FALSE : TRUE;
$info = stream_get_meta_data($fp);
$alive = !$info['eof'] && !$info['timed_out'];
if (empty($options['blocking'])) {
break;
}
}
if ($alive) {
return $result;
}
$fps =& drupal_static('background_process_fps', array());
unset($fps[(string) $fp]);
fclose($fp);
if ($info['timed_out']) {
$result->code = HTTP_REQUEST_TIMEOUT;
$result->error = 'request timed out';
return _background_process_http_request_result($result);
}
// Parse response headers from the response body.
// Be tolerant of malformed HTTP responses that separate header and body with
// \n\n or \r\r instead of \r\n\r\n.
list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $result->response, 2);
$response = preg_split("/\r\n|\n|\r/", $response);
// Parse the response status line.
list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
$result->protocol = $protocol;
$result->status_message = $status_message;
$result->headers = array();
// Parse the response headers.
while ($line = trim(array_shift($response))) {
list($name, $value) = explode(':', $line, 2);
$name = strtolower($name);
if (isset($result->headers[$name]) && $name == 'set-cookie') {
// RFC 2109: the Set-Cookie response header comprises the token Set-
// Cookie:, followed by a comma-separated list of one or more cookies.
$result->headers[$name] .= ',' . trim($value);
}
else {
$result->headers[$name] = trim($value);
}
}
$responses = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
);
// RFC 2616 states that all unknown HTTP codes must be treated the same as the
// base code in their class.
if (!isset($responses[$code])) {
$code = floor($code / 100) * 100;
}
$result->code = $code;
switch ($code) {
case 200:
// OK
case 304:
// Not modified
break;
case 301:
// Moved permanently
case 302:
// Moved temporarily
case 307:
// Moved temporarily
$location = $result->headers['location'];
$options['timeout'] -= timer_read(__FUNCTION__) / 1000;
if ($options['timeout'] <= 0) {
$result->code = -1;
$result->error = 'request timed out';
}
elseif ($options['max_redirects']) {
// Redirect to the new location.
$options['max_redirects']--;
$result = background_process_http_request($location, $options);
if (empty($result->error)) {
background_process_http_request_get_response($result);
}
$result->redirect_code = $code;
}
if (!isset($result->redirect_url)) {
$result->redirect_url = $location;
}
break;
default:
$result->error = $status_message;
}
return _background_process_http_request_result($result);
}
function _background_process_http_request_result($result) {
if (isset($result->code)) {
if (empty($result->end)) {
$result->end = microtime(TRUE);
}
if (!empty($result->options['callback']) && is_callable($result->options['callback'])) {
call_user_func($result->options['callback'], $result);
}
}
return $result;
}
/**
* Process multiple http requests.
*/
function background_process_http_request_process(&$results, $options = array()) {
$options += array(
'timeout' => 30,
'interval' => 0.01,
'limit' => 0,
);
$interval = $options['interval'] * 1000000;
$expire = time() + $options['timeout'];
while ($results && time() < $expire) {
$cnt = 0;
$data_ready = FALSE;
foreach ($results as $i => &$result) {
if (isset($result->code)) {
continue;
}
background_process_http_request_get_response($result);
$data_ready = $data_ready || $result->data_ready ? TRUE : FALSE;
$cnt++;
if ($options['limit'] && $cnt >= $options['limit']) {
break;
}
}
if (!$cnt) {
break;
}
if (!$data_ready) {
usleep($interval);
}
}
}
/**
* Get request headers
*
* @return array headers
*/
function _background_process_request_headers() {
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) == 'HTTP_') {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
if (empty($headers[$key])) {
$headers[$key] = $value;
}
else {
$headers[$key] .= "; {$value}";
}
}
}
return $headers;
}
/**
* Remove headers we do not wish to pass on to the next request.
*
* @param $headers
* Headers to filter
* @return array
* Filtered headers
*/
function _background_process_filter_headers($headers) {
$result = array();
if (empty($headers)) {
return $result;
}
foreach ($headers as $key => $value) {
if (!preg_match('/^(Connection|Keep-Alive|Proxy-Authenticate|Proxy-Authorization|TE|Trailers|Transfer-Encoding|Upgrade|Set-Cookie|Content-Length|Host|Accept-Encoding)$/i', $key)) {
$result[$key] = $value;
}
}
return $result;
}
/**
* Secure a URL by obfuscating the password if present.
*
* @param $url
* @return string
* URL
*/
function _background_process_secure_url($url) {
$url = parse_url($url);
if (!empty($url['pass'])) {
$url['pass'] = 'XXXXXXXX';
}
return _background_process_unparse_url($url);
}
/**
* Reverse logic of parse_url().
*
* @param $parsed_url
* Array from parse_url()
* @return string
* URL
*/
function _background_process_unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = $user || $pass ? "{$pass}@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "{$scheme}{$user}{$pass}{$host}{$port}{$path}{$query}{$fragment}";
}
/**
* This is just to make sure that we close all the file handles that we've opened
*/
function background_process_http_shutdown() {
$fps =& drupal_static('background_process_fps', array());
foreach ($fps as $idx => $fp) {
if (is_resource($fp)) {
fclose($fp);
}
}
}
Functions
Name | Description |
---|---|
background_process_build_headers | Transform header array from key/value to strings. |
background_process_build_request | Build url and headers for http request |
background_process_http_request | Perform an http request. |
background_process_http_request_get_response | Get response for an http request |
background_process_http_request_initiate | Initiate the http request. |
background_process_http_request_process | Process multiple http requests. |
background_process_http_shutdown | This is just to make sure that we close all the file handles that we've opened |
_background_process_filter_headers | Remove headers we do not wish to pass on to the next request. |
_background_process_http_request_result | |
_background_process_request_headers | Get request headers |
_background_process_secure_url | Secure a URL by obfuscating the password if present. |
_background_process_unparse_url | Reverse logic of parse_url(). |