X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_message.c;h=f8b3a4d6a67bcf60efabb352a0d0854564fb3699;hp=0d11d0168ae0b53fe15988bd6748265ad7727022;hb=bd80b17b026a00a254ee8693cd7bacf1ebdec4cf;hpb=4743c5114bcdcf8d478e140beb63fecfeccd01a4 diff --git a/php_http_message.c b/php_http_message.c index 0d11d01..f8b3a4d 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -6,30 +6,28 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */ - -#include "php_http.h" - -#include
-#include -#include +#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) { php_http_message_t *old = *message; /* advance message */ - if (old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) { + if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) { (*message) = php_http_message_init(NULL, 0 TSRMLS_CC); (*message)->parent = old; - (*headers) = &((*message)->hdrs); + if (headers) { + (*headers) = &((*message)->hdrs); + } } - php_http_message_set_info(*message, info); + if (info) { + php_http_message_set_info(*message, info); + } return old != *message; } @@ -43,6 +41,8 @@ PHP_HTTP_API php_http_message_t *php_http_message_init(php_http_message_t *messa TSRMLS_SET_CTX(message->ts); php_http_message_set_type(message, type); + message->http.version.major = 1; + message->http.version.minor = 1; zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0); php_http_message_body_init(&message->body, NULL TSRMLS_CC); @@ -61,9 +61,6 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m 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) TSRMLS_CC); - } else { - message->http.version.major = 1; - message->http.version.minor = 1; } if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) { message->http.info.request.method = estrdup(Z_STRVAL_P(sval)); @@ -82,19 +79,10 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m case PHP_HTTP_RESPONSE: if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line TSRMLS_CC)) { - message->http.version.major = 1; - message->http.version.minor = 1; - switch ((message->http.info.response.code = SG(sapi_headers).http_response_code)) { - case 0: - message->http.info.response.code = 200; - case 200: - message->http.info.response.status = estrdup("Ok"); - break; - default: - message->http.info.response.status = estrdup(""); - break; + if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) { + message->http.info.response.code = 200; } - + message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code)); } php_http_env_get_response_headers(&message->hdrs TSRMLS_CC); @@ -130,17 +118,25 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, { php_http_message_parser_t p; php_http_buffer_t buf; + int free_msg; - if (!msg) { - msg = php_http_message_init(NULL, 0 TSRMLS_CC); - } php_http_buffer_from_string_ex(&buf, str, len); php_http_message_parser_init(&p TSRMLS_CC); - php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg); + + if ((free_msg = !msg)) { + msg = php_http_message_init(NULL, 0 TSRMLS_CC); + } + + if (FAILURE == php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg)) { + if (free_msg) { + php_http_message_free(&msg); + } + msg = NULL; + } + php_http_message_parser_dtor(&p); php_http_buffer_dtor(&buf); - /* FIXME */ return msg; } @@ -149,7 +145,7 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st 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; @@ -167,7 +163,8 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0); ret = header_str; } else { - ret = php_http_ztyp(IS_STRING, *header); + Z_ADDREF_PP(header); + ret = *header; } } @@ -176,6 +173,63 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st return ret; } +PHP_HTTP_API zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary) +{ + zval *ct = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1); + zend_bool is_multipart = 0; + TSRMLS_FETCH_FROM_CTX(msg->ts); + + if (ct) { + php_http_params_opts_t popts; + HashTable params; + + ZEND_INIT_SYMTABLE(¶ms); + php_http_params_opts_default_get(&popts); + popts.input.str = Z_STRVAL_P(ct); + popts.input.len = Z_STRLEN_P(ct); + + if (php_http_params_parse(¶ms, &popts TSRMLS_CC)) { + zval **cur, **arg; + char *ct_str; + + zend_hash_internal_pointer_reset(¶ms); + + if (SUCCESS == zend_hash_get_current_data(¶ms, (void *) &cur) + && Z_TYPE_PP(cur) == IS_ARRAY + && HASH_KEY_IS_STRING == zend_hash_get_current_key(¶ms, &ct_str, NULL, 0) + ) { + if (php_http_match(ct_str, "multipart", PHP_HTTP_MATCH_WORD)) { + is_multipart = 1; + + /* get boundary */ + if (boundary + && SUCCESS == zend_hash_find(Z_ARRVAL_PP(cur), ZEND_STRS("arguments"), (void *) &arg) + && Z_TYPE_PP(arg) == IS_ARRAY + ) { + zval **val; + HashPosition pos; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + + FOREACH_KEYVAL(pos, *arg, key, val) { + if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "boundary")) { + zval *bnd = php_http_ztyp(IS_STRING, *val); + + if (Z_STRLEN_P(bnd)) { + *boundary = estrndup(Z_STRVAL_P(bnd), Z_STRLEN_P(bnd)); + } + zval_ptr_dtor(&bnd); + } + } + } + } + } + } + zend_hash_destroy(¶ms); + zval_ptr_dtor(&ct); + } + + return is_multipart; +} /* */ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_http_message_type_t type) @@ -184,7 +238,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt if (type != message->type) { /* free request info */ - switch (message->type = type) { + switch (message->type) { case PHP_HTTP_REQUEST: STR_FREE(message->http.info.request.method); STR_FREE(message->http.info.request.url); @@ -198,6 +252,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt break; } + message->type = type; memset(&message->http, 0, sizeof(message->http)); } } @@ -328,7 +383,6 @@ PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http if (php_http_message_body_size(&msg->body)) { 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)); } } @@ -342,7 +396,6 @@ PHP_HTTP_API void php_http_message_to_string(php_http_message_t *msg, char **str if (php_http_message_body_size(&msg->body)) { php_http_buffer_appends(&str, PHP_HTTP_CRLF); php_http_message_body_to_callback(&msg->body, (php_http_pass_callback_t) php_http_buffer_append, &str, 0, 0); - php_http_buffer_appends(&str, PHP_HTTP_CRLF); } data = php_http_buffer_data(&str, string, length); @@ -356,16 +409,17 @@ PHP_HTTP_API void php_http_message_to_string(php_http_message_t *msg, char **str PHP_HTTP_API void php_http_message_serialize(php_http_message_t *message, char **string, size_t *length) { char *buf; - size_t len; php_http_buffer_t str; + php_http_message_t *msg; php_http_buffer_init(&str); + msg = message = php_http_message_reverse(message); do { - php_http_message_to_string(message, &buf, &len); - php_http_buffer_prepend(&str, buf, len); - efree(buf); + php_http_message_to_callback(message, (php_http_pass_callback_t) php_http_buffer_append, &str); + php_http_buffer_appends(&str, PHP_HTTP_CRLF); } while ((message = message->parent)); + php_http_message_reverse(msg); buf = php_http_buffer_data(&str, string, length); if (!string) { @@ -382,8 +436,9 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms 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; @@ -400,172 +455,6 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms return msg; } -PHP_HTTP_API php_http_message_t *php_http_message_interconnect(php_http_message_t *m1, php_http_message_t *m2) -{ - if (m1 && m2) { - int i = 0, c1 = 0, c2 = 0; - php_http_message_t *t1 = m1, *t2 = m2, *p1, *p2; - - php_http_message_count(c1, m1); - php_http_message_count(c2, m2); - - while (i++ < (c1 - c2)) { - t1 = t1->parent; - } - while (i++ <= c1) { - p1 = t1->parent; - p2 = t2->parent; - t1->parent = t2; - t2->parent = p1; - t1 = p1; - t2 = p2; - } - } else if (!m1 && m2) { - m1 = m2; - } - return m1; -} - -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)); - - add_assoc_long(&strct, "type", msg->type); - spprintf(&version, 0 ,"%u.%u", msg->http.version.major, msg->http.version.minor); - add_assoc_string_ex(&strct, ZEND_STRL("httpVersion"), version, 0); - switch (msg->type) - { - case PHP_HTTP_RESPONSE: - add_assoc_long(&strct, "responseCode", msg->http.info.response.code); - add_assoc_string(&strct, "responseStatus", STR_PTR(msg->http.info.response.status), 1); - break; - - case PHP_HTTP_REQUEST: - add_assoc_string(&strct, "requestMethod", STR_PTR(msg->http.info.request.method), 1); - add_assoc_string(&strct, "requestUrl", STR_PTR(msg->http.info.request.url), 1); - break; - - case PHP_HTTP_NONE: - /* avoid compiler warning */ - break; - } - - MAKE_STD_ZVAL(headers); - array_init(headers); - zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); - add_assoc_zval(&strct, "headers", headers); - - add_assoc_stringl(&strct, "body", PHP_HTTP_BUFFER_VAL(msg), PHP_HTTP_BUFFER_LEN(msg), 1); - - if (msg->parent) { - zval *parent; - - MAKE_STD_ZVAL(parent); - if (Z_TYPE_P(obj) == IS_ARRAY) { - array_init(parent); - } else { - object_init(parent); - } - add_assoc_zval(&strct, "parentMessage", parent); - php_http_message_to_struct(msg->parent, parent); - } else { - add_assoc_null(&strct, "parentMessage"); - } -} -/* -PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) -{ - STATUS rs = FAILURE; - - switch (message->type) { - case PHP_HTTP_RESPONSE: - { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **val; - HashPosition pos; - - FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) { - if (key.type == HASH_KEY_IS_STRING) { - http_send_header_zval_ex(key.str, key.len-1, val, 1); - } - } - rs = SUCCESS == http_send_status(message->http.info.response.code) && - SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ? - SUCCESS : FAILURE; - break; - } - - case PHP_HTTP_REQUEST: - { -#ifdef PHP_HTTP_HAVE_CURL - char *uri = NULL; - http_request request; - zval **zhost, *options, *headers; - - MAKE_STD_ZVAL(options); - MAKE_STD_ZVAL(headers); - array_init(options); - array_init(headers); - zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); - add_assoc_zval(options, "headers", headers); - - if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) { - char *colon = NULL; - php_url parts, *url = php_url_parse(message->http.info.request.url); - - memset(&parts, 0, sizeof(php_url)); - - if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) { - parts.port = atoi(colon + 1); - parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1)); - } else { - parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost)); - } - - http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL); - php_url_free(url); - efree(parts.host); - } else { - uri = http_absolute_url(message->http.info.request.url); - } - - if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) { - http_request_body body; - - http_request_init_ex(&request, NULL, request.meth, uri); - request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0); - if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) { - http_request_exec(&request); - } - http_request_dtor(&request); - } else { - http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, - "Cannot send HttpMessage. Request method %s not supported", - message->http.info.request.method); - } - efree(uri); - zval_ptr_dtor(&options); -#else - http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl."); -#endif - break; - } - - case PHP_HTTP_NONE: - default: - php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE"); - break; - } - - return rs; -} -*/ - 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; @@ -588,7 +477,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *fr 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); + php_http_message_body_copy(&from->parent->body, &temp->parent->body, 1); temp = temp->parent; from = from->parent; @@ -650,6 +539,10 @@ PHP_HTTP_BEGIN_ARGS(setBody, 1) PHP_HTTP_ARG_VAL(body, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(addBody, 1) + PHP_HTTP_ARG_VAL(body, 0) +PHP_HTTP_END_ARGS; + PHP_HTTP_BEGIN_ARGS(getHeader, 1) PHP_HTTP_ARG_VAL(header, 0) PHP_HTTP_END_ARGS; @@ -709,19 +602,17 @@ PHP_HTTP_BEGIN_ARGS(setHttpVersion, 1) 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_BEGIN_ARGS(toCallback, 1) + PHP_HTTP_ARG_VAL(callback, 0) +PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(toStream, 1) + PHP_HTTP_ARG_VAL(stream, 0) +PHP_HTTP_END_ARGS; PHP_HTTP_EMPTY_ARGS(count); @@ -742,16 +633,28 @@ PHP_HTTP_BEGIN_ARGS(prepend, 1) PHP_HTTP_END_ARGS; PHP_HTTP_EMPTY_ARGS(reverse); +PHP_HTTP_BEGIN_ARGS(isMultipart, 0) + PHP_HTTP_ARG_VAL(boundary, 1) +PHP_HTTP_END_ARGS; +PHP_HTTP_EMPTY_ARGS(splitMultipartBody); + static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, const zend_literal *literal_key TSRMLS_DC); static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, 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); static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC); -zend_class_entry *php_http_message_class_entry; -zend_function_entry php_http_message_method_entry[] = { +static zend_class_entry *php_http_message_class_entry; + +zend_class_entry *php_http_message_get_class_entry(void) +{ + return php_http_message_class_entry; +} + +static zend_function_entry php_http_message_method_entry[] = { PHP_HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC) + PHP_HTTP_MESSAGE_ME(addBody, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(getHeader, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(setHeader, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(addHeader, ZEND_ACC_PUBLIC) @@ -774,6 +677,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) @@ -795,6 +700,9 @@ zend_function_entry php_http_message_method_entry[] = { PHP_HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(reverse, ZEND_ACC_PUBLIC) + PHP_HTTP_MESSAGE_ME(isMultipart, ZEND_ACC_PUBLIC) + PHP_HTTP_MESSAGE_ME(splitMultipartBody, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY }; static zend_object_handlers php_http_message_object_handlers; @@ -811,9 +719,11 @@ static STATUS php_http_message_object_add_prophandler(const char *prop_str, size 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); } @@ -878,6 +788,7 @@ static void php_http_message_object_prophandler_set_response_code(php_http_messa if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) { zval *cpy = php_http_ztyp(IS_LONG, value); obj->message->http.info.response.code = Z_LVAL_P(cpy); + STR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(obj->message->http.info.response.code))); zval_ptr_dtor(&cpy); } } @@ -912,7 +823,7 @@ static void php_http_message_object_prophandler_get_body(php_http_message_object } } static void php_http_message_object_prophandler_set_body(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_body_class_entry TSRMLS_CC)) { + if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_body_get_class_entry() TSRMLS_CC)) { if (obj->body.handle) { zend_objects_store_del_ref_by_handle(obj->body.handle TSRMLS_CC); } @@ -939,7 +850,7 @@ static void php_http_message_object_prophandler_set_parent_message(php_http_mess PHP_MINIT_FUNCTION(http_message) { - PHP_HTTP_REGISTER_CLASS(http, Message, http_message, php_http_object_class_entry, 0); + PHP_HTTP_REGISTER_CLASS(http, Message, http_message, php_http_object_get_class_entry(), 0); php_http_message_class_entry->create_object = php_http_message_object_new; memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_http_message_object_handlers.clone_obj = php_http_message_object_clone; @@ -993,7 +904,6 @@ void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_D php_http_message_count(i, obj->message); if (i > 1) { - zval o; zend_object_value *ovalues = NULL; php_http_message_object_t **objects = NULL; int last = i - 1; @@ -1006,12 +916,9 @@ void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_D ovalues[0] = getThis()->value.obj; /* fetch parents */ - INIT_PZVAL(&o); - o.type = IS_OBJECT; for (i = 1; obj->parent.handle; ++i) { - o.value.obj = obj->parent; - ovalues[i] = o.value.obj; - objects[i] = obj = zend_object_store_get_object(&o TSRMLS_CC); + ovalues[i] = obj->parent; + objects[i] = obj = zend_object_store_get_object_by_handle(obj->parent.handle TSRMLS_CC); } /* reorder parents */ @@ -1019,11 +926,12 @@ void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_D objects[i]->message->parent = objects[i-1]->message; objects[i]->parent = ovalues[i-1]; } + objects[0]->message->parent = NULL; objects[0]->parent.handle = 0; objects[0]->parent.handlers = NULL; - /* add ref (why?) */ + /* add ref, because we previously have not been a parent message */ Z_OBJ_ADDREF_P(getThis()); RETVAL_OBJVAL(ovalues[last], 1); @@ -1097,7 +1005,7 @@ zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_ 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_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(), php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC); } ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC); @@ -1150,7 +1058,7 @@ static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, c if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) { zval_ptr_dtor(©); - return &php_http_property_proxy_init(NULL, object, member TSRMLS_CC)->myself; + return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself; } zval_ptr_dtor(©); @@ -1164,18 +1072,16 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t 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) { - zval_ptr_dtor(©); + if (type == BP_VAR_R) { + ALLOC_ZVAL(return_value); + Z_SET_REFCOUNT_P(return_value, 0); + Z_UNSET_ISREF_P(return_value); + + handler->read(obj, return_value TSRMLS_CC); + } else { zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index"); - return NULL; + return_value = NULL; } - - ALLOC_ZVAL(return_value); - Z_SET_REFCOUNT_P(return_value, 0); - Z_UNSET_ISREF_P(return_value); - - handler->read(obj, return_value TSRMLS_CC); - } else { return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC); } @@ -1265,7 +1171,7 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC) MAKE_STD_ZVAL(body); if (!obj->body.handle) { - php_http_new(&obj->body, php_http_message_body_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); + 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); } ZVAL_OBJVAL(body, obj->body, 1); ASSOC_PROP(array, zval, "body", body); @@ -1285,24 +1191,36 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC) PHP_METHOD(HttpMessage, __construct) { - int length = 0; - char *message = NULL; + zval *zmessage = NULL; + php_http_message_t *msg = NULL; php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - 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; + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) { + if (Z_TYPE_P(zmessage) == IS_RESOURCE) { + php_stream *s; + php_http_message_parser_t p; - php_http_message_dtor(msg); - 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); - } - } else { - obj->message = php_http_message_init(msg, 0 TSRMLS_CC); + php_stream_from_zval(s, &zmessage); + if (s && php_http_message_parser_init(&p TSRMLS_CC)) { + php_http_message_parser_parse_stream(&p, s, &msg); + php_http_message_parser_dtor(&p); } - } end_error_handling(); + } else { + zmessage = php_http_ztyp(IS_STRING, zmessage); + msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC); + zval_ptr_dtor(&zmessage); + } + + if (msg) { + php_http_message_dtor(obj->message); + obj->message = msg; + if (obj->message->parent) { + obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC); + } + } else { + php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, Z_STRVAL_P(zmessage)); + } } if (!obj->message) { obj->message = php_http_message_init(NULL, 0 TSRMLS_CC); @@ -1313,7 +1231,7 @@ PHP_METHOD(HttpMessage, __construct) PHP_METHOD(HttpMessage, getBody) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { if (SUCCESS == zend_parse_parameters_none()) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -1321,7 +1239,7 @@ PHP_METHOD(HttpMessage, getBody) obj->message = php_http_message_init(NULL, 0 TSRMLS_CC); } - if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_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 || 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)) { RETVAL_OBJVAL(obj->body, 1); } } @@ -1332,7 +1250,7 @@ PHP_METHOD(HttpMessage, setBody) { zval *zbody; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_class_entry)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_get_class_entry())) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC); @@ -1348,6 +1266,20 @@ PHP_METHOD(HttpMessage, setBody) RETVAL_ZVAL(getThis(), 1, 0); } +PHP_METHOD(HttpMessage, addBody) +{ + 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_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); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + + PHP_METHOD(HttpMessage, getHeader) { char *header_str; @@ -1397,10 +1329,10 @@ PHP_METHOD(HttpMessage, setHeader) } if (!zvalue) { - 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); - 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); } @@ -1446,7 +1378,7 @@ PHP_METHOD(HttpMessage, addHeader) convert_to_array(header); zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL); } else { - 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); } @@ -1617,6 +1549,7 @@ PHP_METHOD(HttpMessage, setResponseCode) } obj->message->http.info.response.code = code; + STR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code))); } RETVAL_ZVAL(getThis(), 1, 0); } @@ -1747,7 +1680,7 @@ PHP_METHOD(HttpMessage, setRequestUrl) PHP_METHOD(HttpMessage, getParentMessage) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { if (SUCCESS == zend_parse_parameters_none()) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -1789,6 +1722,44 @@ 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) +{ + php_http_pass_fcall_arg_t fcd; + + 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()) { @@ -1830,7 +1801,7 @@ PHP_METHOD(HttpMessage, unserialize) PHP_METHOD(HttpMessage, detach) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { if (SUCCESS == zend_parse_parameters_none()) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -1881,6 +1852,40 @@ PHP_METHOD(HttpMessage, reverse) } } +PHP_METHOD(HttpMessage, isMultipart) +{ + zval *zboundary = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zboundary)) { + php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + char *boundary = NULL; + + RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL)); + + if (zboundary && boundary) { + zval_dtor(zboundary); + ZVAL_STRING(zboundary, boundary, 0); + } + } +} + +PHP_METHOD(HttpMessage, splitMultipartBody) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + char *boundary = NULL; + + if (php_http_message_is_multipart(obj->message, &boundary)) { + php_http_message_t *msg; + + if ((msg = php_http_message_body_split(&obj->message->body, boundary))) { + RETVAL_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0); + } + } + STR_FREE(boundary); + } +} + PHP_METHOD(HttpMessage, count) { if (SUCCESS == zend_parse_parameters_none()) {