followup on #44
[m6w6/ext-http] / src / php_http_client_curl.c
index 317edd3baf00851f80fc40c721ad09e3358200f2..423e12e3c6214c3e5f034e8b5e4c5e375f7dbc4a 100644 (file)
@@ -198,14 +198,10 @@ static php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
 
 static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx)
 {
-       php_http_message_body_t *body = ctx;
+       php_stream *s = php_http_message_body_stream(ctx);
 
-       if (body && body->res) {
-               php_stream *s = php_http_message_body_stream(body);
-
-               if (s) {
-                       return php_stream_read(s, data, len * n);
-               } else abort();
+       if (s) {
+               return php_stream_read(s, data, len * n);
        }
        return 0;
 }
@@ -629,7 +625,7 @@ static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_ha
 {
        php_http_message_t *response;
        php_http_header_parser_t parser;
-       zval *zh;
+       zval *zh, tmp;
 
        response = php_http_message_init(NULL, 0, h->response.body);
        php_http_header_parser_init(&parser);
@@ -658,23 +654,23 @@ static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_ha
 
        /* let's update the response headers */
        if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length")))) {
-               Z_TRY_ADDREF_P(zh);
-               zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), zh);
+               ZVAL_COPY(&tmp, zh);
+               zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), &tmp);
        }
        if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding")))) {
-               Z_TRY_ADDREF_P(zh);
-               zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), zh);
+               ZVAL_COPY(&tmp, zh);
                zend_hash_str_del(&response->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding"));
+               zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), &tmp);
        }
        if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range")))) {
-               Z_TRY_ADDREF_P(zh);
-               zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), zh);
+               ZVAL_COPY(&tmp, zh);
                zend_hash_str_del(&response->hdrs, "Content-Range", lenof("Content-Range"));
+               zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), &tmp);
        }
        if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding")))) {
-               Z_TRY_ADDREF_P(zh);
-               zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), zh);
+               ZVAL_COPY(&tmp, zh);
                zend_hash_str_del(&response->hdrs, "Content-Encoding", lenof("Content-Encoding"));
+               zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), &tmp);
        }
        php_http_message_update_headers(response);
 
@@ -1795,9 +1791,15 @@ static ZEND_RESULT_CODE php_http_curlm_option_set_share_cookies(php_http_option_
        } else {
                rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE);
        }
-       return CURLSHE_OK == rc ? SUCCESS : FAILURE;
+
+       if (CURLSHE_OK != rc) {
+               php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc));
+               return FAILURE;
+       }
+       return SUCCESS;
 }
 
+#if PHP_HTTP_CURL_VERSION(7,23,0)
 static ZEND_RESULT_CODE php_http_curlm_option_set_share_ssl(php_http_option_t *opt, zval *value, void *userdata)
 {
        php_http_client_t *client = userdata;
@@ -1809,8 +1811,14 @@ static ZEND_RESULT_CODE php_http_curlm_option_set_share_ssl(php_http_option_t *o
        } else {
                rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION);
        }
-       return CURLSHE_OK == rc ? SUCCESS : FAILURE;
+
+       if (CURLSHE_OK != rc) {
+               php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc));
+               return FAILURE;
+       }
+       return SUCCESS;
 }
+#endif
 
 static void php_http_curlm_options_init(php_http_options_t *registry)
 {
@@ -1868,10 +1876,12 @@ static void php_http_curlm_options_init(php_http_options_t *registry)
                opt->setter = php_http_curlm_option_set_share_cookies;
                ZVAL_TRUE(&opt->defval);
        }
+#if PHP_HTTP_CURL_VERSION(7,23,0)
        if ((opt = php_http_option_register(registry, ZEND_STRL("share_ssl"), 0, _IS_BOOL))) {
                opt->setter = php_http_curlm_option_set_share_ssl;
                ZVAL_TRUE(&opt->defval);
        }
+#endif
 }
 
 static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *val, void *userdata)
@@ -1901,15 +1911,18 @@ static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *
                case _IS_BOOL:
                        if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) zend_is_true(val)))) {
                                rv = FAILURE;
+                               php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc));
                        }
                        break;
                case IS_LONG:
                        if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, Z_LVAL_P(val)))) {
                                rv = FAILURE;
+                               php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc));
                        }
                        break;
                default:
                        rv = FAILURE;
+                       php_error_docref(NULL, E_NOTICE, "Could not set option %s", opt->name->val);
                        break;
                }
        }
@@ -1918,9 +1931,6 @@ static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *
                zval_ptr_dtor(val);
        }
 
-       if (rv != SUCCESS) {
-               php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
-       }
        return rv;
 }