- move specific initialization code to the respective modules
authorMichael Wallner <mike@php.net>
Wed, 14 Sep 2005 15:19:15 +0000 (15:19 +0000)
committerMichael Wallner <mike@php.net>
Wed, 14 Sep 2005 15:19:15 +0000 (15:19 +0000)
- expose ETAG_MHASH constants

http.c
http_cache_api.c
http_request_api.c
http_response_object.c
php_http_cache_api.h
php_http_request_api.h

diff --git a/http.c b/http.c
index a174960db9ff46d018feda0ffb76ab67a1b795a4..b14befebaee917925b89edb31ced7549a479eefd 100644 (file)
--- a/http.c
+++ b/http.c
 #include "phpstr/phpstr.h"
 
 #ifdef HTTP_HAVE_CURL
-#      if defined(ZTS) && defined(HTTP_HAVE_SSL)
-#              if !defined(HAVE_OPENSSL_CRYPTO_H)
-#                      error "libcurl was compiled with OpenSSL support, but we have no crypto.h"
-#              else
-#                      define HTTP_NEED_SSL
-#                      include <openssl/crypto.h>
-#              endif
-#      endif
 #      ifdef PHP_WIN32
 #              include <winsock2.h>
 #      endif
@@ -232,7 +224,7 @@ PHP_INI_DISP(http_etag_mode_displayer)
                        if (!hash_name) {
                                ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
                        } else {
-                               ZEND_WRITE("HTTP_ETAG_MHASH|MHASH_", lenof("HTTP_ETAG_MHASH|MHASH_"));
+                               ZEND_WRITE("HTTP_ETAG_MHASH_", lenof("HTTP_ETAG_MHASH_"));
                                ZEND_WRITE(hash_name, strlen(hash_name));
                        }
                }
@@ -261,46 +253,6 @@ PHP_INI_END()
 /* {{{ SSL */
 #ifdef HTTP_NEED_SSL
 
-static MUTEX_T *http_ssl_mutex = NULL;
-
-static void http_ssl_lock(int mode, int n, const char * file, int line)
-{
-       if (mode & CRYPTO_LOCK) {
-               tsrm_mutex_lock(http_ssl_mutex[n]);
-       } else {
-               tsrm_mutex_unlock(http_ssl_mutex[n]);
-       }
-}
-static unsigned long http_ssl_id(void)
-{
-       return (unsigned long) tsrm_thread_id();
-}
-static inline void http_ssl_init(void)
-{
-       int i, c = CRYPTO_num_locks();
-       http_ssl_mutex = malloc(c * sizeof(MUTEX_T));
-       
-       for (i = 0; i < c; ++i) {
-               http_ssl_mutex[i] = tsrm_mutex_alloc();
-       }
-       
-       CRYPTO_set_id_callback(http_ssl_id);
-       CRYPTO_set_locking_callback(http_ssl_lock);
-}
-static inline void http_ssl_cleanup(void)
-{
-       int i, c = CRYPTO_num_locks();
-       
-       CRYPTO_set_id_callback(NULL);
-       CRYPTO_set_locking_callback(NULL);
-       
-       for (i = 0; i < c; ++i) {
-               tsrm_mutex_free(http_ssl_mutex[i]);
-       }
-       
-       free(http_ssl_mutex);
-       http_ssl_mutex = NULL;
-}
 #endif
 /* }}} */
 
@@ -313,19 +265,11 @@ PHP_MINIT_FUNCTION(http)
 
        REGISTER_INI_ENTRIES();
        
-       HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5);
-       HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1);
-       HTTP_LONG_CONSTANT("HTTP_ETAG_MHASH", HTTP_ETAG_MHASH);
-
+       if (SUCCESS != http_cache_global_init()) {
+               return FAILURE;
+       }
 #ifdef HTTP_HAVE_CURL
