From 712103ef11a9dfcc2495ce43e4a731d9b59d3439 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 9 Oct 2005 18:27:01 +0000 Subject: [PATCH] - avoid endless loop if attach() is called while the iterator is active - make the behaviour intentional when detach() is called while the iterator is active --- http_requestpool_object.c | 13 ++++++++++--- tests/HttpRequestPool_001.phpt | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/http_requestpool_object.c b/http_requestpool_object.c index f4f0f99..dfb9c60 100644 --- a/http_requestpool_object.c +++ b/http_requestpool_object.c @@ -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)); } } /* }}} */ diff --git a/tests/HttpRequestPool_001.phpt b/tests/HttpRequestPool_001.phpt index d8bff6e..a42650f 100644 --- a/tests/HttpRequestPool_001.phpt +++ b/tests/HttpRequestPool_001.phpt @@ -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 -- 2.30.2