]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
Add custom Store ID code support
[thirdparty/squid.git] / src / store_key_md5.cc
1
2 /*
3 * DEBUG: section 20 Storage Manager MD5 Cache Keys
4 * AUTHOR: Duane Wessels
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "HttpRequest.h"
36 #include "md5.h"
37 #include "Mem.h"
38 #include "store_key_md5.h"
39 #include "URL.h"
40
41 static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
42
43 const char *
44 storeKeyText(const cache_key *key)
45 {
46 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
47 int i;
48
49 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
50 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
51
52 return buf;
53 }
54
55 const cache_key *
56 storeKeyScan(const char *buf)
57 {
58 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
59 int i;
60 int j = 0;
61 char t[3];
62
63 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
64 t[0] = *(buf + (j++));
65 t[1] = *(buf + (j++));
66 t[2] = '\0';
67 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
68 }
69
70 return digest;
71 }
72
73 int
74 storeKeyHashCmp(const void *a, const void *b)
75 {
76 const unsigned char *A = (const unsigned char *)a;
77 const unsigned char *B = (const unsigned char *)b;
78 int i;
79
80 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
81 if (A[i] < B[i])
82 return -1;
83
84 if (A[i] > B[i])
85 return 1;
86 }
87
88 return 0;
89 }
90
91 unsigned int
92 storeKeyHashHash(const void *key, unsigned int n)
93 {
94 /* note, n must be a power of 2! */
95 const unsigned char *digest = (const unsigned char *)key;
96 unsigned int i = digest[0]
97 | digest[1] << 8
98 | digest[2] << 16
99 | digest[3] << 24;
100 return (i & (--n));
101 }
102
103 const cache_key *
104 storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
105 {
106 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
107 SquidMD5_CTX M;
108 assert(id > 0);
109 debugs(20, 3, "storeKeyPrivate: " << method << " " << url);
110 SquidMD5Init(&M);
111 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
112 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
113 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
114 SquidMD5Final(digest, &M);
115 return digest;
116 }
117
118 const cache_key *
119 storeKeyPublic(const char *url, const HttpRequestMethod& method)
120 {
121 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
122 unsigned char m = (unsigned char) method.id();
123 SquidMD5_CTX M;
124 SquidMD5Init(&M);
125 SquidMD5Update(&M, &m, sizeof(m));
126 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
127 SquidMD5Final(digest, &M);
128 debugs(20, 3, "created public key: " << digest << " for: " << method << ' ' << url);
129 return digest;
130 }
131
132 const cache_key *
133 storeKeyPublicByRequest(HttpRequest * request)
134 {
135 return storeKeyPublicByRequestMethod(request, request->method);
136 }
137
138 const cache_key *
139 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
140 {
141 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
142 unsigned char m = (unsigned char) method.id();
143 const char *url = request->storeId(); /* storeId returns the right storeID\canonical URL for the md5 calc */
144 SquidMD5_CTX M;
145 SquidMD5Init(&M);
146 SquidMD5Update(&M, &m, sizeof(m));
147 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
148
149 if (request->vary_headers) {
150 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
151 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
152 }
153 SquidMD5Final(digest, &M);
154 debugs(20, 3, "created public key: " << digest << " for: " << method << ' ' << url);
155
156 return digest;
157 }
158
159 cache_key *
160 storeKeyDup(const cache_key * key)
161 {
162 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
163 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
164 return dup;
165 }
166
167 cache_key *
168 storeKeyCopy(cache_key * dst, const cache_key * src)
169 {
170 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
171 return dst;
172 }
173
174 void
175 storeKeyFree(const cache_key * key)
176 {
177 memFree((void *) key, MEM_MD5_DIGEST);
178 }
179
180 int
181 storeKeyHashBuckets(int nbuckets)
182 {
183 int n = 0x2000;
184
185 while (n < nbuckets)
186 n <<= 1;
187
188 return n;
189 }
190
191 int
192 storeKeyNull(const cache_key * key)
193 {
194 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
195 return 1;
196 else
197 return 0;
198 }
199
200 void
201 storeKeyInit(void)
202 {
203 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
204 }