-static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt)
-{
- Z_ADDREF_P(opt);
-
- if (h) {
- zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL);
- } else {
- zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL);
- }
-
- return opt;
-}
-
-static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type)
-{
- if (options) {
- zval **zoption;
- ulong h = zend_hash_func(key, keylen);
-
- if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
- zval *option = php_http_ztyp(type, *zoption);
-
- if (cache) {
- zval *cached = cache_option(cache, key, keylen, h, option);
-
- zval_ptr_dtor(&option);
- return cached;
- }
- return option;
- }
- }
-
- return NULL;
-}
-
-static STATUS set_options(php_http_client_t *h, HashTable *options)
-{
- zval *zoption;
- php_http_client_curl_t *curl = h->ctx;
- CURL *ch = curl->handle;
- TSRMLS_FETCH_FROM_CTX(h->ts);
-
- /* proxy */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) {
- curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
- }
- /* type */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
- }
- /* port */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
- }
- /* user:pass */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
- }
- /* auth method */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
- }
- /* tunnel */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L);
- }
-#if PHP_HTTP_CURL_VERSION(7,19,4)
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) {
- curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption));
- }
-#endif
-
- /* dns */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
- }
-#if PHP_HTTP_CURL_VERSION(7,21,3)
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) {
- php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
- HashPosition pos;
- zval **data;
-
- FOREACH_KEYVAL(pos, zoption, key, data) {
- zval *cpy = php_http_ztyp(IS_STRING, *data);
-
- curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy));
-
- zval_ptr_dtor(&cpy);
- }
-
- curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve);
- }
-#endif
-#if PHP_HTTP_CURL_VERSION(7,24,0)
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption));
- }
-#endif
-
- /* limits */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
- }
- /* LSF weirdance
- if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
- }
- */
- /* crashes
- if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
- } */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L);
- }
-
- /* outgoing interface */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) {
- curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) {
- zval **prs, **pre;
-
- zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
- if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
- zend_hash_move_forward(Z_ARRVAL_P(zoption));
- if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
- zval *prs_cpy = php_http_ztyp(IS_LONG, *prs);
- zval *pre_cpy = php_http_ztyp(IS_LONG, *pre);
-
- if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
- curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
- curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
- }
- zval_ptr_dtor(&prs_cpy);
- zval_ptr_dtor(&pre_cpy);
- }
- }
- }
-
- /* another port */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
- }
-
- /* RFC4007 zone_id */
-#if PHP_HTTP_CURL_VERSION(7,19,0)
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption));
- }
-#endif
-
- /* auth */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
- }
-
- /* redirects, defaults to 0 */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
- curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption));
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) {
- curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
- }
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) {
-#if PHP_HTTP_CURL_VERSION(7,19,1)
- curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L);
-#else
- curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L);
-#endif
- }
- }
-
- /* retries, defaults to 0 */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) {
- curl->options.retry.count = Z_LVAL_P(zoption);
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) {
- curl->options.retry.delay = Z_DVAL_P(zoption);
- }
- }
-
- /* referer */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
- }
-
- /* useragent, default "PECL::HTTP/version (PHP/version)" */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) {
- /* allow to send no user agent, not even default one */
- if (Z_STRLEN_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
- } else {
- curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL);
- }
- }
-
- /* resume */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
- curl->options.range_request = 1;
- curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
- }
- /* or range of kind array(array(0,499), array(100,1499)) */
- else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
- HashPosition pos1, pos2;
- zval **rr, **rb, **re;
- php_http_buffer_t rs;
-
- php_http_buffer_init(&rs);
- FOREACH_VAL(pos1, zoption, rr) {
- if (Z_TYPE_PP(rr) == IS_ARRAY) {
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
- if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
- zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
- if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
- if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) &&
- ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) {
- zval *rbl = php_http_ztyp(IS_LONG, *rb);
- zval *rel = php_http_ztyp(IS_LONG, *re);
-
- if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
- php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
- }
- zval_ptr_dtor(&rbl);
- zval_ptr_dtor(&rel);
- }
- }
- }
- }
- }
-
- if (PHP_HTTP_BUFFER_LEN(&rs)) {
- zval *cached_range;
-
- curl->options.range_request = 1;
- /* ditch last comma */
- PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0';
- /* cache string */
- MAKE_STD_ZVAL(cached_range);
- ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0);
- curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range)));
- zval_ptr_dtor(&cached_range);
- }
- }
-
- /* etag */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"'));
- php_http_buffer_t header;
-
- php_http_buffer_init(&header);
- php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", curl->options.range_request?"If-Match":"If-None-Match", Z_STRVAL_P(zoption));
- php_http_buffer_fix(&header);
- curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
- php_http_buffer_dtor(&header);
- }
- /* compression */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) {
- curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
- }
- curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers);
-
- /* lastmodified */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) {
- if (Z_LVAL_P(zoption)) {
- if (Z_LVAL_P(zoption) > 0) {
- curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
- } else {
- curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption));
- }
- curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
- } else {
- curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
- }
- }
-
- /* cookies, array('name' => 'value') */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) {
- if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
- zval *urlenc_cookies = NULL;
- /* check whether cookies should not be urlencoded; default is to urlencode them */
- if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
- if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) {
- php_http_buffer_fix(&curl->options.cookies);
- curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data);
- }
- } else {
- HashPosition pos;
- php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
- zval **cookie_val;
-
- FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
- if (cookie_key.type == HASH_KEY_IS_STRING) {
- zval *val = php_http_ztyp(IS_STRING, *cookie_val);
- php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
- zval_ptr_dtor(&val);
- }
- }
-
- php_http_buffer_fix(&curl->options.cookies);
- if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) {
- curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies));
- }
- }
- }
- }
-
- /* don't load session cookies from cookiestore */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L);
- }
-
- /* cookiestore, read initial cookies from that file and store cookies back into that file */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) {
- php_http_client_curl_storage_t *storage = get_storage(curl->handle);
-
- if (Z_STRLEN_P(zoption)) {
- if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) {
- return FAILURE;
- }
- }
- if (storage->cookiestore) {
- pefree(storage->cookiestore, 1);
- }
- storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
- curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore);
- curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore);
- }
-
- /* maxfilesize */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
- }
-
- /* http protocol */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) {
- curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
- }
-
- /* timeout, defaults to 0 */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) {
- curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
- }
- /* connecttimeout, defaults to 0 */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
- curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
- }
-
- /* ssl */
- if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) {
- php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
- zval **param;
- HashPosition pos;
-
- FOREACH_KEYVAL(pos, zoption, key, param) {
- if (key.type == HASH_KEY_IS_STRING) {
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
-
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
-
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
- PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
-
- PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
- PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
-
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
-#if PHP_HTTP_CURL_VERSION(7,19,0)
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1);
- #if defined(PHP_HTTP_HAVE_OPENSSL)
- PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1);
- #endif
-#endif
-#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
- PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3);
-#endif
- }
- }
- }
- return SUCCESS;
-}
-
-static STATUS get_info(CURL *ch, HashTable *info)