#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
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;
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)
{
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()
/* }}} */
/* {{{ 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
/* {{{ 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;
}
/* }}} */
#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"
}
/* }}} */
+/* {{{ 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)
{
#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)
{
}
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;
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 */
} 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 */
}
/* }}} */
http_send_etag(etag, 32);
if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
- http_cache_exit();
+ http_cache_exit_ex(etag, 1, 0);
}
}
}
#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"
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);
}
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 */
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];
#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))) { \
#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);
#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);
/* 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) \