diff options
author | Hannes Magnusson <bjori@php.net> | 2011-09-12 09:16:04 +0000 |
---|---|---|
committer | Hannes Magnusson <bjori@php.net> | 2011-09-12 09:16:04 +0000 |
commit | e43c21e53afcbc09639f16100d342215b7ec19dd (patch) | |
tree | 67f39e3ee879f27dc6c6e7a864ab3a6ea4672799 /ext/curl/interface.c | |
parent | 8ed1dc2fa9a6c0a283371bb24314a2cccaceddf6 (diff) | |
download | php-git-e43c21e53afcbc09639f16100d342215b7ec19dd.tar.gz |
Fixed issues when streams were closed before curl read/write from them, or cleaning
Closing a original handle after copying it now no longer cleans up all resources (fixes missing CURLOPT_POSTFIELDS values among others)
Diffstat (limited to 'ext/curl/interface.c')
-rw-r--r-- | ext/curl/interface.c | 106 |
1 files changed, 80 insertions, 26 deletions
diff --git a/ext/curl/interface.c b/ext/curl/interface.c index f2c185f4d2..935f153acf 100644 --- a/ext/curl/interface.c +++ b/ext/curl/interface.c @@ -150,6 +150,7 @@ static struct gcry_thread_cbs php_curl_gnutls_tsl = { static void _php_curl_close_ex(php_curl *ch TSRMLS_DC); static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC); + #define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err; #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s), (long) v); @@ -204,6 +205,72 @@ static int php_curl_option_url(php_curl *ch, const char *url, const int len) /* } /* }}} */ +int _php_curl_verify_handlers(php_curl *ch, int reporterror TSRMLS_DC) /* {{{ */ +{ + php_stream *stream; + if (!ch || !ch->handlers) { + return 0; + } + + if (ch->handlers->std_err) { + stream = (php_stream *) zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (stream == NULL) { + if (reporterror) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr"); + } + zval_ptr_dtor(&ch->handlers->std_err); + ch->handlers->std_err = NULL; + + curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr); + } + } + if (ch->handlers->read && ch->handlers->read->stream) { + stream = (php_stream *) zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (stream == NULL) { + if (reporterror) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default"); + } + zval_ptr_dtor(&ch->handlers->read->stream); + ch->handlers->read->fd = 0; + ch->handlers->read->fp = 0; + ch->handlers->read->stream = NULL; + + curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch); + } + } + if (ch->handlers->write_header && ch->handlers->write_header->stream) { + stream = (php_stream *) zend_fetch_resource(&ch->handlers->write_header->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (stream == NULL) { + if (reporterror) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default"); + } + zval_ptr_dtor(&ch->handlers->write_header->stream); + ch->handlers->write_header->fp = 0; + ch->handlers->write_header->stream = NULL; + + ch->handlers->write_header->method = PHP_CURL_IGNORE; + curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch); + } + } + if (ch->handlers->write && ch->handlers->write->stream) { + stream = (php_stream *) zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (stream == NULL) { + if (reporterror) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default"); + } + zval_ptr_dtor(&ch->handlers->write->stream); + ch->handlers->write->fp = 0; + ch->handlers->write->stream = NULL; + + ch->handlers->write->method = PHP_CURL_STDOUT; + ch->handlers->write->type = PHP_CURL_ASCII; + curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch); + } + } + return 1; +} +/* }}} */ + /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0) ZEND_ARG_INFO(0, version) @@ -337,7 +404,6 @@ PHP_INI_BEGIN() PHP_INI_END() /* }}} */ -/* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(curl) @@ -1394,6 +1460,7 @@ static void split_certinfo(char *string, zval *hash) efree(org); } } +/* }}} */ /* {{{ create_certinfo */ @@ -1537,6 +1604,7 @@ PHP_FUNCTION(curl_copy_handle) dupch->cp = cp; dupch->uses = 0; + ch->uses++; if (ch->handlers->write->stream) { Z_ADDREF_P(dupch->handlers->write->stream); dupch->handlers->write->stream = ch->handlers->write->stream; @@ -2211,28 +2279,10 @@ PHP_FUNCTION(curl_exec) ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + _php_curl_verify_handlers(ch, 1 TSRMLS_CC); + _php_curl_cleanup_handle(ch); - if (ch->handlers->std_err) { - php_stream *stream; - stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); - if (stream == NULL) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr"); - zval_ptr_dtor(&ch->handlers->std_err); - curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr); - } - } - if (ch->handlers->read && ch->handlers->read->stream) { - php_stream *stream; - stream = (php_stream*)zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); - if (stream == NULL) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default"); - zval_ptr_dtor(&ch->handlers->read->stream); - ch->handlers->read->fd = 0; - ch->handlers->read->fp = 0; - curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch); - } - } error = curl_easy_perform(ch->cp); SAVE_CURL_ERROR(ch, error); /* CURLE_PARTIAL_FILE is returned by HEAD requests */ @@ -2243,6 +2293,14 @@ PHP_FUNCTION(curl_exec) RETURN_FALSE; } + if (ch->handlers->std_err) { + php_stream *stream; + stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (stream) { + php_stream_flush(stream); + } + } + if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) { smart_str_0(&ch->handlers->write->buf); RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1); @@ -2520,11 +2578,7 @@ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC) fprintf(stderr, "DTOR CALLED, ch = %x\n", ch); #endif - /* Prevent crash inside cURL if passed file has already been closed */ - if (ch->handlers->std_err && Z_REFCOUNT_P(ch->handlers->std_err) <= 0) { - curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr); - } - + _php_curl_verify_handlers(ch, 0 TSRMLS_CC); curl_easy_cleanup(ch->cp); zend_llist_clean(&ch->to_free.str); |