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