- added http_get_request_body()
[m6w6/ext-http] / http_methods.c
index a8f5d9489da81df92d4ce113213badc2074d1e93..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()
  *
@@ -2125,15 +2128,13 @@ PHP_METHOD(HttpRequest, send)
        }
 
        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,13 +2144,46 @@ PHP_METHOD(HttpRequest, send)
 
 /* {{{ HttpRequestPool */
 
-/* {{{ proto void HttpRequestPool::__construct()
+/* {{{ 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)
 {
-       NO_ARGS;
+       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);
 }
 /* }}} */
 
@@ -2225,11 +2259,16 @@ 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);
 }
 /* }}} */
 
@@ -2237,15 +2276,15 @@ PHP_METHOD(HttpRequestPool, send)
  *
  * Usage:
  * <pre>
- *     <?php
- *             while ($pool->socketSend()) {
- *                     do_something_else();
- *                     if (!$pool->socketSelect()) {
- *                             die('Socket error');
- *                     }
- *             }
- *             $pool->socketRead();
- *     ?>
+ * <?php
+ *     while ($pool->socketSend()) {
+ *         do_something_else();
+ *         if (!$pool->socketSelect()) {
+ *             die('Socket error');
+ *         }
+ *     }
+ *     $pool->socketRead();
+ * ?>
  * </pre>
  */
 PHP_METHOD(HttpRequestPool, socketSend)
@@ -2284,6 +2323,7 @@ PHP_METHOD(HttpRequestPool, socketRead)
 
        zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
 }
+/* }}} */
 
 /* }}} */
 
@@ -2300,3 +2340,4 @@ PHP_METHOD(HttpRequestPool, socketRead)
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
+