3 * $Id: HttpHdrContRange.cc,v 1.6 1998/07/20 17:19:02 wessels Exp $
5 * DEBUG: section 68 HTTP Content-Range Header
6 * AUTHOR: Alex Rousskov
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * --------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 * Currently only byte ranges are supported
37 * Content-Range = "Content-Range" ":" content-range-spec
38 * content-range-spec = byte-content-range-spec
39 * byte-content-range-spec = bytes-unit SP
40 * ( byte-range-resp-spec | "*") "/"
41 * ( entity-length | "*" )
42 * byte-range-resp-spec = first-byte-pos "-" last-byte-pos
43 * entity-length = 1*DIGIT
48 #define range_spec_unknown ((size_t)-1)
51 #define known_spec(s) ((s) != range_spec_unknown)
52 #define size_min(a,b) ((a) <= (b) ? (a) : (b))
53 #define size_diff(a,b) ((a) >= (b) ? ((a)-(b)) : 0)
57 /* parses range-resp-spec and inits spec, returns true on success */
59 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec
* spec
, const char *field
, int flen
)
63 spec
->offset
= spec
->length
= range_spec_unknown
;
69 /* check format, must be %d-%d */
70 if (!((p
= strchr(field
, '-')) && (p
- field
< flen
))) {
71 debug(68, 2) ("invalid (no '-') resp-range-spec near: '%s'\n", field
);
75 if (!httpHeaderParseSize(field
, &spec
->offset
))
78 /* do we have last-pos ? */
79 if (p
- field
< flen
) {
81 if (!httpHeaderParseSize(p
, &last_pos
))
83 spec
->length
= size_diff(last_pos
+ 1, spec
->offset
);
85 /* we managed to parse, check if the result makes sence */
86 if (known_spec(spec
->length
) && !spec
->length
) {
87 debug(68, 2) ("invalid range (%d += %d) in resp-range-spec near: '%s'\n",
88 spec
->offset
, spec
->length
, field
);
95 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec
* spec
, Packer
* p
)
97 if (!known_spec(spec
->offset
) || !known_spec(spec
->length
))
100 packerPrintf(p
, "%d-%d",
101 spec
->offset
, spec
->offset
+ spec
->length
- 1);
109 httpHdrContRangeCreate()
111 HttpHdrContRange
*r
= memAllocate(MEM_HTTP_HDR_CONTENT_RANGE
);
112 r
->spec
.offset
= r
->spec
.length
= range_spec_unknown
;
113 r
->elength
= range_spec_unknown
;
118 httpHdrContRangeParseCreate(const char *str
)
120 HttpHdrContRange
*r
= httpHdrContRangeCreate();
121 if (!httpHdrContRangeParseInit(r
, str
)) {
122 httpHdrContRangeDestroy(r
);
128 /* returns true if ranges are valid; inits HttpHdrContRange */
130 httpHdrContRangeParseInit(HttpHdrContRange
* range
, const char *str
)
133 assert(range
&& str
);
134 debug(68, 8) ("parsing content-range field: '%s'\n", str
);
135 /* check range type */
136 if (strncasecmp(str
, "bytes ", 6))
140 if (!(p
= strchr(str
, '/')))
143 range
->spec
.offset
= range
->spec
.length
= range_spec_unknown
;
144 else if (!httpHdrRangeRespSpecParseInit(&range
->spec
, str
, p
- str
))
148 range
->elength
= range_spec_unknown
;
149 else if (!httpHeaderParseSize(p
, &range
->elength
))
151 debug(68, 8) ("parsed content-range field: %d-%d / %d\n",
152 range
->spec
.offset
, range
->spec
.offset
+ range
->spec
.length
- 1,
158 httpHdrContRangeDestroy(HttpHdrContRange
* range
)
161 memFree(MEM_HTTP_HDR_CONTENT_RANGE
, range
);
165 httpHdrContRangeDup(const HttpHdrContRange
* range
)
167 HttpHdrContRange
*dup
;
169 dup
= httpHdrContRangeCreate();
175 httpHdrContRangePackInto(const HttpHdrContRange
* range
, Packer
* p
)
178 httpHdrRangeRespSpecPackInto(&range
->spec
, p
);
179 if (!known_spec(range
->elength
))
180 packerPrintf(p
, "/*");
182 packerPrintf(p
, "/%d", range
->elength
);
186 httpHdrContRangeSet(HttpHdrContRange
*cr
, HttpHdrRangeSpec spec
, size_t ent_len
)
188 assert(cr
&& ent_len
>= 0);
190 cr
->elength
= ent_len
;