operate directly on the querystring of the request url
authorMichael Wallner <mike@php.net>
Thu, 29 Mar 2012 15:56:18 +0000 (15:56 +0000)
committerMichael Wallner <mike@php.net>
Thu, 29 Mar 2012 15:56:18 +0000 (15:56 +0000)
php_http_client_request.c
php_http_message.c
php_http_params.c

index 061ab413768e6c515d93b850f2bbd00cef56095d..697dbb5e9f800a3e01cc94f719bf689c8b3d6625 100644 (file)
@@ -118,21 +118,37 @@ PHP_METHOD(HttpClientRequest, setQuery)
        zval *qdata = NULL;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
-               if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
-                       zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), "", 0 TSRMLS_CC);
-               } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
-                       char *query_data_str = NULL;
-                       size_t query_data_len;
-
-                       if (SUCCESS == php_http_url_encode_hash(HASH_OF(qdata), NULL, 0, &query_data_str, &query_data_len TSRMLS_CC)) {
-                               zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), query_data_str, query_data_len TSRMLS_CC);
-                               efree(query_data_str);
-                       }
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_url *old_url = NULL, new_url = {NULL};
+               char empty[] = "";
+
+               if (qdata) {
+                       zval arr, str;
+
+                       INIT_PZVAL(&arr);
+                       array_init(&arr);
+                       INIT_PZVAL(&str);
+                       ZVAL_NULL(&str);
+
+                       php_http_querystring_update(&arr, qdata, &str TSRMLS_CC);
+                       new_url.query = Z_STRVAL(str);
+                       zval_dtor(&arr);
                } else {
-                       zval *data = php_http_ztyp(IS_STRING, qdata);
+                       new_url.query = &empty[0];
+               }
 
-                       zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), Z_STRVAL_P(data), Z_STRLEN_P(data) TSRMLS_CC);
-                       zval_ptr_dtor(&data);
+               if (obj->message->http.info.request.url) {
+                       old_url = php_url_parse(obj->message->http.info.request.url);
+                       efree(obj->message->http.info.request.url);
+               }
+
+               php_http_url(PHP_HTTP_URL_REPLACE, old_url, &new_url, NULL, &obj->message->http.info.request.url, NULL TSRMLS_CC);
+
+               if (old_url) {
+                       php_url_free(old_url);
+               }
+               if (new_url.query != &empty[0]) {
+                       STR_FREE(new_url.query);
                }
        }
        RETVAL_ZVAL(getThis(), 1, 0);
@@ -141,29 +157,55 @@ PHP_METHOD(HttpClientRequest, setQuery)
 PHP_METHOD(HttpClientRequest, getQuery)
 {
        if (SUCCESS == zend_parse_parameters_none()) {
-               RETURN_PROP(php_http_client_request_class_entry, "query");
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (obj->message->http.info.request.url) {
+                       php_url *purl = php_url_parse(obj->message->http.info.request.url);
+
+                       if (purl) {
+                               if (purl->query) {
+                                       RETVAL_STRING(purl->query, 0);
+                                       purl->query = NULL;
+                               }
+                               php_url_free(purl);
+                       }
+               }
        }
-       RETURN_FALSE;
 }
 
 PHP_METHOD(HttpClientRequest, addQuery)
 {
        zval *qdata;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
-               char *query_data_str = NULL;
-               size_t query_data_len = 0;
-               zval *old_qdata = php_http_ztyp(IS_STRING, zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), 0 TSRMLS_CC));
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_url *old_url = NULL, new_url = {NULL};
+               char empty[] = "";
+
+               zval arr, str;
 
-               if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qdata), Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data_str, &query_data_len TSRMLS_CC)) {
-                       zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), query_data_str, query_data_len TSRMLS_CC);
-                       efree(query_data_str);
+               INIT_PZVAL(&arr);
+               array_init(&arr);
+               INIT_PZVAL(&str);
+               ZVAL_NULL(&str);
+
+               php_http_querystring_update(&arr, qdata, &str TSRMLS_CC);
+               new_url.query = Z_STRVAL(str);
+               zval_dtor(&arr);
+
+               if (obj->message->http.info.request.url) {
+                       old_url = php_url_parse(obj->message->http.info.request.url);
+                       efree(obj->message->http.info.request.url);
                }
 
-               zval_ptr_dtor(&old_qdata);
+               php_http_url(PHP_HTTP_URL_JOIN_QUERY, old_url, &new_url, NULL, &obj->message->http.info.request.url, NULL TSRMLS_CC);
+
+               if (old_url) {
+                       php_url_free(old_url);
+               }
+               STR_FREE(new_url.query);
        }
        RETVAL_ZVAL(getThis(), 1, 0);
-
 }
 
 
@@ -182,8 +224,6 @@ PHP_MINIT_FUNCTION(http_client_request)
 {
        PHP_HTTP_REGISTER_CLASS(http\\Client, Request, http_client_request, php_http_message_class_entry, 0);
 
-       zend_declare_property_string(php_http_client_request_class_entry, ZEND_STRL("query"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
-
        return SUCCESS;
 }
 
index 685aba146c5935a19b324264a780cbc458e64a25..42ebe42a0889b240f4e63fb25299c193cc23d40d 100644 (file)
@@ -41,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);
 
@@ -59,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));
@@ -80,8 +79,6 @@ 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;
                                if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
                                        message->http.info.response.code = 200;
                                }
index f3617527bc023896258f5c51b262b8948886586d..4b9844820542bbe9c53f0c92f354d2ece9edca7d 100644 (file)
@@ -206,7 +206,13 @@ PHP_HTTP_API php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf
                        php_http_buffer_appendf(buf, "%lu", key1.num);
                }
 
-               if (Z_TYPE_PP(zparam) == IS_ARRAY) {
+               if (Z_TYPE_PP(zparam) != IS_ARRAY) {
+                       zval *tmp = php_http_ztyp(IS_STRING, *zparam);
+
+                       php_http_buffer_append(buf, vss, vsl);
+                       php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+                       zval_ptr_dtor(&tmp);
+               } else {
                        zval **zvalue, **zargs, **zarg;
 
                        /* got a value? */