| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
- | Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
+ | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
+--------------------------------------------------------------------+
*/
-/* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */
-
-#include "php_http.h"
-
-/* API */
+#include "php_http_api.h"
PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
{
/* advance message */
if (old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
- (*message) = php_http_message_init(NULL, 0);
+ (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
(*message)->parent = old;
(*headers) = &((*message)->hdrs);
}
switch (type) {
case PHP_HTTP_REQUEST:
if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
- php_http_version_parse(&message->http.version, Z_STRVAL_P(sval));
+ php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
} else {
message->http.version.major = 1;
message->http.version.minor = 1;
if ((mbody = php_http_env_get_request_body(TSRMLS_C))) {
php_http_message_body_dtor(&message->body);
- php_http_message_body_copy(mbody, &message->body, 0 TSRMLS_CC);
+ php_http_message_body_copy(mbody, &message->body, 0);
}
break;
php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
- if (php_output_get_level()) {
+ if (php_output_get_level(TSRMLS_C)) {
if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
goto error;
PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len TSRMLS_DC)
{
php_http_message_parser_t p;
- php_http_buffer buf;
+ php_http_buffer_t buf;
if (!msg) {
msg = php_http_message_init(NULL, 0 TSRMLS_CC);
zval *ret = NULL, **header;
char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1);
- if (SUCCESS == zend_hash_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
+ if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
if (join && Z_TYPE_PP(header) == IS_ARRAY) {
zval *header_str, **val;
HashPosition pos;
- php_http_buffer str;
+ php_http_buffer_t str;
+ TSRMLS_FETCH_FROM_CTX(msg->ts);
php_http_buffer_init(&str);
MAKE_STD_ZVAL(header_str);
FOREACH_VAL(pos, *header, val) {
- php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_PP(val));
+ zval *strval = php_http_ztyp(IS_STRING, *val);
+ 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(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
ret = header_str;
} else {
- ret = php_http_zsep(IS_STRING, *header);
+ ret = php_http_ztyp(IS_STRING, *header);
}
}
}
}
-static inline void message_headers(php_http_message_t *msg, php_http_buffer *str)
+static inline 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;
+ zval **header, *h;
+ size_t size;
+ TSRMLS_FETCH_FROM_CTX(msg->ts);
switch (msg->type) {
case PHP_HTTP_REQUEST:
break;
}
+ 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);
+
+ if (msg->body.boundary) {
+ char *str;
+ size_t len;
+
+ if (!(h = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1))) {
+ len = spprintf(&str, 0, "multipart/form-data; boundary=\"%s\"", msg->body.boundary);
+ MAKE_STD_ZVAL(h);
+ ZVAL_STRINGL(h, str, len, 0);
+ zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
+ } else if (!php_http_match(Z_STRVAL_P(h), "boundary=", PHP_HTTP_MATCH_WORD)) {
+ zval_dtor(h);
+ Z_STRLEN_P(h) = spprintf(&Z_STRVAL_P(h), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h), msg->body.boundary);
+ zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
+ } else {
+ zval_ptr_dtor(&h);
+ }
+ }
+ }
+
FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
if (key.type == HASH_KEY_IS_STRING) {
HashPosition pos2;
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 str;
- TSRMLS_FETCH_FROM_CTX(msg->ts);
+ php_http_buffer_t str;
php_http_buffer_init_ex(&str, 0x1000, 0);
message_headers(msg, &str);
php_http_buffer_dtor(&str);
if (php_http_message_body_size(&msg->body)) {
- cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF) TSRMLS_CC);
+ cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
php_http_message_body_to_callback(&msg->body, cb, cb_arg, 0, 0);
- cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF) TSRMLS_CC);
+ cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
}
}
PHP_HTTP_API void php_http_message_to_string(php_http_message_t *msg, char **string, size_t *length)
{
- php_http_buffer str;
+ php_http_buffer_t str;
char *data;
php_http_buffer_init_ex(&str, 0x1000, 0);
{
char *buf;
size_t len;
- php_http_buffer str;
+ php_http_buffer_t str;
php_http_buffer_init(&str);
php_http_message_count(c, msg);
if (c > 1) {
- php_http_message_t *tmp = msg, **arr = ecalloc(c, sizeof(**arr));
-
+ php_http_message_t *tmp = msg, **arr;
+
+ arr = ecalloc(c, sizeof(**arr));
for (i = 0; i < c; ++i) {
arr[i] = tmp;
tmp = tmp->parent;
return m1;
}
-PHP_HTTP_API void php_http_message_to_struct(php_http_message_t *msg, zval *obj TSRMLS_DC)
+PHP_HTTP_API void php_http_message_to_struct(php_http_message_t *msg, zval *obj)
{
zval strct;
zval *headers;
char *version;
+ TSRMLS_FETCH_FROM_CTX(msg->ts);
INIT_PZVAL_ARRAY(&strct, HASH_OF(obj));
}
*/
-PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to)
+PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents)
{
php_http_message_t *temp, *copy = NULL;
php_http_info_t info;
info.type = from->type;
info.http = from->http;
- copy = temp = php_http_message_init(to, 0);
+ copy = temp = php_http_message_init(to, 0 TSRMLS_CC);
php_http_message_set_info(temp, &info);
zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
php_http_message_body_copy(&from->body, &temp->body, 1);
- while (from->parent) {
+ if (parents) while (from->parent) {
info.type = from->parent->type;
info.http = from->parent->http;
- temp->parent = php_http_message_init(NULL, 0);
+ temp->parent = php_http_message_init(NULL, 0 TSRMLS_CC);
php_http_message_set_info(temp->parent, &info);
zend_hash_copy(&temp->parent->hdrs, &from->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
php_http_message_body_copy(&from->body, &temp->body, 1);
return copy;
}
+PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to)
+{
+ return php_http_message_copy_ex(from, to, 1);
+}
+
PHP_HTTP_API void php_http_message_dtor(php_http_message_t *message)
{
if (message) {
}
}
-
-/* PHP */
-
#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
#define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
PHP_HTTP_ARG_VAL(http_version, 0)
PHP_HTTP_END_ARGS;
-PHP_HTTP_BEGIN_ARGS(guessContentType, 1)
- PHP_HTTP_ARG_VAL(magic_file, 0)
- PHP_HTTP_ARG_VAL(magic_mode, 0)
-PHP_HTTP_END_ARGS;
-
PHP_HTTP_EMPTY_ARGS(getParentMessage);
-PHP_HTTP_EMPTY_ARGS(send);
PHP_HTTP_EMPTY_ARGS(__toString);
PHP_HTTP_BEGIN_ARGS(toString, 0)
PHP_HTTP_ARG_VAL(include_parent, 0)
PHP_HTTP_END_ARGS;
-PHP_HTTP_EMPTY_ARGS(toMessageTypeObject);
-
PHP_HTTP_EMPTY_ARGS(count);
PHP_HTTP_EMPTY_ARGS(serialize);
php_http_message_object_prophandler_t h = { read, write };
return zend_hash_add(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) &h, sizeof(h), NULL);
}
+/*
static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) {
return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1);
}
+*/
static STATUS php_http_message_object_get_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_t **handler) {
return zend_hash_find(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) handler);
}
RETVAL_LONG(obj->message->type);
}
static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
- zval *cpy = php_http_zsep(IS_LONG, value);
+ zval *cpy = php_http_ztyp(IS_LONG, value);
php_http_message_set_type(obj->message, Z_LVAL_P(cpy));
zval_ptr_dtor(&cpy);
}
}
static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
- zval *cpy = php_http_zsep(IS_STRING, value);
+ zval *cpy = php_http_ztyp(IS_STRING, value);
STR_SET(obj->message->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
zval_ptr_dtor(&cpy);
}
}
static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
- zval *cpy = php_http_zsep(IS_STRING, value);
+ zval *cpy = php_http_ztyp(IS_STRING, value);
STR_SET(obj->message->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
zval_ptr_dtor(&cpy);
}
}
static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
- zval *cpy = php_http_zsep(IS_STRING, value);
+ zval *cpy = php_http_ztyp(IS_STRING, value);
STR_SET(obj->message->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
zval_ptr_dtor(&cpy);
}
}
static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
- zval *cpy = php_http_zsep(IS_LONG, value);
+ zval *cpy = php_http_ztyp(IS_LONG, value);
obj->message->http.info.response.code = Z_LVAL_P(cpy);
zval_ptr_dtor(&cpy);
}
RETVAL_STRINGL(version_str, version_len, 0);
}
static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
- zval *cpy = php_http_zsep(IS_STRING, value);
+ zval *cpy = php_http_ztyp(IS_STRING, value);
php_http_version_parse(&obj->message->http.version, Z_STRVAL_P(cpy) TSRMLS_CC);
zval_ptr_dtor(&cpy);
}
zend_hash_copy(Z_ARRVAL_P(return_value), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
}
static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
- zval *cpy = php_http_zsep(IS_ARRAY, value);
+ zval *cpy = php_http_ztyp(IS_ARRAY, value);
zend_hash_clean(&obj->message->hdrs);
zend_hash_copy(&obj->message->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
php_http_message_object_t *new_obj = NULL;
php_http_message_object_t *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
- new_ov = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL), &new_obj);
+ new_ov = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL), &new_obj TSRMLS_CC);
zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
return new_ov;
}
-static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC) {
+static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC)
+{
php_http_message_object_prophandler_t *handler;
- zval *copy = php_http_zsep(IS_STRING, member);
+ zval *copy = php_http_ztyp(IS_STRING, member);
if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
zval_ptr_dtor(©);
{
php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
php_http_message_object_prophandler_t *handler;
- zval *return_value, *copy = php_http_zsep(IS_STRING, member);
+ zval *return_value, *copy = php_http_ztyp(IS_STRING, member);
if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
if (type == BP_VAR_W) {
{
php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
php_http_message_object_prophandler_t *handler;
- zval *copy = php_http_zsep(IS_STRING, member);
+ zval *copy = php_http_ztyp(IS_STRING, member);
if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
handler->write(obj, value TSRMLS_CC);
char *message = NULL;
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
- with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
php_http_message_t *msg = obj->message;
php_http_message_dtor(msg);
- with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
- if ((obj->message = php_http_message_parse(msg, message, length))) {
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
+ if ((obj->message = php_http_message_parse(msg, message, length TSRMLS_CC))) {
if (obj->message->parent) {
obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
}
PHP_METHOD(HttpMessage, getBody)
{
- with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
if (SUCCESS == zend_parse_parameters_none()) {
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
}
php_http_message_body_dtor(&obj->message->body);
- php_http_message_body_init(&obj->message->body, php_http_message_body_stream(body_obj->body));
+ php_http_message_body_copy(body_obj->body, &obj->message->body, 0);
Z_OBJ_ADDREF_P(zbody);
obj->body = Z_OBJVAL_P(zbody);
- RETURN_TRUE;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getHeader)
}
if (!zvalue) {
- RETVAL_SUCCESS(zend_hash_del(&obj->message->hdrs, name, name_len + 1));
+ zend_symtable_del(&obj->message->hdrs, name, name_len + 1);
} else {
Z_ADDREF_P(zvalue);
- RETVAL_SUCCESS(zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL));
+ zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
}
efree(name);
- return;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, setHeaders)
if (new_headers) {
array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
}
- RETURN_TRUE;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, addHeader)
Z_ADDREF_P(zvalue);
if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
convert_to_array(header);
- RETVAL_SUCCESS(zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL));
+ zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
} else {
- RETVAL_SUCCESS(zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL));
+ zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
}
efree(name);
- return;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, addHeaders)
}
array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
- RETURN_TRUE;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getType)
}
php_http_message_set_type(obj->message, type);
- RETURN_TRUE;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getInfo)
php_http_message_set_info(obj->message, &inf);
php_http_info_dtor(&inf);
- RETURN_TRUE;
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getHttpVersion)
if (php_http_version_parse(&version, v_str TSRMLS_CC)) {
obj->message->http.version = version;
- RETURN_TRUE;
}
}
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getResponseCode)
}
obj->message->http.info.response.code = code;
- RETURN_TRUE;
}
-
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getResponseStatus)
PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE, obj->message, RETURN_FALSE);
STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
- RETURN_TRUE;
}
-
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getRequestMethod)
}
STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
- RETURN_TRUE;
}
-
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getRequestUrl)
RETURN_FALSE;
}
STR_SET(obj->message->http.info.request.url, estrndup(url_str, url_len));
- RETURN_TRUE;
}
-
- RETURN_FALSE;
+ RETVAL_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(HttpMessage, getParentMessage)
{
- with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
if (SUCCESS == zend_parse_parameters_none()) {
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
PHP_METHOD(HttpMessage, detach)
{
- with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
if (SUCCESS == zend_parse_parameters_none()) {
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);