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