From: Michael Wallner Date: Sat, 7 Jan 2006 15:57:38 +0000 (+0000) Subject: - set curl options that won't change only at initialization time X-Git-Tag: RELEASE_1_0_4~16 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ecdfaaaf81cc29f99e39252b6f2463d328812cbf;p=m6w6%2Fext-http - set curl options that won't change only at initialization time --- diff --git a/http_request_api.c b/http_request_api.c index 8e7d0b0..c35dc91 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -168,7 +168,8 @@ PHP_MSHUTDOWN_FUNCTION(http_request) } \ } -#define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p)) +#define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p)) +#define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), CURLOPT_##OPTION, (p)) #define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc) #define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc) #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc) @@ -209,6 +210,46 @@ static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { r static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *); /* }}} */ +/* {{{ CURL *http_curl_init(http_request *) */ +PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, void *context, char *error_buffer) +{ + if (ch || (ch = curl_easy_init())) { +#if defined(ZTS) + HTTP_CURL_OPT_EX(ch, NOSIGNAL, 1); +#endif + HTTP_CURL_OPT_EX(ch, HEADER, 0); + HTTP_CURL_OPT_EX(ch, FILETIME, 1); + HTTP_CURL_OPT_EX(ch, AUTOREFERER, 1); + HTTP_CURL_OPT_EX(ch, VERBOSE, 1); + HTTP_CURL_OPT_EX(ch, HEADERFUNCTION, NULL); + HTTP_CURL_OPT_EX(ch, DEBUGFUNCTION, http_curl_raw_callback); + HTTP_CURL_OPT_EX(ch, READFUNCTION, http_curl_read_callback); + HTTP_CURL_OPT_EX(ch, IOCTLFUNCTION, http_curl_ioctl_callback); + HTTP_CURL_OPT_EX(ch, WRITEFUNCTION, http_curl_dummy_callback); + HTTP_CURL_OPT_EX(ch, DEBUGDATA, context); + HTTP_CURL_OPT_EX(ch, PRIVATE, context); + HTTP_CURL_OPT_EX(ch, ERRORBUFFER, error_buffer); + } + + return ch; +} +/* }}} */ + +/* {{{ void http_curl_free(CURL **) */ +PHP_HTTP_API void _http_curl_free(CURL **ch) +{ + if (*ch) { + /* avoid nasty segfaults with already cleaned up callbacks */ + HTTP_CURL_OPT_EX(*ch, NOPROGRESS, 1); + HTTP_CURL_OPT_EX(*ch, PROGRESSFUNCTION, NULL); + HTTP_CURL_OPT_EX(*ch, VERBOSE, 0); + HTTP_CURL_OPT_EX(*ch, DEBUGFUNCTION, NULL); + curl_easy_cleanup(*ch); + *ch = NULL; + } +} +/* }}} */ + /* {{{ http_request *http_request_init(http_request *) */ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC) { @@ -241,16 +282,7 @@ PHP_HTTP_API void _http_request_dtor(http_request *request) { TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); - if (request->ch) { - /* avoid nasty segfaults with already cleaned up callbacks */ - HTTP_CURL_OPT(NOPROGRESS, 1); - HTTP_CURL_OPT(PROGRESSFUNCTION, NULL); - HTTP_CURL_OPT(VERBOSE, 0); - HTTP_CURL_OPT(DEBUGFUNCTION, NULL); - curl_easy_cleanup(request->ch); - request->ch = NULL; - } - + http_curl_free(&request->ch); http_request_reset(request); phpstr_dtor(&request->_cache.cookies); @@ -288,6 +320,10 @@ PHP_HTTP_API void _http_request_reset(http_request *request) phpstr_dtor(&request->conv.request); phpstr_dtor(&request->conv.response); http_request_body_dtor(request->body); + + if (request->ch) { + http_request_defaults(request); + } } /* }}} */ @@ -295,23 +331,6 @@ PHP_HTTP_API void _http_request_reset(http_request *request) PHP_HTTP_API void _http_request_defaults(http_request *request) { if (request->ch) { -#ifdef HAVE_CURL_EASY_RESET - curl_easy_reset(request->ch); -#endif -#if defined(ZTS) - HTTP_CURL_OPT(NOSIGNAL, 1); -#endif - HTTP_CURL_OPT(PRIVATE, request); - HTTP_CURL_OPT(ERRORBUFFER, request->_error); - HTTP_CURL_OPT(HEADER, 0); - HTTP_CURL_OPT(FILETIME, 1); - HTTP_CURL_OPT(AUTOREFERER, 1); - HTTP_CURL_OPT(VERBOSE, 1); - HTTP_CURL_OPT(HEADERFUNCTION, NULL); - HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback); - HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback); - HTTP_CURL_OPT(IOCTLFUNCTION, http_curl_ioctl_callback); - HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback); HTTP_CURL_OPT(PROGRESSFUNCTION, NULL); HTTP_CURL_OPT(URL, NULL); HTTP_CURL_OPT(NOPROGRESS, 1); @@ -394,12 +413,10 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); - HTTP_CHECK_CURL_INIT(request->ch, curl_easy_init(), return FAILURE); - + HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE); http_request_defaults(request); /* set options */ - HTTP_CURL_OPT(DEBUGDATA, request); HTTP_CURL_OPT(URL, request->url); /* progress callback */ diff --git a/http_request_object.c b/http_request_object.c index 5e233ad..c8fe26d 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -302,7 +302,7 @@ PHP_MINIT_FUNCTION(http_request_object) zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC) { - return http_request_object_new_ex(ce, curl_easy_init(), NULL); + return http_request_object_new_ex(ce, NULL, NULL); } zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC) @@ -338,6 +338,7 @@ zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC) old_zo = zend_objects_get_address(this_ptr TSRMLS_CC); new_ov = http_request_object_new_ex(old_zo->ce, curl_easy_duphandle(old_obj->request->ch), &new_obj); + http_curl_init_ex(new_obj->request->ch, new_obj->request, new_obj->request->_error); zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); phpstr_append(&new_obj->history, old_obj->history.data, old_obj->history.used); @@ -436,7 +437,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ STATUS status = SUCCESS; http_request_reset(obj->request); - HTTP_CHECK_CURL_INIT(obj->request->ch, curl_easy_init(), return FAILURE); + HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE); obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url))); diff --git a/php_http_request_api.h b/php_http_request_api.h index 731cbf0..28e3a85 100644 --- a/php_http_request_api.h +++ b/php_http_request_api.h @@ -50,6 +50,13 @@ typedef struct { } http_request; +#define http_curl_init(r) http_curl_init_ex(NULL, (r), (r)->_error) +#define http_curl_init_ex(c, r, e) _http_curl_init_ex((c), (r), (e)) +PHP_HTTP_API CURL *_http_curl_init_ex(CURL *ch, void *context, char *error_buffer); + +#define http_curl_free(c) _http_curl_free(c) +PHP_HTTP_API void _http_curl_free(CURL **ch); + #define http_request_new() _http_request_init_ex(NULL, NULL, 0, NULL ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC) #define http_request_init(r) _http_request_init_ex((r), NULL, 0, NULL ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC) #define http_request_init_ex(r, c, m, u) _http_request_init_ex((r), (c), (m), (u) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)