03ea1134be656f6412583633e544c9eaaa9069c5
[m6w6/ext-http] / php_http_cache_api.h
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifndef PHP_HTTP_CACHE_API_H
16 #define PHP_HTTP_CACHE_API_H
17
18 #include "zend_ini.h"
19
20 #include "ext/standard/crc32.h"
21 #include "ext/standard/sha1.h"
22 #include "ext/standard/md5.h"
23
24 #include "php_http_std_defs.h"
25 #include "php_http.h"
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
28
29 #ifdef HTTP_HAVE_EXT_HASH
30 # include "php_hash.h"
31 #endif
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 #define http_etag_digest(d, l) _http_etag_digest((d), (l))
36 static inline char *_http_etag_digest(const unsigned char *digest, int len)
37 {
38 static const char hexdigits[16] = "0123456789abcdef";
39 int i;
40 char *hex = emalloc(len * 2 + 1);
41 char *ptr = hex;
42
43 for (i = 0; i < len; ++i) {
44 *ptr++ = hexdigits[digest[i] >> 4];
45 *ptr++ = hexdigits[digest[i] & 0xF];
46 }
47 *ptr = '\0';
48
49 return hex;
50 }
51
52 #define http_etag_init() _http_etag_init(TSRMLS_C)
53 static inline void *_http_etag_init(TSRMLS_D)
54 {
55 void *ctx = NULL;
56 char *mode = HTTP_G(etag).mode;
57
58 #ifdef HTTP_HAVE_EXT_HASH
59 php_hash_ops *eho = NULL;
60
61 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
62 ctx = emalloc(eho->context_size);
63 eho->hash_init(ctx);
64 } else
65 #endif
66 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
67 ctx = emalloc(sizeof(uint));
68 *((uint *) ctx) = ~0;
69 } else if (mode && !strcasecmp(mode, "sha1")) {
70 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
71 } else {
72 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
73 }
74
75 return ctx;
76 }
77
78 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
79 static inline char *_http_etag_finish(void *ctx TSRMLS_DC)
80 {
81 unsigned char digest[128] = {0};
82 char *etag = NULL;
83
84 #ifdef HTTP_HAVE_EXT_HASH
85 php_hash_ops *eho = NULL;
86 char *mode = HTTP_G(etag).mode;
87
88 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
89 eho->hash_final(digest, ctx);
90 etag = http_etag_digest(digest, eho->digest_size);
91 } else
92 #endif
93 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
94 *((uint *) ctx) = ~*((uint *) ctx);
95 etag = http_etag_digest((const unsigned char *) ctx, sizeof(uint));
96 } else if (mode && (!strcasecmp(mode, "sha1"))) {
97 PHP_SHA1Final(digest, ctx);
98 etag = http_etag_digest(digest, 20);
99 } else {
100 PHP_MD5Final(digest, ctx);
101 etag = http_etag_digest(digest, 16);
102 }
103 efree(ctx);
104
105 return etag;
106 }
107
108 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
109 static inline void _http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC)
110 {
111 #ifdef HTTP_HAVE_EXT_HASH
112 php_hash_ops *eho = NULL;
113 char *mode = HTTP_G(etag).mode;
114
115 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
116 eho->hash_update(ctx, (const unsigned char *) data_ptr, data_len);
117 } else
118 #endif
119 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
120 uint i, c = *((uint *) ctx);
121 for (i = 0; i < data_len; ++i) {
122 c = CRC32(c, data_ptr[i]);
123 }
124 *((uint *)ctx) = c;
125 } else if (mode && (!strcasecmp(mode, "sha1"))) {
126 PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
127 } else {
128 PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
129 }
130 }
131
132 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
133 extern void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
134
135 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
136 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
137
138 #define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC)
139 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC);
140
141 #define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC)
142 #define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC)
143 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC);
144
145 #define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC)
146 #define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC)
147 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC);
148
149 #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC)
150 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);
151
152 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
153 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
154
155 #define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
156 PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D);
157 #define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
158 PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D);
159
160 #endif
161
162 /*
163 * Local variables:
164 * tab-width: 4
165 * c-basic-offset: 4
166 * End:
167 * vim600: noet sw=4 ts=4 fdm=marker
168 * vim<600: noet sw=4 ts=4
169 */
170