commit lingering changes: http\Message::toStream() and ::toCallback()
authorMichael Wallner <mike@php.net>
Mon, 26 Mar 2012 15:49:02 +0000 (15:49 +0000)
committerMichael Wallner <mike@php.net>
Mon, 26 Mar 2012 15:49:02 +0000 (15:49 +0000)
php_http_message.c
php_http_message.h
php_http_message_body.c
php_http_misc.c
php_http_misc.h

index 176a27b..b25f697 100644 (file)
@@ -606,6 +606,12 @@ PHP_HTTP_EMPTY_ARGS(__toString);
 PHP_HTTP_BEGIN_ARGS(toString, 0)
        PHP_HTTP_ARG_VAL(include_parent, 0)
 PHP_HTTP_END_ARGS;
+PHP_HTTP_BEGIN_ARGS(toCallback, 1)
+       PHP_HTTP_ARG_VAL(callback, 0)
+PHP_HTTP_END_ARGS;
+PHP_HTTP_BEGIN_ARGS(toStream, 1)
+       HTTP_ARG_VAL(stream, 0)
+PHP_HTTP_END_ARGS;
 
 PHP_HTTP_EMPTY_ARGS(count);
 
@@ -663,6 +669,8 @@ zend_function_entry php_http_message_method_entry[] = {
        PHP_HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_ME(toCallback, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_ME(toStream, ZEND_ACC_PUBLIC)
 
        /* implements Countable */
        PHP_HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
@@ -1692,6 +1700,46 @@ PHP_METHOD(HttpMessage, toString)
        RETURN_EMPTY_STRING();
 }
 
+PHP_METHOD(HttpMessage, toStream)
+{
+       zval *zstream;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_stream *s;
+
+               php_stream_from_zval(s, &zstream);
+
+               if (!obj->message) {
+                       obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
+               }
+
+               php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
+       }
+}
+
+PHP_METHOD(HttpMessage, toCallback)
+{
+       zend_bool include_parent = 0;
+       php_http_pass_fcall_arg_t fcd;
+       long offset = 0, forlen = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &fcd.fci, &fcd.fcc)) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               fcd.fcz = getThis();
+               Z_ADDREF_P(fcd.fcz);
+               TSRMLS_SET_CTX(fcd.ts);
+
+               php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
+               zend_fcall_info_args_clear(&fcd.fci, 1);
+
+               zval_ptr_dtor(&fcd.fcz);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+
 PHP_METHOD(HttpMessage, serialize)
 {
        if (SUCCESS == zend_parse_parameters_none()) {
index f867d37..81e1dd2 100644 (file)
@@ -118,7 +118,8 @@ PHP_METHOD(HttpMessage, guessContentType);
 PHP_METHOD(HttpMessage, getParentMessage);
 PHP_METHOD(HttpMessage, send);
 PHP_METHOD(HttpMessage, toString);
-PHP_METHOD(HttpMessage, toMessageTypeObject);
+PHP_METHOD(HttpMessage, toStream);
+PHP_METHOD(HttpMessage, toCallback);
 
 PHP_METHOD(HttpMessage, count);
 PHP_METHOD(HttpMessage, serialize);
index 0374df3..3474148 100644 (file)
@@ -694,34 +694,10 @@ PHP_METHOD(HttpMessageBody, toStream)
        RETURN_FALSE;
 }
 
-struct fcd {
-       zval *fcz;
-       zend_fcall_info fci;
-       zend_fcall_info_cache fcc;
-#ifdef ZTS
-       void ***ts;
-#endif
-};
-
-static size_t pass(void *cb_arg, const char *str, size_t len)
-{
-       struct fcd *fcd = cb_arg;
-       zval *zdata;
-       TSRMLS_FETCH_FROM_CTX(fcd->ts);
-
-       MAKE_STD_ZVAL(zdata);
-       ZVAL_STRINGL(zdata, str, len, 1);
-       if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
-               zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
-               zend_fcall_info_args_clear(&fcd->fci, 0);
-       }
-       zval_ptr_dtor(&zdata);
-       return len;
-}
 
 PHP_METHOD(HttpMessageBody, toCallback)
 {
-       struct fcd fcd;
+       php_http_pass_fcall_arg_t fcd;
        long offset = 0, forlen = 0;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
@@ -731,7 +707,7 @@ PHP_METHOD(HttpMessageBody, toCallback)
                Z_ADDREF_P(fcd.fcz);
                TSRMLS_SET_CTX(fcd.ts);
 
-               php_http_message_body_to_callback(obj->body, pass, &fcd, offset, forlen);
+               php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
                zend_fcall_info_args_clear(&fcd.fci, 1);
 
                zval_ptr_dtor(&fcd.fcz);
index fa4bbbf..c4661b7 100644 (file)
@@ -212,6 +212,22 @@ PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb, cons
        return cb->cb_zts(cb->cb_arg, str, len TSRMLS_CC);
 }
 
+PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
+{
+       php_http_pass_fcall_arg_t *fcd = cb_arg;
+       zval *zdata;
+       TSRMLS_FETCH_FROM_CTX(fcd->ts);
+
+       MAKE_STD_ZVAL(zdata);
+       ZVAL_STRINGL(zdata, str, len, 1);
+       if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
+               zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
+               zend_fcall_info_args_clear(&fcd->fci, 0);
+       }
+       zval_ptr_dtor(&zdata);
+       return len;
+}
+
 /* ERROR */
 
 static inline int scope_error_handling(long type TSRMLS_DC)
index 6c00cc5..19ae642 100644 (file)
@@ -418,6 +418,17 @@ typedef struct php_http_pass_callback_arg {
 
 PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb_arg, const char *str, size_t len);
 
+typedef struct php_http_pass_fcall_arg {
+       zval *fcz;
+       zend_fcall_info fci;
+       zend_fcall_info_cache fcc;
+#ifdef ZTS
+       void ***ts;
+#endif
+} php_http_pass_fcall_arg_t;
+
+PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
+
 /* ERROR */
 
 extern void php_http_error(long type TSRMLS_DC, long code, const char *format, ...);