- unify script termination
authorMichael Wallner <mike@php.net>
Tue, 26 Apr 2005 19:48:03 +0000 (19:48 +0000)
committerMichael Wallner <mike@php.net>
Tue, 26 Apr 2005 19:48:03 +0000 (19:48 +0000)
- don't dllexport http_cache_exit()
- move know request methods over to http_std_defs
- introduce http.cache_log

http.c
http_api.c
http_cache_api.c
http_send_api.c
php_http.h
php_http_api.h
php_http_cache_api.h
php_http_std_defs.h

diff --git a/http.c b/http.c
index 494a1aae7a6d198c6808a320a9623cc487b500e3..6a28a5e4fd89690e9befe4c17bdb1f3cfc211378 100644 (file)
--- a/http.c
+++ b/http.c
@@ -37,6 +37,7 @@
 
 #include "php_http.h"
 #include "php_http_std_defs.h"
+#include "php_http_api.h"
 #include "php_http_send_api.h"
 
 #ifdef ZEND_ENGINE_2
@@ -143,8 +144,9 @@ static void free_to_free(void *s)
        efree(*(char **)s);
 }
 
-/* {{{ void _http_init_globals(zend_http_globals *) */
-static void _http_init_globals(zend_http_globals *http_globals)
+/* {{{ void http_globals_ctor(zend_http_globals *) */
+#define http_globals_ctor _http_globals_ctor
+static inline void _http_globals_ctor(zend_http_globals *http_globals)
 {
        http_globals->etag_started = 0;
        http_globals->ctype = NULL;
@@ -157,10 +159,37 @@ static void _http_init_globals(zend_http_globals *http_globals)
        zend_llist_init(&http_globals->to_free, sizeof(char *), free_to_free, 0);
 #endif
        http_globals->allowed_methods = NULL;
+       http_globals->cache_log = NULL;
 }
 /* }}} */
 
-/* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
+/* {{{ void http_globals_dtor() */
+#define http_globals_dtor() _http_globals_dtor(TSRMLS_C)
+static inline void _http_globals_dtor(TSRMLS_D)
+{
+       HTTP_G(etag_started) = 0;
+       HTTP_G(lmod) = 0;
+
+       if (HTTP_G(etag)) {
+               efree(HTTP_G(etag));
+               HTTP_G(etag) = NULL;
+       }
+
+       if (HTTP_G(ctype)) {
+               efree(HTTP_G(ctype));
+               HTTP_G(ctype) = NULL;
+       }
+       
+#ifdef HTTP_HAVE_CURL
+#      if LIBCURL_VERSION_NUM < 0x070c00
+       memset(&HTTP_G(curlerr), 0, sizeof(HTTP_G(curlerr)));
+#      endif
+#endif
+
+}
+/* }}} */
+
+/* {{{ static inline void http_check_allowed_methods(char *, int) */
 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
 {
@@ -178,34 +207,22 @@ static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_
 
        header = emalloc(length + sizeof("Allow: "));
        sprintf(header, "Allow: %s", methods);
-       http_send_header(header);
-       efree(header);
-       http_send_status(405);
-       zend_bailout();
+       http_exit(405, header);
 }
 /* }}} */
 
 /* {{{ PHP_INI */
-PHP_INI_MH(update_allowed_methods)
+PHP_INI_MH(http_update_allowed_methods)
 {
        http_check_allowed_methods(new_value, new_value_length);
        return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
 }
 
+#define http_update_cache_log OnUpdateString
+
 PHP_INI_BEGIN()
