store_key_md5.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/* DEBUG: section 20 Storage Manager MD5 Cache Keys */
10
11#include "squid.h"
12#include "HttpRequest.h"
13#include "md5.h"
14#include "store_key_md5.h"
15
16const char *
18{
19 if (!key)
20 return "[null_store_key]";
21
22 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
23 int i;
24
25 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
26 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
27
28 return buf;
29}
30
31const cache_key *
32storeKeyScan(const char *buf)
33{
34 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
35 int i;
36 int j = 0;
37 char t[3];
38
39 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
40 t[0] = *(buf + (j++));
41 t[1] = *(buf + (j++));
42 t[2] = '\0';
43 *(digest + i) = (unsigned char) strtol(t, nullptr, 16);
44 }
45
46 return digest;
47}
48
49int
50storeKeyHashCmp(const void *a, const void *b)
51{
52 const unsigned char *A = (const unsigned char *)a;
53 const unsigned char *B = (const unsigned char *)b;
54 int i;
55
56 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
57 if (A[i] < B[i])
58 return -1;
59
60 if (A[i] > B[i])
61 return 1;
62 }
63
64 return 0;
65}
66
67unsigned int
68storeKeyHashHash(const void *key, unsigned int n)
69{
70 /* note, n must be a power of 2! */
71 const unsigned char *digest = (const unsigned char *)key;
72 unsigned int i = digest[0]
73 | digest[1] << 8
74 | digest[2] << 16
75 | digest[3] << 24;
76 return (i & (--n));
77}
78
79const cache_key *
81{
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);
92}
93
94const cache_key *
95storeKeyPublic(const char *url, const HttpRequestMethod& method, const KeyScope keyScope)
96{
98 unsigned char m = (unsigned char) method.id();
100 SquidMD5Init(&M);
101 SquidMD5Update(&M, &m, sizeof(m));
102 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
103 if (keyScope)
104 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
105 SquidMD5Final(digest, &M);
106 return digest;
107}
108
109const cache_key *
111{
112 return storeKeyPublicByRequestMethod(request, request->method, keyScope);
113}
114
115const cache_key *
117{
118 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
119 unsigned char m = (unsigned char) method.id();
120 const SBuf url = request->storeId(); /* returns the right storeID\URL for the MD5 calc */
121 SquidMD5_CTX M;
122 SquidMD5Init(&M);
123 SquidMD5Update(&M, &m, sizeof(m));
124 SquidMD5Update(&M, (unsigned char *) url.rawContent(), url.length());
125 if (keyScope)
126 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
127
128 if (!request->vary_headers.isEmpty()) {
129 SquidMD5Update(&M, request->vary_headers.rawContent(), request->vary_headers.length());
130 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
131 }
132
133 SquidMD5Final(digest, &M);
134
135 return digest;
136}
137
138cache_key *
140{
142 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
143 return dup;
144}
145
146cache_key *
148{
149 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
150 return dst;
151}
152
153void
155{
156 memFree((void *) key, MEM_MD5_DIGEST);
157}
158
159int
161{
162 int n = 0x2000;
163
164 while (n < nbuckets)
165 n <<= 1;
166
167 return n;
168}
169
static pid_t pid
Definition: IcmpSquid.cc:34
#define assert(EX)
Definition: assert.h:17
Http::MethodType id() const
Definition: RequestMethod.h:70
HttpRequestMethod method
Definition: HttpRequest.h:114
SBuf vary_headers
The variant second-stage cache key. Generated from Vary header pattern for this request.
Definition: HttpRequest.h:170
const SBuf storeId()
Definition: HttpRequest.cc:733
Definition: SBuf.h:94
const char * rawContent() const
Definition: SBuf.cc:509
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
bool isEmpty() const
Definition: SBuf.h:431
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
int KidIdentifier
static uint32 A
Definition: md4.c:43
static uint32 B
Definition: md4.c:43
SQUIDCEXTERN void SquidMD5Init(struct SquidMD5Context *context)
Definition: md5.c:73
#define SQUID_MD5_DIGEST_LENGTH
Definition: md5.h:66
SQUIDCEXTERN void SquidMD5Update(struct SquidMD5Context *context, const void *buf, unsigned len)
Definition: md5.c:89
SQUIDCEXTERN void SquidMD5Final(uint8_t digest[16], struct SquidMD5Context *context)
void memFree(void *, int type)
Free a element allocated by memAllocate()
Definition: minimal.cc:60
void * memAllocate(mem_type)
Allocate one element from the typed pool.
Definition: old_api.cc:213
@ MEM_MD5_DIGEST
Definition: forward.h:50
struct node * nbuckets[NHASHSIZE]
Definition: parse.c:243
unsigned char cache_key
Store key.
Definition: forward.h:29
const cache_key * storeKeyPublicByRequest(HttpRequest *request, const KeyScope keyScope)
int storeKeyHashCmp(const void *a, const void *b)
int storeKeyHashBuckets(int nbuckets)
const cache_key * storeKeyScan(const char *buf)
cache_key * storeKeyCopy(cache_key *dst, const cache_key *src)
const cache_key * storeKeyPublic(const char *url, const HttpRequestMethod &method, const KeyScope keyScope)
const cache_key * storeKeyPublicByRequestMethod(HttpRequest *request, const HttpRequestMethod &method, const KeyScope keyScope)
const cache_key * storeKeyPrivate()
cache_key * storeKeyDup(const cache_key *key)
void storeKeyFree(const cache_key *key)
unsigned int storeKeyHashHash(const void *key, unsigned int n)
const char * storeKeyText(const cache_key *key)
KeyScope
Definition: store_key_md5.h:18

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors