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