fix crahs with empty urls because of typo
[m6w6/ext-http] / php_http_message.c
index 20ed46f2648dfc2bde6cf1c1f6543d5bd63a1dfe..ab2c86225f8c1328dc6495dfacfe55e9506b8d83 100644 (file)
@@ -6,24 +6,18 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */
-
-#include "php_http.h"
-
-#include <main/SAPI.h>
-#include <ext/spl/spl_iterators.h>
-#include <Zend/zend_interfaces.h>
+#include "php_http_api.h"
 
 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;
 
        /* advance message */
-       if (old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+       if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
                (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
                (*message)->parent = old;
                (*headers) = &((*message)->hdrs);
@@ -130,17 +124,25 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg,
 {
        php_http_message_parser_t p;
        php_http_buffer_t buf;
+       int free_msg;
 
-       if (!msg) {
-               msg = php_http_message_init(NULL, 0 TSRMLS_CC);
-       }
        php_http_buffer_from_string_ex(&buf, str, len);
        php_http_message_parser_init(&p TSRMLS_CC);
-       php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg);
+
+       if ((free_msg = !msg)) {
+               msg = php_http_message_init(NULL, 0 TSRMLS_CC);
+       }
+
+       if (FAILURE == php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg)) {
+               if (free_msg) {
+                       php_http_message_free(&msg);
+               }
+               msg = NULL;
+       }
+
        php_http_message_parser_dtor(&p);
        php_http_buffer_dtor(&buf);
 
-       /* FIXME */
        return msg;
 }
 
@@ -167,7 +169,8 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
                        ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
                        ret = header_str;
                } else {
-                       ret = php_http_ztyp(IS_STRING, *header);
+                       Z_ADDREF_PP(header);
+                       ret = *header;
                }
        }
 
@@ -184,7 +187,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt
        if (type != message->type) {
 
                /* free request info */
-               switch (message->type = type) {
+               switch (message->type) {
                        case PHP_HTTP_REQUEST:
                                STR_FREE(message->http.info.request.method);
                                STR_FREE(message->http.info.request.url);
@@ -198,6 +201,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt
                                break;
                }
 
+               message->type = type;
                memset(&message->http, 0, sizeof(message->http));
        }
 }
@@ -589,7 +593,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *fr
                        temp->parent = php_http_message_init(NULL, 0 TSRMLS_CC);
                        php_http_message_set_info(temp->parent, &info);
                        zend_hash_copy(&temp->parent->hdrs, &from->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
-                       php_http_message_body_copy(&from->body, &temp->body, 1);
+                       php_http_message_body_copy(&from->parent->body, &temp->parent->body, 1);
                
                        temp = temp->parent;
                        from = from->parent;
@@ -1145,7 +1149,7 @@ static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, c
 
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
                zval_ptr_dtor(&copy);
-               return &php_http_property_proxy_init(NULL, object, member TSRMLS_CC)->myself;
+               return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself;
        }
        zval_ptr_dtor(&copy);
 
@@ -1159,18 +1163,16 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
        zval *return_value, *copy = php_http_ztyp(IS_STRING, member);
 
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
-               if (type == BP_VAR_W) {
-                       zval_ptr_dtor(&copy);
+               if (type == BP_VAR_R) {
+                       ALLOC_ZVAL(return_value);
+                       Z_SET_REFCOUNT_P(return_value, 0);
+                       Z_UNSET_ISREF_P(return_value);
+
+                       handler->read(obj, return_value TSRMLS_CC);
+               } else {
                        zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
-                       return NULL;
+                       return_value = NULL;
                }
-
-               ALLOC_ZVAL(return_value);
-               Z_SET_REFCOUNT_P(return_value, 0);
-               Z_UNSET_ISREF_P(return_value);
-
-               handler->read(obj, return_value TSRMLS_CC);
-
        } else {
                return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC);
        }
@@ -1280,24 +1282,36 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
 PHP_METHOD(HttpMessage, __construct)
 {
-       int length = 0;
-       char *message = NULL;
+       zval *zmessage = NULL;
+       php_http_message_t *msg = NULL;
        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
        with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
-                       php_http_message_t *msg = obj->message;
-
-                       php_http_message_dtor(msg);
-                       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-                               if ((obj->message = php_http_message_parse(msg, message, length TSRMLS_CC))) {
-                                       if (obj->message->parent) {
-                                               obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
-                                       }
-                               } else {
-                                       obj->message = php_http_message_init(msg, 0 TSRMLS_CC);
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) {
+                       if (Z_TYPE_P(zmessage) == IS_RESOURCE) {
+                               php_stream *s;
+                               php_http_message_parser_t p;
+
+                               php_stream_from_zval(s, &zmessage);
+                               if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
+                                       php_http_message_parser_parse_stream(&p, s, &msg);
+                                       php_http_message_parser_dtor(&p);
                                }
-                       } end_error_handling();
+                       } else {
+                               zmessage = php_http_ztyp(IS_STRING, zmessage);
+                               msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC);
+                               zval_ptr_dtor(&zmessage);
+                       }
+
+                       if (msg) {
+                               php_http_message_dtor(obj->message);
+                               obj->message = msg;
+                               if (obj->message->parent) {
+                                       obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
+                               }
+                       } else {
+                               php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, Z_STRVAL_P(zmessage));
+                       }
                }
                if (!obj->message) {
                        obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);