]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_key_md5.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / store_key_md5.cc
CommitLineData
9cef6668 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
9cef6668 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9cef6668 7 */
8
bbc27441
AJ
9/* DEBUG: section 20 Storage Manager MD5 Cache Keys */
10
582c2af2 11#include "squid.h"
a2ac85d9 12#include "HttpRequest.h"
582c2af2 13#include "md5.h"
fb548aaf 14#include "store_key_md5.h"
b1bd952a 15#include "URL.h"
6507d007 16
c3031d67 17static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
25535cbe 18
6507d007 19const char *
f1b70fe6 20storeKeyText(const cache_key *key)
6507d007 21{
8c3cd042
AR
22 if (!key)
23 return "[null_store_key]";
24
c3031d67 25 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
6507d007 26 int i;
62e76326 27
5db6bf73 28 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
62e76326 29 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
30
528b2c61 31 return buf;
6507d007 32}
33
b8890359 34const cache_key *
6507d007 35storeKeyScan(const char *buf)
36{
c3031d67 37 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
6507d007 38 int i;
39 int j = 0;
7363fc17 40 char t[3];
62e76326 41
5db6bf73 42 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
62e76326 43 t[0] = *(buf + (j++));
44 t[1] = *(buf + (j++));
45 t[2] = '\0';
46 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
6507d007 47 }
62e76326 48
6507d007 49 return digest;
50}
51
52int
53storeKeyHashCmp(const void *a, const void *b)
54{
e6ccf245 55 const unsigned char *A = (const unsigned char *)a;
56 const unsigned char *B = (const unsigned char *)b;
6507d007 57 int i;
62e76326 58
5db6bf73 59 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
62e76326 60 if (A[i] < B[i])
61 return -1;
62
63 if (A[i] > B[i])
64 return 1;
6507d007 65 }
62e76326 66
6507d007 67 return 0;
68}
69
70unsigned int
71storeKeyHashHash(const void *key, unsigned int n)
72{
73 /* note, n must be a power of 2! */
e6ccf245 74 const unsigned char *digest = (const unsigned char *)key;
6507d007 75 unsigned int i = digest[0]
62e76326 76 | digest[1] << 8
77 | digest[2] << 16
78 | digest[3] << 24;
6507d007 79 return (i & (--n));
80}
81
82const cache_key *
0a132302 83storeKeyPrivate()
6507d007 84{
0a132302
EB
85 // only the count field is required
86 // others just simplify searching for keys in a multi-process cache.log
87 static struct {
88 uint64_t count;
89 pid_t pid;
90 int32_t kid;
91 } key = { 0, getpid(), KidIdentifier };
92 assert(sizeof(key) == SQUID_MD5_DIGEST_LENGTH);
93 ++key.count;
94 return reinterpret_cast<cache_key*>(&key);
6507d007 95}
96
b8890359 97const cache_key *
1a210de4 98storeKeyPublic(const char *url, const HttpRequestMethod& method, const KeyScope keyScope)
b8890359 99{
c3031d67 100 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 101 unsigned char m = (unsigned char) method.id();
c3031d67 102 SquidMD5_CTX M;
103 SquidMD5Init(&M);
104 SquidMD5Update(&M, &m, sizeof(m));
105 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
1a210de4
EB
106 if (keyScope)
107 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
c3031d67 108 SquidMD5Final(digest, &M);
b8890359 109 return digest;
110}
111
f66a9ef4 112const cache_key *
1a210de4 113storeKeyPublicByRequest(HttpRequest * request, const KeyScope keyScope)
f66a9ef4 114{
1a210de4 115 return storeKeyPublicByRequestMethod(request, request->method, keyScope);
f66a9ef4 116}
117
118const cache_key *
1a210de4 119storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method, const KeyScope keyScope)
f66a9ef4 120{
c3031d67 121 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 122 unsigned char m = (unsigned char) method.id();
851feda6 123 const SBuf url = request->storeId(); /* returns the right storeID\URL for the MD5 calc */
c3031d67 124 SquidMD5_CTX M;
125 SquidMD5Init(&M);
126 SquidMD5Update(&M, &m, sizeof(m));
851feda6 127 SquidMD5Update(&M, (unsigned char *) url.rawContent(), url.length());
1a210de4
EB
128 if (keyScope)
129 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
62e76326 130
90ab8f20
AJ
131 if (!request->vary_headers.isEmpty()) {
132 SquidMD5Update(&M, request->vary_headers.rawContent(), request->vary_headers.length());
a8a0b1c2
EC
133 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
134 }
3690da07 135
c3031d67 136 SquidMD5Final(digest, &M);
62e76326 137
f66a9ef4 138 return digest;
139}
140
186477c1 141cache_key *
6507d007 142storeKeyDup(const cache_key * key)
143{
e6ccf245 144 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
41d00cd3 145 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
6507d007 146 return dup;
147}
148
399cabec 149cache_key *
5942e8d4 150storeKeyCopy(cache_key * dst, const cache_key * src)
399cabec 151{
41d00cd3 152 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
399cabec 153 return dst;
154}
155
6507d007 156void
157storeKeyFree(const cache_key * key)
158{
db1cd23c 159 memFree((void *) key, MEM_MD5_DIGEST);
6507d007 160}
161
162int
baf144ad 163storeKeyHashBuckets(int nbuckets)
6507d007 164{
baf144ad 165 int n = 0x2000;
62e76326 166
baf144ad 167 while (n < nbuckets)
62e76326 168 n <<= 1;
169
baf144ad 170 return n;
6507d007 171}
25535cbe 172
173int
174storeKeyNull(const cache_key * key)
175{
c3031d67 176 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
62e76326 177 return 1;
25535cbe 178 else
62e76326 179 return 0;
25535cbe 180}
181
182void
183storeKeyInit(void)
184{
c3031d67 185 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
25535cbe 186}
f53969cc 187