]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/store_key_md5.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / store_key_md5.cc
index d0d4ea344e7b60da7d0c6b30cb994c8efbdbd9fa..df86b7ebb5dd96d6d7daa7a850937c3aba23e3d9 100644 (file)
@@ -1,81 +1,68 @@
-
 /*
- * $Id: store_key_md5.cc,v 1.22 1999/12/11 15:57:42 wessels Exp $
- *
- * DEBUG: section 20    Storage Manager MD5 Cache Keys
- * AUTHOR: Duane Wessels
- *
- * SQUID Internet Object Cache  http://squid.nlanr.net/Squid/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from the
- *  Internet community.  Development is led by Duane Wessels of the
- *  National Laboratory for Applied Network Research and funded by the
- *  National Science Foundation.  Squid is Copyrighted (C) 1998 by
- *  Duane Wessels and the University of California San Diego.  Please
- *  see the COPYRIGHT file for full details.  Squid incorporates
- *  software developed and/or copyrighted by other sources.  Please see
- *  the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *  
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *  
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
+/* DEBUG: section 20    Storage Manager MD5 Cache Keys */
+
 #include "squid.h"
+#include "HttpRequest.h"
+#include "md5.h"
+#include "store_key_md5.h"
 
-static cache_key null_key[MD5_DIGEST_CHARS];
+static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
 
 const char *
-storeKeyText(const unsigned char *key)
+storeKeyText(const cache_key *key)
 {
-    static MemBuf mb = MemBufNULL;
+    if (!key)
+        return "[null_store_key]";
+
+    static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
     int i;
-    memBufReset(&mb);
-    for (i = 0; i < MD5_DIGEST_CHARS; i++)
-       memBufPrintf(&mb, "%02X", *(key + i));
-    return mb.buf;
+
+    for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
+        snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
+
+    return buf;
 }
 
 const cache_key *
 storeKeyScan(const char *buf)
 {
-    static unsigned char digest[MD5_DIGEST_CHARS];
+    static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
     int i;
     int j = 0;
     char t[3];
-    for (i = 0; i < MD5_DIGEST_CHARS; i++) {
-       t[0] = *(buf + (j++));
-       t[1] = *(buf + (j++));
-       t[2] = '\0';
-       *(digest + i) = (unsigned char) strtol(t, NULL, 16);
+
+    for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
+        t[0] = *(buf + (j++));
+        t[1] = *(buf + (j++));
+        t[2] = '\0';
+        *(digest + i) = (unsigned char) strtol(t, NULL, 16);
     }
+
     return digest;
 }
 
 int
 storeKeyHashCmp(const void *a, const void *b)
 {
-    const unsigned char *A = a;
-    const unsigned char *B = b;
+    const unsigned char *A = (const unsigned char *)a;
+    const unsigned char *B = (const unsigned char *)b;
     int i;
-    for (i = 0; i < MD5_DIGEST_CHARS; i++) {
-       if (A[i] < B[i])
-           return -1;
-       if (A[i] > B[i])
-           return 1;
+
+    for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
+        if (A[i] < B[i])
+            return -1;
+
+        if (A[i] > B[i])
+            return 1;
     }
+
     return 0;
 }
 
@@ -83,55 +70,85 @@ unsigned int
 storeKeyHashHash(const void *key, unsigned int n)
 {
     /* note, n must be a power of 2! */
-    const unsigned char *digest = key;
+    const unsigned char *digest = (const unsigned char *)key;
     unsigned int i = digest[0]
-    | digest[1] << 8
-    | digest[2] << 16
-    | digest[3] << 24;
+                     | digest[1] << 8
+                     | digest[2] << 16
+                     | digest[3] << 24;
     return (i & (--n));
 }
 
 const cache_key *
