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 
16 const 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 
31 const cache_key *
32 storeKeyScan(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 
49 int
50 storeKeyHashCmp(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 
67 unsigned int
68 storeKeyHashHash(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 
79 const 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 
94 const cache_key *
95 storeKeyPublic(const char *url, const HttpRequestMethod& method, const KeyScope keyScope)
96 {
97  static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
98  unsigned char m = (unsigned char) method.id();
99  SquidMD5_CTX M;
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 
109 const cache_key *
110 storeKeyPublicByRequest(HttpRequest * request, const KeyScope keyScope)
111 {
112  return storeKeyPublicByRequestMethod(request, request->method, keyScope);
113 }
114 
115 const cache_key *
116 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method, const KeyScope keyScope)
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 
138 cache_key *
139 storeKeyDup(const cache_key * key)
140 {
142  memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
143  return dup;
144 }
145 
146 cache_key *
147 storeKeyCopy(cache_key * dst, const cache_key * src)
148 {
149  memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
150  return dst;
151 }
152 
153 void
155 {
156  memFree((void *) key, MEM_MD5_DIGEST);
157 }
158 
159 int
161 {
162  int n = 0x2000;
163 
164  while (n < nbuckets)
165  n <<= 1;
166 
167  return n;
168 }
169 
const cache_key * storeKeyPrivate()
const cache_key * storeKeyPublicByRequest(HttpRequest *request, const KeyScope keyScope)
SQUIDCEXTERN void SquidMD5Init(struct SquidMD5Context *context)
Definition: md5.c:73
unsigned char cache_key
Store key.
Definition: forward.h:29
bool isEmpty() const
Definition: SBuf.h:435
const cache_key * storeKeyPublic(const char *url, const HttpRequestMethod &method, const KeyScope keyScope)
unsigned int storeKeyHashHash(const void *key, unsigned int n)
int KidIdentifier
Definition: SBuf.h:93
@ MEM_MD5_DIGEST
Definition: forward.h:54
void * memAllocate(mem_type)
Allocate one element from the typed pool.
Definition: old_api.cc:122
SQUIDCEXTERN void SquidMD5Final(uint8_t digest[16], struct SquidMD5Context *context)
static pid_t pid
Definition: IcmpSquid.cc:34
Http::MethodType id() const
Definition: RequestMethod.h:70
#define SQUID_MD5_DIGEST_LENGTH
Definition: md5.h:66
const char * rawContent() const
Definition: SBuf.cc:509
static uint32 A
Definition: md4.c:43
cache_key * storeKeyDup(const cache_key *key)
cache_key * storeKeyCopy(cache_key *dst, const cache_key *src)
#define assert(EX)
Definition: assert.h:17
SBuf vary_headers
The variant second-stage cache key. Generated from Vary header pattern for this request.
Definition: HttpRequest.h:170
int storeKeyHashCmp(const void *a, const void *b)
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:419
HttpRequestMethod method
Definition: HttpRequest.h:114
void storeKeyFree(const cache_key *key)
KeyScope
Definition: store_key_md5.h:18
const SBuf storeId()
Definition: HttpRequest.cc:733
void memFree(void *, int type)
Free a element allocated by memAllocate()
Definition: minimal.cc:61
const char * storeKeyText(const cache_key *key)
const cache_key * storeKeyPublicByRequestMethod(HttpRequest *request, const HttpRequestMethod &method, const KeyScope keyScope)
SQUIDCEXTERN void SquidMD5Update(struct SquidMD5Context *context, const void *buf, unsigned len)
Definition: md5.c:89
static uint32 B
Definition: md4.c:43
int storeKeyHashBuckets(int nbuckets)
const cache_key * storeKeyScan(const char *buf)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
struct node * nbuckets[NHASHSIZE]
Definition: parse.c:243

 

Introduction

Documentation

Support

Miscellaneous