diff --git a/README.txt b/README.txt index aa83565..be85c2e 100644 --- a/README.txt +++ b/README.txt @@ -313,7 +313,7 @@ D6 & D7. ?> -Request mutiple URLs with one httprl_request() call. These URLs will all have +Request multiple URLs with one httprl_request() call. These URLs will all have the same options. + + +Hit 4 different URLs, Using at least 2 that has a status code of 200 and +erroring out on the others that didn't return. Data is truncated as well. + + // Array of URLs to get. + $urls = array( + 'http://google.com/', + 'http://bing.com/', + 'http://yahoo.com/', + 'http://www.duckduckgo.com/', + 'http://www.drupal.org/', + ); + + // Process list of URLs. + $options = array( + 'alter_all_streams_function' => 'need_two_good_results', + 'callback' => array(array('function' => 'limit_data_size')), + ); + // Queue up the requests. + httprl_request($urls, $options); + + // Execute requests. + $requests = httprl_send_request(); + + // Print what was done. + echo httprl_pr($requests); + + function need_two_good_results($id, &$responses, &$streams, $current_time) { + static $counter = 0; + foreach ($responses as $id => &$result) { + // Skip if we got a 200. + if ($result->code == 200) { + $counter = $counter + 1; + continue; + } + if ($result->status == 'Done.') { + continue; + } + + if ($counter >= 2) { + // Set the code to request was aborted. + $result->code = HTTPRL_REQUEST_ABORTED; + $result->status = 'Done.'; + $result->options['timeout'] = $result->options['timeout'] - $current_time; + + // Close the file pointer and remove from the stream from the array. + fclose($streams[$id]); + unset($streams[$id]); + } + } + } + + function limit_data_size(&$result) { + // Only use the first and last 256 characters in the data array. + $result->data = substr($result->data, 0, 256) . "\n\n ... \n\n" . substr($result->data, strlen($result->data)-256); + } + + diff --git a/httprl.module b/httprl.module index 277344d..3618318 100644 --- a/httprl.module +++ b/httprl.module @@ -51,6 +51,13 @@ define('HTTPRL_URL_INVALID_SCHEMA', -1003); define('HTTPRL_ERROR_INITIALIZING_STREAM', -1004); /** + * Error code indicating that software caused the connection to be aborted. + * + * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx + */ +define('HTTPRL_REQUEST_ABORTED', -10053); + +/** * Error code indicating that the request exceeded the specified timeout. * * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx @@ -267,8 +274,15 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { * printed keys defined then this function will run in blocking mode and the * returned and printed data as well as any variables passed by reference * will be available to the parent. - * @return bool - * return value from httprl_send_request(). + * - alter_all_streams_function: Function name. This function runs at the end + * of httprl_post_processing() so that one can alter the $responses and + * $streams variables inside of httprl_send_request(). Defined function + * should have the following parameters: + * ($id, &$responses, &$streams, $current_time). + * + * @return array + * Array where key is the URL and the value is the return value from + * httprl_send_request(). */ function httprl_request($urls, $options = array()) { global $base_root; @@ -679,7 +693,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); continue; } @@ -701,7 +715,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); continue; } @@ -725,7 +739,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); continue; } } @@ -824,7 +838,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); } else { $responses[$id]->status = 'Reading data'; @@ -869,7 +883,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); } elseif ($bytes >= $len) { $stream_write_count--; @@ -917,7 +931,7 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' unset($streams[$id]); // Do post processing on the stream. - httprl_post_processing($responses[$id]); + httprl_post_processing($id, $responses, $streams, $timer_name); } break; } @@ -1145,7 +1159,10 @@ function httprl_parse_data(&$result) { * @param $result * An object from httprl_send_request. */ -function httprl_post_processing(&$result) { +function httprl_post_processing($id, &$responses, &$streams, $timer_name) { + // Create the result reference. + $result = &$responses[$id]; + // Assemble redirects. httprl_reconstruct_redirects($result); @@ -1189,6 +1206,12 @@ function httprl_post_processing(&$result) { ) { httprl_queue_background_callback($result->options['background_callback'], $result); } + + // Allow a user defined function to alter all $responses and $streams. + if ($full_bootstrap && !empty($result->options['alter_all_streams_function']) && function_exists($result->options['alter_all_streams_function'])) { + $current_time = timer_read($timer_name) / 1000; + $result->options['alter_all_streams_function']($id, $responses, $streams, $current_time); + } } /**