Skip to content

Add HTTP/2 Server Push to ext/curl #1692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 84 additions & 74 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,9 @@ PHP_MINIT_FUNCTION(curl)

#if LIBCURL_VERSION_NUM >= 0x072e00 /* Available since 7.46.0 */
REGISTER_CURL_CONSTANT(CURLOPT_STREAM_WEIGHT);
REGISTER_CURL_CONSTANT(CURLMOPT_PUSHFUNCTION);
REGISTER_CURL_CONSTANT(CURL_PUSH_OK);
REGISTER_CURL_CONSTANT(CURL_PUSH_DENY);
#endif

#if LIBCURL_VERSION_NUM >= 0x072f00 /* Available since 7.47.0 */
Expand Down Expand Up @@ -1853,7 +1856,7 @@ PHP_FUNCTION(curl_version)

/* {{{ alloc_curl_handle
*/
static php_curl *alloc_curl_handle()
php_curl *alloc_curl_handle()
{
php_curl *ch = ecalloc(1, sizeof(php_curl));
ch->to_free = ecalloc(1, sizeof(struct _php_curl_free));
Expand Down Expand Up @@ -2032,6 +2035,79 @@ PHP_FUNCTION(curl_init)
}
/* }}} */

void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
{
if (!Z_ISUNDEF(source->handlers->write->stream)) {
Z_ADDREF(source->handlers->write->stream);
}
ch->handlers->write->stream = source->handlers->write->stream;
ch->handlers->write->method = source->handlers->write->method;
if (!Z_ISUNDEF(source->handlers->read->stream)) {
Z_ADDREF(source->handlers->read->stream);
}
ch->handlers->read->stream = source->handlers->read->stream;
ch->handlers->read->method = source->handlers->read->method;
ch->handlers->write_header->method = source->handlers->write_header->method;
if (!Z_ISUNDEF(source->handlers->write_header->stream)) {
Z_ADDREF(source->handlers->write_header->stream);
}
ch->handlers->write_header->stream = source->handlers->write_header->stream;

ch->handlers->write->fp = source->handlers->write->fp;
ch->handlers->write_header->fp = source->handlers->write_header->fp;
ch->handlers->read->fp = source->handlers->read->fp;
ch->handlers->read->res = source->handlers->read->res;
#if CURLOPT_PASSWDDATA != 0
if (!Z_ISUNDEF(source->handlers->passwd)) {
ZVAL_COPY(&ch->handlers->passwd, &source->handlers->passwd);
curl_easy_setopt(source->cp, CURLOPT_PASSWDDATA, (void *) ch);
}
#endif
if (!Z_ISUNDEF(source->handlers->write->func_name)) {
ZVAL_COPY(&ch->handlers->write->func_name, &source->handlers->write->func_name);
}
if (!Z_ISUNDEF(source->handlers->read->func_name)) {
ZVAL_COPY(&ch->handlers->read->func_name, &source->handlers->read->func_name);
}
if (!Z_ISUNDEF(source->handlers->write_header->func_name)) {
ZVAL_COPY(&ch->handlers->write_header->func_name, &source->handlers->write_header->func_name);
}

curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER, ch->err.str);
curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);

if (source->handlers->progress) {
ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
if (!Z_ISUNDEF(source->handlers->progress->func_name)) {
ZVAL_COPY(&ch->handlers->progress->func_name, &source->handlers->progress->func_name);
}
ch->handlers->progress->method = source->handlers->progress->method;
curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, (void *) ch);
}

#if LIBCURL_VERSION_NUM >= 0x071500
if (source->handlers->fnmatch) {
ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
if (!Z_ISUNDEF(source->handlers->fnmatch->func_name)) {
ZVAL_COPY(&ch->handlers->fnmatch->func_name, &source->handlers->fnmatch->func_name);
}
ch->handlers->fnmatch->method = source->handlers->fnmatch->method;
curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, (void *) ch);
}
#endif

