add some more tests; fix leak in php_http_env_get_response_header(); honor content...
[m6w6/ext-http] / php_http_message.c
index 7c91ac5551077a7224f1696d51e23adbb72bb4be..58d8ee9d05c785cb46494e9eaefbd0d60cd4a530 100644 (file)
@@ -17,13 +17,17 @@ PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **messa
        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);
+               if (headers) {
+                       (*headers) = &((*message)->hdrs);
+               }
        }
 
-       php_http_message_set_info(*message, info);
+       if (info) {
+               php_http_message_set_info(*message, info);
+       }
 
        return old != *message;
 }
@@ -179,6 +183,63 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
        return ret;
 }
 
+PHP_HTTP_API zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)
+{
+       zval *ct = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1);
+       zend_bool is_multipart = 0;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
+
+       if (ct) {
+               php_http_params_opts_t popts;
+               HashTable params;
+
+               ZEND_INIT_SYMTABLE(&params);
+               php_http_params_opts_default_get(&popts);
+               popts.input.str = Z_STRVAL_P(ct);
+               popts.input.len = Z_STRLEN_P(ct);
+
+               if (php_http_params_parse(&params, &popts TSRMLS_CC)) {
+                       zval **cur, **arg;
+                       char *ct_str;
+
+                       zend_hash_internal_pointer_reset(&params);
+
+                       if (SUCCESS == zend_hash_get_current_data(&params, (void *) &cur)
+                       &&      Z_TYPE_PP(cur) == IS_ARRAY
+                       &&      HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &ct_str, NULL, 0)
+                       ) {
+                               if (php_http_match(ct_str, "multipart", PHP_HTTP_MATCH_WORD)) {
+                                       is_multipart = 1;
+
+                                       /* get boundary */
+                                       if (boundary
+                                       &&      SUCCESS == zend_hash_find(Z_ARRVAL_PP(cur), ZEND_STRS("arguments"), (void *) &arg)
+                                       &&      Z_TYPE_PP(arg) == IS_ARRAY
+                                       ) {
+                                               zval **val;
+                                               HashPosition pos;
+                                               php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+
+                                               FOREACH_KEYVAL(pos, *arg, key, val) {
+                                                       if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "boundary")) {
+                                                               zval *bnd = php_http_ztyp(IS_STRING, *val);
+
+                                                               if (Z_STRLEN_P(bnd)) {
+                                                                       *boundary = estrndup(Z_STRVAL_P(bnd), Z_STRLEN_P(bnd));
+                                                               }
+                                                               zval_ptr_dtor(&bnd);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               zend_hash_destroy(&params);
+               zval_ptr_dtor(&ct);
+       }
+
+       return is_multipart;
+}
 
 /* */
 PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_http_message_type_t type)
@@ -739,6 +800,11 @@ PHP_HTTP_BEGIN_ARGS(prepend, 1)
 PHP_HTTP_END_ARGS;
 PHP_HTTP_EMPTY_ARGS(reverse);
 
+PHP_HTTP_BEGIN_ARGS(isMultipart, 0)
+       PHP_HTTP_ARG_VAL(boundary, 1)
+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);
@@ -792,6 +858,9 @@ zend_function_entry php_http_message_method_entry[] = {
        PHP_HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(reverse, ZEND_ACC_PUBLIC)
 
+       PHP_HTTP_MESSAGE_ME(isMultipart, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_ME(splitMultipartBody, ZEND_ACC_PUBLIC)
+
        EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers php_http_message_object_handlers;
@@ -1282,25 +1351,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) {
-                       if (message && length) {
-                               php_http_message_t *msg = php_http_message_parse(NULL, message, length 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);
+                               }
+                       } 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_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, message);
-                               } else {
-                                       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);
-                                       }
+                       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);
                                }
-                       } end_error_handling();
+                       } 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);
@@ -1879,6 +1959,40 @@ PHP_METHOD(HttpMessage, reverse)
        }
 }
 
+PHP_METHOD(HttpMessage, isMultipart)
+{
+       zval *zboundary = NULL;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zboundary)) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               char *boundary = NULL;
+
+               RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL));
+
+               if (zboundary && boundary) {
+                       zval_dtor(zboundary);
+                       ZVAL_STRING(zboundary, boundary, 0);
+               }
+       }
+}
+
+PHP_METHOD(HttpMessage, splitMultipartBody)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               char *boundary = NULL;
+
+               if (php_http_message_is_multipart(obj->message, &boundary)) {
+                       php_http_message_t *msg;
+
+                       if ((msg = php_http_message_body_split(&obj->message->body, boundary))) {
+                               RETVAL_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0);
+                       }
+               }
+               STR_FREE(boundary);
+       }
+}
+
 PHP_METHOD(HttpMessage, count)
 {
        if (SUCCESS == zend_parse_parameters_none()) {