-storeKeyPrivate(const char *url, method_t method, int id)
+storeKeyPrivate()
 {
-    static cache_key digest[MD5_DIGEST_CHARS];
-    MD5_CTX M;
-    assert(id > 0);
-    debug(20, 3) ("storeKeyPrivate: %s %s\n",
-       RequestMethodStr[method], url);
-    MD5Init(&M);
-    MD5Update(&M, (unsigned char *) &id, sizeof(id));
-    MD5Update(&M, (unsigned char *) &method, sizeof(method));
-    MD5Update(&M, (unsigned char *) url, strlen(url));
-    MD5Final(digest, &M);
-    return digest;
+    // only the count field is required
+    // others just simplify searching for keys in a multi-process cache.log
+    static struct {
+        uint64_t count;
+        pid_t pid;
+        int32_t kid;
+    } key = { 0, getpid(), KidIdentifier };
+    assert(sizeof(key) == SQUID_MD5_DIGEST_LENGTH);
+    ++key.count;
+    return reinterpret_cast<cache_key*>(&key);
 }
 
 const cache_key *
-storeKeyPublic(const char *url, const method_t method)
+storeKeyPublic(const char *url, const HttpRequestMethod& method, const KeyScope keyScope)
 {
-    static cache_key digest[MD5_DIGEST_CHARS];
-    unsigned char m = (unsigned char) method;
-    MD5_CTX M;
-    MD5Init(&M);
-    MD5Update(&M, &m, sizeof(m));
-    MD5Update(&M, (unsigned char *) url, strlen(url));
-    MD5Final(digest, &M);
+    static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
+    unsigned char m = (unsigned char) method.id();
+    SquidMD5_CTX M;
+    SquidMD5Init(&M);
+    SquidMD5Update(&M, &m, sizeof(m));
+    SquidMD5Update(&M, (unsigned char *) url, strlen(url));
+    if (keyScope)
+        SquidMD5Update(&M, &keyScope, sizeof(keyScope));
+    SquidMD5Final(digest, &M);
     return digest;
 }
 
 const cache_key *
+storeKeyPublicByRequest(HttpRequest * request, const KeyScope keyScope)
+{
+    return storeKeyPublicByRequestMethod(request, request->method, keyScope);
+}
+
+const cache_key *
+storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method, const KeyScope keyScope)
+{
+    static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
+    unsigned char m = (unsigned char) method.id();
+    const SBuf url = request->storeId(); /* returns the right storeID\URL for the MD5 calc */
+    SquidMD5_CTX M;
+    SquidMD5Init(&M);
+    SquidMD5Update(&M, &m, sizeof(m));
+    SquidMD5Update(&M, (unsigned char *) url.rawContent(), url.length());
+    if (keyScope)
+        SquidMD5Update(&M, &keyScope, sizeof(keyScope));
+
+    if (!request->vary_headers.isEmpty()) {
+        SquidMD5Update(&M, request->vary_headers.rawContent(), request->vary_headers.length());
+        debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
+    }
+
+    SquidMD5Final(digest, &M);
+
+    return digest;
+}
+
+cache_key *
 storeKeyDup(const cache_key * key)
 {
-    cache_key *dup = memAllocate(MEM_MD5_DIGEST);
-    xmemcpy(dup, key, MD5_DIGEST_CHARS);
+    cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
+    memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
     return dup;
 }
 
 cache_key *
 storeKeyCopy(cache_key * dst, const cache_key * src)
 {
-    xmemcpy(dst, src, MD5_DIGEST_CHARS);
+    memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
     return dst;
 }
 
@@ -145,22 +162,25 @@ int
 storeKeyHashBuckets(int nbuckets)
 {
     int n = 0x2000;
+
     while (n < nbuckets)
-       n <<= 1;
+        n <<= 1;
+
     return n;
 }
 
 int
 storeKeyNull(const cache_key * key)
 {
-    if (memcmp(key, null_key, MD5_DIGEST_CHARS) == 0)
-       return 1;
+    if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
+        return 1;
     else
-       return 0;
+        return 0;
 }
 
 void
 storeKeyInit(void)
 {
-    memset(null_key, '\0', MD5_DIGEST_CHARS);
+    memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
 }
+