- fix switch() CS
[m6w6/ext-http] / http_request_api.c
index 6d21d9b7107e0e33e02179822552e816870ddf65..1addfd96d0b418cd0a8d33a0e3ccc4a85aea57e4 100644 (file)
@@ -130,7 +130,7 @@ PHP_MINIT_FUNCTION(http_request)
        CRYPTO_set_id_callback(http_openssl_thread_id);
        CRYPTO_set_locking_callback(http_openssl_thread_lock);
 #endif
-#ifdef HTTP_NED_GNUTLS_TSL
+#ifdef HTTP_NEED_GNUTLS_TSL
        gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
 #endif
 
@@ -257,7 +257,7 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
        }
 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
        if (!strcasecmp(key, keyname)) { \
-               zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_STRING, *param)); \
+               zval *copy = http_request_option_cache_ex(request, keyname, strlen(keyname)+1, 0, zval_copy(IS_STRING, *param)); \
                if (obdc) { \
                        HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
                } \
@@ -272,9 +272,10 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
        }
 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
        if (!strcasecmp(key, keyname)) { \
-               zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_LONG, *param)); \
+               zval *copy = zval_copy(IS_LONG, *param); \
                HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
                key = NULL; \
+               zval_free(&copy); \
                continue; \
        }
 /* }}} */
@@ -373,8 +374,6 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch
 /* {{{ void http_request_dtor(http_request *) */
 PHP_HTTP_API void _http_request_dtor(http_request *request)
 {
-       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-       
        http_curl_free(&request->ch);
        http_request_reset(request);
        
@@ -493,8 +492,6 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
 
 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
 {
-       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-       
        if (request->_progress_callback) {
                zval_ptr_dtor(&request->_progress_callback);
        }
@@ -560,13 +557,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                        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)) {
+                       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)) {
+                               if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
                                        zval *prs_cpy = zval_copy(IS_LONG, *prs), *pre_cpy = zval_copy(IS_LONG, *pre);
                                        
                                        if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
-                                               fprintf(stderr, "Using portrange: %ld-%ld\n", Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy));
                                                HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
                                                HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
                                        }
@@ -630,9 +626,9 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                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)) {
+                               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 (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 = zval_copy(IS_LONG, *rb), *rel = zval_copy(IS_LONG, *re);
@@ -674,7 +670,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                FOREACH_KEY(pos, zoption, header_key, header_idx) {
                        if (header_key) {
                                zval **header_val;
-                               if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
+                               if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &header_val, &pos)) {
                                        char header[1024] = {0};
                                        
                                        ZVAL_ADDREF(*header_val);
@@ -742,7 +738,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                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)) {
+                                               if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &cookie_val, &pos)) {
                                                        zval *val = zval_copy(IS_STRING, *cookie_val);
                                                        phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
                                                        zval_free(&val);
@@ -842,23 +838,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* request method */
-       switch (request->meth)
-       {
+       switch (request->meth) {
                case HTTP_GET:
                        HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
-               break;
+                       break;
 
                case HTTP_HEAD:
                        HTTP_CURL_OPT(CURLOPT_NOBODY, 1);
-               break;
+                       break;
 
                case HTTP_POST:
                        HTTP_CURL_OPT(CURLOPT_POST, 1);
-               break;
+                       break;
 
                case HTTP_PUT:
                        HTTP_CURL_OPT(CURLOPT_UPLOAD, 1);
-               break;
+                       break;
 
                default:
                        if (http_request_method_exists(0, request->meth, NULL)) {
@@ -867,20 +862,19 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
                                return FAILURE;
                        }
-               break;
+                       break;
        }
 
        /* attach request body */
        if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_EMPTY:
                                /* nothing */
-                       break;
+                               break;
                        
                        case HTTP_REQUEST_BODY_CURLPOST:
                                HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
-                       break;
+                               break;
 
                        case HTTP_REQUEST_BODY_CSTRING:
                                if (request->meth != HTTP_PUT) {
@@ -893,13 +887,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
                                HTTP_CURL_OPT(CURLOPT_READDATA, request);
                                HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
-                       break;
+                               break;
 
                        default:
                                /* shouldn't ever happen */
                                http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
                                return FAILURE;
-                       break;
                }
        }
 
@@ -965,8 +958,7 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
        if (request->body) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_CSTRING:
                        {
                                size_t out = MIN(len * n, request->body->size - request->body->priv);
@@ -976,12 +968,11 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct
                                        request->body->priv += out;
                                        return out;
                                }
+                               break;
                        }
-                       break;
                        
                        case HTTP_REQUEST_BODY_UPLOADFILE:
                                return php_stream_read((php_stream *) request->body->data, data, len * n);
-                       break;
                }
        }
        return 0;
@@ -1005,7 +996,9 @@ static int http_curl_progress_callback(void *ctx, double dltotal, double 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, &param TSRMLS_CC);
+       with_error_handling(EH_NORMAL, NULL) {
+               call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+       } end_error_handling();
        
        zval_ptr_dtor(&param);
        zval_dtor(&retval);
@@ -1025,18 +1018,17 @@ static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
        }
        
        if (request->body) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_CSTRING:
                                request->body->priv = 0;
                                return CURLIOE_OK;
-                       break;
+                               break;
                                
                        case HTTP_REQUEST_BODY_UPLOADFILE:
                                if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
                                        return CURLIOE_OK;
                                }
-                       break;
+                               break;
                }
        }
        
@@ -1049,22 +1041,21 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
 {
        http_request *request = (http_request *) ctx;
 
-       switch (type)
-       {
+       switch (type) {
                case CURLINFO_DATA_IN:
                        if (request->conv.last_type == CURLINFO_HEADER_IN) {
                                phpstr_appends(&request->conv.response, HTTP_CRLF);
                        }
                case CURLINFO_HEADER_IN:
                        phpstr_append(&request->conv.response, data, length);
-               break;
+                       break;
                case CURLINFO_DATA_OUT:
                        if (request->conv.last_type == CURLINFO_HEADER_OUT) {
                                phpstr_appends(&request->conv.request, HTTP_CRLF);
                        }
                case CURLINFO_HEADER_OUT:
                        phpstr_append(&request->conv.request, data, length);
-               break;
+                       break;
                default:
 #if 0
                        fprintf(stderr, "## ", type);
@@ -1084,7 +1075,7 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
                                fprintf(stderr, "\n");
                        }
 #endif
-               break;
+                       break;
        }
 
        if (type) {
@@ -1106,9 +1097,9 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options,
        
        if (!options || 
 #ifdef ZEND_ENGINE_2
-                       (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))
+                       (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void *) &zoption))
 #else
-                       (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))
+                       (SUCCESS != zend_hash_find(options, key, keylen, (void *) &zoption))
 #endif
        ) {
                return NULL;