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