branch off v1 as R_1_7
[m6w6/ext-http] / http_functions.c
index 4527c25388f0f8b16a0d215513e7d52649a76a14..8fc26f2cd4ef46b93ef2197daa68a845923e7cc3 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2007, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
@@ -43,6 +43,7 @@
 PHP_FUNCTION(http_date)
 {
        long t = -1;
+       char *date;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
                RETURN_FALSE;
@@ -52,7 +53,12 @@ PHP_FUNCTION(http_date)
                t = HTTP_G->request.time;
        }
 
-       RETURN_STRING(http_date(t), 0);
+       if (!(date = http_date(t))) {
+               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Could not compose date of timestamp %ld", t);
+               RETURN_FALSE;
+       }
+       
+       RETURN_STRING(date, 0);
 }
 /* }}} */
 
@@ -159,10 +165,22 @@ PHP_FUNCTION(http_build_str)
                        RETVAL_NULL(); \
                } \
        }
-#define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
-{ \
-       HashTable *result; \
-       if ((result = http_negotiate_ ##type(supported))) { \
+
+#define HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
+       HTTP_DO_NEGOTIATE_DEFAULT(supported); \
+       if (rs_array) { \
+               HashPosition pos; \
+               zval **value_ptr; \
+                \
+               FOREACH_VAL(pos, supported, value_ptr) { \
+                       zval *value = http_zsep(IS_STRING, *value_ptr); \
+                       add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \
+                       zval_ptr_dtor(&value); \
+               } \
+       }
+
+#define HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
+       { \
                char *key; \
                uint key_len; \
                ulong idx; \
@@ -179,21 +197,17 @@ PHP_FUNCTION(http_build_str)
                \
                zend_hash_destroy(result); \
                FREE_HASHTABLE(result); \
-               \
-       } else { \
-               HTTP_DO_NEGOTIATE_DEFAULT(supported); \
-               \
-               if (rs_array) { \
-                       HashPosition pos; \
-                       zval **value; \
-                        \
-                       FOREACH_VAL(pos, supported, value) { \
-                               convert_to_string_ex(value); \
-                               add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
-                       } \
+       }
+
+#define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
+       { \
+               HashTable *result; \
+               if ((result = http_negotiate_ ##type(supported))) { \
+                       HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
+               } else { \
+                       HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
                } \
-       } \
-}
+       }
 
 /* {{{ proto string http_negotiate_language(array supported[, array &result])
        Negotiate the clients preferred language. */
@@ -252,11 +266,35 @@ PHP_FUNCTION(http_negotiate_content_type)
 }
 /* }}} */
 
+/* {{{ proto string http_negotiate(mixed value, array supported[, array &result])
+       Negotiate the user supplied value. */
+PHP_FUNCTION(http_negotiate)
+{
+       zval *value, *supported, *rs_array = NULL;
+       HashTable *rs;
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za|z", &value, &supported, &rs_array)) {
+               RETURN_FALSE;
+       }
+
+       if (rs_array) {
+               zval_dtor(rs_array);
+               array_init(rs_array);
+       }
+
+       if ((rs = http_negotiate_z(value, Z_ARRVAL_P(supported), http_negotiate_default_func))) {
+               HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
+       } else {
+               HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
+       }
+}
+/* }}} */
+
 /* {{{ proto bool http_send_status(int status)
        Send HTTP status code. */
 PHP_FUNCTION(http_send_status)
 {
-       int status = 0;
+       long status = 0;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
                RETURN_FALSE;
@@ -675,10 +713,9 @@ PHP_FUNCTION(http_parse_cookie)
        if (allowed_extras_array) {
                allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
                FOREACH_VAL(pos, allowed_extras_array, entry) {
-                       ZVAL_ADDREF(*entry);
-                       convert_to_string_ex(entry);
-                       allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
-                       zval_ptr_dtor(entry);
+                       zval *data = http_zsep(IS_STRING, *entry);
+                       allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
+                       zval_ptr_dtor(&data);
                }
        }
        
@@ -856,7 +893,7 @@ PHP_FUNCTION(http_persistent_handles_ident)
                zval **bodyonly; \
                 \
                /* check if only the body should be returned */ \
-               if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
+               if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && i_zend_is_true(*bodyonly)) { \
                        http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
                         \
                        if (msg) { \