X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_cache_api.h;h=14ae720d0886ce9829f430a744db8d2dc3fc554f;hp=4f1695741c04715e4ab66c9e5af01eb2c3247599;hb=2e1cd7f9942bb07d7c3a2efb79090215fc1406d6;hpb=0ac32c9b8590e88a5f110cc8b3154001d5c0c089 diff --git a/php_http_cache_api.h b/php_http_cache_api.h index 4f16957..14ae720 100644 --- a/php_http_cache_api.h +++ b/php_http_cache_api.h @@ -18,22 +18,185 @@ #ifndef PHP_HTTP_CACHE_API_H #define PHP_HTTP_CACHE_API_H +#include "zend_ini.h" + +#include "ext/standard/md5.h" +#include "ext/standard/sha1.h" +#include "ext/standard/crc32.h" + #include "php_http_std_defs.h" +#include "php_http.h" +#include "php_http_api.h" #include "php_http_send_api.h" +#ifdef HTTP_HAVE_MHASH +# include +#endif + +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_CRC32 = -3, + HTTP_ETAG_MD5 = -2, + HTTP_ETAG_SHA1 = -1, +} http_etag_mode; + +#ifdef HTTP_HAVE_MHASH +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 = NULL; + long mode = INI_INT("http.etag_mode"); + + switch (mode) + { + case HTTP_ETAG_CRC32: + ctx = emalloc(sizeof(unsigned int)); + memset(ctx, 1, sizeof(unsigned int)); + break; + + case HTTP_ETAG_SHA1: + PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX))); + break; + + case HTTP_ETAG_MD5: +#ifndef HTTP_HAVE_MHASH + default: +#endif + PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX))); + break; + +#ifdef HTTP_HAVE_MHASH + default: + if ((mode < 0) || ((ulong)mode > mhash_count()) || (!(ctx = mhash_init(mode)))) { + http_error_ex(HE_ERROR, HTTP_E_RUNTIME, "Invalid ETag mode: %ld", mode); + } + break; +#endif + } + + 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_CRC32: + *((unsigned int *) ctx) = ~*((unsigned int *) ctx); + etag = http_etag_digest((const unsigned char *) ctx, sizeof(unsigned int)); + efree(ctx); + break; + + case HTTP_ETAG_SHA1: + PHP_SHA1Final(digest, ctx); + etag = http_etag_digest(digest, 20); + efree(ctx); + break; + + case HTTP_ETAG_MD5: +#ifndef HTTP_HAVE_MHASH + default: +#endif + PHP_MD5Final(digest, ctx); + etag = http_etag_digest(digest, 16); + efree(ctx); + break; + +#ifdef HTTP_HAVE_MHASH + default: + { + 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); + } + break; +#endif + } + + 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_CRC32: + { + unsigned int i, c = *((unsigned int *) ctx); + + for (i = 0; i < data_len; ++i) { + c = CRC32(c, data_ptr[i]); + } + *((unsigned int *)ctx) = c; + } + break; + + case HTTP_ETAG_SHA1: + PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len); + break; + + case HTTP_ETAG_MD5: +#ifndef HTTP_HAVE_MHASH + default: +#endif + PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len); + break; + +#ifdef HTTP_HAVE_MHASH + default: + mhash(ctx, data_ptr, data_len); + break; +#endif + } +} + #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_lmod(p, m) _http_lmod((p), (m) TSRMLS_CC) -PHP_HTTP_API time_t _http_lmod(const void *data_ptr, http_send_mode data_mode TSRMLS_DC); +#define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC) +PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC); -#define http_modified_match(entry, modified) _http_modified_match_ex((entry), (modified), 1 TSRMLS_CC) -#define http_modified_match_ex(entry, modified, ep) _http_modified_match_ex((entry), (modified), (ep) TSRMLS_CC) -PHP_HTTP_API zend_bool _http_modified_match_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC); +#define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC) +#define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC) +PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC); -#define http_etag_match(entry, etag) _http_etag_match_ex((entry), (etag), 1 TSRMLS_CC) -#define http_etag_match_ex(entry, etag, ep) _http_etag_match_ex((entry), (etag), (ep) TSRMLS_CC) -PHP_HTTP_API zend_bool _http_etag_match_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC); +#define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC) +#define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC) +PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC); #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC) PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified, time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC); @@ -41,6 +204,9 @@ 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_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); + #endif /*