X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_api.c;h=662c2ab7bc48311cff20747aa411f167fc3a2558;hp=26b4b0992cccd8db395c8cb3e772823b4c5d8ca8;hb=e81100d361a8dc48fa491d973e936a19ea428853;hpb=df2638b79da782a9d3a927fcdd426d0067e33c6b diff --git a/http_request_api.c b/http_request_api.c index 26b4b09..662c2ab 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2005, Michael Wallner | + | Copyright (c) 2004-2006, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -29,8 +29,6 @@ # include "php_http_request_object.h" #endif -ZEND_EXTERN_MODULE_GLOBALS(http); - /* {{{ cruft for thread safe SSL crypto locks */ #if defined(ZTS) && defined(HTTP_HAVE_SSL) # ifdef PHP_WIN32 @@ -172,28 +170,21 @@ PHP_MSHUTDOWN_FUNCTION(http_request) #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc) #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \ if (!strcasecmp(key, #keyname)) { \ - int ok = 1; \ - zval *orig = *param; \ - convert_to_string_ex(param); \ + zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_STRING, *param)); \ if (obdc) { \ - HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_PP(param), ok = 0); \ + HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \ } \ + HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \ key = NULL; \ - if (ok) { \ - HTTP_CURL_OPT(optname, Z_STRVAL_PP(param)); \ - if (orig != *param) zval_ptr_dtor(param); \ - continue; \ - } \ - if (orig != *param) zval_ptr_dtor(param); \ - return FAILURE; \ + continue; \ } #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname) #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname) #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname) #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \ if (!strcasecmp(key, #keyname)) { \ - convert_to_long(*param); \ - HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \ + zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \ + HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \ key = NULL; \ continue; \ } @@ -203,6 +194,9 @@ PHP_MSHUTDOWN_FUNCTION(http_request) #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC) #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC) static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC); +#define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC) +#define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC) +static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC); static size_t http_curl_read_callback(void *, size_t, size_t, void *); static int http_curl_progress_callback(void *, double, double, double, double); @@ -224,7 +218,7 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch memset(r, 0, sizeof(http_request)); r->ch = ch; - r->url = (url && *url) ? estrdup(url) : NULL; + r->url = (url) ? http_absolute_url(url) : NULL; r->meth = (meth > 0) ? meth : HTTP_GET; phpstr_init(&r->conv.request); @@ -245,10 +239,10 @@ PHP_HTTP_API void _http_request_dtor(http_request *request) if (request->ch) { /* avoid nasty segfaults with already cleaned up callbacks */ - curl_easy_setopt(request->ch, CURLOPT_NOPROGRESS, 1); - curl_easy_setopt(request->ch, CURLOPT_PROGRESSFUNCTION, NULL); - curl_easy_setopt(request->ch, CURLOPT_VERBOSE, 0); - curl_easy_setopt(request->ch, CURLOPT_DEBUGFUNCTION, NULL); + 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; } @@ -332,6 +326,9 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")"); HTTP_CURL_OPT(HTTPHEADER, NULL); HTTP_CURL_OPT(COOKIE, NULL); +#if LIBCURL_VERSION_NUM >= 0x070e01 + HTTP_CURL_OPT(COOKIELIST, NULL); +#endif HTTP_CURL_OPT(COOKIEFILE, NULL); HTTP_CURL_OPT(COOKIEJAR, NULL); HTTP_CURL_OPT(RESUME_FROM, 0); @@ -372,10 +369,16 @@ PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zva if (request->_progress_callback) { zval_ptr_dtor(&request->_progress_callback); } - if (cb) { + if ((request->_progress_callback = cb)) { ZVAL_ADDREF(cb); + HTTP_CURL_OPT(NOPROGRESS, 0); + HTTP_CURL_OPT(PROGRESSDATA, request); + HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback); + } else { + HTTP_CURL_OPT(NOPROGRESS, 1); + HTTP_CURL_OPT(PROGRESSDATA, NULL); + HTTP_CURL_OPT(PROGRESSFUNCTION, NULL); } - request->_progress_callback = cb; } /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */ @@ -395,10 +398,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti HTTP_CURL_OPT(URL, request->url); /* progress callback */ - if ((zoption = http_request_option(request, options, "onprogress", 0))) { - HTTP_CURL_OPT(NOPROGRESS, 0); - HTTP_CURL_OPT(PROGRESSDATA, request); - HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback); + if ((zoption = http_request_option(request, options, "onprogress", -1))) { http_request_set_progress_callback(request, zoption); } @@ -471,16 +471,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti zval **header_val; if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) { char header[1024] = {0}; - zval val; - - val = **header_val; - zval_copy_ctor(&val); - INIT_PZVAL(&val); - convert_to_string(&val); - snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL(val)); + ZVAL_ADDREF(*header_val); + convert_to_string_ex(header_val); + snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val)); request->_cache.headers = curl_slist_append(request->_cache.headers, header); - zval_dtor(&val); + zval_ptr_dtor(header_val); } /* reset */ @@ -495,40 +491,14 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti /* cookies, array('name' => 'value') */ if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) { - char *cookie_key = NULL; - ulong cookie_idx = 0; - HashPosition pos; - phpstr_dtor(&request->_cache.cookies); - - FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) { - if (cookie_key) { - zval **cookie_val; - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) { - zval val; - - val = **cookie_val; - INIT_PZVAL(&val); - zval_copy_ctor(&val); - convert_to_string(&val); - - phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL(val)); - - zval_dtor(&val); - } - - /* reset */ - cookie_key = NULL; - } - } - - if (request->_cache.cookies.used) { + if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", sizeof("; ")-1, NULL, 0)) { phpstr_fix(&request->_cache.cookies); HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data); } } -#if LIBCURL_VERSIONNUM >= 0x070f01 +#if LIBCURL_VERSION_NUM >= 0x070e01 /* reset cookies */ if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) { HTTP_CURL_OPT(COOKIELIST, "ALL"); @@ -627,24 +597,24 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti switch (request->meth) { case HTTP_GET: - curl_easy_setopt(request->ch, CURLOPT_HTTPGET, 1); + HTTP_CURL_OPT(HTTPGET, 1); break; case HTTP_HEAD: - curl_easy_setopt(request->ch, CURLOPT_NOBODY, 1); + HTTP_CURL_OPT(NOBODY, 1); break; case HTTP_POST: - curl_easy_setopt(request->ch, CURLOPT_POST, 1); + HTTP_CURL_OPT(POST, 1); break; case HTTP_PUT: - curl_easy_setopt(request->ch, CURLOPT_UPLOAD, 1); + HTTP_CURL_OPT(UPLOAD, 1); break; default: if (http_request_method_exists(0, request->meth, NULL)) { - curl_easy_setopt(request->ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth)); + HTTP_CURL_OPT(CUSTOMREQUEST, http_request_method_name(request->meth)); } else { http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url); return FAILURE; @@ -657,18 +627,18 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti switch (request->body->type) { case HTTP_REQUEST_BODY_CSTRING: - curl_easy_setopt(request->ch, CURLOPT_POSTFIELDS, request->body->data); - curl_easy_setopt(request->ch, CURLOPT_POSTFIELDSIZE, request->body->size); + HTTP_CURL_OPT(POSTFIELDS, request->body->data); + HTTP_CURL_OPT(POSTFIELDSIZE, request->body->size); break; case HTTP_REQUEST_BODY_CURLPOST: - curl_easy_setopt(request->ch, CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data); + HTTP_CURL_OPT(HTTPPOST, (struct curl_httppost *) request->body->data); break; case HTTP_REQUEST_BODY_UPLOADFILE: - curl_easy_setopt(request->ch, CURLOPT_IOCTLDATA, request); - curl_easy_setopt(request->ch, CURLOPT_READDATA, request); - curl_easy_setopt(request->ch, CURLOPT_INFILESIZE, request->body->size); + HTTP_CURL_OPT(IOCTLDATA, request); + HTTP_CURL_OPT(READDATA, request); + HTTP_CURL_OPT(INFILESIZE, request->body->size); break; default: @@ -749,30 +719,26 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */ static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow) { - int rc; - zval *params_pass[4], params_local[4], retval; + zval *param, retval; http_request *request = (http_request *) ctx; TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); - params_pass[0] = ¶ms_local[0]; - params_pass[1] = ¶ms_local[1]; - params_pass[2] = ¶ms_local[2]; - params_pass[3] = ¶ms_local[3]; - - INIT_PZVAL(params_pass[0]); - INIT_PZVAL(params_pass[1]); - INIT_PZVAL(params_pass[2]); - INIT_PZVAL(params_pass[3]); - ZVAL_DOUBLE(params_pass[0], dltotal); - ZVAL_DOUBLE(params_pass[1], dlnow); - ZVAL_DOUBLE(params_pass[2], ultotal); - ZVAL_DOUBLE(params_pass[3], ulnow); - INIT_PZVAL(&retval); ZVAL_NULL(&retval); - rc = call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 4, params_pass TSRMLS_CC); + + MAKE_STD_ZVAL(param); + array_init(param); + add_assoc_double(param, "dltotal", dltotal); + add_assoc_double(param, "dlnow", dlnow); + add_assoc_double(param, "ultotal", ultotal); + add_assoc_double(param, "ulnow", ulnow); + + call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, ¶m TSRMLS_CC); + + zval_ptr_dtor(¶m); zval_dtor(&retval); - return rc; + + return 0; } /* }}} */ @@ -850,6 +816,8 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options, zval **zoption; #ifdef ZEND_ENGINE_2 ulong h = zend_get_hash_value(key, keylen); +#else + ulong h = 0; #endif if (!options || @@ -861,34 +829,32 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options, ) { return NULL; } + + return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption)); +} +/* }}} */ - if (Z_TYPE_PP(zoption) != type) { - switch (type) - { - case IS_BOOL: convert_to_boolean_ex(zoption); break; - case IS_LONG: convert_to_long_ex(zoption); break; - case IS_DOUBLE: convert_to_double_ex(zoption); break; - case IS_STRING: convert_to_string_ex(zoption); break; - case IS_ARRAY: convert_to_array_ex(zoption); break; - case IS_OBJECT: convert_to_object_ex(zoption); break; - default: - break; - } - } +/* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */ +static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC) +{ + ZVAL_ADDREF(opt); - /* cache strings */ - if (type == IS_STRING) { - ZVAL_ADDREF(*zoption); #ifdef ZEND_ENGINE_2 - _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, zoption, sizeof(zval *), NULL, + if (h) { + _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL, zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC); -#else - zend_hash_add_or_update(&r->_cache.options, key, keylen, zoption, sizeof(zval *), NULL, - zend_hash_exists(&r->_cache.options, key, keylen)?HASH_UPDATE:HASH_ADD); + } + else #endif + { + if (zend_hash_exists(&r->_cache.options, key, keylen)) { + zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL); + } else { + zend_hash_add(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL); + } } - return *zoption; + return opt; } /* }}} */