]> git.ipfire.org Git - thirdparty/squid.git/blame - src/esi/Segment.cc
Fix switch wrapping in log Format custom
[thirdparty/squid.git] / src / esi / Segment.cc
CommitLineData
43ae1d95 1
2/*
262a0e14 3 * $Id$
43ae1d95 4 *
5 * DEBUG: section 86 ESI processing
6 * AUTHOR: Robert Collins
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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.
26ac0430 24 *
43ae1d95 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.
26ac0430 29 *
43ae1d95 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
36#include "squid.h"
f99c2cfe 37#include "esi/Segment.h"
43ae1d95 38#include "SquidString.h"
39
40CBDATA_TYPE(ESISegment);
41
42/* ESISegment */
43void
44ESISegmentFreeList (ESISegment::Pointer &head)
45{
46 while (head.getRaw()) {
47 ESISegment::Pointer temp = head;
48 head = head->next;
49 temp->next = NULL;
50 }
51}
52
53size_t
54ESISegment::space() const
55{
56 assert (len <= sizeof(buf));
57 return sizeof (buf) - len;
58}
59
60void
61ESISegment::adsorbList (ESISegment::Pointer from)
62{
63 assert (next.getRaw() == NULL);
64 assert (from.getRaw() != NULL);
65 /* prevent worst case */
66 assert (!(len == 0 && from->len == space() ));
67 Pointer copyFrom = from;
68
69 while (copyFrom.getRaw() && space() >= copyFrom->len) {
70 assert (append (copyFrom) == copyFrom->len);
71 copyFrom = copyFrom->next;
72 }
73
74 next = copyFrom;
75}
76
77void
78ESISegment::ListTransfer (ESISegment::Pointer &from, ESISegment::Pointer &to)
79{
80 if (!to.getRaw()) {
81 to = from;
82 from = NULL;
83 return;
84 }
85
86 ESISegment::Pointer temp = to->tail();
87 temp->adsorbList (from);
88 from = NULL;
89}
90
91size_t
92ESISegment::listLength() const
93{
94 size_t result = 0;
95 ESISegment const* temp = this;
96
97 while (temp) {
98 result += temp->len;
99 temp = temp->next.getRaw();
100 }
101
102 return result;
103}
104
105char *
106ESISegment::listToChar() const
107{
108 size_t length = listLength();
109 char *rv = (char *)xmalloc (length + 1);
110 assert (rv);
111 rv [length] = '\0';
112
113 ESISegment::Pointer temp = this;
114 size_t pos = 0;
115
116 while (temp.getRaw()) {
117 xmemcpy (&rv[pos], temp->buf, temp->len);
118 pos += temp->len;
119 temp = temp->next;
120 }
121
122 return rv;
123}
124
125void
126ESISegment::listAppend (char const *s, size_t length)
127{
128 assert (next.getRaw() == NULL);
129 ESISegment::Pointer output = this;
130 /* copy the string to output */
131 size_t pos=0;
132
133 while (pos < length) {
134 if (output->space() == 0) {
135 assert (output->next.getRaw() == NULL);
136 output->next = new ESISegment;
137 output = output->next;
138 }
139
140 pos += output->append(s + pos, length - pos);
141 }
142}
143
144void
145ESISegment::ListAppend (ESISegment::Pointer &head, char const *s, size_t len)
146{
147 if (!head.getRaw())
148 head = new ESISegment;
149
150 head->tail()->listAppend (s, len);
151}
152
153void *
154ESISegment::operator new(size_t byteCount)
155{
156 assert (byteCount == sizeof (ESISegment));
157 void *rv;
158 CBDATA_INIT_TYPE(ESISegment);
159 rv = (void *)cbdataAlloc (ESISegment);
160 return rv;
161}
162
163void
164ESISegment::operator delete (void *address)
165{
101a3d6f 166 cbdataFree (address);
43ae1d95 167}
168
43ae1d95 169/* XXX: if needed, make this iterative */
170ESISegment::Pointer
171ESISegment::cloneList () const
172{
173 ESISegment::Pointer result = new ESISegment (*this);
174 result->next = next.getRaw() ? next->cloneList() : NULL;
175 return result;
176}
177
178size_t
179ESISegment::append(char const *appendBuffer, size_t appendLength)
180{
181 size_t toCopy = min(appendLength, space());
182 xmemcpy (&buf[len], appendBuffer, toCopy);
183 len += toCopy;
184 return toCopy;
185}
186
187size_t
188ESISegment::append(ESISegment::Pointer from)
189{
190 return append (from->buf, from->len);
191}
192
193ESISegment const *
194ESISegment::tail() const
195{
196 ESISegment const *result = this;
197
198 while (result->next.getRaw())
199 result = result->next.getRaw();
200
201 return result;
202}
203
204ESISegment *
205ESISegment::tail()
206{
207 ESISegment::Pointer result = this;
208
209 while (result->next.getRaw())
210 result = result->next;
211
212 return result.getRaw();
213}
214
215ESISegment::ESISegment() : len(0), next(NULL)
216{}
217
218ESISegment::ESISegment(ESISegment const &old) : len (0), next(NULL)
219{
220 append (old.buf, old.len);
221}
222
223void
224ESISegment::dumpToLog() const
225{
226 ESISegment::Pointer temp = this;
227
228 while (temp.getRaw()) {
229 temp->dumpOne();
230 temp = temp->next;
231 }
232}
233
234void
235ESISegment::dumpOne() const
236{
30abd221 237 String temp;
43ae1d95 238 temp.limitInit(buf, len);
17bc3f22 239 debugs(86, 9, "ESISegment::dumpOne: \"" << temp << "\"");
43ae1d95 240}