X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=src%2Fphp_http_message.c;h=413edab57711604aa5350a5b4a4cd506e58a43dc;hp=591022da4f9edd0f5bdca9ca5d1bae398f472967;hb=8158548a80733b3af9539356b47527d960a13287;hpb=e1d5ca36e13696a2e87b618165669fc7c53709c1 diff --git a/src/php_http_message.c b/src/php_http_message.c index 591022d..413edab 100644 --- a/src/php_http_message.c +++ b/src/php_http_message.c @@ -68,7 +68,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h message->http.info.request.method = estrdup(Z_STRVAL_P(sval)); } if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1))) { - message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0); + message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), PHP_HTTP_URL_STDFLAGS); } php_http_env_get_request_headers(&message->hdrs); @@ -330,24 +330,14 @@ void php_http_message_update_headers(php_http_message_t *msg) static void message_headers(php_http_message_t *msg, php_http_buffer_t *str) { char *tmp = NULL; + size_t len = 0; - switch (msg->type) { - case PHP_HTTP_REQUEST: - php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF)); - PTR_FREE(tmp); - break; - - case PHP_HTTP_RESPONSE: - php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF)); - PTR_FREE(tmp); - break; - - default: - break; - } - + php_http_info_to_string((php_http_info_t *) msg, &tmp, &len, PHP_HTTP_CRLF TSRMLS_CC); php_http_message_update_headers(msg); + + php_http_buffer_append(str, tmp, len); php_http_header_to_string(str, &msg->hdrs); + PTR_FREE(tmp); } void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg) @@ -415,7 +405,7 @@ php_http_message_t *php_http_message_reverse(php_http_message_t *msg) if (c > 1) { php_http_message_t *tmp = msg, **arr; - arr = ecalloc(c, sizeof(**arr)); + arr = ecalloc(c, sizeof(*arr)); for (i = 0; i < c; ++i) { arr[i] = tmp; tmp = tmp->parent; @@ -579,7 +569,7 @@ static void php_http_message_object_prophandler_get_request_url(php_http_message } static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value) { if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) { - PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0)); + PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, PHP_HTTP_URL_STDFLAGS)); } } static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value) { @@ -626,18 +616,18 @@ static void php_http_message_object_prophandler_get_headers(php_http_message_obj array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value)); } static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value) { - HashTable *headers; - zval *orig_value = value; + int converted = 0; if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) { - convert_to_array_ex(value); + converted = 1; + SEPARATE_ZVAL(value); + convert_to_array(value); } - headers = HASH_OF(value); zend_hash_clean(&obj->message->hdrs); - array_copy(headers, &obj->message->hdrs); + array_copy(HASH_OF(value), &obj->message->hdrs); - if (orig_value != value) { + if (converted) { zval_ptr_dtor(value); } } @@ -693,7 +683,7 @@ void php_http_message_object_reverse(zval *zmsg, zval *return_value) php_http_message_object_t **objects; int last; - objects = ecalloc(i, sizeof(**objects)); + objects = ecalloc(i, sizeof(*objects)); /* we are the first message */ objects[0] = obj; @@ -795,6 +785,7 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg if (!body_obj->body) { body_obj->body = php_http_message_body_init(NULL, NULL); + php_stream_to_zval(php_http_message_body_stream(body_obj->body), body_obj->gc); } if (msg_obj->body) { zend_object_release(&msg_obj->body->zo); @@ -892,12 +883,13 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t if (handler) { php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object); + zval tmp2; PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - handler->read(obj, tmp); + handler->read(obj, &tmp2); zval_ptr_dtor(return_value); - ZVAL_COPY_VALUE(return_value, tmp); + ZVAL_COPY_VALUE(return_value, &tmp2); } zend_string_release(member_name); return return_value; @@ -1112,7 +1104,6 @@ static PHP_METHOD(HttpMessage, getBody) if (!obj->body) { php_http_message_object_init_body_object(obj); - } if (obj->body) { RETVAL_OBJECT(&obj->body->zo, 1); @@ -1256,6 +1247,38 @@ static PHP_METHOD(HttpMessage, setHeaders) RETVAL_ZVAL(getThis(), 1, 0); } +static inline void php_http_message_object_add_header(php_http_message_object_t *obj, const char *name_str, size_t name_len, zval *zvalue) +{ + char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); + zend_string *hstr, *vstr; + zval *header, tmp; + + if (Z_TYPE_P(zvalue) == IS_NULL) { + return; + } + + vstr = php_http_header_value_to_string(zvalue); + + if ((name_len != lenof("Set-Cookie") && strcmp(name, "Set-Cookie")) + && (hstr = php_http_message_header_string(obj->message, name, name_len))) { + char *hdr_str; + size_t hdr_len = spprintf(&hdr_str, 0, "%s, %s", hstr->val, vstr->val); + + ZVAL_STR(&tmp, php_http_cs2zs(hdr_str, hdr_len)); + zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp); + zend_string_release(hstr); + zend_string_release(vstr); + } else if ((header = php_http_message_header(obj->message, name, name_len))) { + convert_to_array(header); + ZVAL_STR(&tmp, vstr); + zend_hash_next_index_insert(Z_ARRVAL_P(header), &tmp); + } else { + ZVAL_STR(&tmp, vstr); + zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp); + } + efree(name); +} + ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2) ZEND_ARG_INFO(0, header) ZEND_ARG_INFO(0, value) @@ -1268,30 +1291,10 @@ static PHP_METHOD(HttpMessage, addHeader) if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str, &name_len, &zvalue)) { php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); - char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); - zend_string *hstr, *vstr = php_http_header_value_to_string(zvalue); - zval tmp, *header; PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - if ((name_len != lenof("Set-Cookie") && strcmp(name, "Set-Cookie")) - && (hstr = php_http_message_header_string(obj->message, name, name_len))) { - char *hdr_str; - size_t hdr_len = spprintf(&hdr_str, 0, "%s, %s", hstr->val, vstr->val); - - ZVAL_STR(&tmp, php_http_cs2zs(hdr_str, hdr_len)); - zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp); - zend_string_release(hstr); - zend_string_release(vstr); - } else if ((header = php_http_message_header(obj->message, name, name_len))) { - convert_to_array(header); - ZVAL_STR(&tmp, vstr); - zend_hash_next_index_insert(Z_ARRVAL_P(header), &tmp); - } else { - ZVAL_STR(&tmp, vstr); - zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp); - } - efree(name); + php_http_message_object_add_header(obj, name_str, name_len, zvalue); } RETVAL_ZVAL(getThis(), 1, 0); } @@ -1310,7 +1313,20 @@ static PHP_METHOD(HttpMessage, addHeaders) PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY); + if (append) { + php_http_arrkey_t key = {0}; + zval *val; + + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(new_headers), key.h, key.key, val) + { + php_http_arrkey_stringify(&key, NULL); + php_http_message_object_add_header(obj, key.key->val, key.key->len, val); + php_http_arrkey_dtor(&key); + } + ZEND_HASH_FOREACH_END(); + } else { + array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY); + } } RETVAL_ZVAL(getThis(), 1, 0); } @@ -1350,25 +1366,12 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(HttpMessage, getInfo) { if (SUCCESS == zend_parse_parameters_none()) { - char *str, *tmp = NULL; - size_t len; + char *str = NULL; + size_t len = 0; php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - - switch (obj->message->type) { - case PHP_HTTP_REQUEST: - len = spprintf(&str, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, "")); - PTR_FREE(tmp); - break; - case PHP_HTTP_RESPONSE: - len = spprintf(&str, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, "")); - PTR_FREE(tmp); - break; - default: - RETURN_NULL(); - break; - } + php_http_info_to_string((php_http_info_t *) obj->message, &str, &len, ""); RETVAL_STR(php_http_cs2zs(str, len)); } @@ -1630,7 +1633,7 @@ static PHP_METHOD(HttpMessage, setRequestUrl) } zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh); - url = php_http_url_from_zval(zurl, ~0); + url = php_http_url_from_zval(zurl, PHP_HTTP_URL_STDFLAGS); zend_restore_error_handling(&zeh); if (url && php_http_url_is_empty(url)) {