-       STD_PHP_INI_ENTRY("http.allowed_methods",
-               /* HTTP 1.1 */
-               "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, CONNECT, "
-               /* WebDAV - RFC 2518 * /
-               "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, "
-               /* WebDAV Versioning - RFC 3253 * /
-               "VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, "
-               "MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, "
-               /* WebDAV Access Control - RFC 3744 * /
-               "ACL, "
-               /* END */
-               ,
-               PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
+       HTTP_INI_ENTRY("http.allowed_methods", HTTP_KNOWN_METHODS, PHP_INI_ALL, allowed_methods)
+       HTTP_INI_ENTRY("http.cache_log", NULL, PHP_INI_ALL, cache_log)
 PHP_INI_END()
 /* }}} */
 
@@ -222,7 +239,7 @@ static void *http_curl_calloc(size_t n, size_t s)   { return ecalloc(n, s); }
 /* {{{ PHP_MINIT_FUNCTION */
 PHP_MINIT_FUNCTION(http)
 {
-       ZEND_INIT_MODULE_GLOBALS(http, _http_init_globals, NULL);
+       ZEND_INIT_MODULE_GLOBALS(http, http_globals_ctor, NULL);
        REGISTER_INI_ENTRIES();
 
 #ifdef HTTP_HAVE_CURL
@@ -275,25 +292,7 @@ PHP_RINIT_FUNCTION(http)
 /* {{{ PHP_RSHUTDOWN_FUNCTION */
 PHP_RSHUTDOWN_FUNCTION(http)
 {
-       HTTP_G(etag_started) = 0;
-       HTTP_G(lmod) = 0;
-
-       if (HTTP_G(etag)) {
-               efree(HTTP_G(etag));
-               HTTP_G(etag) = NULL;
-       }
-
-       if (HTTP_G(ctype)) {
-               efree(HTTP_G(ctype));
-               HTTP_G(ctype) = NULL;
-       }
-
-#ifdef HTTP_HAVE_CURL
-#      if LIBCURL_VERSION_NUM < 0x070c00
-       memset(&HTTP_G(curlerr), 0, sizeof(HTTP_G(curlerr)));
-#      endif
-#endif
-
+       http_globals_dtor();
        return SUCCESS;
 }
 /* }}} */
index e9f3bf093afaa037b5955ba10633e080cc8317d8..5a0dd3728a8a4f655bbb609d89b0aca73b071935 100644 (file)
@@ -27,6 +27,7 @@
 #include "php_http_std_defs.h"
 #include "php_http_api.h"
 #include "php_http_headers_api.h"
+#include "php_http_send_api.h"
 
 #ifdef ZEND_ENGINE_2
 #      include "php_http_exception_object.h"
@@ -81,6 +82,25 @@ void _http_error_ex(long type, long code, const char *format, ...)
 }
 /* }}} */
 
+/* {{{ STATUS http_exit(int, char*) */
+STATUS _http_exit_ex(int status, char *header, zend_bool free_header TSRMLS_DC)
+{
+       if (SUCCESS != http_send_status_header(status, header)) {
+               http_error_ex(E_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
+               if (free_header && header) {
+                       efree(header);
+               }
+               return FAILURE;
+       }
+       if (free_header && header) {
+               efree(header);
+       }
+       zend_bailout();
+       /* fake */
+       return SUCCESS;
+}
+/* }}} */
+
 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
 {
index 7fc25cf845773623d67cb00a96f054ad6739fe43..8d2b47f5e23cd29e9bfc76efaad131a2f56ccc45 100644 (file)
@@ -24,6 +24,8 @@
 #include "php_output.h"
 #include "ext/standard/md5.h"
 
+#include "SAPI.h"
+
 #include "php_http.h"
 #include "php_http_std_defs.h"
 #include "php_http_api.h"
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
+/* {{{ STATUS http_cache_exit(char *, zend_bool) */
+STATUS _http_cache_exit_ex(char *cache_token, zend_bool etag, zend_bool free_token TSRMLS_DC)
+{
+       if (HTTP_G(cache_log) && strlen(HTTP_G(cache_log))) {
+               php_stream *log = php_stream_open_wrapper(HTTP_G(cache_log), "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
+
+               if (log) {
+                       time_t now;
+                       struct tm nowtm;
+                       char datetime[128];
+
+                       time(&now);
+                       strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
+                       php_stream_printf(log TSRMLS_CC, "%s [%s] %32s %s\n", datetime, etag ? "ETAG":"LMOD", cache_token, SG(request_info).request_uri);
+                       php_stream_close(log);
+               }
+       }
+       if (free_token && cache_token) {
+               efree(cache_token);
+       }
+       return http_exit_ex(304, NULL, 0);
+}
+/* }}} */
+
 /* {{{ 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)
 {
@@ -159,7 +185,7 @@ PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
        }
 
        if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
-               return http_cache_exit();
+               return http_cache_exit(http_date(last_modified), 0);
        }
 
        return SUCCESS;
@@ -181,7 +207,7 @@ PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
                if (!http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
                        return SUCCESS;
                }
-               return http_cache_exit();
+               return http_cache_exit_ex(etag, 1, 0);
        }
 
        /* if no etag is given and we didn't already start ob_etaghandler -- start it */
@@ -194,20 +220,7 @@ PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
        } else {
                return FAILURE;
        }
-               
-}
-/* }}} */
 
-/* {{{ STATUS http_cache_exit() */
-PHP_HTTP_API STATUS _http_cache_exit(TSRMLS_D)
-{
-       if (SUCCESS != http_send_status(304)) {
-               http_error(E_WARNING, HTTP_E_HEADER, "Could not send 304 Not Modified");
-               return FAILURE;
-       }
-       /* TODO: cache_log */
-       zend_bailout();
-       return SUCCESS; /* fake */
 }
 /* }}} */
 
@@ -235,7 +248,7 @@ PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
                        http_send_etag(etag, 32);
 
                        if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
-                               http_cache_exit();
+                               http_cache_exit_ex(etag, 1, 0);
                        }
                }
        }
index 52e2b83f59883fa718e904155d0c7748c96f7279..0fa18daa01c546c12b34c120c5b318eda0e1586a 100644 (file)
@@ -27,6 +27,7 @@
 #include "php_http.h"
 #include "php_http_std_defs.h"
 #include "php_http_api.h"
