| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
- | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
+ | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
+--------------------------------------------------------------------+
*/
/* $Id$ */
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
#include "php_http.h"
#ifdef ZEND_ENGINE_2
# endif
#endif
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
#define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
#define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
#define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
HTTP_ARG_VAL(request_method, 0)
HTTP_END_ARGS;
-HTTP_EMPTY_ARGS(getRequestUri, 0);
-HTTP_BEGIN_ARGS(setRequestUri, 0, 1)
- HTTP_ARG_VAL(uri, 0)
+HTTP_EMPTY_ARGS(getRequestUrl, 0);
+HTTP_BEGIN_ARGS(setRequestUrl, 0, 1)
+ HTTP_ARG_VAL(url, 0)
HTTP_END_ARGS;
HTTP_EMPTY_ARGS(getHttpVersion, 0);
HTTP_ARG_VAL(http_version, 0)
HTTP_END_ARGS;
-HTTP_EMPTY_ARGS(getParentMessage, 1);
+HTTP_EMPTY_ARGS(getParentMessage, 0);
HTTP_EMPTY_ARGS(send, 0);
HTTP_BEGIN_ARGS(toString, 0, 0)
HTTP_ARG_VAL(include_parent, 0)
HTTP_ARG_VAL(serialized, 0)
HTTP_END_ARGS;
+HTTP_EMPTY_ARGS(detach, 0);
+
+HTTP_BEGIN_ARGS(prepend, 0, 1)
+ HTTP_ARG_OBJ(HttpMessage, message, 0)
+HTTP_END_ARGS;
+
#define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
static inline void _http_message_object_declare_default_properties(TSRMLS_D);
#define http_message_object_read_prop _http_message_object_read_prop
HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
- HTTP_MESSAGE_ME(getRequestUri, ZEND_ACC_PUBLIC)
- HTTP_MESSAGE_ME(setRequestUri, ZEND_ACC_PUBLIC)
+ HTTP_MESSAGE_ME(getRequestUrl, ZEND_ACC_PUBLIC)
+ HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
HTTP_MESSAGE_ME(fromString, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+ HTTP_MESSAGE_ME(detach, ZEND_ACC_PUBLIC)
+
+ HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
+
EMPTY_FUNCTION_ENTRY
};
static zend_object_handlers http_message_object_handlers;
DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
DCL_PROP(PROTECTED, string, body, "");
DCL_PROP(PROTECTED, string, requestMethod, "");
- DCL_PROP(PROTECTED, string, requestUri, "");
+ DCL_PROP(PROTECTED, string, requestUrl, "");
DCL_PROP(PROTECTED, long, responseCode, 0);
DCL_PROP_N(PROTECTED, httpVersion);
DCL_PROP_N(PROTECTED, headers);
http_message_dtor(o->message);
efree(o->message);
}
+ if (o->parent.handle) {
+ zval p;
+
+ INIT_PZVAL(&p);
+ p.type = IS_OBJECT;
+ p.value.obj = o->parent;
+ zend_objects_store_del_ref(&p TSRMLS_CC);
+ }
efree(o);
}
}
break;
- case HTTP_MSG_PROPHASH_REQUEST_URI:
- case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
- if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.URI) {
- RETVAL_STRING(msg->http.info.request.URI, 1);
+ case HTTP_MSG_PROPHASH_REQUEST_URL:
+ case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
+ if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.url) {
+ RETVAL_STRING(msg->http.info.request.url, 1);
} else {
RETVAL_NULL();
}
}
#endif
- ALLOC_ZVAL(cpy);
- *cpy = *value;
- zval_copy_ctor(cpy);
- INIT_PZVAL(cpy);
+ cpy = zval_copy(Z_TYPE_P(value), value);
#ifdef WONKY
switch (h)
}
break;
- case HTTP_MSG_PROPHASH_REQUEST_URI:
- case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
+ case HTTP_MSG_PROPHASH_REQUEST_URL:
+ case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
if (HTTP_MSG_TYPE(REQUEST, msg)) {
convert_to_string(cpy);
- STR_SET(msg->http.info.request.URI, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
+ STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
}
break;
#endif
break;
}
- zval_ptr_dtor(&cpy);
+ zval_free(&cpy);
}
static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
char *m_prop_name; \
int m_prop_len; \
zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
- add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
+ add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
efree(m_prop_name); \
}
#define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
char *m_prop_name; \
int m_prop_len; \
zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
- add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
+ add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
efree(m_prop_name); \
}
ASSOC_PROP(array, long, "responseCode", 0);
ASSOC_STRINGL(array, "responseStatus", "", 0);
ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
- ASSOC_STRING(array, "requestUri", msg->http.info.request.URI);
+ ASSOC_STRING(array, "requestUrl", msg->http.info.request.url);
break;
case HTTP_MSG_RESPONSE:
ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
ASSOC_STRINGL(array, "requestMethod", "", 0);
- ASSOC_STRINGL(array, "requestUri", "", 0);
+ ASSOC_STRINGL(array, "requestUrl", "", 0);
break;
case HTTP_MSG_NONE:
ASSOC_PROP(array, long, "responseCode", 0);
ASSOC_STRINGL(array, "responseStatus", "", 0);
ASSOC_STRINGL(array, "requestMethod", "", 0);
- ASSOC_STRINGL(array, "requestUri", "", 0);
+ ASSOC_STRINGL(array, "requestUrl", "", 0);
break;
}
}
/* }}} */
-/* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
+/* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
*
* Create an HttpMessage object from a string. Kind of a static constructor.
*
* Expects a string parameter containing a sinlge or several consecutive
- * HTTP messages.
+ * HTTP messages. Accepts an optionsl string parameter specifying the class to use.
*
* Returns an HttpMessage object on success or NULL on failure.
*
*/
PHP_METHOD(HttpMessage, fromString)
{
- char *string = NULL;
- int length = 0;
+ char *string = NULL, *class_name = NULL;
+ int length = 0, class_length = 0;
http_message *msg = NULL;
RETVAL_NULL();
SET_EH_THROW_HTTP();
- if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &length, &class_name, &class_length)) {
if ((msg = http_message_parse(string, length))) {
- ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
+ zend_class_entry *ce = http_message_object_ce;
+
+ if (class_name && *class_name) {
+ ce = zend_fetch_class(class_name, class_length, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
+ }
+ if (ce) {
+ ZVAL_OBJVAL(return_value, http_message_object_new_ex(ce, msg, NULL));
+ }
}
}
SET_EH_NORMAL();
}
/* }}} */
-/* {{{ proto string HttpMessage::getRequestUri()
+/* {{{ proto string HttpMessage::getRequestUrl()
*
- * Get the Request URI of the Message.
+ * Get the Request URL of the Message.
*
- * Returns the request uri as string on success, or FALSE if the message
+ * Returns the request url as string on success, or FALSE if the message
* is not of type HttpMessage::TYPE_REQUEST.
*/
-PHP_METHOD(HttpMessage, getRequestUri)
+PHP_METHOD(HttpMessage, getRequestUrl)
{
NO_ARGS;
IF_RETVAL_USED {
getObject(http_message_object, obj);
HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
- RETURN_STRING(obj->message->http.info.request.URI, 1);
+ RETURN_STRING(obj->message->http.info.request.url, 1);
}
}
/* }}} */
-/* {{{ proto bool HttpMessage::setRequestUri(string URI)
+/* {{{ proto bool HttpMessage::setRequestUrl(string url)
*
- * Set the Request URI of the HTTP Message.
+ * Set the Request URL of the HTTP Message.
*
- * Expects a string parameters containing the request uri.
+ * Expects a string parameters containing the request url.
*
* Returns TRUE on success, or FALSE if the message is not of type
- * HttpMessage::TYPE_REQUEST or supplied URI was empty.
+ * HttpMessage::TYPE_REQUEST or supplied URL was empty.
*/
-PHP_METHOD(HttpMessage, setRequestUri)
+PHP_METHOD(HttpMessage, setRequestUrl)
{
char *URI;
int URIlen;
}
HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
if (URIlen < 1) {
- http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
+ http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
RETURN_FALSE;
}
- STR_SET(obj->message->http.info.request.URI, estrndup(URI, URIlen));
+ STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
RETURN_TRUE;
}
/* }}} */
}
/* }}} */
+/* {{{ proto HttpMessage HttpMessage::detach(void)
+ *
+ * Returns a clone of an HttpMessage object detached from any parent messages.
+ */
+PHP_METHOD(HttpMessage, detach)
+{
+ http_info info;
+ http_message *msg;
+ getObject(http_message_object, obj);
+
+ NO_ARGS;
+
+ info.type = obj->message->type;
+ memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
+
+ msg = http_message_new();
+ http_message_set_info(msg, &info);
+
+ zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
+
+ RETURN_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL));
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::prepend(HttpMessage message)
+ *
+ * Prepends message(s) to the HTTP message.
+ *
+ * Expects an HttpMessage object as parameter.
+ */
+PHP_METHOD(HttpMessage, prepend)
+{
+ zval *prepend;
+ zend_bool top = 1;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
+ zval m;
+ http_message *save_parent_msg;
+ zend_object_value save_parent_obj;
+ getObject(http_message_object, obj);
+ getObjectEx(http_message_object, prepend_obj, prepend);
+
+ INIT_PZVAL(&m);
+ m.type = IS_OBJECT;
+
+ if (!top) {
+ save_parent_obj = obj->parent;
+ save_parent_msg = obj->message->parent;
+ } else {
+ /* iterate to the most parent object */
+ while (obj->parent.handle) {
+ m.value.obj = obj->parent;
+ obj = zend_object_store_get_object(&m TSRMLS_CC);
+ }
+ }
+
+ /* prepend */
+ obj->parent = prepend->value.obj;
+ obj->message->parent = prepend_obj->message;
+
+ /* add ref */
+ zend_objects_store_add_ref(prepend TSRMLS_CC);
+ while (prepend_obj->parent.handle) {
+ m.value.obj = prepend_obj->parent;
+ zend_objects_store_add_ref(&m TSRMLS_CC);
+ prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
+ }
+
+ if (!top) {
+ prepend_obj->parent = save_parent_obj;
+ prepend_obj->message->parent = save_parent_msg;
+ }
+ }
+}
+/* }}} */
+
#endif /* ZEND_ENGINE_2 */
/*