X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=src%2Fphp_http_message.c;h=3186b79edca07ed13a09e982c9ced7bf64d5a239;hp=388c0ad7e5c4a750a38abdaad0ca20691ef9a936;hb=refs%2Fheads%2Fv2.6.x;hpb=4c7a60297b3aaae7edd8337144a83039c9633d71 diff --git a/src/php_http_message.c b/src/php_http_message.c index 388c0ad..3186b79 100644 --- a/src/php_http_message.c +++ b/src/php_http_message.c @@ -358,25 +358,15 @@ 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; TSRMLS_FETCH_FROM_CTX(msg->ts); - 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 TSRMLS_CC); + STR_FREE(tmp); } void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg) @@ -446,7 +436,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; @@ -712,7 +702,7 @@ void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_D 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; @@ -1273,6 +1263,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 TSRMLS_DC) +{ + char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); + zval *header, *cpy; + + if (Z_TYPE_P(zvalue) == IS_NULL) { + return; + } + + cpy = php_http_header_value_to_string(zvalue TSRMLS_CC); + + if ((name_len != lenof("Set-Cookie") && strcmp(name, "Set-Cookie")) + && (header = php_http_message_header(obj->message, name, name_len, 1))) { + zval *tmp; + char *hdr_str; + size_t hdr_len = spprintf(&hdr_str, 0, "%s, %s", Z_STRVAL_P(header), Z_STRVAL_P(cpy)); + + MAKE_STD_ZVAL(tmp); + ZVAL_STRINGL(tmp, hdr_str, hdr_len, 0); + zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &tmp, sizeof(void *), NULL); + zval_ptr_dtor(&header); + zval_ptr_dtor(&cpy); + } else if ((header = php_http_message_header(obj->message, name, name_len, 0))) { + convert_to_array(header); + zend_hash_next_index_insert(Z_ARRVAL_P(header), &cpy, sizeof(void *), NULL); + zval_ptr_dtor(&header); + } else { + zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &cpy, sizeof(void *), NULL); + } + efree(name); +} + ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2) ZEND_ARG_INFO(0, header) ZEND_ARG_INFO(0, value) @@ -1285,30 +1307,10 @@ static PHP_METHOD(HttpMessage, addHeader) if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &zvalue)) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); - zval *header, *cpy = php_http_header_value_to_string(zvalue TSRMLS_CC); PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - if ((name_len != lenof("Set-Cookie") && strcmp(name, "Set-Cookie")) - && (header = php_http_message_header(obj->message, name, name_len, 1))) { - zval *tmp; - char *hdr_str; - size_t hdr_len = spprintf(&hdr_str, 0, "%s, %s", Z_STRVAL_P(header), Z_STRVAL_P(cpy)); - - MAKE_STD_ZVAL(tmp); - ZVAL_STRINGL(tmp, hdr_str, hdr_len, 0); - zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &tmp, sizeof(void *), NULL); - zval_ptr_dtor(&header); - zval_ptr_dtor(&cpy); - } else if ((header = php_http_message_header(obj->message, name, name_len, 0))) { - convert_to_array(header); - zend_hash_next_index_insert(Z_ARRVAL_P(header), &cpy, sizeof(void *), NULL); - zval_ptr_dtor(&header); - } else { - zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &cpy, sizeof(void *), NULL); - } - efree(name); + php_http_message_object_add_header(obj, name_str, name_len, zvalue TSRMLS_CC); } RETVAL_ZVAL(getThis(), 1, 0); } @@ -1327,7 +1329,19 @@ 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) { + HashPosition pos; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval **val; + + FOREACH_KEYVAL(pos, new_headers, key, val) { + php_http_array_hashkey_stringify(&key); + php_http_message_object_add_header(obj, key.str, key.len-1, *val TSRMLS_CC); + php_http_array_hashkey_stringfree(&key); + } + } else { + array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY); + } } RETVAL_ZVAL(getThis(), 1, 0); } @@ -1367,24 +1381,13 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(HttpMessage, getInfo) { if (SUCCESS == zend_parse_parameters_none()) { - char *tmp = NULL; + size_t len = 0; php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); PHP_HTTP_MESSAGE_OBJECT_INIT(obj); - switch (obj->message->type) { - case PHP_HTTP_REQUEST: - Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, "")); - PTR_FREE(tmp); - break; - case PHP_HTTP_RESPONSE: - Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 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, &Z_STRVAL_P(return_value), &len, "" TSRMLS_CC); + Z_STRLEN_P(return_value) = len; Z_TYPE_P(return_value) = IS_STRING; return; }