3 * $Id: HttpHdrContRange.cc,v 1.22 2007/08/13 18:25:14 hno Exp $
5 * DEBUG: section 68 HTTP Content-Range Header
6 * AUTHOR: Alex Rousskov
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
37 #include "HttpHdrContRange.h"
40 * Currently only byte ranges are supported
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
53 #define range_spec_unknown (-1)
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)
62 /* parses range-resp-spec and inits spec, returns true on success */
64 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec
* spec
, const char *field
, int flen
)
68 spec
->offset
= spec
->length
= range_spec_unknown
;
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
<< "'");
84 if (!httpHeaderParseOffset(field
, &spec
->offset
))
89 /* do we have last-pos ? */
90 if (p
- field
< flen
) {
93 if (!httpHeaderParseOffset(p
, &last_pos
))
96 spec
->length
= size_diff(last_pos
+ 1, spec
->offset
);
97 /* Ensure typecast is safe */
98 assert (spec
->length
>= 0);
101 /* we managed to parse, check if the result makes sence */
102 if (known_spec(spec
->length
) && spec
->length
== 0) {
103 debugs(68, 2, "invalid range (" << spec
->offset
<< " += " <<
104 (long int) spec
->length
<< ") in resp-range-spec near: '" << field
<< "'");
112 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec
* spec
, Packer
* p
)
114 /* Ensure typecast is safe */
115 assert (spec
->length
>= 0);
117 if (!known_spec(spec
->offset
) || !known_spec(spec
->length
))
118 packerPrintf(p
, "*");
120 packerPrintf(p
, "bytes %"PRId64
"-%"PRId64
,
121 spec
->offset
, spec
->offset
+ spec
->length
- 1);
129 httpHdrContRangeCreate(void)
131 HttpHdrContRange
*r
= (HttpHdrContRange
*)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE
);
132 r
->spec
.offset
= r
->spec
.length
= range_spec_unknown
;
133 r
->elength
= range_spec_unknown
;
138 httpHdrContRangeParseCreate(const char *str
)
140 HttpHdrContRange
*r
= httpHdrContRangeCreate();
142 if (!httpHdrContRangeParseInit(r
, str
)) {
143 httpHdrContRangeDestroy(r
);
150 /* returns true if ranges are valid; inits HttpHdrContRange */
152 httpHdrContRangeParseInit(HttpHdrContRange
* range
, const char *str
)
155 assert(range
&& str
);
156 debugs(68, 8, "parsing content-range field: '" << str
<< "'");
157 /* check range type */
159 if (strncasecmp(str
, "bytes ", 6))
165 if (!(p
= strchr(str
, '/')))
169 range
->spec
.offset
= range
->spec
.length
= range_spec_unknown
;
170 else if (!httpHdrRangeRespSpecParseInit(&range
->spec
, str
, p
- str
))
176 range
->elength
= range_spec_unknown
;
177 else if (!httpHeaderParseOffset(p
, &range
->elength
))
180 debugs(68, 8, "parsed content-range field: " <<
181 (long int) range
->spec
.offset
<< "-" <<
182 (long int) range
->spec
.offset
+ range
->spec
.length
- 1 << " / " <<
183 (long int) range
->elength
);
189 httpHdrContRangeDestroy(HttpHdrContRange
* range
)
192 memFree(range
, MEM_HTTP_HDR_CONTENT_RANGE
);
196 httpHdrContRangeDup(const HttpHdrContRange
* range
)
198 HttpHdrContRange
*dup
;
200 dup
= httpHdrContRangeCreate();
206 httpHdrContRangePackInto(const HttpHdrContRange
* range
, Packer
* p
)
209 httpHdrRangeRespSpecPackInto(&range
->spec
, p
);
210 /* Ensure typecast is safe */
211 assert (range
->elength
>= 0);
213 if (!known_spec(range
->elength
))
214 packerPrintf(p
, "/*");
216 packerPrintf(p
, "/%"PRId64
, range
->elength
);
220 httpHdrContRangeSet(HttpHdrContRange
* cr
, HttpHdrRangeSpec spec
, int64_t ent_len
)
222 assert(cr
&& ent_len
>= 0);
224 cr
->elength
= ent_len
;