better PHP-5.3 compatibility
[m6w6/ext-http] / php_http_message.c
index f1169d01b8418412f9e55dd7f935801a8aac255c..e4b4660d7d3de54259c4d6600f24f66b9d3cfc15 100644 (file)
@@ -12,6 +12,9 @@
 
 #include "php_http_api.h"
 
+static zval *message_header_strval(zval **header TSRMLS_DC);
+static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
+
 PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
 {
        php_http_message_t *old = *message;
@@ -86,7 +89,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                        }
                        
                        php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
-
+#if PHP_VERSION_ID >= 50400
                        if (php_output_get_level(TSRMLS_C)) {
                                if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
                                        php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
@@ -99,6 +102,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                                        zval_dtor(&tval);
                                }
                        }
+#endif
                        break;
                        
                default:
@@ -147,21 +151,9 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
 
        if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
                if (join && Z_TYPE_PP(header) == IS_ARRAY) {
-                       zval *header_str, **val;
-                       HashPosition pos;
-                       php_http_buffer_t str;
                        TSRMLS_FETCH_FROM_CTX(msg->ts);
 
-                       php_http_buffer_init(&str);
-                       MAKE_STD_ZVAL(header_str);
-                       FOREACH_VAL(pos, *header, val) {
-                               zval *strval = php_http_ztyp(IS_STRING, *val);
-                               php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval));
-                               zval_ptr_dtor(&strval);
-                       }
-                       php_http_buffer_fix(&str);
-                       ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
-                       ret = header_str;
+                       ret = message_header_strval(header TSRMLS_CC);
                } else {
                        Z_ADDREF_PP(header);
                        ret = *header;
@@ -277,27 +269,12 @@ PHP_HTTP_API void php_http_message_set_info(php_http_message_t *message, php_htt
        }
 }
 
-static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
+PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg)
 {
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-       HashPosition pos1;
-       zval **header, *h;
+       zval *h;
        size_t size;
        TSRMLS_FETCH_FROM_CTX(msg->ts);
 
-       switch (msg->type) {
-               case PHP_HTTP_REQUEST:
-                       php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
-                       break;
-
-               case PHP_HTTP_RESPONSE:
-                       php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
-                       break;
-
-               default:
-                       break;
-       }
-
        if ((size = php_http_message_body_size(&msg->body))) {
                MAKE_STD_ZVAL(h);
                ZVAL_LONG(h, size);
@@ -321,51 +298,90 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s
                        }
                }
        }
+}
+
+static zval *message_header_strval(zval **header TSRMLS_DC)
+{
+       zval *ret;
+
+       if (Z_TYPE_PP(header) == IS_BOOL) {
+               MAKE_STD_ZVAL(ret);
+               ZVAL_STRING(ret, Z_BVAL_PP(header) ? "true" : "false", 1);
+       } else if (Z_TYPE_PP(header) == IS_ARRAY) {
+               zval **val;
+               HashPosition pos;
+               php_http_buffer_t str;
+
+               php_http_buffer_init(&str);
+               MAKE_STD_ZVAL(ret);
+               FOREACH_VAL(pos, *header, val) {
+                       zval *strval = message_header_strval(val TSRMLS_CC);
+
+                       php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval));
+                       zval_ptr_dtor(&strval);
+               }
+               php_http_buffer_fix(&str);
+               ZVAL_STRINGL(ret, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
+       } else  {
+               ret = php_http_zsep(1, IS_STRING, *header);
+       }
+
+       return ret;
+}
+
+static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
+{
+       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+       HashPosition pos1;
+       zval **header;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
+
+       switch (msg->type) {
+               case PHP_HTTP_REQUEST:
+                       php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
+                       break;
+
+               case PHP_HTTP_RESPONSE:
+                       php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
+                       break;
+
+               default:
+                       break;
+       }
+
+       php_http_message_update_headers(msg);
 
        FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
                if (key.type == HASH_KEY_IS_STRING) {
-                       HashPosition pos2;
-                       zval **single_header;
-
-                       switch (Z_TYPE_PP(header)) {
-                               case IS_BOOL:
-                                       php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(header)?"true":"false");
-                                       break;
-                                       
-                               case IS_LONG:
-                                       php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(header));
-                                       break;
-                                       
-                               case IS_DOUBLE:
-                                       php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(header));
-                                       break;
-                                       
-                               case IS_STRING:
-                                       if (Z_STRVAL_PP(header)[Z_STRLEN_PP(header)-1] == '\r') fprintf(stderr, "DOH!\n");
-                                       php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(header));
-                                       break;
-
-                               case IS_ARRAY:
-                                       FOREACH_VAL(pos2, *header, single_header) {
-                                               switch (Z_TYPE_PP(single_header)) {
-                                                       case IS_BOOL:
-                                                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(single_header)?"true":"false");
-                                                               break;
-                                                               
-                                                       case IS_LONG:
-                                                               php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(single_header));
-                                                               break;
-                                                               
-                                                       case IS_DOUBLE:
-                                                               php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(single_header));
-                                                               break;
-                                                               
-                                                       case IS_STRING:
-                                                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(single_header));
-                                                               break;
+                       if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) {
+                               HashPosition pos2;
+                               zval **single_header;
+
+                               FOREACH_VAL(pos2, *header, single_header) {
+                                       if (Z_TYPE_PP(single_header) == IS_ARRAY) {
+                                               php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC);
+
+                                               if (cookie) {
+                                                       char *buf;
+                                                       size_t len;
+
+                                                       php_http_cookie_list_to_string(cookie, &buf, &len);
+                                                       php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, buf);
+                                                       php_http_cookie_list_free(&cookie);
+                                                       efree(buf);
                                                }
+                                       } else {
+                                               zval *strval = message_header_strval(single_header TSRMLS_CC);
+
+                                               php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, Z_STRVAL_P(strval));
+                                               zval_ptr_dtor(&strval);
                                        }
