fix env request
[m6w6/ext-http] / php_http_env_response.c
index f9e8c4cb43ce7045679a3963810e5ac06270df9c..0b195b6a7abdc7545e06931233916449bb48592f 100644 (file)
@@ -62,12 +62,14 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int
 }
 static zval *get_option(zval *options, const char *name_str, size_t name_len)
 {
-       zval *val;
+       zval *val = NULL;
 
        if (Z_TYPE_P(options) == IS_OBJECT) {
                val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0);
-       } else {
+       } else if (Z_TYPE_P(options) == IS_ARRAY) {
                val = zend_symtable_str_find(Z_ARRVAL_P(options), name_str, name_len);
+       } else {
+               abort();
        }
        if (val) {
                Z_TRY_ADDREF_P(val);
@@ -85,7 +87,7 @@ static php_http_message_body_t *get_body(zval *options)
 
                        body = body_obj->body;
                }
-               zval_ptr_dtor(zbody);
+               Z_TRY_DELREF_P(zbody);
        }
 
        return body;
@@ -101,40 +103,37 @@ static php_http_message_t *get_request(zval *options)
 
                        request = request_obj->message;
                }
-               zval_ptr_dtor(zrequest);
+               Z_TRY_DELREF_P(zrequest);
        }
 
        return request;
 }
-static void set_cookie(zval *options, zval *zcookie_new TSRMLS_DC)
+static void set_cookie(zval *options, zval *zcookie_new)
 {
-       zval *zcookies_set;
+       zval tmp, *zcookies_set;
        php_http_arrkey_t key;
        php_http_cookie_object_t *obj = PHP_HTTP_OBJ(NULL, zcookie_new);
 
+       array_init(&tmp);
        zcookies_set = get_option(options, ZEND_STRL("cookies"));
-       if (!zcookies_set || Z_TYPE_P(zcookies_set) != IS_ARRAY) {
-               if (zcookies_set) {
-                       zval_ptr_dtor(zcookies_set);
-               }
-               array_init_size(zcookies_set, zend_hash_num_elements(&obj->list->cookies));
-       } else {
-               SEPARATE_ZVAL(zcookies_set);
+       if (zcookies_set && Z_TYPE_P(zcookies_set) == IS_ARRAY) {
+               array_copy(Z_ARRVAL_P(zcookies_set), Z_ARRVAL(tmp));
+               zval_ptr_dtor(zcookies_set);
        }
 
        ZEND_HASH_FOREACH_KEY(&obj->list->cookies, key.h, key.key)
        {
                Z_ADDREF_P(zcookie_new);
                if (key.key) {
-                       add_assoc_zval_ex(zcookies_set, key.key->val, key.key->len, zcookie_new);
+                       add_assoc_zval_ex(&tmp, key.key->val, key.key->len, zcookie_new);
                } else {
-                       add_index_zval(zcookies_set, key.h, zcookie_new);
+                       add_index_zval(&tmp, key.h, zcookie_new);
                }
        }
        ZEND_HASH_FOREACH_END();
 
-       set_option(options, ZEND_STRL("cookies"), IS_ARRAY, zcookies_set, 0);
-       zval_ptr_dtor(zcookies_set);
+       set_option(options, ZEND_STRL("cookies"), IS_ARRAY, &tmp, 0);
+       zval_ptr_dtor(&tmp);
 }
 
 php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request)
@@ -149,7 +148,7 @@ php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, c
                return ret;
        }
 
