ensure options are applied before the headers are set
[m6w6/ext-http] / php_http_env.c
index c98701b253ae71f0aa6bd22cb3460ee62032f385..25759a65e83c5da71bd866a876471d8ecc9c5de6 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2013, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
@@ -15,8 +15,6 @@
 
 PHP_RINIT_FUNCTION(http_env)
 {
-       PHP_HTTP_G->env.request.time = sapi_get_request_time(TSRMLS_C);
-
        /* populate form data on non-POST requests */
        if (SG(request_info).request_method && strcasecmp(SG(request_info).request_method, "POST") && SG(request_info).content_type && *SG(request_info).content_type) {
                uint ct_len = strlen(SG(request_info).content_type);
@@ -72,7 +70,7 @@ PHP_RINIT_FUNCTION(http_env)
                }
        }
 
-       STR_SET(SG(request_info).content_type_dup, NULL);
+       PTR_SET(SG(request_info).content_type_dup, NULL);
 
        return SUCCESS;
 }
@@ -289,7 +287,7 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                return PHP_HTTP_RANGE_NO;
        }
        if (strncmp(range, "bytes=", lenof("bytes="))) {
-               STR_FREE(range);
+               PTR_FREE(range);
                return PHP_HTTP_RANGE_NO;
        }
 
@@ -345,7 +343,7 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                                                        switch (end) {
                                                                /* "0-" */
                                                                case -1:
-                                                                       STR_FREE(range);
+                                                                       PTR_FREE(range);
                                                                        return PHP_HTTP_RANGE_NO;
 
                                                                /* "0-0" */
@@ -366,7 +364,7 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                                                case -1:
                                                        /* "-", "-0" */
                                                        if (end == -1 || end == -10) {
-                                                               STR_FREE(range);
+                                                               PTR_FREE(range);
                                                                return PHP_HTTP_RANGE_ERR;
                                                        }
                                                        begin = length - end;
@@ -376,13 +374,13 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                                                /* "12345-(NNN)" */
                                                default:
                                                        if (length <= (size_t) begin) {
-                                                               STR_FREE(range);
+                                                               PTR_FREE(range);
                                                                return PHP_HTTP_RANGE_ERR;
                                                        }
                                                        switch (end) {
                                                                /* "12345-0" */
                                                                case -10:
-                                                                       STR_FREE(range);
+                                                                       PTR_FREE(range);
                                                                        return PHP_HTTP_RANGE_ERR;
 
                                                                /* "12345-" */
@@ -395,7 +393,7 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                                                                        if (length <= (size_t) end) {
                                                                                end = length - 1;
                                                                        } else if (end <  begin) {
-                                                                               STR_FREE(range);
+                                                                               PTR_FREE(range);
                                                                                return PHP_HTTP_RANGE_ERR;
                                                                        }
                                                                        break;
@@ -417,12 +415,12 @@ php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_
                                break;
 
                        default:
-                               STR_FREE(range);
+                               PTR_FREE(range);
                                return PHP_HTTP_RANGE_NO;
                }
        } while (c != 0);
 
-       STR_FREE(range);
+       PTR_FREE(range);
        return PHP_HTTP_RANGE_OK;
 }
 
@@ -575,7 +573,7 @@ STATUS php_http_env_set_response_header_value(long http_code, const char *name_s
                        ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
 
                        zval_ptr_dtor(&data);
-                       STR_FREE(h.line);
+                       PTR_FREE(h.line);
 
                        return ret;
                }
@@ -715,6 +713,7 @@ static PHP_METHOD(HttpEnv, getRequestBody)
 
        body = php_http_env_get_request_body(TSRMLS_C);
        if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body, NULL TSRMLS_CC)) {
+               php_http_message_body_addref(body);
                RETVAL_OBJVAL(ov, 0);
        }
 }
@@ -957,26 +956,29 @@ static SAPI_POST_HANDLER_FUNC(php_http_json_post_handler)
        size_t json_len = 0;
 
 #if PHP_VERSION_ID >= 50600
-       php_http_message_body_to_string(php_http_env_get_request_body(TSRMLS_C),
-                       &json_str, &json_len, 0, -1);
+       if (SG(request_info).request_body) {
+               /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
+               php_stream_rewind(SG(request_info).request_body);
+               json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0);
+       }
 #else
        json_str = SG(request_info).raw_post_data;
        json_len = SG(request_info).raw_post_data_length;
 #endif
 
        if (json_len) {
-               zval_dtor(zarg);
-               ZVAL_NULL(zarg);
-               php_json_decode(zarg, json_str, json_len, 1, PG(max_input_nesting_level) TSRMLS_CC);
+               zval zjson;
+
+               INIT_ZVAL(zjson);
+               php_json_decode(&zjson, json_str, json_len, 1, PG(max_input_nesting_level) TSRMLS_CC);
+               if (Z_TYPE(zjson) != IS_NULL) {
+                       zval_dtor(zarg);
+                       ZVAL_COPY_VALUE(zarg, (&zjson));
+               }
        }
 #if PHP_VERSION_ID >= 50600
-       STR_FREE(json_str);
+       PTR_FREE(json_str);
 #endif
-
-       /* always let $_POST be array() */
-       if (Z_TYPE_P(zarg) == IS_NULL) {
-               array_init(zarg);
-       }
 }
 
 static void php_http_env_register_json_handler(TSRMLS_D)