-       if (CURLE_OK == curl_global_init(CURL_GLOBAL_ALL)) {
-#      ifdef HTTP_NEED_SSL
-               curl_version_info_data *cvid = curl_version_info(CURLVERSION_NOW);
-               if (cvid && (cvid->features & CURL_VERSION_SSL)) {
-                       http_ssl_init();
-               }
-#      endif
-       } else {
+       if (SUCCESS != http_request_global_init()) {
                return FAILURE;
        }
 #endif /* HTTP_HAVE_CURL */
@@ -352,12 +296,7 @@ PHP_MSHUTDOWN_FUNCTION(http)
 {
        UNREGISTER_INI_ENTRIES();
 #ifdef HTTP_HAVE_CURL
-       curl_global_cleanup();
-#      ifdef HTTP_NEED_SSL
-       if (http_ssl_mutex) {
-               http_ssl_cleanup();
-       }
-#      endif
+       http_request_global_cleanup();
 #endif
        return SUCCESS;
 }
index 9b2b8aacfacbdd3c03171affdb8e054b4a5a7784..c7196c1891acb0018cadadce19e1cde8bcedff5f 100644 (file)
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
+STATUS _http_cache_global_init(INIT_FUNC_ARGS)
+{
+       HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5);
+       HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1);
+
+#ifdef HTTP_HAVE_MHASH
+       {
+               int l, i, c = mhash_count();
+               
+               for (i = 0; i < c; ++i) {
+                       char const_name[256] = {0};
+                       const char *hash_name = mhash_get_hash_name_static(i);
+                       
+                       if (hash_name) {
+                               l = snprintf(const_name, 255, "HTTP_ETAG_MHASH_%s", hash_name);
+                               zend_register_long_constant(const_name, l + 1, i, CONST_CS|CONST_PERSISTENT, module_number TSRMLS_CC);
+                       }
+               }
+       }
+#endif
+
+       return SUCCESS;
+}
+
 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
 {
index 2af5774c131f27e1ebbfb31355e6b0fb1ab5a366..748cede69b2ace8d653f865a5c2f6f883eb17bbf 100644 (file)
 
 #ifdef HTTP_HAVE_CURL
 
+#if defined(ZTS) && defined(HTTP_HAVE_SSL)
+#      if !defined(HAVE_OPENSSL_CRYPTO_H)
+#              error "libcurl was compiled with OpenSSL support, but we have no openssl/crypto.h"
+#      else
+#              define HTTP_NEED_SSL
+#              include <openssl/crypto.h>
+#      endif
+#endif
+
 #include "php_http.h"
 #include "php_http_std_defs.h"
 #include "php_http_api.h"
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
+#ifdef HTTP_NEED_SSL
+static inline void http_ssl_init(void);
+static inline void http_ssl_cleanup(void);
+#endif
+
+STATUS _http_request_global_init(INIT_FUNC_ARGS)
+{
+       if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
+               return FAILURE;
+       }
+       
+#ifdef HTTP_NEED_SSL
+       {
+               curl_version_info_data *cvid = curl_version_info(CURLVERSION_NOW);
+               if (cvid && (cvid->features & CURL_VERSION_SSL)) {
+                       http_ssl_init();
+               }
+       }
+#endif
+
+       return SUCCESS;
+}
+
+void _http_request_global_cleanup(TSRMLS_D)
+{
+       curl_global_cleanup();
+#ifdef HTTP_NEED_SSL
+       http_ssl_cleanup();
+#endif
+}
+
 #ifndef HAVE_CURL_EASY_STRERROR
 #      define curl_easy_strerror(code) HTTP_G(request).error
 #endif
@@ -108,7 +148,6 @@ static int http_curl_progress_callback(void *, double, double, double, double);
 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
 
-
 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
 http_request_callback_ctx *_http_request_callback_data_ex(void *data, zend_bool cpy TSRMLS_DC)
 {
@@ -848,7 +887,56 @@ static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t k
 }
 /* }}} */
 