+#include "php_http_date_api.h"
 #include "php_http_send_api.h"
 #include "php_http_headers_api.h"
 #include "php_http_date_api.h"
@@ -125,7 +126,7 @@ static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_
 PHP_HTTP_API STATUS _http_send_status_header(int status, const char *header TSRMLS_DC)
 {
        STATUS ret;
-       sapi_header_line h = {(char *) header, strlen(header), status};
+       sapi_header_line h = {(char *) header, header ? strlen(header) : 0, status};
        if (SUCCESS != (ret = sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC))) {
                http_error_ex(E_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status);
        }
@@ -361,15 +362,14 @@ PHP_HTTP_API STATUS _http_send(const void *data_ptr, size_t data_size, http_send
                        return FAILURE;
                }
                if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
-                       efree(etag);
-                       return http_cache_exit();
+                       return http_cache_exit(etag, 1, 1);
                }
                efree(etag);
        }
 
        /* send 304 Not Modified if last modified matches */
        if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
-               return http_cache_exit();
+               return http_cache_exit(http_date(HTTP_G(lmod)), 0, 1);
        }
 
        /* send full entity */
index 737721d74aae4b9e596f0fe312336068c97e4b8e..c2c8b106dbc94679263865a356d3a1314459714a 100644 (file)
@@ -41,6 +41,8 @@ ZEND_BEGIN_MODULE_GLOBALS(http)
        char *etag;
        time_t lmod;
        char *allowed_methods;
+       char *cache_log;
+
 #ifdef HTTP_HAVE_CURL
 #      if LIBCURL_VERSION_NUM < 0x070c00
        char curlerr[CURL_ERROR_SIZE + 1];
index 105f3d1673c065a93c4facb1e54921dfea535eef..3e596bac321a687632c3c584b924f0de82324ab2 100644 (file)
@@ -27,6 +27,10 @@ extern char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend
 #define http_error_ex _http_error_ex
 extern void _http_error_ex(long type, long code, const char *format, ...);
 
+#define http_exit(s, h) http_exit_ex((s), (h), 1)
+#define http_exit_ex(s, h, f) _http_exit_ex((s), (h), (f) TSRMLS_CC)
+extern STATUS _http_exit_ex(int status, char *header, zend_bool free_header TSRMLS_DC);
+
 #define HTTP_GSC(var, name, ret)  HTTP_GSP(var, name, return ret)
 #define HTTP_GSP(var, name, ret) \
                if (!(var = _http_get_server_var_ex(name, strlen(name)+1, 1 TSRMLS_CC))) { \
index 0a93f681f01efaea587e41f77a110d33ed9d4faa..e5b618ff4dec3b59bee582146dfb2dae75aee9f9 100644 (file)
 #define PHP_HTTP_CACHE_API_H
 
 #include "php_http_std_defs.h"
+#include "php_http_api.h"
 #include "php_http_send_api.h"
 
+#define http_cache_exit(t, e) http_cache_exit_ex((t), (e), 1)
+#define http_cache_exit_ex(t, e, f) _http_cache_exit_ex((t), (e), (f) TSRMLS_CC)
+extern STATUS _http_cache_exit_ex(char *cache_token, zend_bool etag, zend_bool free_token TSRMLS_DC);
+
 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
 
@@ -41,9 +46,6 @@ PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified, time_t send_
 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
 
-#define http_cache_exit() _http_cache_exit(TSRMLS_C)
-PHP_HTTP_API STATUS _http_cache_exit(TSRMLS_D);
-
 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
 
index a15a32acd61e11309b80ae91cf60ecb99d55b2ec..59edb29935f319185dc336a1c091b1e5fca75eaa 100644 (file)
@@ -59,9 +59,26 @@ typedef int STATUS;
 /* CURL buffer size */
 #define HTTP_CURLBUF_SIZE 16384
 
+/* known methods */
+#define HTTP_KNOWN_METHODS \
+               /* HTTP 1.1 */ \
+               "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, CONNECT, " \
+               /* WebDAV - RFC 2518 */ \
+               /* "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, " */ \
+               /* WebDAV Versioning - RFC 3253 */ \
+               /* "VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, " */ \
+               /* "MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, " */ \
+               /* WebDAV Access Control - RFC 3744 */ \
+               /* "ACL, " */ \
+               /* END */
+
+
 /* server vars shorthand */
 #define HTTP_SERVER_VARS Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])
 
+#define HTTP_INI_ENTRY(entry, default, scope, global) \
+       STD_PHP_INI_ENTRY(entry, default, scope, http_update_##global, global, zend_http_globals, http_globals)
+
 /* {{{ arrays */
 #define FOREACH_VAL(array, val) FOREACH_HASH_VAL(Z_ARRVAL_P(array), val)
 #define FOREACH_HASH_VAL(hash, val) \