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