]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_swapmeta.cc
add range header, Range objects represent [start,end)
[thirdparty/squid.git] / src / store_swapmeta.cc
CommitLineData
25535cbe 1
9cef6668 2/*
332dafa2 3 * $Id: store_swapmeta.cc,v 1.19 2002/10/15 08:03:31 robertc Exp $
9cef6668 4 *
5 * DEBUG: section 20 Storage Manager Swapfile Metadata
6 * AUTHOR: Kostas Anagnostakis
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9cef6668 9 * ----------------------------------------------------------
10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
9cef6668 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
f09f5b26 36#include "squid.h"
e6ccf245 37#include "Store.h"
f09f5b26 38
69c01cd7 39static tlv **
40storeSwapTLVAdd(int type, const void *ptr, size_t len, tlv ** tail)
f09f5b26 41{
e6ccf245 42 tlv *t = (tlv *)memAllocate(MEM_TLV);
69c01cd7 43 t->type = (char) type;
44 t->length = (int) len;
45 t->value = xmalloc(len);
46 xmemcpy(t->value, ptr, len);
47 *tail = t;
48 return &t->next; /* return new tail pointer */
f09f5b26 49}
50
69c01cd7 51void
52storeSwapTLVFree(tlv * n)
f09f5b26 53{
69c01cd7 54 tlv *t;
55 while ((t = n) != NULL) {
56 n = t->next;
57 xfree(t->value);
58cd5bbd 58 memFree(t, MEM_TLV);
f09f5b26 59 }
f09f5b26 60}
61
69c01cd7 62/*
63 * Build a TLV list for a StoreEntry
64 */
65tlv *
66storeSwapMetaBuild(StoreEntry * e)
f09f5b26 67{
69c01cd7 68 tlv *TLV = NULL; /* we'll return this */
69 tlv **T = &TLV;
70 const char *url;
f66a9ef4 71 const char *vary;
10602161 72 assert(e->mem_obj != NULL);
69c01cd7 73 assert(e->swap_status == SWAPOUT_WRITING);
74 url = storeUrl(e);
75 debug(20, 3) ("storeSwapMetaBuild: %s\n", url);
332dafa2 76 T = storeSwapTLVAdd(STORE_META_KEY, e->key, MD5_DIGEST_CHARS, T);
69c01cd7 77 T = storeSwapTLVAdd(STORE_META_STD, &e->timestamp, STORE_HDR_METASIZE, T);
5830cdb3 78 T = storeSwapTLVAdd(STORE_META_URL, url, strlen(url) + 1, T);
f66a9ef4 79 vary = e->mem_obj->vary_headers;
80 if (vary)
81 T = storeSwapTLVAdd(STORE_META_VARY_HEADERS, vary, strlen(vary) + 1, T);
69c01cd7 82 return TLV;
f09f5b26 83}
84
69c01cd7 85char *
86storeSwapMetaPack(tlv * tlv_list, int *length)
f09f5b26 87{
e3ef2b09 88 int buflen = 0;
69c01cd7 89 tlv *t;
90 off_t j = 0;
91 char *buf;
92 assert(length != NULL);
93 buflen++; /* STORE_META_OK */
94 buflen += sizeof(int); /* size of header to follow */
95 for (t = tlv_list; t; t = t->next)
e3ef2b09 96 buflen += sizeof(char) + sizeof(int) + t->length;
69c01cd7 97 buflen++; /* STORE_META_END */
e6ccf245 98 buf = (char *)xmalloc(buflen);
69c01cd7 99 buf[j++] = (char) STORE_META_OK;
e3ef2b09 100 xmemcpy(&buf[j], &buflen, sizeof(int));
101 j += sizeof(int);
69c01cd7 102 for (t = tlv_list; t; t = t->next) {
103 buf[j++] = (char) t->type;
104 xmemcpy(&buf[j], &t->length, sizeof(int));
105 j += sizeof(int);
106 xmemcpy(&buf[j], t->value, t->length);
107 j += t->length;
f09f5b26 108 }
69c01cd7 109 buf[j++] = (char) STORE_META_END;
e3ef2b09 110 assert((int) j == buflen);
111 *length = buflen;
69c01cd7 112 return buf;
f09f5b26 113}
114
69c01cd7 115tlv *
116storeSwapMetaUnpack(const char *buf, int *hdr_len)
f09f5b26 117{
69c01cd7 118 tlv *TLV; /* we'll return this */
119 tlv **T = &TLV;
120 char type;
121 int length;
122 int buflen;
123 off_t j = 0;
69c01cd7 124 assert(buf != NULL);
125 assert(hdr_len != NULL);
126 if (buf[j++] != (char) STORE_META_OK)
127 return NULL;
128 xmemcpy(&buflen, &buf[j], sizeof(int));
129 j += sizeof(int);
42b51993 130 /*
131 * sanity check on 'buflen' value. It should be at least big
132 * enough to hold one type and one length.
133 */
e6ccf245 134 if (buflen <= (off_t) (sizeof(char) + sizeof(int)))
42b51993 135 return NULL;
e6ccf245 136 while (buflen - j > (off_t)(sizeof(char) + sizeof(int))) {
69c01cd7 137 type = buf[j++];
5f341392 138 /* VOID is reserved, but allow some slack for new types.. */
139 if (type <= STORE_META_VOID || type > STORE_META_END + 10) {
fff97a60 140 debug(20, 0) ("storeSwapMetaUnpack: bad type (%d)!\n", type);
141 break;
142 }
69c01cd7 143 xmemcpy(&length, &buf[j], sizeof(int));
5ba01c92 144 if (length < 0 || length > (1 << 16)) {
fff97a60 145 debug(20, 0) ("storeSwapMetaUnpack: insane length (%d)!\n", length);
146 break;
147 }
69c01cd7 148 j += sizeof(int);
149 if (j + length > buflen) {
150 debug(20, 0) ("storeSwapMetaUnpack: overflow!\n");
151 debug(20, 0) ("\ttype=%d, length=%d, buflen=%d, offset=%d\n",
152 type, length, buflen, (int) j);
153 break;
154 }
155 T = storeSwapTLVAdd(type, &buf[j], (size_t) length, T);
156 j += length;
157 }
e3ef2b09 158 *hdr_len = buflen;
69c01cd7 159 return TLV;
f09f5b26 160}