renamed http\Message\Body::add() to ::addForm() because it acually adds form-data...
authorMichael Wallner <mike@php.net>
Thu, 15 Mar 2012 15:06:43 +0000 (15:06 +0000)
committerMichael Wallner <mike@php.net>
Thu, 15 Mar 2012 15:06:43 +0000 (15:06 +0000)
php_http_message_body.c
php_http_message_body.h
phpunit/MessageBodyTest.php

index e02c2e4..e1a0288 100644 (file)
@@ -211,7 +211,7 @@ PHP_HTTP_API size_t php_http_message_body_appendf(php_http_message_body_t *body,
        return print_len;
 }
 
-PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, HashTable *fields, HashTable *files)
+PHP_HTTP_API STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
 {
        zval tmp;
 
@@ -231,8 +231,17 @@ PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, Has
        return SUCCESS;
 }
 
+PHP_HTTP_API void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
+{
+       TSRMLS_FETCH_FROM_CTX(body->ts);
 
-PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
+       BOUNDARY_OPEN(body);
+       php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
+       BOUNDARY_CLOSE(body);
+}
+
+
+PHP_HTTP_API STATUS php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
 {
        char *safe_name;
        TSRMLS_FETCH_FROM_CTX(body->ts);
@@ -253,7 +262,7 @@ PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *bod
        return SUCCESS;
 }
 
-PHP_HTTP_API STATUS php_http_message_body_add_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
+PHP_HTTP_API STATUS php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
 {
        char *safe_name, *path_dup = estrdup(path);
        TSRMLS_FETCH_FROM_CTX(body->ts);
@@ -321,7 +330,7 @@ static STATUS add_recursive_fields(php_http_message_body_t *body, const char *na
                }
        } else {
                zval *cpy = php_http_ztyp(IS_STRING, value);
-               php_http_message_body_add_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
+               php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
                zval_ptr_dtor(&cpy);
        }
 
@@ -389,7 +398,7 @@ static STATUS add_recursive_files(php_http_message_body_t *body, const char *nam
                } else {
                        zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
                        char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
-                       STATUS ret =  php_http_message_body_add_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
+                       STATUS ret =  php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
 
                        efree(key);
                        zval_ptr_dtor(&znc);
@@ -538,11 +547,15 @@ PHP_HTTP_BEGIN_ARGS(append, 1)
        PHP_HTTP_ARG_VAL(string, 0)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(add, 0)
+PHP_HTTP_BEGIN_ARGS(addForm, 0)
        PHP_HTTP_ARG_ARR(fields, 1, 0)
        PHP_HTTP_ARG_ARR(files, 1, 0)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_BEGIN_ARGS(addPart, 1)
+       PHP_HTTP_ARG_OBJ("http\\Message", "message", 0)
+PHP_HTTP_END_ARGS;
+
 PHP_HTTP_EMPTY_ARGS(etag);
 
 PHP_HTTP_BEGIN_ARGS(stat, 0)
@@ -557,7 +570,8 @@ zend_function_entry php_http_message_body_method_entry[] = {
        PHP_HTTP_MESSAGE_BODY_ME(toStream, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(toCallback, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(append, ZEND_ACC_PUBLIC)
-       PHP_HTTP_MESSAGE_BODY_ME(add, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_BODY_ME(addForm, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_BODY_ME(addPart, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(etag, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(stat, ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
@@ -739,14 +753,27 @@ PHP_METHOD(HttpMessageBody, append)
        RETURN_FALSE;
 }
 
-PHP_METHOD(HttpMessageBody, add)
+PHP_METHOD(HttpMessageBody, addForm)
 {
        HashTable *fields = NULL, *files = NULL;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files)) {
                php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-               RETURN_SUCCESS(php_http_message_body_add(obj->body, fields, files));
+               RETURN_SUCCESS(php_http_message_body_add_form(obj->body, fields, files));
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpMessageBody, addPart)
+{
+       php_http_message_object_t *mobj;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &mobj, php_http_message_class_entry)) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               php_http_message_body_add_part(obj->body, mobj->message);
+               RETURN_TRUE;
        }
        RETURN_FALSE;
 }
index 53e5654..61fdb75 100644 (file)
@@ -26,9 +26,10 @@ struct php_http_message;
 
 PHP_HTTP_API php_http_message_body_t *php_http_message_body_init(php_http_message_body_t *body, php_stream *stream TSRMLS_DC);
 PHP_HTTP_API php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to, zend_bool dup_internal_stream_and_contents);
-PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, HashTable *fields, HashTable *files);
-PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len);
-PHP_HTTP_API STATUS php_http_message_body_add_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *file, php_stream *stream);
+PHP_HTTP_API STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files);
+PHP_HTTP_API STATUS php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len);
+PHP_HTTP_API STATUS php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *file, php_stream *stream);
+PHP_HTTP_API void php_http_message_body_add_part(php_http_message_body_t *body, struct php_http_message *part);
 PHP_HTTP_API size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len);
 PHP_HTTP_API size_t php_http_message_body_appendf(php_http_message_body_t *body, const char *fmt, ...);
 PHP_HTTP_API void php_http_message_body_to_string(php_http_message_body_t *body, char **buf, size_t *len, off_t offset, size_t forlen);
@@ -70,7 +71,8 @@ PHP_METHOD(HttpMessageBody, __toString);
 PHP_METHOD(HttpMessageBody, toStream);
 PHP_METHOD(HttpMessageBody, toCallback);
 PHP_METHOD(HttpMessageBody, append);
-PHP_METHOD(HttpMessageBody, add);
+PHP_METHOD(HttpMessageBody, addForm);
+PHP_METHOD(HttpMessageBody, addPart);
 PHP_METHOD(HttpMessageBody, etag);
 PHP_METHOD(HttpMessageBody, stat);
 
index 0218af1..4ef4efe 100644 (file)
@@ -29,9 +29,9 @@ class MessageBodyTest extends PHPUnit_Framework_TestCase {
         $this->assertEquals(3, $this->temp->append("yes"));
     }
 
-    function testAdd() {
+    function testAddForm() {
         $this->assertTrue( 
-            $this->temp->add(
+            $this->temp->addForm(
                 array(
                     "foo" => "bar",
                     "more" => array(