2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 68 HTTP Content-Range Header */
12 #include "base/Packable.h"
15 #include "HttpHdrContRange.h"
16 #include "HttpHeaderTools.h"
19 * Currently only byte ranges are supported
21 * Content-Range = "Content-Range" ":" content-range-spec
22 * content-range-spec = byte-content-range-spec
23 * byte-content-range-spec = bytes-unit SP
24 * ( byte-range-resp-spec | "*") "/"
25 * ( entity-length | "*" )
26 * byte-range-resp-spec = first-byte-pos "-" last-byte-pos
27 * entity-length = 1*DIGIT
31 #define range_spec_unknown (-1)
34 #define known_spec(s) ((s) != range_spec_unknown)
35 #define size_min(a,b) ((a) <= (b) ? (a) : (b))
36 #define size_diff(a,b) ((a) >= (b) ? ((a)-(b)) : 0)
40 /* parses range-resp-spec and inits spec, returns true on success */
42 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec
* spec
, const char *field
, int flen
)
46 spec
->offset
= spec
->length
= range_spec_unknown
;
55 /* check format, must be %d-%d */
56 if (!((p
= strchr(field
, '-')) && (p
- field
< flen
))) {
57 debugs(68, 2, "invalid (no '-') resp-range-spec near: '" << field
<< "'");
62 if (!httpHeaderParseOffset(field
, &spec
->offset
))
65 /* Additional check for BUG2155 - there MUST BE first-byte-pos and it MUST be positive*/
66 if (spec
->offset
< 0) {
67 debugs(68, 2, "invalid (no first-byte-pos or it is negative) resp-range-spec near: '" << field
<< "'");
73 /* do we have last-pos ? */
74 if (p
- field
>= flen
) {
75 debugs(68, 2, "invalid (no last-byte-pos) resp-range-spec near: '" << field
<< "'");
81 if (!httpHeaderParseOffset(p
, &last_pos
))
84 if (last_pos
< spec
->offset
) {
85 debugs(68, 2, "invalid (negative last-byte-pos) resp-range-spec near: '" << field
<< "'");
89 spec
->length
= size_diff(last_pos
+ 1, spec
->offset
);
91 /* we managed to parse, check if the result makes sence */
92 if (spec
->length
<= 0) {
93 debugs(68, 2, "invalid range (" << spec
->offset
<< " += " <<
94 (long int) spec
->length
<< ") in resp-range-spec near: '" << field
<< "'");
102 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec
* spec
, Packable
* p
)
104 /* Ensure typecast is safe */
105 assert (spec
->length
>= 0);
107 if (!known_spec(spec
->offset
) || !known_spec(spec
->length
))
110 p
->appendf("bytes %" PRId64
"-%" PRId64
, spec
->offset
, spec
->offset
+ spec
->length
- 1);
118 httpHdrContRangeCreate(void)
120 HttpHdrContRange
*r
= (HttpHdrContRange
*)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE
);
121 r
->spec
.offset
= r
->spec
.length
= range_spec_unknown
;
122 r
->elength
= range_spec_unknown
;
127 httpHdrContRangeParseCreate(const char *str
)
129 HttpHdrContRange
*r
= httpHdrContRangeCreate();
131 if (!httpHdrContRangeParseInit(r
, str
)) {
132 httpHdrContRangeDestroy(r
);
139 /* returns true if ranges are valid; inits HttpHdrContRange */
141 httpHdrContRangeParseInit(HttpHdrContRange
* range
, const char *str
)
144 assert(range
&& str
);
145 debugs(68, 8, "parsing content-range field: '" << str
<< "'");
146 /* check range type */
148 if (strncasecmp(str
, "bytes ", 6))
154 if (!(p
= strchr(str
, '/')))
158 range
->spec
.offset
= range
->spec
.length
= range_spec_unknown
;
159 else if (!httpHdrRangeRespSpecParseInit(&range
->spec
, str
, p
- str
))
165 range
->elength
= range_spec_unknown
;
166 else if (!httpHeaderParseOffset(p
, &range
->elength
))
168 else if (range
->elength
<= 0) {
169 /* Additional paranoidal check for BUG2155 - entity-length MUST be > 0 */
170 debugs(68, 2, "invalid (entity-length is negative) content-range-spec near: '" << str
<< "'");
172 } else if (known_spec(range
->spec
.length
) && range
->elength
< (range
->spec
.offset
+ range
->spec
.length
)) {
173 debugs(68, 2, "invalid (range is outside entity-length) content-range-spec near: '" << str
<< "'");
177 debugs(68, 8, "parsed content-range field: " <<
178 (long int) range
->spec
.offset
<< "-" <<
179 (long int) range
->spec
.offset
+ range
->spec
.length
- 1 << " / " <<
180 (long int) range
->elength
);
186 httpHdrContRangeDestroy(HttpHdrContRange
* range
)
189 memFree(range
, MEM_HTTP_HDR_CONTENT_RANGE
);
193 httpHdrContRangeDup(const HttpHdrContRange
* range
)
195 HttpHdrContRange
*dup
;
197 dup
= httpHdrContRangeCreate();
203 httpHdrContRangePackInto(const HttpHdrContRange
* range
, Packable
* p
)
206 httpHdrRangeRespSpecPackInto(&range
->spec
, p
);
207 /* Ensure typecast is safe */
208 assert (range
->elength
>= 0);
210 if (!known_spec(range
->elength
))
213 p
->appendf("/%" PRId64
, range
->elength
);
217 httpHdrContRangeSet(HttpHdrContRange
* cr
, HttpHdrRangeSpec spec
, int64_t ent_len
)
219 assert(cr
&& ent_len
>= 0);
221 cr
->elength
= ent_len
;