-#endif
+#ifdef HTTP_NEED_SSL
+
+static MUTEX_T *http_ssl_mutex = NULL;
+
+static void http_ssl_lock(int mode, int n, const char * file, int line)
+{
+       if (mode & CRYPTO_LOCK) {
+               tsrm_mutex_lock(http_ssl_mutex[n]);
+       } else {
+               tsrm_mutex_unlock(http_ssl_mutex[n]);
+       }
+}
+
+static unsigned long http_ssl_id(void)
+{
+       return (unsigned long) tsrm_thread_id();
+}
+
+static inline void http_ssl_init(void)
+{
+       int i, c = CRYPTO_num_locks();
+       http_ssl_mutex = malloc(c * sizeof(MUTEX_T));
+       
+       for (i = 0; i < c; ++i) {
+               http_ssl_mutex[i] = tsrm_mutex_alloc();
+       }
+       
+       CRYPTO_set_id_callback(http_ssl_id);
+       CRYPTO_set_locking_callback(http_ssl_lock);
+}
+
+static inline void http_ssl_cleanup(void)
+{
+       if (http_ssl_mutex) {
+               int i, c = CRYPTO_num_locks();
+               
+               CRYPTO_set_id_callback(NULL);
+               CRYPTO_set_locking_callback(NULL);
+               
+               for (i = 0; i < c; ++i) {
+                       tsrm_mutex_free(http_ssl_mutex[i]);
+               }
+               
+               free(http_ssl_mutex);
+               http_ssl_mutex = NULL;
+       }
+}
+#endif /* HTTP_NEED_SSL */
+
+#endif /* HTTP_HAVE_CURL */
 
 /*
  * Local variables:
index 9ef4fc93f32773be4ad76c87e59605398f33084c..f2c5e5730d13a262b5967385f5dc937b259640c1 100644 (file)
 #include "php_http_cache_api.h"
 #include "php_http_headers_api.h"
 
+#ifdef HTTP_HAVE_MHASH
+#      include <mhash.h>
+#endif
+
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
 #define GET_STATIC_PROP(n)                     *GET_STATIC_PROP_EX(http_response_object_ce, n)
@@ -232,11 +236,31 @@ static inline void _http_response_object_declare_default_properties(TSRMLS_D)
        DCL_STATIC_PROP_N(PROTECTED, contentDisposition);
        DCL_STATIC_PROP(PROTECTED, long, bufferSize, HTTP_SENDBUF_SIZE);
        DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0);
+
+       DCL_CONST(long, "ETAG_MD5", HTTP_ETAG_MD5);
+       DCL_CONST(long, "ETAG_SHA1", HTTP_ETAG_SHA1);
+       
+#ifdef HTTP_HAVE_MHASH
+       {
+               int l, i, c = mhash_count();
+               
+               for (i = 0; i < c; ++i) {
+                       char const_name[256] = {0};
+                       const char *hash_name = mhash_get_hash_name_static(i);
+                       
+                       if (hash_name) {
+                               l = snprintf(const_name, 255, "ETAG_MHASH_%s", hash_name);
+                               zend_declare_class_constant_long(ce, const_name, l, i TSRMLS_CC);
+                       }
+               }
+       }
+#endif
 }
 
 static void _http_grab_response_headers(void *data, void *arg TSRMLS_DC)
 {
-       phpstr_appendf(PHPSTR(arg), "%s\r\n", ((sapi_header_struct *)data)->header);
+       phpstr_appendl(PHPSTR(arg), ((sapi_header_struct *)data)->header);
+       phpstr_appends(PHPSTR(arg), HTTP_CRLF);
 }
 
 /* ### USERLAND ### */
@@ -476,7 +500,6 @@ PHP_METHOD(HttpResponse, getContentType)
 /* {{{ proto static string HttpResponse::guessContentType(string magic_file[, long magic_mode])
  *
  * Attempts to guess the content type of supplied payload through libmagic.
- * See docs/KnownIssues.txt! 
  */
 PHP_METHOD(HttpResponse, guessContentType)
 {
index 4f067f5a0c72c5fcb8936faa3e80f3f3a52b95f8..d7e53b77b41bc5c073ae52e6ef3c66a750919387 100644 (file)
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
+#define http_cache_global_init() _http_cache_global_init(INIT_FUNC_ARGS_PASSTHRU)
+extern STATUS _http_cache_global_init(INIT_FUNC_ARGS);
+
 typedef enum {
        HTTP_ETAG_MD5 = -2,
        HTTP_ETAG_SHA1 = -1,
-       HTTP_ETAG_MHASH = 0,
 } http_etag_mode;
 
 #ifdef HTTP_HAVE_MHASH
index 3b773a3edbb939ba9d036449d7f81ef31526be68..3ff2769a9db82413c91d59663f598fd0a07d7904 100644 (file)
 
 #include <curl/curl.h>
 
+#define http_request_global_init() _http_request_global_init(INIT_FUNC_ARGS_PASSTHRU)
+extern STATUS _http_request_global_init(INIT_FUNC_ARGS);
+#define http_request_global_cleanup() _http_request_global_cleanup(TSRMLS_C)
+extern void _http_request_global_cleanup(TSRMLS_D);
+
 #define HTTP_REQUEST_BODY_CSTRING              1
 #define HTTP_REQUEST_BODY_CURLPOST             2
 #define HTTP_REQUEST_BODY_UPLOADFILE   3
@@ -50,7 +55,6 @@ typedef struct {
        curl_infotype last_info;
 } http_request_conv;
 
-
 #define HTTP_REQUEST_CALLBACK_DATA(from, type, var) \
        http_request_callback_ctx *__CTX = (http_request_callback_ctx *) (from); \
        TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \