- improve internal array handling
[m6w6/ext-http] / http_message_object.c
index 3d736cdaaafa4d19d8b151bb17abe84d32fca23b..495cd28759e0f5728713edfae56b7a9a23dba142 100644 (file)
@@ -14,6 +14,7 @@
 
 #define HTTP_WANT_SAPI
 #define HTTP_WANT_CURL
+#define HTTP_WANT_MAGIC
 #include "php_http.h"
 
 #ifdef ZEND_ENGINE_2
@@ -32,7 +33,7 @@
 #include "php_http_request_api.h"
 #include "php_http_request_object.h"
 
-#if HTTP_HAVE_EXT(SPL) && !defined(WONKY)
+#if defined(HTTP_HAVE_SPL) && !defined(WONKY)
 /* SPL doesn't install its headers */
 extern PHPAPI zend_class_entry *spl_ce_Countable;
 #endif
@@ -54,6 +55,10 @@ HTTP_BEGIN_ARGS(setBody, 1)
        HTTP_ARG_VAL(body, 0)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(getHeader, 1)
+       HTTP_ARG_VAL(header, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getHeaders);
 HTTP_BEGIN_ARGS(setHeaders, 1)
        HTTP_ARG_VAL(headers, 0)
@@ -94,6 +99,11 @@ HTTP_BEGIN_ARGS(setHttpVersion, 1)
        HTTP_ARG_VAL(http_version, 0)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(guessContentType, 1)
+       HTTP_ARG_VAL(magic_file, 0)
+       HTTP_ARG_VAL(magic_mode, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getParentMessage);
 HTTP_EMPTY_ARGS(send);
 HTTP_BEGIN_ARGS(toString, 0)
@@ -134,6 +144,7 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(getHeader, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
@@ -149,6 +160,7 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(guessContentType, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
@@ -185,7 +197,7 @@ PHP_MINIT_FUNCTION(http_message_object)
        HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
        
 #ifndef WONKY
-#      if HTTP_HAVE_EXT(SPL)
+#      ifdef HTTP_HAVE_SPL
        zend_class_implements(http_message_object_ce TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
 #      else
        zend_class_implements(http_message_object_ce TSRMLS_CC, 2, zend_ce_serializable, zend_ce_iterator);
@@ -338,7 +350,8 @@ zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message
        }
 
        ALLOC_HASHTABLE(OBJ_PROP(o));
-       zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 
        ov.handle = putObject(http_message_object, o);
        ov.handlers = &http_message_object_handlers;
@@ -348,18 +361,20 @@ zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message
 
 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
 {
-       getObject(http_message_object, obj);
-       return http_message_object_new_ex(Z_OBJCE_P(this_ptr), http_message_dup(obj->message), NULL);
+       zend_object_value new_ov;
+       http_message_object *new_obj = NULL;
+       getObject(http_message_object, old_obj);
+       
+       new_ov = http_message_object_new_ex(old_obj->zo.ce, http_message_dup(old_obj->message), &new_obj);
+       zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
+       
+       return new_ov;
 }
 
 void _http_message_object_free(zend_object *object TSRMLS_DC)
 {
        http_message_object *o = (http_message_object *) object;
 
-       if (OBJ_PROP(o)) {
-               zend_hash_destroy(OBJ_PROP(o));
-               FREE_HASHTABLE(OBJ_PROP(o));
-       }
        if (o->message) {
                http_message_dtor(o->message);
                efree(o->message);
@@ -372,7 +387,7 @@ void _http_message_object_free(zend_object *object TSRMLS_DC)
                p.value.obj = o->parent;
                zend_objects_store_del_ref(&p TSRMLS_CC);
        }
-       efree(o);
+       freeObject(o);
 }
 
 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
@@ -622,13 +637,13 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                case HTTP_MSG_REQUEST:
                        ASSOC_PROP(array, long, "responseCode", 0);
                        ASSOC_STRINGL(array, "responseStatus", "", 0);
-                       ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
-                       ASSOC_STRING(array, "requestUrl", msg->http.info.request.url);
+                       ASSOC_STRING(array, "requestMethod", STR_PTR(msg->http.info.request.method));
+                       ASSOC_STRING(array, "requestUrl", STR_PTR(msg->http.info.request.url));
                        break;
 
                case HTTP_MSG_RESPONSE:
                        ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
-                       ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
+                       ASSOC_STRING(array, "responseStatus", STR_PTR(msg->http.info.response.status));
                        ASSOC_STRINGL(array, "requestMethod", "", 0);
                        ASSOC_STRINGL(array, "requestUrl", "", 0);
                        break;
@@ -776,6 +791,31 @@ PHP_METHOD(HttpMessage, setBody)
 }
 /* }}} */
 
+/* {{{ proto string HttpMessage::getHeader(string header)
+ *
+ * Get message header.
+ *
+ * Returns the header value on success or NULL if the header does not exist.
+ */
+PHP_METHOD(HttpMessage, getHeader)
+{
+       zval *header;
+       char *orig_header, *nice_header;
+       int header_len;
+       getObject(http_message_object, obj);
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &orig_header, &header_len)) {
+               RETURN_FALSE;
+       }
+       
+       nice_header = pretty_key(estrndup(orig_header, header_len), header_len, 1, 1);
+       if ((header = http_message_header_ex(obj->message, nice_header, header_len + 1, 0))) {
+               RETVAL_ZVAL(header, 1, 1);
+       }
+       efree(nice_header);
+}
+/* }}} */
+
 /* {{{ proto array HttpMessage::getHeaders()
  *
  * Get Message Headers.
@@ -787,12 +827,10 @@ PHP_METHOD(HttpMessage, getHeaders)
        NO_ARGS;
 
        if (return_value_used) {
-               zval headers;
                getObject(http_message_object, obj);
 
-               INIT_ZARR(headers, &obj->message->hdrs);
                array_init(return_value);
-               array_copy(&headers, return_value);
+               array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
        }
 }
 /* }}} */
