]>
Commit | Line | Data |
---|---|---|
7c887d1f | 1 | |
2 | /* | |
ed013b6c | 3 | * $Id: HttpHdrContRange.cc,v 1.22 2007/08/13 18:25:14 hno Exp $ |
7c887d1f | 4 | * |
5 | * DEBUG: section 68 HTTP Content-Range Header | |
6 | * AUTHOR: Alex Rousskov | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
7c887d1f | 10 | * |
2b6662ba | 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. | |
7c887d1f | 19 | * |
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. | |
24 | * | |
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. | |
29 | * | |
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 | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
7c887d1f | 34 | */ |
35 | ||
36 | #include "squid.h" | |
528b2c61 | 37 | #include "HttpHdrContRange.h" |
7c887d1f | 38 | |
b644367b | 39 | /* |
40 | * Currently only byte ranges are supported | |
41 | * | |
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 | |
49 | */ | |
7c887d1f | 50 | |
51 | ||
52 | /* local constants */ | |
ed013b6c | 53 | #define range_spec_unknown (-1) |
7c887d1f | 54 | |
55 | /* local routines */ | |
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) | |
59 | ||
60 | /* globals */ | |
61 | ||
62 | /* parses range-resp-spec and inits spec, returns true on success */ | |
63 | static int | |
b644367b | 64 | httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec * spec, const char *field, int flen) |
7c887d1f | 65 | { |
66 | const char *p; | |
67 | assert(spec); | |
68 | spec->offset = spec->length = range_spec_unknown; | |
62e76326 | 69 | |
7c887d1f | 70 | if (flen < 2) |
62e76326 | 71 | return 0; |
72 | ||
7c887d1f | 73 | /* is spec given ? */ |
74 | if (*field == '*') | |
62e76326 | 75 | return 1; |
76 | ||
7c887d1f | 77 | /* check format, must be %d-%d */ |
b644367b | 78 | if (!((p = strchr(field, '-')) && (p - field < flen))) { |
bf8fe701 | 79 | debugs(68, 2, "invalid (no '-') resp-range-spec near: '" << field << "'"); |
62e76326 | 80 | return 0; |
7c887d1f | 81 | } |
62e76326 | 82 | |
7c887d1f | 83 | /* parse offset */ |
47f6e231 | 84 | if (!httpHeaderParseOffset(field, &spec->offset)) |
62e76326 | 85 | return 0; |
86 | ||
7c887d1f | 87 | p++; |
62e76326 | 88 | |
7c887d1f | 89 | /* do we have last-pos ? */ |
90 | if (p - field < flen) { | |
47f6e231 | 91 | int64_t last_pos; |
62e76326 | 92 | |
47f6e231 | 93 | if (!httpHeaderParseOffset(p, &last_pos)) |
62e76326 | 94 | return 0; |
95 | ||
96 | spec->length = size_diff(last_pos + 1, spec->offset); | |
7c887d1f | 97 | } |
62e76326 | 98 | |
e6ccf245 | 99 | /* Ensure typecast is safe */ |
100 | assert (spec->length >= 0); | |
62e76326 | 101 | |
7c887d1f | 102 | /* we managed to parse, check if the result makes sence */ |
47f6e231 | 103 | if (known_spec(spec->length) && spec->length == 0) { |
4a7a3d56 | 104 | debugs(68, 2, "invalid range (" << spec->offset << " += " << |
bf8fe701 | 105 | (long int) spec->length << ") in resp-range-spec near: '" << field << "'"); |
62e76326 | 106 | return 0; |
7c887d1f | 107 | } |
62e76326 | 108 | |
7c887d1f | 109 | return 1; |
110 | } | |
111 | ||
112 | static void | |
b644367b | 113 | httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec * spec, Packer * p) |
7c887d1f | 114 | { |
e6ccf245 | 115 | /* Ensure typecast is safe */ |
116 | assert (spec->length >= 0); | |
62e76326 | 117 | |
47f6e231 | 118 | if (!known_spec(spec->offset) || !known_spec(spec->length)) |
62e76326 | 119 | packerPrintf(p, "*"); |
7c887d1f | 120 | else |
47f6e231 | 121 | packerPrintf(p, "bytes %"PRId64"-%"PRId64, |
122 | spec->offset, spec->offset + spec->length - 1); | |
7c887d1f | 123 | } |
124 | ||
125 | /* | |
126 | * Content Range | |
127 | */ | |
128 | ||
129 | HttpHdrContRange * | |
9bc73deb | 130 | httpHdrContRangeCreate(void) |
7c887d1f | 131 | { |
e6ccf245 | 132 | HttpHdrContRange *r = (HttpHdrContRange *)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE); |
7c887d1f | 133 | r->spec.offset = r->spec.length = range_spec_unknown; |
134 | r->elength = range_spec_unknown; | |
135 | return r; | |
136 | } | |
137 | ||
138 | HttpHdrContRange * | |
139 | httpHdrContRangeParseCreate(const char *str) | |
140 | { | |
141 | HttpHdrContRange *r = httpHdrContRangeCreate(); | |
62e76326 | 142 | |
7c887d1f | 143 | if (!httpHdrContRangeParseInit(r, str)) { |
62e76326 | 144 | httpHdrContRangeDestroy(r); |
145 | r = NULL; | |
7c887d1f | 146 | } |
62e76326 | 147 | |
7c887d1f | 148 | return r; |
149 | } | |
150 | ||
151 | /* returns true if ranges are valid; inits HttpHdrContRange */ | |
152 | int | |
b644367b | 153 | httpHdrContRangeParseInit(HttpHdrContRange * range, const char *str) |
7c887d1f | 154 | { |
155 | const char *p; | |
156 | assert(range && str); | |
bf8fe701 | 157 | debugs(68, 8, "parsing content-range field: '" << str << "'"); |
de336bbe | 158 | /* check range type */ |
62e76326 | 159 | |
de336bbe | 160 | if (strncasecmp(str, "bytes ", 6)) |
62e76326 | 161 | return 0; |
162 | ||
de336bbe | 163 | str += 6; |
62e76326 | 164 | |
7c887d1f | 165 | /* split */ |
166 | if (!(p = strchr(str, '/'))) | |
62e76326 | 167 | return 0; |
168 | ||
7c887d1f | 169 | if (*str == '*') |
62e76326 | 170 | range->spec.offset = range->spec.length = range_spec_unknown; |
b644367b | 171 | else if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p - str)) |
62e76326 | 172 | return 0; |
173 | ||
7c887d1f | 174 | p++; |
62e76326 | 175 | |
7c887d1f | 176 | if (*p == '*') |
62e76326 | 177 | range->elength = range_spec_unknown; |
47f6e231 | 178 | else if (!httpHeaderParseOffset(p, &range->elength)) |
62e76326 | 179 | return 0; |
180 | ||
bf8fe701 | 181 | debugs(68, 8, "parsed content-range field: " << |
182 | (long int) range->spec.offset << "-" << | |
183 | (long int) range->spec.offset + range->spec.length - 1 << " / " << | |
184 | (long int) range->elength); | |
62e76326 | 185 | |
7c887d1f | 186 | return 1; |
187 | } | |
188 | ||
189 | void | |
b644367b | 190 | httpHdrContRangeDestroy(HttpHdrContRange * range) |
7c887d1f | 191 | { |
192 | assert(range); | |
db1cd23c | 193 | memFree(range, MEM_HTTP_HDR_CONTENT_RANGE); |
7c887d1f | 194 | } |
195 | ||
196 | HttpHdrContRange * | |
197 | httpHdrContRangeDup(const HttpHdrContRange * range) | |
198 | { | |
199 | HttpHdrContRange *dup; | |
200 | assert(range); | |
201 | dup = httpHdrContRangeCreate(); | |
202 | *dup = *range; | |
203 | return dup; | |
204 | } | |
205 | ||
206 | void | |
207 | httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p) | |
208 | { | |
209 | assert(range && p); | |
210 | httpHdrRangeRespSpecPackInto(&range->spec, p); | |
e6ccf245 | 211 | /* Ensure typecast is safe */ |
212 | assert (range->elength >= 0); | |
62e76326 | 213 | |
47f6e231 | 214 | if (!known_spec(range->elength)) |
62e76326 | 215 | packerPrintf(p, "/*"); |
7c887d1f | 216 | else |
47f6e231 | 217 | packerPrintf(p, "/%"PRId64, range->elength); |
7c887d1f | 218 | } |
d192d11f | 219 | |
220 | void | |
47f6e231 | 221 | httpHdrContRangeSet(HttpHdrContRange * cr, HttpHdrRangeSpec spec, int64_t ent_len) |
d192d11f | 222 | { |
223 | assert(cr && ent_len >= 0); | |
224 | cr->spec = spec; | |
225 | cr->elength = ent_len; | |
226 | } |