From 8ff4483ee69aafbe1cb62a96d5dcd7ce20138220 Mon Sep 17 00:00:00 2001
From: Nathan Gass
Date: Mon, 19 Nov 2012 14:27:26 +0000
Subject: allow handler function per url which can abort in get_multi

---
 it_url.class | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/it_url.class b/it_url.class
index 1e61578..2739ba4 100644
--- a/it_url.class
+++ b/it_url.class
@@ -381,13 +381,18 @@ function get_multi($p=null)
 	);
 	$mh = curl_multi_init();
 
+	$urls = array();
 	foreach ($p['urls'] as $key => $url)
+		$urls[$key] = is_array($url) ? $url : array('url' => $url);
+
+	foreach ($urls as $key => $url)
 	{
-		$ch[$key] = curl_init();
-		curl_setopt($ch[$key], CURLOPT_URL, $url);
-		curl_setopt_array($ch[$key], $opts);
-		curl_multi_add_handle($mh, $ch[$key]);
-		$keys[$ch[$key]] = $key;
+		$handle = curl_init();
+		curl_setopt($handle, CURLOPT_URL, $url['url']);
+		curl_setopt_array($handle, $opts);
+		curl_multi_add_handle($mh, $handle);
+		$keys[$handle] = $key;
+		$handles[$key] = $handle;
 	}
 
 	$start = microtime(true);
@@ -398,8 +403,9 @@ function get_multi($p=null)
 		$mrc = curl_multi_exec($mh, $active);
 	} while ($mrc == CURLM_CALL_MULTI_PERFORM);
 
+	$results = array();
 	$timeout = 0.001;	# Very short timeout to work around problem with first select call on cURL 7.25.0
-	while ($active && $mrc == CURLM_OK) 
+	while (!$abort && $active && $mrc == CURLM_OK) 
 	{
 		if (curl_multi_select($mh, $timeout) != -1) 
 		{
@@ -410,9 +416,14 @@ function get_multi($p=null)
 				{
 					if ($info['msg'] == CURLMSG_DONE)
 					{
-						EDC('reqtimings', $keys[$info['handle']], $info['result'], (microtime(true) - $start) * 1000);
-						if ($info['result'] != CURLE_OK)
-							$error[$info['handle']] = $info['result'];
+						$key = $keys[$info['handle']];
+						EDC('reqtimings', $key, $info['result'], (microtime(true) - $start) * 1000);
+						if ($info['result'] == CURLE_OK)
+							$results[$key] = curl_multi_getcontent($info['handle']);
+						else
+							$results[$key] = false;
+						if (($handler = $urls[$keys[$info['handle']]]['handler']))
+							$abort = $handler($info['result'], $results[$key]);
 					}
 				}
 			} while ($mrc == CURLM_CALL_MULTI_PERFORM);
@@ -420,12 +431,9 @@ function get_multi($p=null)
 		$timeout = 0.1;	# Longer delay to avoid busy loop but shorter than default of 1s in case we stil hit cURL 7.25.0 problem
 	}
 
-	$results = array();
-	foreach ($p['urls'] as $key => $url)
-	{
-		$results[$key] = $error[$ch[$key]] ? false : curl_multi_getcontent($ch[$key]);
-		curl_multi_remove_handle($mh, $ch[$key]);
-		curl_close($ch[$key]);
+	foreach ($handles as $key => $handle) {
+		curl_multi_remove_handle($mh, $handle);
+		curl_close($handle);
 	}
 	curl_multi_close($mh);
 	return $results;
-- 
cgit v1.2.3