3 * $Id: HttpHdrContRange.cc,v 1.1 1998/03/08 21:03:01 rousskov 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., 675 Mass Ave, Cambridge, MA 02139, 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 resp-range-spec near: '%s'\n", field
);
75 if (!httpHeaderParseSize(field
+1, &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
, 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 resp-range-spec near: '%s'\n", field
);
94 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec
*spec
, Packer
*p
)
96 if (!known_spec(spec
->offset
) || !known_spec(spec
->length
))
99 packerPrintf(p
, "%d-%d",
100 spec
->offset
, spec
->offset
+spec
->length
-1);
108 httpHdrContRangeCreate()
110 HttpHdrContRange
*r
= memAllocate(MEM_HTTP_HDR_CONTENT_RANGE
);
111 r
->spec
.offset
= r
->spec
.length
= range_spec_unknown
;
112 r
->elength
= range_spec_unknown
;
117 httpHdrContRangeParseCreate(const char *str
)
119 HttpHdrContRange
*r
= httpHdrContRangeCreate();
120 if (!httpHdrContRangeParseInit(r
, str
)) {
121 httpHdrContRangeDestroy(r
);
127 /* returns true if ranges are valid; inits HttpHdrContRange */
129 httpHdrContRangeParseInit(HttpHdrContRange
*range
, const char *str
)
132 assert(range
&& str
);
134 if (!(p
= strchr(str
, '/')))
137 range
->spec
.offset
= range
->spec
.length
= range_spec_unknown
;
139 if (!httpHdrRangeRespSpecParseInit(&range
->spec
, str
, p
-str
))
143 range
->elength
= range_spec_unknown
;
145 if (!httpHeaderParseSize(p
, &range
->elength
))
151 httpHdrContRangeDestroy(HttpHdrContRange
*range
)
154 memFree(MEM_HTTP_HDR_CONTENT_RANGE
, range
);
158 httpHdrContRangeDup(const HttpHdrContRange
* range
)
160 HttpHdrContRange
*dup
;
162 dup
= httpHdrContRangeCreate();
168 httpHdrContRangePackInto(const HttpHdrContRange
* range
, Packer
* p
)
171 httpHdrRangeRespSpecPackInto(&range
->spec
, p
);
172 if (!known_spec(range
->elength
))
173 packerPrintf(p
, "/*");
175 packerPrintf(p
, "/%d", range
->elength
);