#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;
/* advance message */
- if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+ if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
(*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
(*message)->parent = old;
if (headers) {
return msg;
}
-PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_str, size_t key_len, int join)
+PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
{
zval *ret = NULL, **header;
char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1);
if (join && Z_TYPE_PP(header) == IS_ARRAY) {
TSRMLS_FETCH_FROM_CTX(msg->ts);
- ret = message_header_strval(header TSRMLS_CC);
+ ret = php_http_header_value_to_string(*header TSRMLS_CC);
} else {
Z_ADDREF_PP(header);
ret = *header;
{
zval *h;
size_t size;
- TSRMLS_FETCH_FROM_CTX(msg->ts);
- if ((size = php_http_message_body_size(&msg->body))) {
+ if (php_http_message_body_stream(&msg->body)->readfilters.head) {
+ /* if a read stream filter is attached to the body the caller must also care for the headers */
+ } else if ((size = php_http_message_body_size(&msg->body))) {
MAKE_STD_ZVAL(h);
ZVAL_LONG(h, size);
zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
}
}
-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) {
}
php_http_message_update_headers(msg);
-
- FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
- if (key.type == HASH_KEY_IS_STRING) {
- 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);
- }
- }
- } 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);
- }
- }
- }
+ php_http_headers_to_string(str, &msg->hdrs TSRMLS_CC);
}
PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
php_http_buffer_init_ex(&str, 0x1000, 0);
message_headers(msg, &str);
- cb(cb_arg, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str));
+ cb(cb_arg, str.data, str.used);
php_http_buffer_dtor(&str);
if (php_http_message_body_size(&msg->body)) {
}
static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry TSRMLS_CC)) {
+ php_http_message_object_t *parent_obj = zend_object_store_get_object(value TSRMLS_CC);
+
if (obj->message->parent) {
zend_objects_store_del_ref_by_handle(obj->parent.handle TSRMLS_CC);
}
Z_OBJ_ADDREF_P(value);
obj->parent = Z_OBJVAL_P(value);
+ obj->message->parent = parent_obj->message;
}
}
zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
- zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
+ zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED TSRMLS_CC);
php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method, php_http_message_object_prophandler_set_request_method);
}
if (msg) {
+ php_http_message_body_object_t *body_obj;
+
o->message = msg;
if (msg->parent) {
o->parent = php_http_message_object_new_ex(ce, msg->parent, NULL TSRMLS_CC);
}
- o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC);
+ o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &msg->body, &body_obj TSRMLS_CC);
+ body_obj->shared = 1;
}
ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC);
if (!obj->message) {
obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
}
-
- if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) {
+ if (!obj->body.handle) {
+ php_http_message_body_object_t *body_obj;
+ obj->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &obj->message->body, &body_obj TSRMLS_CC);
+ body_obj->shared = 1;
+ }
+ if (obj->body.handle) {
RETVAL_OBJVAL(obj->body, 1);
}
}
zval *new_body;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_get_class_entry())) {
- php_http_message_body_object_t *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC);
- php_http_message_body_to_callback(old_obj->body, (php_http_pass_callback_t) php_http_message_body_append, new_obj->body, 0, 0);
+ php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, &obj->message->body, 0, 0);
}
RETVAL_ZVAL(getThis(), 1, 0);
}
if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
convert_to_array(header);
zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
+ zval_ptr_dtor(&header);
} else {
zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
}
obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
}
- RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy(obj->message, NULL), NULL TSRMLS_CC), 0);
+ RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0);
}
} end_error_handling();
}