Newer
Older
Mike Carper
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* @file
* Handle HTTP Parallel Request Library installation and upgrade tasks.
*/
/**
* Implementation of hook_enable().
*/
function httprl_enable() {
}
/**
* Implementation of hook_disable().
*/
function httprl_disable() {
}
/**
* Implementation of hook_install().
*/
function httprl_install() {
}
/**
* Implementation of hook_uninstall().
*/
function httprl_uninstall() {
}
/**
* Implementation of hook_requirements().
*/
function httprl_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
$t = get_t();
if ($phase == 'runtime' || $phase == 'install') {
$function_list = array(
'stream_socket_client',
'stream_select',
'stream_set_blocking',
'stream_get_meta_data',
);
// Check each function to make sure it exists.
foreach ($function_list as $function_name) {
if (!function_exists($function_name)) {
$requirements['httprl_function_' . $function_name] = array(
Mike Carper
committed
'title' => $t('HTTPRL'),
'value' => $phase == 'install' ? FALSE : $function_name,
'severity' => REQUIREMENT_ERROR,
'description' => $t('<a href="!url">%name()</a> is disabled on this server. Please contact your hosting provider and see if they can re-enable this function for you.', array(
'!url' => 'http://php.net/' . str_replace('_', '-', $function_name),
'%name' => $function_name,
)),
);
}
}
}
if ($phase == 'runtime') {
$url_path = 'admin/httprl-test';
// Check that the menu router handler is working. If it's not working the rest
// of the tests are pointless.
$item = menu_get_item($url_path);
if (empty($item['page_callback']) || strpos($item['page_callback'], 'httprl') === FALSE) {
$item = str_replace(' ', ' ', nl2br(htmlentities(print_r($item, TRUE))));
$requirements['httprl_callback'] = array(
'title' => $t('HTTPRL - Menu Callback'),
'severity' => REQUIREMENT_WARNING,
'value' => $t('Flush your caches'),
'description' => $t('You need to flush your menu cache. The test callback for httprl is not there.'),
);
return $requirements;
}
// Test the non blocking url
global $base_root, $base_path, $conf;
$id = 'httprl_' . md5(mt_rand() . time());
// Build URL to point to admin/httprl-test on this server.
$url = httprl_build_url_self($url_path . '?key=' . md5(drupal_get_private_key()) . '&id=' . $id);
// Set the headers to point to this hostname.
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
);
// Add in the headers and set the blocking mode.
$options = array(
'headers' => $headers,
'blocking' => FALSE,
'timeout' => 6.0,
);
// Start the timer & Get a lock.
timer_start($id);
lock_acquire($id, 6);
// Queue up the requests & execute them.
httprl_request($url, $options);
$requests = httprl_send_request();
Mike Carper
committed
// Wait for the lock and stop the timer.
lock_wait($id);
$time = timer_stop($id);
if (($time['time'] / 1000) > 5) {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
$ip = variable_get('httprl_server_addr', FALSE);
$lasttime = '';
$lastrequests = '';
if (empty($ip)) {
$lasttime = $time;
$lastrequests = $requests;
$conf['httprl_server_addr'] = -1;
// If lock failed to release in time, try not using an IP when calling self.
$id = 'httprl_' . md5(mt_rand() . time());
// Build URL to point to admin/httprl-test on this server.
$url = httprl_build_url_self($url_path . '?key=' . md5(drupal_get_private_key()) . '&id=' . $id);
// Set the headers to point to this hostname.
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
);
// Add in the headers and set the blocking mode.
$options = array(
'headers' => $headers,
'blocking' => FALSE,
'timeout' => 6.0,
);
// Start the timer & Get a lock.
timer_start($id);
lock_acquire($id, 6);
// Queue up the requests & execute them.
httprl_request($url, $options);
$requests = httprl_send_request();
// Wait for the lock and stop the timer.
lock_wait($id);
$time = timer_stop($id);
}
if (($time['time'] / 1000) > 5) {
// Lock timed out and was not reset by the URL ping.
$requirements['httprl_nonblocking'] = array(
'title' => $t('HTTPRL - Non Blocking'),
'severity' => REQUIREMENT_WARNING,
'value' => $t('This server does not handle hanging connections.'),
'description' => $t('Using non blocking mode on this server my not work correctly. More info: !info', array('!info' => str_replace(' ', ' ', nl2br(htmlentities(print_r(array($time, $requests, $lasttime, $lastrequests), TRUE)))))),
);
}
else {
// Request worked when using the hostname not the ip.
variable_set('httprl_server_addr', -1);
}
Mike Carper
committed
}
}
if (empty($requirements)) {
$requirements['httprl'] = array(
'title' => $t('HTTPRL'),
'severity' => REQUIREMENT_OK,
'value' => $phase == 'install' ? TRUE : $t('All the required functions are enabled and non blocking requests are working.'),
);
}
Mike Carper
committed
return $requirements;
}