- return zvals directly and don'T assume a type
authorMichael Wallner <mike@php.net>
Wed, 28 Dec 2005 14:16:27 +0000 (14:16 +0000)
committerMichael Wallner <mike@php.net>
Wed, 28 Dec 2005 14:16:27 +0000 (14:16 +0000)
- fix crash with http_request_body_fill(NULL,NULL,NULL)
- add test

http_request_body_api.c
http_request_object.c
tests/HttpRequest_005.phpt [new file with mode: 0644]

index 8476b63e48c9ee6f1afbbffafad0da8b6d457e6b..401e0ac453500c15d9b5a34d616224c2632263c9 100644 (file)
@@ -52,31 +52,33 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body,
                struct curl_httppost *http_post_data[2] = {NULL, NULL};
 
                /* normal data */
-               FOREACH_HASH_KEYVAL(pos, fields, key, idx, data) {
-                       if (key) {
-                               CURLcode err;
-                               zval *orig = *data;
-                               
-                               convert_to_string_ex(data);
-                               err = curl_formadd(&http_post_data[0], &http_post_data[1],
-                                       CURLFORM_COPYNAME,                      key,
-                                       CURLFORM_COPYCONTENTS,          Z_STRVAL_PP(data),
-                                       CURLFORM_CONTENTSLENGTH,        (long) Z_STRLEN_PP(data),
-                                       CURLFORM_END
-                               );
-                               
-                               if (orig != *data) {
-                                       zval_ptr_dtor(data);
-                               }
-                               
-                               if (CURLE_OK != err) {
-                                       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
-                                       curl_formfree(http_post_data[0]);
-                                       return NULL;
+               if (fields) {
+                       FOREACH_HASH_KEYVAL(pos, fields, key, idx, data) {
+                               if (key) {
+                                       CURLcode err;
+                                       zval *orig = *data;
+                                       
+                                       convert_to_string_ex(data);
+                                       err = curl_formadd(&http_post_data[0], &http_post_data[1],
+                                               CURLFORM_COPYNAME,                      key,
+                                               CURLFORM_COPYCONTENTS,          Z_STRVAL_PP(data),
+                                               CURLFORM_CONTENTSLENGTH,        (long) Z_STRLEN_PP(data),
+                                               CURLFORM_END
+                                       );
+                                       
+                                       if (orig != *data) {
+                                               zval_ptr_dtor(data);
+                                       }
+                                       
+                                       if (CURLE_OK != err) {
+                                               http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
+                                               curl_formfree(http_post_data[0]);
+                                               return NULL;
+                                       }
+       
+                                       /* reset */
+                                       key = NULL;
                                }
-
-                               /* reset */
-                               key = NULL;
                        }
                }
 
@@ -129,7 +131,7 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body,
                
                return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CURLPOST, http_post_data[0], 0, 1);
 
-       } else {
+       } else if (fields) {
                char *encoded;
                size_t encoded_len;
 
@@ -139,6 +141,8 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body,
                }
                
                return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, encoded, encoded_len, 1);
+       } else {
+               return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, estrndup("", 0), 0, 1);
        }
 }
 
index 4b85676f3e47a5fbec98c2f2964795d9aac6c600..f763b482898c408613dcb5fade3891035e51909c 100644 (file)
@@ -643,7 +643,8 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS,
 
                array_init(return_value);
 
-               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) {
+               if (    (Z_TYPE_P(opts) == IS_ARRAY) && 
+                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) {
                        convert_to_array(*options);
                        array_copy(*options, return_value);
                }
@@ -715,6 +716,7 @@ PHP_METHOD(HttpRequest, setOptions)
                
        if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
                SET_PROP(obj, options, new_opts);
+               zval_ptr_dtor(&new_opts);
                RETURN_TRUE;
        }
        
@@ -1369,6 +1371,7 @@ PHP_METHOD(HttpRequest, setPostFiles)
                array_copy(files, post);
        }
        SET_PROP(obj, postFiles, post);
+       zval_ptr_dtor(&post);
 
        RETURN_TRUE;
 }
@@ -1386,9 +1389,9 @@ PHP_METHOD(HttpRequest, getPostFiles)
 
        IF_RETVAL_USED {
                getObject(http_request_object, obj);
-
-               array_init(return_value);
-               array_copy(GET_PROP(obj, postFiles), return_value);
+               zval *files = GET_PROP(obj, postFiles);
+               
+               RETURN_ZVAL(files, 1, 0);
        }
 }
 /* }}} */
@@ -1453,9 +1456,9 @@ PHP_METHOD(HttpRequest, getResponseData)
 
        IF_RETVAL_USED {
                getObject(http_request_object, obj);
+               zval *data = GET_PROP(obj, responseData);
                
-               array_init(return_value);
-               array_copy(GET_PROP(obj, responseData), return_value);
+               RETURN_ZVAL(data, 1, 0);
        }
 }
 /* }}} */
diff --git a/tests/HttpRequest_005.phpt b/tests/HttpRequest_005.phpt
new file mode 100644 (file)
index 0000000..7fc0dc5
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+HttpRequest accessors
+--SKIPIF--
+<?php
+include 'skip.inc';
+checkcls('HttpRequest');
+?>
+--FILE--
+<?php
+echo "-TEST\n";
+error_reporting(0);
+$r = new HttpRequest;
+foreach (get_class_methods('HttpRequest') as $method) {
+       try {
+               if (strlen($method) > 3 && substr($method, 0, 3) == 'get')
+                       $x = $r->$method();
+               else
+                       $x = $r->$method(null, null);
+       } catch (HttpException $e) {
+       }
+}
+echo "Done\n";
+?>
+--EXPECTF--
+%sTEST
+Done