- example fixup
[m6w6/ext-http] / php_http_cache_api.h
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifndef PHP_HTTP_CACHE_API_H
19 #define PHP_HTTP_CACHE_API_H
20
21 #include "zend_ini.h"
22
23 #include "ext/standard/md5.h"
24 #include "ext/standard/sha1.h"
25
26 #include "php_http_std_defs.h"
27 #include "php_http.h"
28 #include "php_http_api.h"
29 #include "php_http_send_api.h"
30
31 #ifdef HTTP_HAVE_MHASH
32 # include <mhash.h>
33 #endif
34
35 ZEND_EXTERN_MODULE_GLOBALS(http);
36
37 #define http_cache_global_init() _http_cache_global_init(INIT_FUNC_ARGS_PASSTHRU)
38 extern STATUS _http_cache_global_init(INIT_FUNC_ARGS);
39
40 typedef enum {
41 HTTP_ETAG_MD5 = -2,
42 HTTP_ETAG_SHA1 = -1,
43 } http_etag_mode;
44
45 #ifdef HTTP_HAVE_MHASH
46 static void *http_etag_alloc_mhash_digest(size_t size)
47 {
48 return emalloc(size);
49 }
50 #endif
51
52 #define http_etag_digest(d, l) _http_etag_digest((d), (l) TSRMLS_CC)
53 static inline char *_http_etag_digest(const unsigned char *digest, int len TSRMLS_DC)
54 {
55 int i;
56 char *hex = emalloc(len * 2 + 1);
57 char *ptr = hex;
58
59 /* optimize this --
60 look at apache's make_etag */
61 for (i = 0; i < len; ++i) {
62 sprintf(ptr, "%02x", digest[i]);
63 ptr += 2;
64 }
65 *ptr = '\0';
66
67 return hex;
68 }
69
70 #define http_etag_init() _http_etag_init(TSRMLS_C)
71 static inline void *_http_etag_init(TSRMLS_D)
72 {
73 void *ctx = NULL;
74 long mode = INI_INT("http.etag_mode");
75
76 switch (mode)
77 {
78 case HTTP_ETAG_SHA1:
79 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
80 break;
81
82 case HTTP_ETAG_MD5:
83 #ifndef HTTP_HAVE_MHASH
84 default:
85 #endif
86 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
87 break;
88
89 #ifdef HTTP_HAVE_MHASH
90 default:
91 if ((mode < 0) || ((ulong)mode > mhash_count()) || (!(ctx = mhash_init(mode)))) {
92 http_error_ex(HE_ERROR, HTTP_E_RUNTIME, "Invalid ETag mode: %ld", mode);
93 }
94 break;
95 #endif
96 }
97
98 return ctx;
99 }
100
101 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
102 static inline char *_http_etag_finish(void *ctx TSRMLS_DC)
103 {
104 char *etag = NULL;
105 unsigned char digest[20];
106 long mode = INI_INT("http.etag_mode");
107
108 switch (mode)
109 {
110 case HTTP_ETAG_SHA1:
111 PHP_SHA1Final(digest, ctx);
112 etag = http_etag_digest(digest, 20);
113 efree(ctx);
114 break;
115
116 case HTTP_ETAG_MD5:
117 #ifndef HTTP_HAVE_MHASH
118 default:
119 #endif
120 PHP_MD5Final(digest, ctx);
121 etag = http_etag_digest(digest, 16);
122 efree(ctx);
123 break;
124
125 #ifdef HTTP_HAVE_MHASH
126 default:
127 {
128 unsigned char *mhash_digest = mhash_end_m(ctx, http_etag_alloc_mhash_digest);
129 etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode));
130 efree(mhash_digest);
131 }
132 break;
133 #endif
134 }
135
136 return etag;
137 }
138
139 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
140 static inline void _http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC)
141 {
142 switch (INI_INT("http.etag_mode"))
143 {
144 case HTTP_ETAG_SHA1:
145 PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
146 break;
147
148 case HTTP_ETAG_MD5:
149 #ifndef HTTP_HAVE_MHASH
150 default:
151 #endif
152 PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
153 break;
154
155 #ifdef HTTP_HAVE_MHASH
156 default:
157 mhash(ctx, data_ptr, data_len);
158 break;
159 #endif
160 }
161 }
162
163 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
164 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
165
166 #define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC)
167 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC);
168
169 #define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC)
170 #define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC)
171 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC);
172
173 #define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC)
174 #define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC)
175 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC);
176
177 #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC)
178 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);
179
180 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
181 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
182
183 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
184 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
185
186 #endif
187
188 /*
189 * Local variables:
190 * tab-width: 4
191 * c-basic-offset: 4
192 * End:
193 * vim600: noet sw=4 ts=4 fdm=marker
194 * vim<600: noet sw=4 ts=4
195 */
196