efree(ch->to_free->slist);
efree(ch->to_free);
ch->to_free = source->to_free;
efree(ch->clone);
ch->clone = source->clone;

/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
(*source->clone)++;
}

/* {{{ proto resource curl_copy_handle(resource ch)
Copy a cURL handle along with all of it's preferences */
PHP_FUNCTION(curl_copy_handle)
Expand All @@ -2055,79 +2131,11 @@ PHP_FUNCTION(curl_copy_handle)
}

dupch = alloc_curl_handle();

dupch->cp = cp;
Z_ADDREF_P(zid);
if (!Z_ISUNDEF(ch->handlers->write->stream)) {
Z_ADDREF(ch->handlers->write->stream);
}
dupch->handlers->write->stream = ch->handlers->write->stream;
dupch->handlers->write->method = ch->handlers->write->method;
if (!Z_ISUNDEF(ch->handlers->read->stream)) {
Z_ADDREF(ch->handlers->read->stream);
}
dupch->handlers->read->stream = ch->handlers->read->stream;
dupch->handlers->read->method = ch->handlers->read->method;
dupch->handlers->write_header->method = ch->handlers->write_header->method;
if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
Z_ADDREF(ch->handlers->write_header->stream);
}
dupch->handlers->write_header->stream = ch->handlers->write_header->stream;

dupch->handlers->write->fp = ch->handlers->write->fp;
dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
dupch->handlers->read->fp = ch->handlers->read->fp;
dupch->handlers->read->res = ch->handlers->read->res;
#if CURLOPT_PASSWDDATA != 0
if (!Z_ISUNDEF(ch->handlers->passwd)) {
ZVAL_COPY(&dupch->handlers->passwd, &ch->handlers->passwd);
curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
}
#endif
if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
ZVAL_COPY(&dupch->handlers->write->func_name, &ch->handlers->write->func_name);
}
if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
ZVAL_COPY(&dupch->handlers->read->func_name, &ch->handlers->read->func_name);
}
if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
ZVAL_COPY(&dupch->handlers->write_header->func_name, &ch->handlers->write_header->func_name);
}

curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER, dupch->err.str);
curl_easy_setopt(dupch->cp, CURLOPT_FILE, (void *) dupch);
curl_easy_setopt(dupch->cp, CURLOPT_INFILE, (void *) dupch);
curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER, (void *) dupch);
_php_setup_easy_copy_handlers(dupch, ch);

if (ch->handlers->progress) {
dupch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
ZVAL_COPY(&dupch->handlers->progress->func_name, &ch->handlers->progress->func_name);
}
dupch->handlers->progress->method = ch->handlers->progress->method;
curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA, (void *) dupch);
}

/* Available since 7.21.0 */
#if LIBCURL_VERSION_NUM >= 0x071500
if (ch->handlers->fnmatch) {
dupch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
ZVAL_COPY(&dupch->handlers->fnmatch->func_name, &ch->handlers->fnmatch->func_name);
}
dupch->handlers->fnmatch->method = ch->handlers->fnmatch->method;
curl_easy_setopt(dupch->cp, CURLOPT_FNMATCH_DATA, (void *) dupch);
}
#endif

efree(dupch->to_free->slist);
efree(dupch->to_free);
dupch->to_free = ch->to_free;
efree(dupch->clone);
dupch->clone = ch->clone;

/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
(*ch->clone)++;
Z_ADDREF_P(zid);

ZVAL_RES(return_value, zend_register_resource(dupch, le_curl));
dupch->res = Z_RES_P(return_value);
Expand Down Expand Up @@ -3394,10 +3402,12 @@ static void _php_curl_close_ex(php_curl *ch)
*
* Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
*/
curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
if (ch->cp != NULL) {
curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);

curl_easy_cleanup(ch->cp);
curl_easy_cleanup(ch->cp);
}

/* cURL destructors should be invoked only by last curl handle */
if (--(*ch->clone) == 0) {
Expand Down
Loading