restore append functionality of http\Message::addHeaders
[m6w6/ext-http] / src / php_http_message.c
index c6b03ffe597b773ec93dea1b6c5f5f81ce473f8a..0701af00c562d1241ca3106c3f17f844f323ebfc 100644 (file)
@@ -56,7 +56,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
        int free_msg = !message;
        zval *sval, tval;
        php_http_message_body_t *mbody;
-       
+
        switch (type) {
                case PHP_HTTP_REQUEST:
                        mbody = php_http_env_get_request_body(TSRMLS_C);
@@ -69,12 +69,12 @@ 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 TSRMLS_CC))) {
-                               message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0 TSRMLS_CC);
+                               message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), PHP_HTTP_URL_STDFLAGS TSRMLS_CC);
                        }
-                       
+
                        php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
                        break;
-                       
+
                case PHP_HTTP_RESPONSE:
                        message = php_http_message_init(NULL, type, NULL TSRMLS_CC);
                        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)) {
@@ -83,7 +83,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                                }
                                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);
 #if PHP_VERSION_ID >= 50400
                        if (php_output_get_level(TSRMLS_C)) {
@@ -113,7 +113,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                        }
 #endif
                        break;
-                       
+
                default:
                error:
                        if (free_msg) {
@@ -125,7 +125,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                        }
                        break;
        }
-       
+
        return message;
 }
 
@@ -256,11 +256,11 @@ void php_http_message_set_type(php_http_message_t *message, php_http_message_typ
                                PTR_FREE(message->http.info.request.method);
                                PTR_FREE(message->http.info.request.url);
                                break;
-                       
+
                        case PHP_HTTP_RESPONSE:
                                PTR_FREE(message->http.info.response.status);
                                break;
-                       
+
                        default:
                                break;
                }
@@ -279,12 +279,12 @@ void php_http_message_set_info(php_http_message_t *message, php_http_info_t *inf
                        PTR_SET(PHP_HTTP_INFO(message).request.url, PHP_HTTP_INFO(info).request.url ? php_http_url_copy(PHP_HTTP_INFO(info).request.url, 0) : NULL);
                        PTR_SET(PHP_HTTP_INFO(message).request.method, PHP_HTTP_INFO(info).request.method ? estrdup(PHP_HTTP_INFO(info).request.method) : NULL);
                        break;
-               
+
                case PHP_HTTP_RESPONSE:
                        PHP_HTTP_INFO(message).response.code = PHP_HTTP_INFO(info).response.code;
                        PTR_SET(PHP_HTTP_INFO(message).response.status, PHP_HTTP_INFO(info).response.status ? estrdup(PHP_HTTP_INFO(info).response.status) : NULL);
                        break;
-               
+
                default:
                        break;
        }
@@ -331,6 +331,27 @@ void php_http_message_update_headers(php_http_message_t *msg)
                        zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
                }
                zval_ptr_dtor(&h_cpy);
+       } else if (msg->type == PHP_HTTP_REQUEST) {
+               if ((h = php_http_message_header(msg, ZEND_STRL("Transfer-Encoding"), 0))) {
+                       zval_ptr_dtor(&h);
+               } else {
+                       /* no filter, no CR, no size, no TE, no CL */
+                       if (0 <= php_http_select_str(msg->http.info.request.method, 3, "POST", "PUT", "PATCH")) {
+                               /* quoting RFC7230#section-3.3.2
+                                       A user agent SHOULD send a Content-Length in a request message when
+                                       no Transfer-Encoding is sent and the request method defines a meaning
+                                       for an enclosed payload body.  For example, a Content-Length header
+                                       field is normally sent in a POST request even when the value is 0
+                                       (indicating an empty payload body).  A user agent SHOULD NOT send a
+                                       Content-Length header field when the request message does not contain
+                                       a payload body and the method semantics do not anticipate such a
+                                       body.
+                               */
+                               MAKE_STD_ZVAL(h);
+                               ZVAL_LONG(h, 0);
+                               zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
+                       }
+               }
        }
 }
 
@@ -419,9 +440,9 @@ void php_http_message_serialize(php_http_message_t *message, char **string, size
 php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
 {
        int i, c = 0;
-       
+
        php_http_message_count(c, msg);
-       
+
        if (c > 1) {
                php_http_message_t *tmp = msg, **arr;
 
@@ -434,11 +455,11 @@ php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
                for (i = 0; i < c-1; ++i) {
                        arr[i+1]->parent = arr[i];
                }
-               
+
                msg = arr[c-1];
                efree(arr);
        }
-       
+
        return msg;
 }
 
@@ -465,28 +486,28 @@ php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_
        php_http_message_t *temp, *copy = NULL;
        php_http_info_t info;
        TSRMLS_FETCH_FROM_CTX(from->ts);
-       
+
        if (from) {
                info.type = from->type;
                info.http = from->http;
-               
+
                copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL) 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 *));
-       
+
                if (parents) while (from->parent) {
                        info.type = from->parent->type;
                        info.http = from->parent->http;
-               
+
                        temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL) 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 *));
-               
+
                        temp = temp->parent;
                        from = from->parent;
                }
        }
-       
+
        return copy;
 }
 
@@ -500,17 +521,17 @@ void php_http_message_dtor(php_http_message_t *message)
        if (message) {
                zend_hash_destroy(&message->hdrs);
                php_http_message_body_free(&message->body);
-               
+
                switch (message->type) {
                        case PHP_HTTP_REQUEST:
                                PTR_SET(message->http.info.request.method, NULL);
                                PTR_SET(message->http.info.request.url, NULL);
                                break;
-                       
+
                        case PHP_HTTP_RESPONSE:
                                PTR_SET(message->http.info.response.status, NULL);
                                break;
-                       
+
                        default:
                                break;
                }
@@ -584,7 +605,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 TSRMLS_DC) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
-               PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0 TSRMLS_CC));
+               PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, PHP_HTTP_URL_STDFLAGS TSRMLS_CC));
        }
 }
 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
@@ -952,7 +973,7 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
        INIT_PZVAL_ARRAY(&array, props);
-       
+
 #define ASSOC_PROP(ptype, n, val) \
        do { \
                zend_property_info *pi; \
@@ -1252,6 +1273,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)
@@ -1264,20 +1317,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;
 
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 
-               Z_ADDREF_P(zvalue);
-               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), &zvalue, sizeof(void *), NULL);
-                       zval_ptr_dtor(&header);
-               } else {
-                       zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
-               }
-               efree(name);
+               php_http_message_object_add_header(obj, name_str, name_len, zvalue TSRMLS_CC);
        }
        RETVAL_ZVAL(getThis(), 1, 0);
 }
@@ -1296,7 +1339,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);
 }
@@ -1616,7 +1671,7 @@ static PHP_METHOD(HttpMessage, setRequestUrl)
        }
 
        zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC);
-       url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
+       url = php_http_url_from_zval(zurl, PHP_HTTP_URL_STDFLAGS TSRMLS_CC);
        zend_restore_error_handling(&zeh TSRMLS_CC);
 
        if (url && php_http_url_is_empty(url)) {
@@ -2082,4 +2137,3 @@ PHP_MSHUTDOWN_FUNCTION(http_message)
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
-