#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"
PHP_METHOD(HttpRequest, setOptions)
{
char *key = NULL;
- long idx = 0;
+ ulong idx = 0;
zval *opts, *old_opts, **opt;
getObject(http_request_object, obj);
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()
*
}
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();
/* {{{ 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);
}
/* }}} */
* Send all attached HttpRequest objects in parallel.
*/
PHP_METHOD(HttpRequestPool, send)
+{
+ STATUS status;
+ getObject(http_requestpool_object, obj);
+
+ NO_ARGS;
+
+ 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;
- RETURN_SUCCESS(http_request_pool_send(&obj->pool));
+ zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
}
/* }}} */
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
+