X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_cache_api.h;h=8a9e55d83c820dab7ab6ec9ff6b1ee712d9c94fa;hp=e5b618ff4dec3b59bee582146dfb2dae75aee9f9;hb=dad65c0a7d7ee7c090ae5d3c30409678adaa8cee;hpb=ae1297f5908f5683e7384395e5ae0e5a295be576 diff --git a/php_http_cache_api.h b/php_http_cache_api.h index e5b618f..8a9e55d 100644 --- a/php_http_cache_api.h +++ b/php_http_cache_api.h @@ -19,12 +19,140 @@ #define PHP_HTTP_CACHE_API_H #include "php_http_std_defs.h" +#include "php_http.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); +#include "zend_ini.h" + +#ifdef HAVE_LIBMHASH +# include +#endif + +ZEND_EXTERN_MODULE_GLOBALS(http); + +typedef enum { + HTTP_ETAG_MD5 = -2, + HTTP_ETAG_SHA1 = -1, + HTTP_ETAG_MHASH = 0, +} http_etag_mode; + +#ifdef HAVE_LIBMHASH +static void *http_etag_alloc_mhash_digest(size_t size) +{ + return emalloc(size); +} +#endif + +#define http_etag_digest(d, l) _http_etag_digest((d), (l) TSRMLS_CC) +static inline char *_http_etag_digest(const unsigned char *digest, int len TSRMLS_DC) +{ + int i; + char *hex = emalloc(len * 2 + 1); + char *ptr = hex; + + /* optimize this -- + look at apache's make_etag */ + for (i = 0; i < len; ++i) { + sprintf(ptr, "%02x", digest[i]); + ptr += 2; + } + *ptr = '\0'; + + return hex; +} + +#define http_etag_init() _http_etag_init(TSRMLS_C) +static inline void *_http_etag_init(TSRMLS_D) +{ + void *ctx; + long mode = INI_INT("http.etag_mode"); + + switch (mode) + { + case HTTP_ETAG_SHA1: + PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX))); + break; + + case HTTP_ETAG_MD5: +invalid_flag: + PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX))); + break; + + default: + { +#ifdef HAVE_LIBMHASH + if ((mode >= 0) && (mode <= mhash_count())) { + ctx = mhash_init(mode); + } + if ((!ctx) || (ctx == MHASH_FAILED)) +#endif + { + HTTP_G(etag).mode = HTTP_ETAG_MD5; + goto invalid_flag; + } + } + break; + } + + return ctx; +} + +#define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC) +static inline char *_http_etag_finish(void *ctx TSRMLS_DC) +{ + char *etag = NULL; + unsigned char digest[20]; + long mode = INI_INT("http.etag_mode"); + + switch (mode) + { + case HTTP_ETAG_SHA1: + PHP_SHA1Final(digest, ctx); + etag = http_etag_digest(digest, 20); + efree(ctx); + break; + + case HTTP_ETAG_MD5: + PHP_MD5Final(digest, ctx); + etag = http_etag_digest(digest, 16); + efree(ctx); + break; + + default: + { +#ifdef HAVE_LIBMHASH + unsigned char *mhash_digest = mhash_end_m(ctx, http_etag_alloc_mhash_digest); + etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode)); + efree(mhash_digest); +#endif + } + break; + } + + return etag; +} + +#define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC) +static inline void _http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC) +{ + switch (INI_INT("http.etag_mode")) + { + case HTTP_ETAG_SHA1: + PHP_SHA1Update(ctx, data_ptr, data_len); + break; + + case HTTP_ETAG_MD5: + PHP_MD5Update(ctx, data_ptr, data_len); + break; + + default: +#ifdef HAVE_LIBMHASH + mhash(ctx, data_ptr, data_len); +#endif + break; + } +} #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);