- avoid endless loop if attach() is called while the iterator is active
authorMichael Wallner <mike@php.net>
Sun, 9 Oct 2005 18:27:01 +0000 (18:27 +0000)
committerMichael Wallner <mike@php.net>
Sun, 9 Oct 2005 18:27:01 +0000 (18:27 +0000)
- make the behaviour intentional when detach() is called while the iterator is active

http_requestpool_object.c
tests/HttpRequestPool_001.phpt

index f4f0f9918359b71208561205ff763f37631e764b..dfb9c604f72f6d387f2209e491535f8a3abe1f74 100644 (file)
@@ -24,6 +24,7 @@
 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
 
 #include "php_http_std_defs.h"
+#include "php_http_api.h"
 #include "php_http_requestpool_object.h"
 #include "php_http_request_pool_api.h"
 #include "php_http_request_object.h"
@@ -217,7 +218,8 @@ PHP_METHOD(HttpRequestPool, reset)
        getObject(http_requestpool_object, obj);
 
        NO_ARGS;
-
+       
+       obj->iterator.pos = 0;
        http_request_pool_detach_all(&obj->pool);
 }
 
@@ -242,7 +244,11 @@ PHP_METHOD(HttpRequestPool, attach)
 
        SET_EH_THROW_HTTP();
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
-               status = http_request_pool_attach(&obj->pool, request);
+               if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool.handles)) {
+                       http_error(HE_THROW, HTTP_E_REQUEST_POOL, "Cannot attach to the HttpRequestPool while the iterator is active");
+               } else {
+                       status = http_request_pool_attach(&obj->pool, request);
+               }
        }
        SET_EH_NORMAL();
        RETURN_SUCCESS(status);
@@ -268,6 +274,7 @@ PHP_METHOD(HttpRequestPool, detach)
 
        SET_EH_THROW_HTTP();
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
+               obj->iterator.pos = -1;
                status = http_request_pool_detach(&obj->pool, request);
        }
        SET_EH_NORMAL();
@@ -358,7 +365,7 @@ PHP_METHOD(HttpRequestPool, valid)
 
        IF_RETVAL_USED {
                getObject(http_requestpool_object, obj);
-               RETURN_BOOL(obj->iterator.pos < zend_llist_count(&obj->pool.handles));
+               RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool.handles));
        }
 }
 /* }}} */
index d8bff6ec6772d9c086a9ab6702bca140eea7aec8..a42650ffcf8a1cf069d5223b8e88aad14a36c2ea 100644 (file)
@@ -26,6 +26,16 @@ foreach ($pool as $req) {
         $req->getResponseCode(), ':',
         $req->getResponseMessage()->getResponseCode(), "\n";
 }
+foreach ($pool as $req) {
+       try {
+               $pool->attach(new HttpRequest('http://foo.bar'));
+       } catch (HttpRequestPoolException $x) {
+               echo ".\n";
+       }
+}
+foreach ($pool as $req) {
+       $pool->detach($req);
+}
 echo "Done\n";
 ?>
 --EXPECTF--
@@ -34,4 +44,8 @@ http://www.php.net/=200:200
 http://pear.php.net/=200:200
 http://pecl.php.net/=200:200
 http://dev.iworks.at/.print_request.php=200:200
+.
+.
+.
+.
 Done