etag test & fixes; set default etag mode for temp streams to crc32(b)
[m6w6/ext-http] / php_http.c
index 47fba6412ac76eb2736c24867e75dce222c8609d..889cb09a46d1310f1e0c34ab33d36e6103da49f2 100644 (file)
@@ -6,23 +6,26 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http.c 300300 2010-06-09 07:29:35Z mike $ */
+#include "php_http_api.h"
 
-#include "php_http.h"
+#include <php_ini.h>
+#include <ext/standard/info.h>
 
 #include <zlib.h>
-#include <curl/curl.h>
-#ifdef PHP_HTTP_HAVE_EVENT
-#      include <event.h>
-#endif
 
-#include <main/php_ini.h>
-#include <ext/standard/info.h>
-#include <Zend/zend_extensions.h>
+#if PHP_HTTP_HAVE_CURL
+#      include <curl/curl.h>
+#      if PHP_HTTP_HAVE_EVENT
+#              include <event.h>
+#      endif
+#endif
+#if PHP_HTTP_HAVE_SERF
+#      include <serf.h>
+#endif
 
 ZEND_DECLARE_MODULE_GLOBALS(php_http);
 
@@ -71,6 +74,27 @@ zend_module_entry http_module_entry = {
 
 int http_module_number;
 
+#if PHP_DEBUG
+void _dpf(int type, const char *data, size_t length)
+{
+       static const char _sym[] = "><><><";
+       if (type) {
+               int nwp = 0;
+               for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
+                       int ip = PHP_HTTP_IS_CTYPE(print, *data);
+                       if (!ip && *data != '\r' && *data != '\n') nwp = 1;
+                       fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
+                       if (!nwp && *data == '\n' && length) {
+                               fprintf(stderr, "\n%c ", _sym[type-1]);
+                       }
+               }
+               fprintf(stderr, "\n");
+       } else {
+               fprintf(stderr, "# %.*s\n", (int) length, data);
+       }
+}
+#endif
+
 static void php_http_globals_init_once(zend_php_http_globals *G)
 {
        memset(G, 0, sizeof(*G));
@@ -86,27 +110,20 @@ static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
 
 #if defined(ZTS) && defined(PHP_DEBUG)
 #if ZTS && PHP_DEBUG
-zend_http_globals *php_http_globals(void)
+zend_php_http_globals *php_http_globals(void)
 {
        TSRMLS_FETCH();
        return PHP_HTTP_G;
 }
 #endif
 #endif
-PHP_INI_MH(http_update_persistent_handle_ident)
-{
-       PHP_HTTP_G->persistent_handle.ident.h = zend_hash_func(new_value, PHP_HTTP_G->persistent_handle.ident.l = new_value_length+1);
-       return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
-}
-
 PHP_INI_BEGIN()
-       PHP_HTTP_INI_ENTRY("http.etag.mode", "md5", PHP_INI_ALL, OnUpdateString, env.etag_mode)
+       PHP_HTTP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode)
        PHP_HTTP_INI_ENTRY("http.request_datashare.cookie", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.cookie)
        PHP_HTTP_INI_ENTRY("http.request_datashare.dns", "1", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.dns)
        PHP_HTTP_INI_ENTRY("http.request_datashare.ssl", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.ssl)
        PHP_HTTP_INI_ENTRY("http.request_datashare.connect", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.connect)
        PHP_HTTP_INI_ENTRY("http.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit)
-       PHP_HTTP_INI_ENTRY("http.persistent_handle.ident", "GLOBAL", PHP_INI_ALL, http_update_persistent_handle_ident, persistent_handle.ident.s)
 PHP_INI_END()
 
 PHP_MINIT_FUNCTION(http)
@@ -118,20 +135,27 @@ PHP_MINIT_FUNCTION(http)
        if (0
        || SUCCESS != PHP_MINIT_CALL(http_object)
        || SUCCESS != PHP_MINIT_CALL(http_exception)
+       || SUCCESS != PHP_MINIT_CALL(http_persistent_handle)
        || SUCCESS != PHP_MINIT_CALL(http_cookie)
        || SUCCESS != PHP_MINIT_CALL(http_encoding)
        || SUCCESS != PHP_MINIT_CALL(http_filter)
+       || SUCCESS != PHP_MINIT_CALL(http_header)
        || SUCCESS != PHP_MINIT_CALL(http_message)
        || SUCCESS != PHP_MINIT_CALL(http_message_body)
-       || SUCCESS != PHP_MINIT_CALL(http_persistent_handle)
        || SUCCESS != PHP_MINIT_CALL(http_property_proxy)
        || SUCCESS != PHP_MINIT_CALL(http_querystring)
+       || SUCCESS != PHP_MINIT_CALL(http_request_factory)
        || SUCCESS != PHP_MINIT_CALL(http_request)
+#if PHP_HTTP_HAVE_CURL
+       || SUCCESS != PHP_MINIT_CALL(http_curl)
+#endif
        || SUCCESS != PHP_MINIT_CALL(http_request_datashare)
        || SUCCESS != PHP_MINIT_CALL(http_request_method)
        || SUCCESS != PHP_MINIT_CALL(http_request_pool)
        || SUCCESS != PHP_MINIT_CALL(http_url)
        || SUCCESS != PHP_MINIT_CALL(http_env)
+       || SUCCESS != PHP_MINIT_CALL(http_env_response)
+       || SUCCESS != PHP_MINIT_CALL(http_params)
        ) {
                return FAILURE;
        }
@@ -147,8 +171,11 @@ PHP_MSHUTDOWN_FUNCTION(http)
        
        if (0
        || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
-       || SUCCESS != PHP_MSHUTDOWN_CALL(http_request)
+#if PHP_HTTP_HAVE_CURL
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
+#endif
        || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_datashare)
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_factory)
        || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle)
        ) {
                return FAILURE;
@@ -162,7 +189,9 @@ PHP_RINIT_FUNCTION(http)
        if (0
        || SUCCESS != PHP_RINIT_CALL(http_env)
        || SUCCESS != PHP_RINIT_CALL(http_request_datashare)
-       || SUCCESS != PHP_RINIT_CALL(http_request_pool)
+#if PHP_HTTP_HAVE_CURL
+       || SUCCESS != PHP_RINIT_CALL(http_curl)
+#endif
        ) {
                return FAILURE;
        }
@@ -185,25 +214,40 @@ PHP_RSHUTDOWN_FUNCTION(http)
 PHP_MINFO_FUNCTION(http)
 {
        php_info_print_table_start();
-       {
-               php_info_print_table_header(2, "HTTP Support", "enabled");
-               php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
-       }
+       php_info_print_table_header(2, "HTTP Support", "enabled");
+       php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
        php_info_print_table_end();
        
        php_info_print_table_start();
        php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
+       php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
+#if PHP_HTTP_HAVE_CURL
        {
                curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
-               php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
                php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
-#ifdef PHP_HTTP_HAVE_EVENT
-               php_info_print_table_row(3, "libevent", PHP_HTTP_EVENT_VERSION, event_get_version());
+       }
 #else
-               php_info_print_table_row(3, "libevent", "disabled", "disabled");
+       php_info_print_table_row(3, "libcurl", "disabled", "disabled");
 #endif
-               php_info_print_table_row(3, "libz", "disabled", "disabled");
+
+#if PHP_HTTP_HAVE_EVENT
+       php_info_print_table_row(3, "libevent", PHP_HTTP_EVENT_VERSION, event_get_version());
+#else
+       php_info_print_table_row(3, "libevent", "disabled", "disabled");
+#endif
+
+#if PHP_HTTP_HAVE_SERF
+       {
+               int v[3];
+               char sl_v[16] = {0};
+
+               serf_lib_version(&v[0], &v[1], &v[2]);
+               slprintf(sl_v, lenof(sl_v), "%d.%d.%d", v[0], v[1], v[2]);
+               php_info_print_table_row(3, "libserf", SERF_VERSION_STRING, sl_v);
        }
+#else
+       php_info_print_table_row(3, "libserf", "disabled", "disabled");
+#endif
        php_info_print_table_end();
        
        php_info_print_table_start();
@@ -221,8 +265,8 @@ PHP_MINFO_FUNCTION(http)
                                        FOREACH_KEYVAL(pos2, *val, ident, sub) {
                                                if (    SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("used"), (void *) &zused) &&
                                                                SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("free"), (void *) &zfree)) {
-                                                       zval *used = php_http_zsep(IS_STRING, *zused);
-                                                       zval *free = php_http_zsep(IS_STRING, *zfree);
+                                                       zval *used = php_http_ztyp(IS_STRING, *zused);
+                                                       zval *free = php_http_ztyp(IS_STRING, *zfree);
                                                        php_info_print_table_row(4, provider.str, ident.str, Z_STRVAL_P(used), Z_STRVAL_P(free));
                                                        zval_ptr_dtor(&used);
                                                        zval_ptr_dtor(&free);