X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_cache_api.h;h=8df20a0cce159931d5adf339c643044937c628f4;hp=f92e30aac7c13075341afe95b59eb3a9fdaf8441;hb=refs%2Fheads%2Fv1.7.x;hpb=ab58ba30b72ffb197f5f2429540380cd96e2d315 diff --git a/php_http_cache_api.h b/php_http_cache_api.h index f92e30a..8df20a0 100644 --- a/php_http_cache_api.h +++ b/php_http_cache_api.h @@ -1,16 +1,13 @@ /* - +----------------------------------------------------------------------+ - | PECL :: http | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 of the PHP license, that | - | is bundled with this package in the file LICENSE, and is available | - | through the world-wide-web at http://www.php.net/license/3_0.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Copyright (c) 2004-2005 Michael Wallner | - +----------------------------------------------------------------------+ + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2010, Michael Wallner | + +--------------------------------------------------------------------+ */ /* $Id$ */ @@ -18,47 +15,27 @@ #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 "php_http_std_defs.h" -#include "php_http.h" -#include "php_http_api.h" #include "php_http_send_api.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; +#include "ext/standard/crc32.h" +#include "ext/standard/sha1.h" +#include "ext/standard/md5.h" -#ifdef HAVE_LIBMHASH -static void *http_etag_alloc_mhash_digest(size_t size) -{ - return emalloc(size); -} +#ifdef HTTP_HAVE_HASH +# include "php_hash.h" #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) +#define http_etag_digest(d, l) _http_etag_digest((d), (l)) +static inline char *_http_etag_digest(const unsigned char *digest, int len) { + static const char hexdigits[17] = "0123456789abcdef"; 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++ = hexdigits[digest[i] >> 4]; + *ptr++ = hexdigits[digest[i] & 0xF]; } *ptr = '\0'; @@ -69,28 +46,23 @@ static inline char *_http_etag_digest(const unsigned char *digest, int len TSRML static inline void *_http_etag_init(TSRMLS_D) { void *ctx = NULL; - long mode = INI_INT("http.etag_mode"); + char *mode = HTTP_G->etag.mode; - switch (mode) - { - case HTTP_ETAG_SHA1: - PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX))); - break; - - case HTTP_ETAG_MD5: -#ifndef HAVE_LIBMHASH - default: -#endif - PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX))); - break; - -#ifdef HAVE_LIBMHASH - 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; +#ifdef HTTP_HAVE_HASH + const php_hash_ops *eho = NULL; + + if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) { + ctx = emalloc(eho->context_size); + eho->hash_init(ctx); + } else #endif + if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) { + ctx = emalloc(sizeof(uint)); + *((uint *) ctx) = ~0; + } else if (mode && !strcasecmp(mode, "sha1")) { + PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX))); + } else { + PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX))); } return ctx; @@ -99,37 +71,28 @@ static inline void *_http_etag_init(TSRMLS_D) #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"); + unsigned char digest[128] = {0}; + char *etag = NULL, *mode = HTTP_G->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: -#ifndef HAVE_LIBMHASH - default: -#endif - PHP_MD5Final(digest, ctx); - etag = http_etag_digest(digest, 16); - efree(ctx); - break; - -#ifdef HAVE_LIBMHASH - 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; +#ifdef HTTP_HAVE_HASH + const php_hash_ops *eho = NULL; + + if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) { + eho->hash_final(digest, ctx); + etag = http_etag_digest(digest, eho->digest_size); + } else #endif + if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) { + *((uint *) ctx) = ~*((uint *) ctx); + etag = http_etag_digest((const unsigned char *) ctx, sizeof(uint)); + } else if (mode && (!strcasecmp(mode, "sha1"))) { + PHP_SHA1Final(digest, ctx); + etag = http_etag_digest(digest, 20); + } else { + PHP_MD5Final(digest, ctx); + etag = http_etag_digest(digest, 16); } + efree(ctx); return etag; } @@ -137,27 +100,30 @@ static inline char *_http_etag_finish(void *ctx TSRMLS_DC) #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: -#ifndef HAVE_LIBMHASH - default: -#endif - PHP_MD5Update(ctx, data_ptr, data_len); - break; - -#ifdef HAVE_LIBMHASH - default: - mhash(ctx, data_ptr, data_len); - break; + char *mode = HTTP_G->etag.mode; +#ifdef HTTP_HAVE_HASH + const php_hash_ops *eho = NULL; + + if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) { + eho->hash_update(ctx, (const unsigned char *) data_ptr, data_len); + } else #endif + if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) { + uint i, c = *((uint *) ctx); + for (i = 0; i < data_len; ++i) { + CRC32(c, data_ptr[i]); + } + *((uint *)ctx) = c; + } else if (mode && (!strcasecmp(mode, "sha1"))) { + PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len); + } else { + PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len); } } +#define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC) +extern void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode 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); @@ -178,8 +144,10 @@ 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); +#define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C) +PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D); +#define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C) +PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D); #endif