]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/store_key_md5.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / store_key_md5.cc
index 0daed041e65879798924fc50a92a8b9e8cbc667a..4d306e489732eb6625d624e75a4cb4d644fdb611 100644 (file)
@@ -1,48 +1,69 @@
+/*
+ * Copyright (C) 1996-2015 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"
+#include "URL.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)
 {
-    LOCAL_ARRAY(char, buf, 33);
+    if (!key)
+        return "[null_store_key]";
+
+    static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
     int i;
-    int o;
-    for (i = 0; i < MD5_DIGEST_CHARS; i++) {
-       o = i << 1;
-       snprintf(buf + o, 33 - o, "%02X", *(key + i));
-    }
+
+    for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
+        snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
+
     return buf;
 }
 
-const unsigned char *
+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;
-    unsigned 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);
+    char t[3];
+
+    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;
 }
 
@@ -50,88 +71,113 @@ 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 num)
+storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
 {
-    static cache_key digest[MD5_DIGEST_CHARS];
-    MD5_CTX M;
-    int n;
-    char key_buf[MAX_URL + 100];
-    assert(num > 0);
-    debug(20, 3) ("storeKeyPrivate: '%s'\n", url);
-    n = snprintf(key_buf, MAX_URL + 100, "%d %s %s",
-       num,
-       RequestMethodStr[method],
-       url);
-    MD5Init(&M);
-    MD5Update(&M, key_buf, n);
-    MD5Final(digest, &M);
+    static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
+    SquidMD5_CTX M;
+    assert(id > 0);
+    debugs(20, 3, "storeKeyPrivate: " << method << " " << url);
+    SquidMD5Init(&M);
+    SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
+    SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
+    SquidMD5Update(&M, (unsigned char *) url, strlen(url));
+    SquidMD5Final(digest, &M);
     return digest;
 }
 
 const cache_key *
-storeKeyPublic(const char *url, method_t method)
+storeKeyPublic(const char *url, const HttpRequestMethod& method)
 {
-    static cache_key digest[MD5_DIGEST_CHARS];
-    MD5_CTX M;
-    int n;
-    char key_buf[MAX_URL + 100];
-    n = snprintf(key_buf, MAX_URL + 100, "%s %s",
-       RequestMethodStr[method],
-       url);
-    MD5Init(&M);
-    MD5Update(&M, key_buf, n);
-    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));
+    SquidMD5Final(digest, &M);
     return digest;
 }
 
 const cache_key *
+storeKeyPublicByRequest(HttpRequest * request)
+{
+    return storeKeyPublicByRequestMethod(request, request->method);
+}
+
+const cache_key *
+storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
+{
+    static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
+    unsigned char m = (unsigned char) method.id();
+    const char *url = request->storeId(); /* storeId returns the right storeID\canonical URL for the md5 calc */
+    SquidMD5_CTX M;
+    SquidMD5Init(&M);
+    SquidMD5Update(&M, &m, sizeof(m));
+    SquidMD5Update(&M, (unsigned char *) url, strlen(url));
+
+    if (request->vary_headers) {
+        SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
+        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 = xmalloc(MD5_DIGEST_CHARS);
-    xmemcpy(dup, key, MD5_DIGEST_CHARS);
-    meta_data.store_keys += 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)
+{
+    memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
+    return dst;
+}
+
 void
 storeKeyFree(const cache_key * key)
 {
-    xfree((void *) key);
-    meta_data.store_keys -= MD5_DIGEST_CHARS;
+    memFree((void *) key, MEM_MD5_DIGEST);
 }
 
 int
-storeKeyHashBuckets(int nobj)
+storeKeyHashBuckets(int nbuckets)
 {
-    if (nobj < 0x2000)
-       return 0x2000;
-    if (nobj < 0x4000)
-       return 0x4000;
-    if (nobj < 0x8000)
-       return 0x8000;
-    return 0x10000;
+    int n = 0x2000;
+
+    while (n < nbuckets)
+        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);
 }
+