Merge branch 'v2.6.x' of github.com:m6w6/ext-http
[m6w6/ext-http] / src / php_http_message.c
index e6cdbc8f143782b56784239e6ebde8e16b552477..56499bb8158d69db9f3e1f14c3d0cd849786fcea 100644 (file)
@@ -55,7 +55,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();
@@ -70,10 +70,10 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                        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);
                        }
-                       
+
                        php_http_env_get_request_headers(&message->hdrs);
                        break;
-                       
+
                case PHP_HTTP_RESPONSE:
                        message = php_http_message_init(NULL, type, NULL);
                        if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line)) {
@@ -82,11 +82,12 @@ 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);
                        if (php_output_get_level()) {
                                if (php_output_get_status() & PHP_OUTPUT_SENT) {
                                        php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(), php_output_get_start_lineno());
+
                                        goto error;
                                } else if (SUCCESS != php_output_get_contents(&tval)) {
                                        php_error_docref(NULL, E_WARNING, "Could not fetch response body");
@@ -97,7 +98,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                                }
                        }
                        break;
-                       
+
                default:
                error:
                        if (free_msg) {
@@ -109,7 +110,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                        }
                        break;
        }
-       
+
        return message;
 }
 
@@ -233,11 +234,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;
                }
@@ -256,12 +257,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;
        }
@@ -305,6 +306,24 @@ void php_http_message_update_headers(php_http_message_t *msg)
                        zend_hash_str_del(&msg->hdrs, ZEND_STRL("Content-Length"));
                }
                zend_string_release(cl);
+       } else if (msg->type == PHP_HTTP_REQUEST) {
+               if (!php_http_message_header(msg, ZEND_STRL("Transfer-Encoding"))) {
+                       /* 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.
+                               */
+                               ZVAL_LONG(&h, 0);
+                               zend_hash_str_update(&msg->hdrs, "Content-Length", lenof("Content-Length"), &h);
+                       }
+               }
        }
 }
 
@@ -392,7 +411,7 @@ 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)
 {
        size_t i, c = php_http_message_count(msg);
-       
+
        if (c > 1) {
                php_http_message_t *tmp = msg, **arr;
 
@@ -405,11 +424,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;
 }
 
@@ -435,28 +454,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;
-       
+
        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));
                php_http_message_set_info(temp, &info);
                zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref);
-       
+
                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));
                        php_http_message_set_info(temp->parent, &info);
                        array_copy(&from->parent->hdrs, &temp->parent->hdrs);
-               
+
                        temp = temp->parent;
                        from = from->parent;
                }
        }
-       
+
        return copy;
 }
 
@@ -465,17 +484,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;
                }
@@ -494,6 +513,12 @@ void php_http_message_free(php_http_message_t **message)
        }
 }
 
+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 zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *rv);
 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot);
 
@@ -745,14 +770,14 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg
                        is_resource:
 
                        body = php_http_message_body_init(NULL, s);
-                       if (!(body_obj = php_http_message_body_object_new_ex(php_http_message_body_class_entry, body))) {
+                       if (!(body_obj = php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), body))) {
                                php_http_message_body_free(&body);
                                return FAILURE;
                        }
                        break;
 
                case IS_OBJECT:
-                       if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
+                       if (instanceof_function(Z_OBJCE_P(zbody), php_http_get_message_body_class_entry())) {
                                Z_ADDREF_P(zbody);
                                body_obj = PHP_HTTP_OBJ(NULL, zbody);
                                break;
@@ -789,7 +814,7 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg
 ZEND_RESULT_CODE php_http_message_object_init_body_object(php_http_message_object_t *obj)
 {
        php_http_message_body_addref(obj->message->body);
-       return php_http_new((void *) &obj->body, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body);
+       return php_http_new((void *) &obj->body, php_http_get_message_body_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body);
 }
 
 zend_object *php_http_message_object_new(zend_class_entry *ce)
@@ -810,7 +835,7 @@ php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce,
                if (msg->parent) {
                        o->parent = php_http_message_object_new_ex(ce, msg->parent);
                }
-               o->body = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL));
+               o->body = php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), php_http_message_body_init(&msg->body, NULL));
        }
 
        o->zo.handlers = &php_http_message_object_handlers;
@@ -920,7 +945,7 @@ static HashTable *php_http_message_object_get_debug_info(zval *object, int *is_t
 
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
        *is_temp = 0;
-       
+
 #define UPDATE_PROP(name_str, action_with_tmp) \
        do { \
                zend_property_info *pi; \
@@ -1003,13 +1028,13 @@ static PHP_METHOD(HttpMessage, __construct)
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage, &greedy), invalid_arg, return);
 
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh);
+       zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_message_class_entry(), &zeh);
        if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
                php_stream *s;
                php_http_message_parser_t p;
                zend_error_handling zeh;
 
-               zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
+               zend_replace_error_handling(EH_THROW, php_http_get_exception_unexpected_val_class_entry(), &zeh);
                php_stream_from_zval(s, zmessage);
                zend_restore_error_handling(&zeh);
 
@@ -1079,7 +1104,7 @@ static PHP_METHOD(HttpMessage, setBody)
 {
        zval *zbody;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody, php_http_message_body_class_entry)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody, php_http_get_message_body_class_entry())) {
                php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
 
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
@@ -1095,7 +1120,7 @@ static PHP_METHOD(HttpMessage, addBody)
 {
        zval *new_body;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body, php_http_message_body_class_entry)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body, php_http_get_message_body_class_entry())) {
                php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                php_http_message_body_object_t *new_obj = PHP_HTTP_OBJ(NULL, new_body);
 
@@ -1124,7 +1149,7 @@ static PHP_METHOD(HttpMessage, getHeader)
                if ((header = php_http_message_header(obj->message, header_str, header_len))) {
                        if (!header_ce) {
                                RETURN_ZVAL(header, 1, 0);
-                       } else if (instanceof_function(header_ce, php_http_header_class_entry)) {
+                       } else if (instanceof_function(header_ce, php_http_header_get_class_entry())) {
                                php_http_object_method_t cb;
                                zval argv[2];
 
@@ -1222,16 +1247,27 @@ 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);
-               zval *header;
+               zend_string *hstr, *vstr = php_http_header_value_to_string(zvalue);
+               zval tmp, *header;
 
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 
-               Z_TRY_ADDREF_P(zvalue);
-               if ((header = php_http_message_header(obj->message, name, name_len))) {
+               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);
-                       zend_hash_next_index_insert(Z_ARRVAL_P(header), zvalue);
+                       ZVAL_STR(&tmp, vstr);
+                       zend_hash_next_index_insert(Z_ARRVAL_P(header), &tmp);
                } else {
-                       zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
+                       ZVAL_STR(&tmp, vstr);
+                       zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp);
                }
                efree(name);
        }
@@ -1571,7 +1607,7 @@ static PHP_METHOD(HttpMessage, setRequestUrl)
                return;
        }
 
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
+       zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
        url = php_http_url_from_zval(zurl, ~0);
        zend_restore_error_handling(&zeh);
 
@@ -1970,8 +2006,6 @@ static zend_function_entry php_http_message_methods[] = {
        EMPTY_FUNCTION_ENTRY
 };
 
-zend_class_entry *php_http_message_class_entry;
-
 PHP_MINIT_FUNCTION(http_message)
 {
        zend_class_entry ce = {0};
@@ -2032,4 +2066,3 @@ PHP_MSHUTDOWN_FUNCTION(http_message)
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
-