-                                       break;
+                               }
+                       } else {
+                               zval *strval = message_header_strval(header TSRMLS_CC);
+
+                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_P(strval));
+                               zval_ptr_dtor(&strval);
                        }
                }
        }
@@ -455,6 +471,24 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms
        return msg;
 }
 
+PHP_HTTP_API php_http_message_t *php_http_message_zip(php_http_message_t *one, php_http_message_t *two)
+{
+       php_http_message_t *dst = php_http_message_copy(one, NULL), *src = php_http_message_copy(two, NULL), *tmp_dst, *tmp_src, *ret = dst;
+
+       while(dst && src) {
+               tmp_dst = dst->parent;
+               tmp_src = src->parent;
+               dst->parent = src;
+               if (tmp_dst) {
+                       src->parent = tmp_dst;
+               }
+               src = tmp_src;
+               dst = tmp_dst;
+       }
+
+       return ret;
+}
+
 PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents)
 {
        php_http_message_t *temp, *copy = NULL;
@@ -638,9 +672,9 @@ PHP_HTTP_BEGIN_ARGS(isMultipart, 0)
 PHP_HTTP_END_ARGS;
 PHP_HTTP_EMPTY_ARGS(splitMultipartBody);
 
-static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, const zend_literal *literal_key TSRMLS_DC);
-static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC);
-static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC);
+static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
+static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
+static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
 static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC);
 
 static zend_class_entry *php_http_message_class_entry;
@@ -978,7 +1012,7 @@ void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool to
 
 STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody TSRMLS_DC)
 {
-       zval *tmp;
+       zval *tmp = NULL;
        php_stream *s;
        zend_object_value ov;
        php_http_message_body_t *body;
@@ -995,12 +1029,13 @@ STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval
                        is_resource:
 
                        body = php_http_message_body_init(NULL, s TSRMLS_CC);
-                       if (SUCCESS != php_http_new(&ov, php_http_message_body_get_class_entry(), php_http_message_body_object_new_ex, NULL, body, NULL TSRMLS_CC)) {
+                       if (SUCCESS != php_http_new(&ov, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, body, NULL TSRMLS_CC)) {
                                php_http_message_body_free(&body);
                                return FAILURE;
                        }
-                       MAKE_STD_ZVAL(zbody);
-                       ZVAL_OBJVAL(zbody, ov, 0);
+                       MAKE_STD_ZVAL(tmp);
+                       ZVAL_OBJVAL(tmp, ov, 0);
+                       zbody = tmp;
                        break;
 
                case IS_OBJECT:
@@ -1015,6 +1050,7 @@ STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval
                        s = php_stream_temp_new();
                        php_stream_write(s, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
                        zval_ptr_dtor(&tmp);
+                       tmp = NULL;
                        goto is_resource;
 
        }
@@ -1029,6 +1065,10 @@ STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval
        php_http_message_body_copy(body_obj->body, &msg_obj->message->body, 0);
        msg_obj->body = Z_OBJVAL_P(zbody);
 
+       if (tmp) {
+               zval_ptr_dtor(&tmp);
+       }
+
        return SUCCESS;
 }
 
@@ -1101,7 +1141,7 @@ void php_http_message_object_free(void *object TSRMLS_DC)
 }
 
 
-static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC)
+static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_prophandler_t *handler;
        zval *copy = php_http_ztyp(IS_STRING, member);
@@ -1112,10 +1152,10 @@ static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, c
        }
        zval_ptr_dtor(&copy);
 
-       return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member, literal_key TSRMLS_CC);
+       return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
 }
 
-static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, const zend_literal *literal_key TSRMLS_DC)
+static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
        php_http_message_object_prophandler_t *handler;
@@ -1133,7 +1173,7 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
                        return_value = NULL;
                }
        } else {
-               return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC);
+               return_value = zend_get_std_object_handlers()->read_property(object, member, type PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
        }
 
        zval_ptr_dtor(&copy);
@@ -1141,7 +1181,7 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
        return return_value;
 }
 
-static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC)
+static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
        php_http_message_object_prophandler_t *handler;
@@ -1150,7 +1190,7 @@ static void php_http_message_object_write_prop(zval *object, zval *member, zval
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
                handler->write(obj, value TSRMLS_CC);
        } else {
-               zend_get_std_object_handlers()->write_property(object, member, value, literal_key TSRMLS_CC);
+               zend_get_std_object_handlers()->write_property(object, member, value PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
        }
 
        zval_ptr_dtor(&copy);