]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrContRange.cc
Removed squid-old.h
[thirdparty/squid.git] / src / HttpHdrContRange.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 68 HTTP Content-Range Header
6 * AUTHOR: Alex Rousskov
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.
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
36 #include "squid.h"
37 #include "Debug.h"
38 #include "enums.h"
39 #include "HttpHdrContRange.h"
40 #include "HttpHeaderTools.h"
41 #include "protos.h"
42
43 /*
44 * Currently only byte ranges are supported
45 *
46 * Content-Range = "Content-Range" ":" content-range-spec
47 * content-range-spec = byte-content-range-spec
48 * byte-content-range-spec = bytes-unit SP
49 * ( byte-range-resp-spec | "*") "/"
50 * ( entity-length | "*" )
51 * byte-range-resp-spec = first-byte-pos "-" last-byte-pos
52 * entity-length = 1*DIGIT
53 */
54
55
56 /* local constants */
57 #define range_spec_unknown (-1)
58
59 /* local routines */
60 #define known_spec(s) ((s) != range_spec_unknown)
61 #define size_min(a,b) ((a) <= (b) ? (a) : (b))
62 #define size_diff(a,b) ((a) >= (b) ? ((a)-(b)) : 0)
63
64 /* globals */
65
66 /* parses range-resp-spec and inits spec, returns true on success */
67 static int
68 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec * spec, const char *field, int flen)
69 {
70 const char *p;
71 assert(spec);
72 spec->offset = spec->length = range_spec_unknown;
73
74 if (flen < 2)
75 return 0;
76
77 /* is spec given ? */
78 if (*field == '*')
79 return 1;
80
81 /* check format, must be %d-%d */
82 if (!((p = strchr(field, '-')) && (p - field < flen))) {
83 debugs(68, 2, "invalid (no '-') resp-range-spec near: '" << field << "'");
84 return 0;
85 }
86
87 /* parse offset */
88 if (!httpHeaderParseOffset(field, &spec->offset))
89 return 0;
90
91 /* Additional check for BUG2155 - there MUST BE first-byte-pos and it MUST be positive*/
92 if (spec->offset < 0) {
93 debugs(68, 2, "invalid (no first-byte-pos or it is negative) resp-range-spec near: '" << field << "'");
94 return 0;
95 }
96
97 ++p;
98
99 /* do we have last-pos ? */
100 if (p - field >= flen) {
101 debugs(68, 2, "invalid (no last-byte-pos) resp-range-spec near: '" << field << "'");
102 return 0;
103 }
104
105 int64_t last_pos;
106
107 if (!httpHeaderParseOffset(p, &last_pos))
108 return 0;
109
110 if (last_pos < spec->offset) {
111 debugs(68, 2, "invalid (negative last-byte-pos) resp-range-spec near: '" << field << "'");
112 return 0;
113 }
114
115 spec->length = size_diff(last_pos + 1, spec->offset);
116
117 /* we managed to parse, check if the result makes sence */
118 if (spec->length <= 0) {
119 debugs(68, 2, "invalid range (" << spec->offset << " += " <<
120 (long int) spec->length << ") in resp-range-spec near: '" << field << "'");
121 return 0;
122 }
123
124 return 1;
125 }
126
127 static void
128 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec * spec, Packer * p)
129 {
130 /* Ensure typecast is safe */
131 assert (spec->length >= 0);
132
133 if (!known_spec(spec->offset) || !known_spec(spec->length))
134 packerPrintf(p, "*");
135 else
136 packerPrintf(p, "bytes %" PRId64 "-%" PRId64,
137 spec->offset, spec->offset + spec->length - 1);
138 }
139
140 /*
141 * Content Range
142 */
143
144 HttpHdrContRange *
145 httpHdrContRangeCreate(void)
146 {
147 HttpHdrContRange *r = (HttpHdrContRange *)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE);
148 r->spec.offset = r->spec.length = range_spec_unknown;
149 r->elength = range_spec_unknown;
150 return r;
151 }
152
153 HttpHdrContRange *
154 httpHdrContRangeParseCreate(const char *str)
155 {
156 HttpHdrContRange *r = httpHdrContRangeCreate();
157
158 if (!httpHdrContRangeParseInit(r, str)) {
159 httpHdrContRangeDestroy(r);
160 r = NULL;
161 }
162
163 return r;
164 }
165
166 /* returns true if ranges are valid; inits HttpHdrContRange */
167 int
168 httpHdrContRangeParseInit(HttpHdrContRange * range, const char *str)
169 {
170 const char *p;
171 assert(range && str);
172 debugs(68, 8, "parsing content-range field: '" << str << "'");
173 /* check range type */
174
175 if (strncasecmp(str, "bytes ", 6))
176 return 0;
177
178 str += 6;
179
180 /* split */
181 if (!(p = strchr(str, '/')))
182 return 0;
183
184 if (*str == '*')
185 range->spec.offset = range->spec.length = range_spec_unknown;
186 else if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p - str))
187 return 0;
188
189 ++p;
190
191 if (*p == '*')
192 range->elength = range_spec_unknown;
193 else if (!httpHeaderParseOffset(p, &range->elength))
194 return 0;
195 else if (range->elength <= 0) {
196 /* Additional paranoidal check for BUG2155 - entity-length MUST be > 0 */
197 debugs(68, 2, "invalid (entity-length is negative) content-range-spec near: '" << str << "'");
198 return 0;
199 } else if (known_spec(range->spec.length) && range->elength < (range->spec.offset + range->spec.length)) {
200 debugs(68, 2, "invalid (range is outside entity-length) content-range-spec near: '" << str << "'");
201 return 0;
202 }
203
204 debugs(68, 8, "parsed content-range field: " <<
205 (long int) range->spec.offset << "-" <<
206 (long int) range->spec.offset + range->spec.length - 1 << " / " <<
207 (long int) range->elength);
208
209 return 1;
210 }
211
212 void
213 httpHdrContRangeDestroy(HttpHdrContRange * range)
214 {
215 assert(range);
216 memFree(range, MEM_HTTP_HDR_CONTENT_RANGE);
217 }
218
219 HttpHdrContRange *
220 httpHdrContRangeDup(const HttpHdrContRange * range)
221 {
222 HttpHdrContRange *dup;
223 assert(range);
224 dup = httpHdrContRangeCreate();
225 *dup = *range;
226 return dup;
227 }
228
229 void
230 httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p)
231 {
232 assert(range && p);
233 httpHdrRangeRespSpecPackInto(&range->spec, p);
234 /* Ensure typecast is safe */
235 assert (range->elength >= 0);
236
237 if (!known_spec(range->elength))
238 packerPrintf(p, "/*");
239 else
240 packerPrintf(p, "/%" PRId64, range->elength);
241 }
242
243 void
244 httpHdrContRangeSet(HttpHdrContRange * cr, HttpHdrRangeSpec spec, int64_t ent_len)
245 {
246 assert(cr && ent_len >= 0);
247 cr->spec = spec;
248 cr->elength = ent_len;
249 }