@@ -806,16 +844,17 @@ PHP_METHOD(HttpMessage, getHeaders)
  */
 PHP_METHOD(HttpMessage, setHeaders)
 {
-       zval *new_headers, old_headers;
+       zval *new_headers = NULL;
        getObject(http_message_object, obj);
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
                return;
        }
 
        zend_hash_clean(&obj->message->hdrs);
-       INIT_ZARR(old_headers, &obj->message->hdrs);
-       array_copy(new_headers, &old_headers);
+       if (new_headers) {
+               array_copy(Z_ARRVAL_P(new_headers), &obj->message->hdrs);
+       }
 }
 /* }}} */
 
@@ -831,7 +870,7 @@ PHP_METHOD(HttpMessage, setHeaders)
  */
 PHP_METHOD(HttpMessage, addHeaders)
 {
-       zval old_headers, *new_headers;
+       zval *new_headers;
        zend_bool append = 0;
        getObject(http_message_object, obj);
 
@@ -839,12 +878,7 @@ PHP_METHOD(HttpMessage, addHeaders)
                return;
        }
 
-       INIT_ZARR(old_headers, &obj->message->hdrs);
-       if (append) {
-               array_append(new_headers, &old_headers);
-       } else {
-               array_merge(new_headers, &old_headers);
-       }
+       array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
 }
 /* }}} */
 
@@ -1126,6 +1160,41 @@ PHP_METHOD(HttpMessage, setHttpVersion)
 }
 /* }}} */
 
+/* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
+ *
+ * Attempts to guess the content type of supplied payload through libmagic.
+ * 
+ * Expects a string parameter specifying the magic.mime database to use.
+ * Additionally accepts an optional int parameter, being flags for libmagic.
+ * 
+ * Returns the guessed content type on success, or FALSE on failure.
+ * 
+ * Throws HttpRuntimeException, HttpInvalidParamException 
+ * if http.only_exceptions is TRUE.
+ */
+PHP_METHOD(HttpMessage, guessContentType)
+{
+#ifdef HTTP_HAVE_MAGIC
+       char *magic_file, *ct = NULL;
+       int magic_file_len;
+       long magic_mode = MAGIC_MIME;
+       
+       RETVAL_FALSE;
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
+               getObject(http_message_object, obj);
+               if ((ct = http_guess_content_type(magic_file, magic_mode, PHPSTR_VAL(&obj->message->body), PHPSTR_LEN(&obj->message->body), SEND_DATA))) {
+                       RETVAL_STRING(ct, 0);
+               }
+       }
+       SET_EH_NORMAL();
+#else
+       http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
+       RETURN_FALSE;
+#endif
+}
+/* }}} */
+
 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
  *
  * Get parent Message.
@@ -1221,7 +1290,7 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
 #ifdef HTTP_HAVE_CURL
                                int method;
                                char *url;
-                               zval tmp, body, *array, *headers, *host = http_message_header(obj->message, "Host");
+                               zval body, *array, *headers, *host = http_message_header(obj->message, "Host");
                                php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
                                
                                MAKE_STD_ZVAL(array);
@@ -1229,6 +1298,7 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                                
                                memset(&hurl, 0, sizeof(php_url));
                                hurl.host = host ? Z_STRVAL_P(host) : NULL;
+                               zval_ptr_dtor(&host);
                                http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
                                php_url_free(purl);
                                add_assoc_string(array, "url", url, 0);
@@ -1244,8 +1314,7 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                                
                                MAKE_STD_ZVAL(headers);
                                array_init(headers);
-                               INIT_ZARR(tmp, &obj->message->hdrs);
-                               array_copy(&tmp, headers);
+                               array_copy(&obj->message->hdrs, Z_ARRVAL_P(headers));
                                add_assoc_zval(array, "headers", headers);
                                
                                object_init_ex(return_value, http_request_object_ce);
@@ -1265,9 +1334,7 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                        {
 #ifndef WONKY
                                HashPosition pos1, pos2;
-                               ulong idx;
-                               uint key_len;
-                               char *key = NULL;
+                               HashKey key = initHashKey(0);
                                zval **header, **h, *body;
                                
                                if (obj->message->http.info.response.code) {
@@ -1276,30 +1343,30 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                                
                                object_init_ex(return_value, http_response_object_ce);
                                
-                               FOREACH_HASH_KEYLENVAL(pos1, &obj->message->hdrs, key, key_len, idx, header) {
-                                       if (key) {
-                                               zval zkey;
+                               FOREACH_HASH_KEYVAL(pos1, &obj->message->hdrs, key, header) {
+                                       if (key.type == HASH_KEY_IS_STRING) {
+                                               zval *zkey;
                                                
-                                               INIT_PZVAL(&zkey);
-                                               ZVAL_STRINGL(&zkey, key, key_len, 0);
+                                               MAKE_STD_ZVAL(zkey);
+                                               ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
                                                
                                                switch (Z_TYPE_PP(header)) {
                                                        case IS_ARRAY:
                                                        case IS_OBJECT:
                                                                FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
                                                                        ZVAL_ADDREF(*h);
-                                                                       zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *h);
+                                                                       zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *h);
                                                                        zval_ptr_dtor(h);
                                                                }
                                                                break;
                                                        
                                                        default:
                                                                ZVAL_ADDREF(*header);
-                                                               zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *header);
+                                                               zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *header);
                                                                zval_ptr_dtor(header);
                                                                break;
                                                }
-                                               key = NULL;
+                                               zval_ptr_dtor(&zkey);
                                        }
                                }