3 * $Id: HttpHdrContRange.cc,v 1.10 1998/12/05 00:54:09 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 the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * 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.
39 * Currently only byte ranges are supported
41 * Content-Range = "Content-Range" ":" content-range-spec
42 * content-range-spec = byte-content-range-spec
43 * byte-content-range-spec = bytes-unit SP
44 * ( byte-range-resp-spec | "*") "/"
45 * ( entity-length | "*" )
46 * byte-range-resp-spec = first-byte-pos "-" last-byte-pos
47 * entity-length = 1*DIGIT
52 #define range_spec_unknown ((size_t)-1)
55 #define known_spec(s) ((s) != range_spec_unknown)
56 #define size_min(a,b) ((a) <= (b) ? (a) : (b))
57 #define size_diff(a,b) ((a) >= (b) ? ((a)-(b)) : 0)
61 /* parses range-resp-spec and inits spec, returns true on success */
63 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec
* spec
, const char *field
, int flen
)
67 spec
->offset
= spec
->length
= range_spec_unknown
;
73 /* check format, must be %d-%d */
74 if (!((p
= strchr(field
, '-')) && (p
- field
< flen
))) {
75 debug(68, 2) ("invalid (no '-') resp-range-spec near: '%s'\n", field
);
79 if (!httpHeaderParseSize(field
, &spec
->offset
))
82 /* do we have last-pos ? */
83 if (p
- field
< flen
) {
85 if (!httpHeaderParseSize(p
, &last_pos
))
87 spec
->length
= size_diff(last_pos
+ 1, spec
->offset
);
89 /* we managed to parse, check if the result makes sence */
90 if (known_spec(spec
->length
) && !spec
->length
) {
91 debug(68, 2) ("invalid range (%d += %d) in resp-range-spec near: '%s'\n",
92 spec
->offset
, spec
->length
, field
);
99 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec
* spec
, Packer
* p
)
101 if (!known_spec(spec
->offset
) || !known_spec(spec
->length
))
102 packerPrintf(p
, "*");
104 packerPrintf(p
, "bytes %d-%d",
105 spec
->offset
, spec
->offset
+ spec
->length
- 1);
113 httpHdrContRangeCreate()
115 HttpHdrContRange
*r
= memAllocate(MEM_HTTP_HDR_CONTENT_RANGE
);
116 r
->spec
.offset
= r
->spec
.length
= range_spec_unknown
;
117 r
->elength
= range_spec_unknown
;
122 httpHdrContRangeParseCreate(const char *str
)
124 HttpHdrContRange
*r
= httpHdrContRangeCreate();
125 if (!httpHdrContRangeParseInit(r
, str
)) {
126 httpHdrContRangeDestroy(r
);
132 /* returns true if ranges are valid; inits HttpHdrContRange */
134 httpHdrContRangeParseInit(HttpHdrContRange
* range
, const char *str
)
137 assert(range
&& str
);
138 debug(68, 8) ("parsing content-range field: '%s'\n", str
);
139 /* check range type */
140 if (strncasecmp(str
, "bytes ", 6))
144 if (!(p
= strchr(str
, '/')))
147 range
->spec
.offset
= range
->spec
.length
= range_spec_unknown
;
148 else if (!httpHdrRangeRespSpecParseInit(&range
->spec
, str
, p
- str
))
152 range
->elength
= range_spec_unknown
;
153 else if (!httpHeaderParseSize(p
, &range
->elength
))
155 debug(68, 8) ("parsed content-range field: %d-%d / %d\n",
156 range
->spec
.offset
, range
->spec
.offset
+ range
->spec
.length
- 1,
162 httpHdrContRangeDestroy(HttpHdrContRange
* range
)
165 memFree(range
, MEM_HTTP_HDR_CONTENT_RANGE
);
169 httpHdrContRangeDup(const HttpHdrContRange
* range
)
171 HttpHdrContRange
*dup
;
173 dup
= httpHdrContRangeCreate();
179 httpHdrContRangePackInto(const HttpHdrContRange
* range
, Packer
* p
)
182 httpHdrRangeRespSpecPackInto(&range
->spec
, p
);
183 if (!known_spec(range
->elength
))
184 packerPrintf(p
, "/*");
186 packerPrintf(p
, "/%d", range
->elength
);
190 httpHdrContRangeSet(HttpHdrContRange
* cr
, HttpHdrRangeSpec spec
, size_t ent_len
)
192 assert(cr
&& ent_len
>= 0);
194 cr
->elength
= ent_len
;