void _http_message_object_prepend_ex(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
{
zval m;
- http_message *save_parent_msg;
- zend_object_value save_parent_obj;
+ http_message *save_parent_msg = NULL;
+ zend_object_value save_parent_obj = {0, NULL};
getObject(http_message_object, obj);
getObjectEx(http_message_object, prepend_obj, prepend);
getObjectEx(http_message_object, obj, object);
http_message *msg = obj->message;
HashTable *props = OBJ_PROP(obj);
- zval array;
+ zval array, *parent;
INIT_ZARR(array, props);
zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
ASSOC_PROP(array, zval, "headers", headers);
ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
+
+ MAKE_STD_ZVAL(parent);
+ if (msg->parent) {
+ ZVAL_OBJVAL(parent, obj->parent, 1);
+ } else {
+ ZVAL_NULL(parent);
+ }
+ ASSOC_PROP(array, zval, "parentMessage", parent);
return OBJ_PROP(obj);
}
* Prepends message(s) to the HTTP message.
*
* Expects an HttpMessage object as parameter.
+ *
+ * Throws HttpInvalidParamException if the messages are located within the same message chain.
*/
PHP_METHOD(HttpMessage, prepend)
{
zend_bool top = 1;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
+ http_message *msg[2];
+ getObject(http_message_object, obj);
+ getObjectEx(http_message_object, prepend_obj, prepend);
+
+ /* safety check */
+ for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
+ for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
+ if (msg[0] == msg[1]) {
+ http_error(HE_THROW, HTTP_E_INVALID_PARAM, "Cannot prepend a message located within the same message chain");
+ return;
+ }
+ }
+ }
+
http_message_object_prepend_ex(getThis(), prepend, top);
}
}