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