-       if ((zetag = get_option(options, ZEND_STRL("etag")))) {
+       if ((zetag = get_option(options, ZEND_STRL("etag"))) && Z_TYPE_P(zetag) != IS_NULL) {
                zend_string *zs = zval_get_string(zetag);
                etag = estrndup(zs->val, zs->len);
                zend_string_release(zs);
@@ -459,24 +458,19 @@ static ZEND_RESULT_CODE php_http_env_response_send_head(php_http_env_response_t
                }
 
                if ((zoption = get_option(options, ZEND_STRL("contentDisposition")))) {
-                       php_http_buffer_t buf;
 
-                       if (Z_TYPE_P(zoption) != IS_ARRAY) {
-                               zval *tmp = zoption;
-                               SEPARATE_ZVAL(tmp);
-                               convert_to_array(tmp);
-                               zval_ptr_dtor(zoption);
-                               zoption = tmp;
-                       }
+                       if (Z_TYPE_P(zoption) == IS_ARRAY) {
+                               php_http_buffer_t buf;
 
-                       php_http_buffer_init(&buf);
-                       if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT)) {
-                               if (buf.used) {
-                                       ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
+                               php_http_buffer_init(&buf);
+                               if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT)) {
+                                       if (buf.used) {
+                                               ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
+                                       }
                                }
-                       }
 
-                       php_http_buffer_dtor(&buf);
+                               php_http_buffer_dtor(&buf);
+                       }
                        zval_ptr_dtor(zoption);
                }
 
@@ -502,7 +496,7 @@ static ZEND_RESULT_CODE php_http_env_response_send_head(php_http_env_response_t
                                                zend_ulong index = 0;
 
                                                zend_hash_internal_pointer_reset(result);
-                                               if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &index, NULL)) {
+                                               if (HASH_KEY_IS_STRING == zend_hash_get_current_key(result, &key_str, &index)) {
                                                        if (zend_string_equals_literal(key_str, "gzip")) {
                                                                if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP))) {
                                                                        ret = FAILURE;
@@ -936,6 +930,8 @@ static ZEND_RESULT_CODE php_http_env_response_stream_set_header_ex(php_http_env_
        char *header_end, *header_str = NULL;
        size_t header_len = 0;
        zval zheader, *zheader_ptr;
+       zend_string *header_key;
+       ZEND_RESULT_CODE rv;
 
        if (stream_ctx->started || stream_ctx->finished) {
                return FAILURE;
@@ -948,22 +944,21 @@ static ZEND_RESULT_CODE php_http_env_response_stream_set_header_ex(php_http_env_
                return FAILURE;
        }
 
-       *header_end = '\0';
+       header_key = zend_string_init(header_str, header_end - header_str, 0);
 
-       if (!replace && (zheader_ptr = zend_hash_str_find(&stream_ctx->header, header_str, header_end - header_str))) {
+       if (!replace && (zheader_ptr = zend_hash_find(&stream_ctx->header, header_key))) {
                convert_to_array(zheader_ptr);
-               *header_end = ':';
-               return add_next_index_str(zheader_ptr, php_http_cs2zs(header_str, header_len));
+               rv = add_next_index_str(zheader_ptr, php_http_cs2zs(header_str, header_len));
        } else {
                ZVAL_STR(&zheader, php_http_cs2zs(header_str, header_len));
 
-               if (SUCCESS != zend_hash_str_update(&stream_ctx->header, header_str, header_end - header_str, &zheader)) {
-                       return FAILURE;
-               }
-
-               *header_end = ':';
-               return SUCCESS;
+               rv = zend_hash_update(&stream_ctx->header, header_key, &zheader)
+                       ? SUCCESS : FAILURE;
        }
+
+       zend_string_release(header_key);
+
+       return rv;
 }
 static ZEND_RESULT_CODE php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
 {
@@ -1278,14 +1273,15 @@ static PHP_METHOD(HttpEnvResponse, setCookie)
        case IS_ARRAY:
                list = php_http_cookie_list_from_struct(NULL, zcookie_new);
                zcookie_new = &tmp;
-               ZVAL_OBJ(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
+               ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1);
                break;
 
        default:
                zs = zval_get_string(zcookie_new);
                list = php_http_cookie_list_parse(NULL, zs->val, zs->len, 0, NULL);
+               zend_string_release(zs);
                zcookie_new = &tmp;
-               ZVAL_OBJ(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
+               ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1);
        }
        zend_restore_error_handling(&zeh);