Commit 61322d18 authored by mikeytown2's avatar mikeytown2

Issue #1325662 by mikeytown2: Allow httprl_request() calls to be altered.

parent 55a52d58
......@@ -312,3 +312,29 @@ D6 & D7.
echo httprl_pr(current($modules));
?>
Request mutiple URLs with one httprl_request() call. These URLs will all have
the same options.
<?php
// Set the blocking mode.
$options = array(
'method' => 'HEAD',
'blocking' => FALSE,
);
// Queue up the requests.
$max = 10;
$urls = array();
for ($i=1; $i <= $max; $i++) {
// Build URL to a page that doesn't exist.
$urls[] = httprl_build_url_self('asdf-asdf-asdf-' . $i);
}
// Queue up the requests.
httprl_request($urls, $options);
// Execute requests.
$request = httprl_send_request();
// Echo out the results.
echo httprl_pr($request);
?>
......@@ -260,7 +260,9 @@ function httprl_request($urls, $options = array()) {
unset($temp);
}
$connections = array();
$return = array();
// Set things up; but do not perform any IO.
foreach ($urls as $url) {
$result = new stdClass();
......@@ -389,7 +391,7 @@ function httprl_request($urls, $options = array()) {
$result->code = HTTPRL_URL_INVALID_SCHEMA;
// Add in failed request to the output.
// Exit if an invalid scheme was passed in.
// Continue if an invalid scheme was passed in.
$return[$url] = httprl_send_request(FALSE, $url, $result, $options);
continue;
}
......@@ -410,6 +412,62 @@ function httprl_request($urls, $options = array()) {
$flags = STREAM_CLIENT_CONNECT;
}
// Construct the path to act on.
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
// Encode data if not already done.
if (!is_null($options['data']) && !is_string($options['data'])) {
$options['data'] = http_build_query($options['data'], '', '&');
}
// 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 = httprl_strlen($options['data']);
if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
$options['headers']['Content-Length'] = $content_length;
}
// Set the Content-Type to application/x-www-form-urlencoded if the data is
// not empty, the Content-Type is not set, and the method is POST.
if ($content_length > 0 && !isset($options['headers']['Content-Type']) && $options['method'] == 'POST') {
$options['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
}
// 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'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
}
// Assemble the request together. HTTP version requires to be a float.
$request = $options['method'] . ' ' . $path . ' HTTP/' . sprintf("%.1F", $options['version']) . "\r\n";
foreach ($options['headers'] as $name => $value) {
$request .= $name . ': ' . trim($value) . "\r\n";
}
$request .= "\r\n" . $options['data'];
$connections[] = array($socket, $flags, $uri, $url, $options, $result, $request);
}
// hook_httprl_request_alter()
// Allow other programs to alter the connections before they are made.
drupal_alter('httprl_request', $data);
// Make the connection and queue up the request.
foreach ($connections as $connection) {
list($socket, $flags, $uri, $url, $options, $result, $request) = $connection;
if (isset($result->error)) {
// Add in failed request to the output.
// Continue if an error was triggered in the alter hook.
$return[$url] = httprl_send_request(FALSE, $url, $result, $options);
continue;
}
// Start the timer.
$timer_name = mt_rand();
timer_start($timer_name);
......@@ -479,44 +537,6 @@ function httprl_request($urls, $options = array()) {
// Set the stream to be non blocking.
stream_set_blocking($fp, 0);
// Construct the path to act on.
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
// Encode data if not already done.
if (!is_null($options['data']) && !is_string($options['data'])) {
$options['data'] = http_build_query($options['data'], '', '&');
}
// 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 = httprl_strlen($options['data']);
if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
$options['headers']['Content-Length'] = $content_length;
}
// Set the Content-Type to application/x-www-form-urlencoded if the data is
// not empty, the Content-Type is not set, and the method is POST.
if ($content_length > 0 && !isset($options['headers']['Content-Type']) && $options['method'] == 'POST') {
$options['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
}
// 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'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
}
// Assemble the request together. HTTP version requires to be a float.
$request = $options['method'] . ' ' . $path . ' HTTP/' . sprintf("%.1F", $options['version']) . "\r\n";
foreach ($options['headers'] as $name => $value) {
$request .= $name . ': ' . trim($value) . "\r\n";
}
$request .= "\r\n" . $options['data'];
// Put this request into the queue to be processed.
$return[$url] = httprl_send_request($fp, $url, $request, $options);
continue;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment