2002-11-13 22:25:33 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 09:03:12 +00:00
|
|
|
| Copyright (c) The PHP Group |
|
2002-11-13 22:25:33 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2002-11-13 22:25:33 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 12:51:34 +00:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2002-11-13 22:25:33 +00:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Sterling Hughes <sterling@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-25 07:19:44 +00:00
|
|
|
#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2003-04-05 08:14:56 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-11-13 22:25:33 +00:00
|
|
|
#include "php.h"
|
2020-06-17 14:05:55 +00:00
|
|
|
#include "Zend/zend_interfaces.h"
|
2020-09-07 09:12:41 +00:00
|
|
|
#include "Zend/zend_smart_str.h"
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-09-07 09:12:41 +00:00
|
|
|
#include "curl_private.h"
|
2002-11-13 22:25:33 +00:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/multi.h>
|
|
|
|
|
2003-02-25 07:19:44 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2002-11-13 22:25:33 +00:00
|
|
|
#include <sys/select.h>
|
2003-02-25 07:19:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2002-11-13 22:25:33 +00:00
|
|
|
#include <sys/time.h>
|
2003-02-25 07:19:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2002-11-13 22:25:33 +00:00
|
|
|
#include <sys/types.h>
|
2003-02-25 07:19:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-11-13 22:25:33 +00:00
|
|
|
#include <unistd.h>
|
2003-02-25 07:19:44 +00:00
|
|
|
#endif
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-06-26 17:29:42 +00:00
|
|
|
#define SAVE_CURLM_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
/* CurlMultiHandle class */
|
|
|
|
|
2020-09-07 09:12:41 +00:00
|
|
|
zend_class_entry *curl_multi_ce;
|
2020-06-17 14:05:55 +00:00
|
|
|
|
|
|
|
static inline php_curlm *curl_multi_from_obj(zend_object *obj) {
|
|
|
|
return (php_curlm *)((char *)(obj) - XtOffsetOf(php_curlm, std));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Z_CURL_MULTI_P(zv) curl_multi_from_obj(Z_OBJ_P(zv))
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Returns a new cURL multi handle */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_init)
|
|
|
|
{
|
|
|
|
php_curlm *mh;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2019-10-29 16:16:27 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
object_init_ex(return_value, curl_multi_ce);
|
|
|
|
mh = Z_CURL_MULTI_P(return_value);
|
2002-11-13 22:25:33 +00:00
|
|
|
mh->multi = curl_multi_init();
|
|
|
|
|
2006-10-05 15:06:40 +00:00
|
|
|
zend_llist_init(&mh->easyh, sizeof(zval), _php_curl_multi_cleanup_list, 0);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Add a normal cURL handle to a cURL multi handle */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_add_handle)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
zval *z_ch;
|
|
|
|
php_curlm *mh;
|
|
|
|
php_curl *ch;
|
2016-06-26 17:29:42 +00:00
|
|
|
CURLMcode error = CURLM_OK;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2,2)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
|
|
|
ch = Z_CURL_P(z_ch);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-01-08 10:52:24 +00:00
|
|
|
_php_curl_verify_handlers(ch, 1);
|
|
|
|
|
2002-11-13 22:25:33 +00:00
|
|
|
_php_curl_cleanup_handle(ch);
|
2004-03-11 00:11:18 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_ADDREF_P(z_ch);
|
2018-01-08 04:55:57 +00:00
|
|
|
zend_llist_add_element(&mh->easyh, z_ch);
|
2004-03-11 00:11:18 +00:00
|
|
|
|
2016-06-26 17:29:42 +00:00
|
|
|
error = curl_multi_add_handle(mh->multi, ch->cp);
|
|
|
|
SAVE_CURLM_ERROR(mh, error);
|
|
|
|
|
|
|
|
RETURN_LONG((zend_long) error);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2006-10-05 15:06:40 +00:00
|
|
|
void _php_curl_multi_cleanup_list(void *data) /* {{{ */
|
|
|
|
{
|
|
|
|
zval *z_ch = (zval *)data;
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
zval_ptr_dtor(z_ch);
|
2006-10-05 15:06:40 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
2006-05-08 15:29:28 +00:00
|
|
|
|
|
|
|
/* Used internally as comparison routine passed to zend_list_del_element */
|
2020-06-17 14:05:55 +00:00
|
|
|
static int curl_compare_objects( zval *z1, zval *z2 ) /* {{{ */
|
2006-05-08 15:29:28 +00:00
|
|
|
{
|
2015-01-03 09:22:58 +00:00
|
|
|
return (Z_TYPE_P(z1) == Z_TYPE_P(z2) &&
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_TYPE_P(z1) == IS_OBJECT &&
|
|
|
|
Z_OBJ_P(z1) == Z_OBJ_P(z2));
|
2006-05-08 15:29:28 +00:00
|
|
|
}
|
2007-12-04 08:56:07 +00:00
|
|
|
/* }}} */
|
2006-05-08 15:29:28 +00:00
|
|
|
|
2015-12-02 21:47:52 +00:00
|
|
|
/* Used to find the php_curl resource for a given curl easy handle */
|
|
|
|
static zval *_php_curl_multi_find_easy_handle(php_curlm *mh, CURL *easy) /* {{{ */
|
|
|
|
{
|
|
|
|
php_curl *tmp_ch;
|
|
|
|
zend_llist_position pos;
|
|
|
|
zval *pz_ch_temp;
|
|
|
|
|
|
|
|
for(pz_ch_temp = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch_temp;
|
|
|
|
pz_ch_temp = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
|
2020-06-17 14:05:55 +00:00
|
|
|
tmp_ch = Z_CURL_P(pz_ch_temp);
|
2015-12-02 21:47:52 +00:00
|
|
|
|
|
|
|
if (tmp_ch->cp == easy) {
|
|
|
|
return pz_ch_temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Remove a multi handle from a set of cURL handles */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_remove_handle)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
zval *z_ch;
|
|
|
|
php_curlm *mh;
|
|
|
|
php_curl *ch;
|
2016-06-26 17:29:42 +00:00
|
|
|
CURLMcode error = CURLM_OK;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2,2)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
|
|
|
ch = Z_CURL_P(z_ch);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-06-26 17:29:42 +00:00
|
|
|
error = curl_multi_remove_handle(mh->multi, ch->cp);
|
|
|
|
SAVE_CURLM_ERROR(mh, error);
|
|
|
|
|
|
|
|
RETVAL_LONG((zend_long) error);
|
2020-06-17 14:05:55 +00:00
|
|
|
zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_objects);
|
2013-01-05 16:07:59 +00:00
|
|
|
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get all the sockets associated with the cURL extension, which can then be "selected" */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_select)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
php_curlm *mh;
|
2018-09-18 00:28:44 +00:00
|
|
|
double timeout = 1.0;
|
|
|
|
int numfds = 0;
|
2016-06-26 17:29:42 +00:00
|
|
|
CURLMcode error = CURLM_OK;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1,2)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_DOUBLE(timeout)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2019-05-21 21:44:11 +00:00
|
|
|
error = curl_multi_wait(mh->multi, NULL, 0, (unsigned long) (timeout * 1000.0), &numfds);
|
2018-09-18 00:28:44 +00:00
|
|
|
if (CURLM_OK != error) {
|
|
|
|
SAVE_CURLM_ERROR(mh, error);
|
|
|
|
RETURN_LONG(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_LONG(numfds);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Run the sub-connections of the current cURL handle */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_exec)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
zval *z_still_running;
|
|
|
|
php_curlm *mh;
|
|
|
|
int still_running;
|
2016-06-26 17:29:42 +00:00
|
|
|
CURLMcode error = CURLM_OK;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2019-01-07 11:28:51 +00:00
|
|
|
Z_PARAM_ZVAL(z_still_running)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2011-09-12 09:16:04 +00:00
|
|
|
{
|
|
|
|
zend_llist_position pos;
|
|
|
|
php_curl *ch;
|
|
|
|
zval *pz_ch;
|
|
|
|
|
2015-02-02 05:23:16 +00:00
|
|
|
for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
|
2011-09-12 09:16:04 +00:00
|
|
|
pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
|
2020-06-17 14:05:55 +00:00
|
|
|
ch = Z_CURL_P(pz_ch);
|
2015-02-02 05:23:16 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
_php_curl_verify_handlers(ch, 1);
|
2011-09-12 09:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:55:59 +00:00
|
|
|
still_running = zval_get_long(z_still_running);
|
2016-06-26 17:29:42 +00:00
|
|
|
error = curl_multi_perform(mh->multi, &still_running);
|
2019-04-24 15:28:29 +00:00
|
|
|
ZEND_TRY_ASSIGN_REF_LONG(z_still_running, still_running);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-06-26 17:29:42 +00:00
|
|
|
SAVE_CURLM_ERROR(mh, error);
|
|
|
|
RETURN_LONG((zend_long) error);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_getcontent)
|
|
|
|
{
|
|
|
|
zval *z_ch;
|
|
|
|
php_curl *ch;
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
ch = Z_CURL_P(z_ch);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2021-01-04 14:58:45 +00:00
|
|
|
if (ch->handlers.write->method == PHP_CURL_RETURN) {
|
|
|
|
if (!ch->handlers.write->buf.s) {
|
2014-07-17 15:21:52 +00:00
|
|
|
RETURN_EMPTY_STRING();
|
|
|
|
}
|
2021-01-04 14:58:45 +00:00
|
|
|
smart_str_0(&ch->handlers.write->buf);
|
|
|
|
RETURN_STR_COPY(ch->handlers.write->buf.s);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
2014-01-28 10:47:14 +00:00
|
|
|
|
2014-07-17 15:21:52 +00:00
|
|
|
RETURN_NULL();
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
2006-06-11 01:42:17 +00:00
|
|
|
/* }}} */
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get information about the current transfers */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_info_read)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
php_curlm *mh;
|
2006-05-08 15:29:28 +00:00
|
|
|
CURLMsg *tmp_msg;
|
2002-11-13 22:25:33 +00:00
|
|
|
int queued_msgs;
|
2006-05-08 15:29:28 +00:00
|
|
|
zval *zmsgs_in_queue = NULL;
|
2019-07-17 18:46:40 +00:00
|
|
|
php_curl *ch;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
Z_PARAM_OPTIONAL
|
2019-01-07 11:28:51 +00:00
|
|
|
Z_PARAM_ZVAL(zmsgs_in_queue)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
|
|
|
tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs);
|
|
|
|
if (tmp_msg == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2019-01-07 11:28:51 +00:00
|
|
|
|
2006-05-08 15:29:28 +00:00
|
|
|
if (zmsgs_in_queue) {
|
2019-04-24 15:28:29 +00:00
|
|
|
ZEND_TRY_ASSIGN_REF_LONG(zmsgs_in_queue, queued_msgs);
|
2006-05-08 15:29:28 +00:00
|
|
|
}
|
2002-11-13 22:25:33 +00:00
|
|
|
|
|
|
|
array_init(return_value);
|
2014-08-25 17:24:55 +00:00
|
|
|
add_assoc_long(return_value, "msg", tmp_msg->msg);
|
|
|
|
add_assoc_long(return_value, "result", tmp_msg->data.result);
|
2006-05-08 15:29:28 +00:00
|
|
|
|
|
|
|
/* find the original easy curl handle */
|
|
|
|
{
|
2020-06-17 14:05:55 +00:00
|
|
|
zval *pz_ch = _php_curl_multi_find_easy_handle(mh, tmp_msg->easy_handle);
|
2015-12-02 21:47:52 +00:00
|
|
|
if (pz_ch != NULL) {
|
2019-07-17 18:46:40 +00:00
|
|
|
/* we must save result to be able to read error message */
|
2020-06-17 14:05:55 +00:00
|
|
|
ch = Z_CURL_P(pz_ch);
|
2019-07-17 18:46:40 +00:00
|
|
|
SAVE_CURL_ERROR(ch, tmp_msg->data.result);
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_ADDREF_P(pz_ch);
|
2015-12-02 21:47:52 +00:00
|
|
|
add_assoc_zval(return_value, "handle", pz_ch);
|
2006-05-08 15:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Close a set of cURL handles */
|
2002-11-13 22:25:33 +00:00
|
|
|
PHP_FUNCTION(curl_multi_close)
|
|
|
|
{
|
|
|
|
php_curlm *mh;
|
2020-06-17 14:05:55 +00:00
|
|
|
zval *z_mh;
|
|
|
|
|
|
|
|
zend_llist_position pos;
|
|
|
|
zval *pz_ch;
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1,1)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2003-03-17 15:06:11 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
|
|
|
|
pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
|
|
|
|
php_curl *ch = Z_CURL_P(pz_ch);
|
|
|
|
_php_curl_verify_handlers(ch, 1);
|
|
|
|
curl_multi_remove_handle(mh->multi, ch->cp);
|
2004-03-09 15:04:17 +00:00
|
|
|
}
|
2020-06-17 14:05:55 +00:00
|
|
|
zend_llist_clean(&mh->easyh);
|
2002-11-13 22:25:33 +00:00
|
|
|
}
|
2007-12-04 08:56:07 +00:00
|
|
|
/* }}} */
|
2002-11-13 22:25:33 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return an integer containing the last multi curl error number */
|
2016-06-26 17:29:42 +00:00
|
|
|
PHP_FUNCTION(curl_multi_errno)
|
|
|
|
{
|
|
|
|
zval *z_mh;
|
|
|
|
php_curlm *mh;
|
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1,1)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2016-06-26 17:29:42 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2016-06-26 17:29:42 +00:00
|
|
|
|
|
|
|
RETURN_LONG(mh->err.no);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ return string describing error code */
|
2012-12-23 20:45:39 +00:00
|
|
|
PHP_FUNCTION(curl_multi_strerror)
|
|
|
|
{
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long code;
|
2012-12-23 20:45:39 +00:00
|
|
|
const char *str;
|
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1,1)
|
|
|
|
Z_PARAM_LONG(code)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2012-12-23 20:45:39 +00:00
|
|
|
|
|
|
|
str = curl_multi_strerror(code);
|
|
|
|
if (str) {
|
2014-05-09 04:17:09 +00:00
|
|
|
RETURN_STRING(str);
|
2012-12-23 20:45:39 +00:00
|
|
|
} else {
|
|
|
|
RETURN_NULL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-10-02 07:31:10 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x072C00 /* Available since 7.44.0 */
|
|
|
|
|
|
|
|
static int _php_server_push_callback(CURL *parent_ch, CURL *easy, size_t num_headers, struct curl_pushheaders *push_headers, void *userp) /* {{{ */
|
|
|
|
{
|
|
|
|
php_curl *ch;
|
|
|
|
php_curl *parent;
|
|
|
|
php_curlm *mh = (php_curlm *)userp;
|
|
|
|
size_t rval = CURL_PUSH_DENY;
|
2021-01-04 14:53:34 +00:00
|
|
|
php_curl_callback *t = mh->handlers.server_push;
|
2015-10-02 07:31:10 +00:00
|
|
|
zval *pz_parent_ch = NULL;
|
|
|
|
zval pz_ch;
|
|
|
|
zval headers;
|
|
|
|
zval retval;
|
|
|
|
char *header;
|
|
|
|
int error;
|
|
|
|
zend_fcall_info fci = empty_fcall_info;
|
|
|
|
|
|
|
|
pz_parent_ch = _php_curl_multi_find_easy_handle(mh, parent_ch);
|
|
|
|
if (pz_parent_ch == NULL) {
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
parent = Z_CURL_P(pz_parent_ch);
|
2015-10-02 07:31:10 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
ch = init_curl_handle_into_zval(&pz_ch);
|
2015-10-02 07:31:10 +00:00
|
|
|
ch->cp = easy;
|
|
|
|
_php_setup_easy_copy_handlers(ch, parent);
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
array_init(&headers);
|
|
|
|
for(i=0; i<num_headers; i++) {
|
|
|
|
header = curl_pushheader_bynum(push_headers, i);
|
|
|
|
add_next_index_string(&headers, header);
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_fcall_info_init(&t->func_name, 0, &fci, &t->fci_cache, NULL, NULL);
|
|
|
|
|
|
|
|
zend_fcall_info_argn(
|
|
|
|
&fci, 3,
|
|
|
|
pz_parent_ch,
|
|
|
|
&pz_ch,
|
|
|
|
&headers
|
|
|
|
);
|
|
|
|
|
|
|
|
fci.retval = &retval;
|
|
|
|
|
|
|
|
error = zend_call_function(&fci, &t->fci_cache);
|
|
|
|
zend_fcall_info_args_clear(&fci, 1);
|
2018-07-05 12:25:55 +00:00
|
|
|
zval_ptr_dtor_nogc(&headers);
|
2015-10-02 07:31:10 +00:00
|
|
|
|
|
|
|
if (error == FAILURE) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
|
|
|
|
} else if (!Z_ISUNDEF(retval)) {
|
|
|
|
if (CURL_PUSH_DENY != zval_get_long(&retval)) {
|
|
|
|
rval = CURL_PUSH_OK;
|
2018-01-08 04:55:57 +00:00
|
|
|
zend_llist_add_element(&mh->easyh, &pz_ch);
|
2015-10-02 07:31:10 +00:00
|
|
|
} else {
|
|
|
|
/* libcurl will free this easy handle, avoid double free */
|
|
|
|
ch->cp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
2018-01-08 04:55:57 +00:00
|
|
|
/* }}} */
|
2015-10-02 07:31:10 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
|
2015-01-03 09:22:58 +00:00
|
|
|
{
|
2012-12-23 19:59:41 +00:00
|
|
|
CURLMcode error = CURLM_OK;
|
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
case CURLMOPT_PIPELINING:
|
|
|
|
case CURLMOPT_MAXCONNECTS:
|
2016-04-27 22:43:41 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x071e00 /* 7.30.0 */
|
|
|
|
case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
|
|
|
|
case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
|
|
|
|
case CURLMOPT_MAX_HOST_CONNECTIONS:
|
|
|
|
case CURLMOPT_MAX_PIPELINE_LENGTH:
|
|
|
|
case CURLMOPT_MAX_TOTAL_CONNECTIONS:
|
2012-12-23 19:59:41 +00:00
|
|
|
#endif
|
2019-05-07 15:05:07 +00:00
|
|
|
{
|
|
|
|
zend_long lval = zval_get_long(zvalue);
|
|
|
|
|
|
|
|
if (option == CURLMOPT_PIPELINING && (lval & 1)) {
|
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073e00 /* 7.62.0 */
|
|
|
|
php_error_docref(NULL, E_WARNING, "CURLPIPE_HTTP1 is no longer supported");
|
|
|
|
#else
|
|
|
|
php_error_docref(NULL, E_DEPRECATED, "CURLPIPE_HTTP1 is deprecated");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
error = curl_multi_setopt(mh->multi, option, lval);
|
2012-12-23 19:59:41 +00:00
|
|
|
break;
|
2019-05-07 15:05:07 +00:00
|
|
|
}
|
2019-05-03 14:43:53 +00:00
|
|
|
#if LIBCURL_VERSION_NUM > 0x072D00 /* Available since 7.45.0 */
|
2015-10-02 07:31:10 +00:00
|
|
|
case CURLMOPT_PUSHFUNCTION:
|
2021-01-04 14:53:34 +00:00
|
|
|
if (mh->handlers.server_push == NULL) {
|
|
|
|
mh->handlers.server_push = ecalloc(1, sizeof(php_curl_callback));
|
|
|
|
} else if (!Z_ISUNDEF(mh->handlers.server_push->func_name)) {
|
|
|
|
zval_ptr_dtor(&mh->handlers.server_push->func_name);
|
|
|
|
mh->handlers.server_push->fci_cache = empty_fcall_info_cache;
|
2015-10-02 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:53:34 +00:00
|
|
|
ZVAL_COPY(&mh->handlers.server_push->func_name, zvalue);
|
|
|
|
mh->handlers.server_push->method = PHP_CURL_USER;
|
2015-10-02 07:31:10 +00:00
|
|
|
error = curl_multi_setopt(mh->multi, option, _php_server_push_callback);
|
|
|
|
if (error != CURLM_OK) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
error = curl_multi_setopt(mh->multi, CURLMOPT_PUSHDATA, mh);
|
|
|
|
break;
|
|
|
|
#endif
|
2012-12-23 19:59:41 +00:00
|
|
|
default:
|
2020-08-09 09:05:26 +00:00
|
|
|
zend_argument_value_error(2, "is not a valid cURL multi option");
|
2012-12-23 19:59:41 +00:00
|
|
|
error = CURLM_UNKNOWN_OPTION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-26 17:29:42 +00:00
|
|
|
SAVE_CURLM_ERROR(mh, error);
|
2018-08-19 04:32:00 +00:00
|
|
|
|
|
|
|
return error != CURLM_OK;
|
2012-12-23 19:59:41 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Set an option for the curl multi handle */
|
2012-12-23 19:59:41 +00:00
|
|
|
PHP_FUNCTION(curl_multi_setopt)
|
|
|
|
{
|
2014-05-09 04:17:09 +00:00
|
|
|
zval *z_mh, *zvalue;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long options;
|
2012-12-23 19:59:41 +00:00
|
|
|
php_curlm *mh;
|
|
|
|
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(3,3)
|
2020-06-17 14:05:55 +00:00
|
|
|
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
|
2016-12-28 16:38:01 +00:00
|
|
|
Z_PARAM_LONG(options)
|
2017-06-19 09:55:59 +00:00
|
|
|
Z_PARAM_ZVAL(zvalue)
|
2016-12-28 16:38:01 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2012-12-23 19:59:41 +00:00
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
mh = Z_CURL_MULTI_P(z_mh);
|
2012-12-23 19:59:41 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (!_php_curl_multi_setopt(mh, options, zvalue, return_value)) {
|
2012-12-23 19:59:41 +00:00
|
|
|
RETURN_TRUE;
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
/* CurlMultiHandle class */
|
|
|
|
|
|
|
|
static zend_object_handlers curl_multi_handlers;
|
|
|
|
|
|
|
|
static zend_object *curl_multi_create_object(zend_class_entry *class_type) {
|
|
|
|
php_curlm *intern = zend_object_alloc(sizeof(php_curlm), class_type);
|
|
|
|
|
|
|
|
zend_object_std_init(&intern->std, class_type);
|
|
|
|
object_properties_init(&intern->std, class_type);
|
|
|
|
intern->std.handlers = &curl_multi_handlers;
|
|
|
|
|
|
|
|
return &intern->std;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_function *curl_multi_get_constructor(zend_object *object) {
|
|
|
|
zend_throw_error(NULL, "Cannot directly construct CurlMultiHandle, use curl_multi_init() instead");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void curl_multi_free_obj(zend_object *object)
|
|
|
|
{
|
|
|
|
php_curlm *mh = curl_multi_from_obj(object);
|
|
|
|
|
|
|
|
zend_llist_position pos;
|
|
|
|
php_curl *ch;
|
|
|
|
zval *pz_ch;
|
|
|
|
|
2020-10-01 15:05:23 +00:00
|
|
|
if (!mh->multi) {
|
|
|
|
/* Can happen if constructor throws. */
|
|
|
|
zend_object_std_dtor(&mh->std);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:05:55 +00:00
|
|
|
for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
|
|
|
|
pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
|
|
|
|
if (!(OBJ_FLAGS(Z_OBJ_P(pz_ch)) & IS_OBJ_FREE_CALLED)) {
|
|
|
|
ch = Z_CURL_P(pz_ch);
|
|
|
|
_php_curl_verify_handlers(ch, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_multi_cleanup(mh->multi);
|
|
|
|
zend_llist_clean(&mh->easyh);
|
2021-01-04 14:53:34 +00:00
|
|
|
if (mh->handlers.server_push) {
|
|
|
|
zval_ptr_dtor(&mh->handlers.server_push->func_name);
|
|
|
|
efree(mh->handlers.server_push);
|
2020-06-17 14:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_object_std_dtor(&mh->std);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HashTable *curl_multi_get_gc(zend_object *object, zval **table, int *n)
|
|
|
|
{
|
|
|
|
php_curlm *curl_multi = curl_multi_from_obj(object);
|
|
|
|
|
|
|
|
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
|
|
|
|
|
2021-01-04 14:53:34 +00:00
|
|
|
if (curl_multi->handlers.server_push) {
|
|
|
|
zend_get_gc_buffer_add_zval(gc_buffer, &curl_multi->handlers.server_push->func_name);
|
2020-06-17 14:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_llist_position pos;
|
|
|
|
for (zval *pz_ch = (zval *) zend_llist_get_first_ex(&curl_multi->easyh, &pos); pz_ch;
|
|
|
|
pz_ch = (zval *) zend_llist_get_next_ex(&curl_multi->easyh, &pos)) {
|
|
|
|
zend_get_gc_buffer_add_zval(gc_buffer, pz_ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_get_gc_buffer_use(gc_buffer, table, n);
|
|
|
|
|
|
|
|
return zend_std_get_properties(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void curl_multi_register_class(const zend_function_entry *method_entries) {
|
|
|
|
zend_class_entry ce_multi;
|
|
|
|
INIT_CLASS_ENTRY(ce_multi, "CurlMultiHandle", method_entries);
|
|
|
|
curl_multi_ce = zend_register_internal_class(&ce_multi);
|
Add flag to forbid dynamic property creation on internal classes
While performing resource -> object migrations, we're adding
defensive classes that are final, non-serializable and non-clonable
(unless they are, of course). This path adds a ZEND_ACC_NO_DYNAMIC_PROPERTIES
flag, that also forbids the creation of dynamic properties on these objects.
This is a subset of #3931 and targeted at internal usage only
(though may be extended to userland at some point in the future).
It's already possible to achieve this (what the removed
WeakRef/WeakMap code does), but there's some caveats: First, this
simple approach is only possible if the class has no declared
properties, otherwise it's necessary to special-case those
properties. Second, it's easy to make it overly strict, e.g. by
forbidding isset($obj->prop) as well. And finally, it requires a
lot of boilerplate code for each class.
Closes GH-5572.
2020-05-14 09:51:36 +00:00
|
|
|
curl_multi_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
|
2020-06-17 14:05:55 +00:00
|
|
|
curl_multi_ce->create_object = curl_multi_create_object;
|
|
|
|
curl_multi_ce->serialize = zend_class_serialize_deny;
|
|
|
|
curl_multi_ce->unserialize = zend_class_unserialize_deny;
|
|
|
|
|
|
|
|
memcpy(&curl_multi_handlers, &std_object_handlers, sizeof(zend_object_handlers));
|
|
|
|
curl_multi_handlers.offset = XtOffsetOf(php_curlm, std);
|
|
|
|
curl_multi_handlers.free_obj = curl_multi_free_obj;
|
|
|
|
curl_multi_handlers.get_gc = curl_multi_get_gc;
|
|
|
|
curl_multi_handlers.get_constructor = curl_multi_get_constructor;
|
|
|
|
curl_multi_handlers.clone_obj = NULL;
|
2020-06-22 13:09:22 +00:00
|
|
|
curl_multi_handlers.cast_object = curl_cast_object;
|
2020-06-17 14:05:55 +00:00
|
|
|
}
|