]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
Made storeKeyText safe for entries that do not have a key set yet.
[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 if (!key)
47 return "[null_store_key]";
48
49 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
50 int i;
51
52 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
53 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
54
55 return buf;
56 }
57
58 const cache_key *
59 storeKeyScan(const char *buf)
60 {
61 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
62 int i;
63 int j = 0;
64 char t[3];
65
66 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
67 t[0] = *(buf + (j++));
68 t[1] = *(buf + (j++));
69 t[2] = '\0';
70 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
71 }
72
73 return digest;
74 }
75
76 int
77 storeKeyHashCmp(const void *a, const void *b)
78 {
79 const unsigned char *A = (const unsigned char *)a;
80 const unsigned char *B = (const unsigned char *)b;
81 int i;
82
83 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
84 if (A[i] < B[i])
85 return -1;
86
87 if (A[i] > B[i])
88 return 1;
89 }
90
91 return 0;
92 }
93
94 unsigned int
95 storeKeyHashHash(const void *key, unsigned int n)
96 {
97 /* note, n must be a power of 2! */
98 const unsigned char *digest = (const unsigned char *)key;
99 unsigned int i = digest[0]
100 | digest[1] << 8
101 | digest[2] << 16
102 | digest[3] << 24;
103 return (i & (--n));
104 }
105
106 const cache_key *
107 storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
108 {
109 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
110 SquidMD5_CTX M;
111 assert(id > 0);
112 debugs(20, 3, "storeKeyPrivate: " << method << " " << url);
113 SquidMD5Init(&M);
114 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
115 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
116 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
117 SquidMD5Final(digest, &M);
118 return digest;
119 }
120
121 const cache_key *
122 storeKeyPublic(const char *url, const HttpRequestMethod& method)
123 {
124 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
125 unsigned char m = (unsigned char) method.id();
126 SquidMD5_CTX M;
127 SquidMD5Init(&M);
128 SquidMD5Update(&M, &m, sizeof(m));
129 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
130 SquidMD5Final(digest, &M);
131 return digest;
132 }
133
134 const cache_key *
135 storeKeyPublicByRequest(HttpRequest * request)
136 {
137 return storeKeyPublicByRequestMethod(request, request->method);
138 }
139
140 const cache_key *
141 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
142 {
143 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
144 unsigned char m = (unsigned char) method.id();
145 const char *url = request->storeId(); /* storeId returns the right storeID\canonical URL for the md5 calc */
146 SquidMD5_CTX M;
147 SquidMD5Init(&M);
148 SquidMD5Update(&M, &m, sizeof(m));
149 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
150
151 if (request->vary_headers) {
152 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
153 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
154 }
155
156 SquidMD5Final(digest, &M);
157
158 return digest;
159 }
160
161 cache_key *
162 storeKeyDup(const cache_key * key)
163 {
164 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
165 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
166 return dup;
167 }
168
169 cache_key *
170 storeKeyCopy(cache_key * dst, const cache_key * src)
171 {
172 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
173 return dst;
174 }
175
176 void
177 storeKeyFree(const cache_key * key)
178 {
179 memFree((void *) key, MEM_MD5_DIGEST);
180 }
181
182 int
183 storeKeyHashBuckets(int nbuckets)
184 {
185 int n = 0x2000;
186
187 while (n < nbuckets)
188 n <<= 1;
189
190 return n;
191 }
192
193 int
194 storeKeyNull(const cache_key * key)
195 {
196 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
197 return 1;
198 else
199 return 0;
200 }
201
202 void
203 storeKeyInit(void)
204 {
205 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
206 }