- added http_get_request_body()
[m6w6/ext-http] / http_methods.c
index cf8f25dd371807f818f86f264b739034694c4aeb..c18e5aec630aa7603f5e678ab23296862ed82b81 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"
@@ -1132,7 +1133,7 @@ PHP_METHOD(HttpRequest, __destruct)
 PHP_METHOD(HttpRequest, setOptions)
 {
        char *key = NULL;
-       long idx = 0;
+       ulong idx = 0;
        zval *opts, *old_opts, **opt;
        getObject(http_request_object, obj);
 
@@ -2068,12 +2069,14 @@ PHP_METHOD(HttpRequest, getResponseMessage)
                getObject(http_request_object, obj);
 
                message = GET_PROP(obj, responseMessage);
-               Z_TYPE_P(return_value) = IS_OBJECT;
-               return_value->is_ref = 1;
-               return_value->value.obj = message->value.obj;
-               zval_add_ref(&return_value);
+               if (Z_TYPE_P(message) == IS_OBJECT) {
+                       RETVAL_OBJECT(message);
+               } else {
+                       RETURN_NULL();
+               }
        }
 }
+/* }}} */
 
 /* {{{ proto bool HttpRequest::send()
  *
@@ -2119,21 +2122,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 +2144,76 @@ PHP_METHOD(HttpRequest, send)
 
 /* {{{ HttpRequestPool */
 
-/* {{{ proto void HttpRequestPool::__construct(void)
+/* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
+ *
+ * Instantiate a new HttpRequestPool object.  An HttpRequestPool is
+ * able to send several HttpRequests in parallel.
  *
- * Instantiate a new HttpRequestPool object.
+ * 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.
@@ -2198,11 +2259,69 @@ PHP_METHOD(HttpRequestPool, detach)
  */
 PHP_METHOD(HttpRequestPool, send)
 {
+       STATUS status;
        getObject(http_requestpool_object, obj);
 
        NO_ARGS;
 
-       RETURN_SUCCESS(http_request_pool_send(&obj->pool));
+       SET_EH_THROW_HTTP();
+       status = http_request_pool_send(&obj->pool);
+       SET_EH_NORMAL();
+
+       RETURN_SUCCESS(status);
+}
+/* }}} */
+
+/* {{{ 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);
 }
 /* }}} */
 
@@ -2221,3 +2340,4 @@ PHP_METHOD(HttpRequestPool, send)
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
+