- request->_retry.count = 0;
- }
-
- /* referer */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) {
- PHP_HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
- }
-
- /* useragent, default "PECL::HTTP/version (PHP/version)" */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("useragent"), IS_STRING))) {
- /* allow to send no user agent, not even default one */
- if (Z_STRLEN_P(zoption)) {
- PHP_HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
- } else {
- PHP_HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
- }
- }
-
- /* resume */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
- range_req = 1;
- PHP_HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
- }
- /* or range of kind array(array(0,499), array(100,1499)) */
- else if ((zoption = php_http_request_option(request, 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_zsep(IS_LONG, *rb);
- zval *rel = php_http_zsep(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;
-
- /* 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);
- PHP_HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(php_http_request_option_cache(request, ZEND_STRS("range"), 0, cached_range)));
- zval_ptr_dtor(&cached_range);
- }
- }
-
- /* additional headers, array('name' => 'value') */
- if (request->_cache.headers) {
- curl_slist_free_all(request->_cache.headers);
- request->_cache.headers = NULL;
- }
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("headers"), IS_ARRAY))) {
- php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
- zval **header_val;
- HashPosition pos;
- php_http_buffer_t header;
-
- php_http_buffer_init(&header);
- FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
- if (header_key.type == HASH_KEY_IS_STRING) {
- zval *header_cpy = php_http_zsep(IS_STRING, *header_val);
-
- if (!strcasecmp(header_key.str, "range")) {
- range_req = 1;
- }
-
- php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
- php_http_buffer_fix(&header);
- request->_cache.headers = curl_slist_append(request->_cache.headers, PHP_HTTP_BUFFER_VAL(&header));
- php_http_buffer_reset(&header);
-
- zval_ptr_dtor(&header_cpy);
- }
- }
- php_http_buffer_dtor(&header);
- }
- /* etag */
- if ((zoption = php_http_request_option(request, 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\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption));
- php_http_buffer_fix(&header);
- request->_cache.headers = curl_slist_append(request->_cache.headers, PHP_HTTP_BUFFER_VAL(&header));
- php_http_buffer_dtor(&header);
- }
- /* compression */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) {
- request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
- }
- PHP_HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
-
- /* lastmodified */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("lastmodified"), IS_LONG))) {
- if (Z_LVAL_P(zoption)) {
- if (Z_LVAL_P(zoption) > 0) {
- PHP_HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
- } else {
- PHP_HTTP_CURL_OPT(CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption));
- }
- PHP_HTTP_CURL_OPT(CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
- } else {
- PHP_HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
- }
- }
-
- /* cookies, array('name' => 'value') */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("cookies"), IS_ARRAY))) {
- php_http_buffer_dtor(&request->_cache.cookies);
- 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 = php_http_request_option(request, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
- if (SUCCESS == php_http_url_encode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0 TSRMLS_CC)) {
- php_http_buffer_fix(&request->_cache.cookies);
- PHP_HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.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_zsep(IS_STRING, *cookie_val);
- php_http_buffer_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
- zval_ptr_dtor(&val);
- }
- }
-
- php_http_buffer_fix(&request->_cache.cookies);
- if (PHP_HTTP_BUFFER_LEN(&request->_cache.cookies)) {
- PHP_HTTP_CURL_OPT(CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&request->_cache.cookies));
- }
- }
- }
- }
-
- /* don't load session cookies from cookiestore */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) {
- PHP_HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1L);
- }
-
- /* cookiestore, read initial cookies from that file and store cookies back into that file */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("cookiestore"), IS_STRING))) {
- 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);
- PHP_HTTP_CURL_OPT(CURLOPT_COOKIEFILE, storage->cookiestore);
- PHP_HTTP_CURL_OPT(CURLOPT_COOKIEJAR, storage->cookiestore);
- }
-
- /* maxfilesize */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("maxfilesize"), IS_LONG))) {
- PHP_HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
- }
-
- /* http protocol */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("protocol"), IS_LONG))) {
- PHP_HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
- }
-
- /* timeout, defaults to 0 */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("timeout"), IS_DOUBLE))) {
- PHP_HTTP_CURL_OPT(CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
- }
- /* connecttimeout, defaults to 0 */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
- PHP_HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
- }
-
- /* ssl */
- if ((zoption = php_http_request_option(request, options, ZEND_STRS("ssl"), IS_ARRAY))) {
- php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
- zval **param;
- HashPosition pos;