- enable $pool = new HttpRequestPool(HttpRequest r, ...);
[m6w6/ext-http] / http_methods.c
index cf8f25dd371807f818f86f264b739034694c4aeb..1e454d83a3477dc884a98a07cb38bb0c48f6bfa2 100644 (file)
@@ -27,6 +27,7 @@
 #include "php_http_api.h"
 #include "php_http_cache_api.h"
 #include "php_http_request_api.h"
+#include "php_http_request_pool_api.h"
 #include "php_http_date_api.h"
 #include "php_http_headers_api.h"
 #include "php_http_message_api.h"
@@ -2119,21 +2120,19 @@ PHP_METHOD(HttpRequest, send)
 
        SET_EH_THROW_HTTP();
 
-       if (obj->attached) {
+       if (obj->pool) {
                http_error(E_WARNING, HTTP_E_CURL, "You cannot call HttpRequest::send() while attached to an HttpRequestPool");
                RETURN_FALSE;
        }
 
        if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) {
-               zval *info = GET_PROP(obj, responseInfo);
-               status = http_request_exec(obj->ch, Z_ARRVAL_P(info));
-               SET_PROP(obj, responseInfo, info);
+               status = http_request_exec(obj->ch, NULL);
        }
        http_request_body_dtor(&body);
 
        /* final data handling */
        if (SUCCESS == status) {
-               status = http_request_object_responsehandler(obj, getThis(), NULL);
+               status = http_request_object_responsehandler(obj, getThis());
        }
 
        SET_EH_NORMAL();
@@ -2143,16 +2142,76 @@ PHP_METHOD(HttpRequest, send)
 
 /* {{{ HttpRequestPool */
 
-/* {{{ proto void HttpRequestPool::__construct(void)
+/* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
  *
- * Instantiate a new HttpRequestPool object.
+ * Instantiate a new HttpRequestPool object.  An HttpRequestPool is
+ * able to send several HttpRequests in parallel.
+ *
+ * Example:
+ * <pre>
+ * <?php
+ *     $urls = array('www.php.net', 'pecl.php.net', 'pear.php.net')
+ *     $pool = new HttpRequestPool;
+ *     foreach ($urls as $url) {
+ *         $req[$url] = new HttpRequest("http://$url", HTTP_HEAD);
+ *         $pool->attach($req[$url]);
+ *     }
+ *     $pool->send();
+ *     foreach ($urls as $url) {
+ *         printf("%s (%s) is %s\n", 
+ *             $url, $req[$url]->getResponseInfo('effective_url'), 
+ *             $r->getResponseCode() == 200 ? 'alive' : 'not alive'
+ *         );
+ *     }
+ * ?>
+ * </pre>
  */
 PHP_METHOD(HttpRequestPool, __construct)
 {
+       int argc = ZEND_NUM_ARGS();
+       zval ***argv = safe_emalloc(argc, sizeof(zval *), 0);
+       getObject(http_requestpool_object, obj);
+
+       if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
+               int i;
+
+               for (i = 0; i < argc; ++i) {
+                       if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), http_request_object_ce TSRMLS_CC)) {
+                               http_request_pool_attach(&obj->pool, *(argv[i]));
+                       }
+               }
+       }
+       efree(argv);
+}
+/* }}} */
+
+/* {{{ proto void HttpRequestPool::__destruct()
+ *
+ * Clean up HttpRequestPool object.
+ */
+PHP_METHOD(HttpRequestPool, __destruct)
+{
+       getObject(http_requestpool_object, obj);
+
        NO_ARGS;
+
+       http_request_pool_detach_all(&obj->pool);
 }
 /* }}} */
 
+/* {{{ proto void HttpRequestPool::reset()
+ *
+ * Detach all attached HttpRequest objects.
+ */
+PHP_METHOD(HttpRequestPool, reset)
+{
+       getObject(http_requestpool_object, obj);
+
+       NO_ARGS;
+
+       http_request_pool_detach_all(&obj->pool);
+}
+
 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
  *
  * Attach an HttpRequest object to this HttpRequestPool.
@@ -2206,6 +2265,59 @@ PHP_METHOD(HttpRequestPool, send)
 }
 /* }}} */
 
+/* {{{ proto protected bool HttpRequestPool::socketSend()
+ *
+ * Usage:
+ * <pre>
+ * <?php
+ *     while ($pool->socketSend()) {
+ *         do_something_else();
+ *         if (!$pool->socketSelect()) {
+ *             die('Socket error');
+ *         }
+ *     }
+ *     $pool->socketRead();
+ * ?>
+ * </pre>
+ */
+PHP_METHOD(HttpRequestPool, socketSend)
+{
+       getObject(http_requestpool_object, obj);
+
+       NO_ARGS;
+
+       RETURN_BOOL(0 < http_request_pool_perform(&obj->pool));
+}
+/* }}} */
+
+/* {{{ proto protected bool HttpRequestPool::socketSelect()
+ *
+ * See HttpRequestPool::socketSend().
+ */
+PHP_METHOD(HttpRequestPool, socketSelect)
+{
+       getObject(http_requestpool_object, obj);
+
+       NO_ARGS;
+
+       RETURN_SUCCESS(http_request_pool_select(&obj->pool));
+}
+/* }}} */
+
+/* {{{ proto protected void HttpRequestPool::socketRead()
+ *
+ * See HttpRequestPool::socketSend().
+ */
+PHP_METHOD(HttpRequestPool, socketRead)
+{
+       getObject(http_requestpool_object, obj);
+
+       NO_ARGS;
+
+       zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
+}
+/* }}} */
+
 /* }}} */
 
 /* }}} */