]>
Commit | Line | Data |
---|---|---|
7c887d1f | 1 | |
2 | /* | |
528b2c61 | 3 | * $Id: HttpHdrContRange.cc,v 1.16 2003/01/23 00:37:12 robertc 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 */ | |
53 | #define range_spec_unknown ((size_t)-1) | |
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; | |
69 | if (flen < 2) | |
70 | return 0; | |
71 | /* is spec given ? */ | |
72 | if (*field == '*') | |
de336bbe | 73 | return 1; |
7c887d1f | 74 | /* check format, must be %d-%d */ |
b644367b | 75 | if (!((p = strchr(field, '-')) && (p - field < flen))) { |
de336bbe | 76 | debug(68, 2) ("invalid (no '-') resp-range-spec near: '%s'\n", field); |
7c887d1f | 77 | return 0; |
78 | } | |
79 | /* parse offset */ | |
de336bbe | 80 | if (!httpHeaderParseSize(field, &spec->offset)) |
b644367b | 81 | return 0; |
7c887d1f | 82 | p++; |
83 | /* do we have last-pos ? */ | |
84 | if (p - field < flen) { | |
9bc73deb | 85 | ssize_t last_pos; |
7c887d1f | 86 | if (!httpHeaderParseSize(p, &last_pos)) |
87 | return 0; | |
b644367b | 88 | spec->length = size_diff(last_pos + 1, spec->offset); |
7c887d1f | 89 | } |
e6ccf245 | 90 | /* Ensure typecast is safe */ |
91 | assert (spec->length >= 0); | |
92 | ||
7c887d1f | 93 | /* we managed to parse, check if the result makes sence */ |
528b2c61 | 94 | if (known_spec((size_t)spec->length) && spec->length == 0) { |
ed19251a | 95 | debug(68, 2) ("invalid range (%ld += %ld) in resp-range-spec near: '%s'\n", |
96 | (long int) spec->offset, (long int) spec->length, field); | |
7c887d1f | 97 | return 0; |
98 | } | |
99 | return 1; | |
100 | } | |
101 | ||
102 | static void | |
b644367b | 103 | httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec * spec, Packer * p) |
7c887d1f | 104 | { |
e6ccf245 | 105 | /* Ensure typecast is safe */ |
106 | assert (spec->length >= 0); | |
107 | assert (spec->length >= 0); | |
108 | ||
109 | if (!known_spec((size_t)spec->offset) || !known_spec((size_t)spec->length)) | |
7c887d1f | 110 | packerPrintf(p, "*"); |
111 | else | |
ed19251a | 112 | packerPrintf(p, "bytes %ld-%ld", |
113 | (long int) spec->offset, (long int) spec->offset + spec->length - 1); | |
7c887d1f | 114 | } |
115 | ||
116 | /* | |
117 | * Content Range | |
118 | */ | |
119 | ||
120 | HttpHdrContRange * | |
9bc73deb | 121 | httpHdrContRangeCreate(void) |
7c887d1f | 122 | { |
e6ccf245 | 123 | HttpHdrContRange *r = (HttpHdrContRange *)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE); |
7c887d1f | 124 | r->spec.offset = r->spec.length = range_spec_unknown; |
125 | r->elength = range_spec_unknown; | |
126 | return r; | |
127 | } | |
128 | ||
129 | HttpHdrContRange * | |
130 | httpHdrContRangeParseCreate(const char *str) | |
131 | { | |
132 | HttpHdrContRange *r = httpHdrContRangeCreate(); | |
133 | if (!httpHdrContRangeParseInit(r, str)) { | |
134 | httpHdrContRangeDestroy(r); | |
135 | r = NULL; | |
136 | } | |
137 | return r; | |
138 | } | |
139 | ||
140 | /* returns true if ranges are valid; inits HttpHdrContRange */ | |
141 | int | |
b644367b | 142 | httpHdrContRangeParseInit(HttpHdrContRange * range, const char *str) |
7c887d1f | 143 | { |
144 | const char *p; | |
145 | assert(range && str); | |
de336bbe | 146 | debug(68, 8) ("parsing content-range field: '%s'\n", str); |
147 | /* check range type */ | |
148 | if (strncasecmp(str, "bytes ", 6)) | |
149 | return 0; | |
150 | str += 6; | |
7c887d1f | 151 | /* split */ |
152 | if (!(p = strchr(str, '/'))) | |
153 | return 0; | |
154 | if (*str == '*') | |
155 | range->spec.offset = range->spec.length = range_spec_unknown; | |
b644367b | 156 | else if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p - str)) |
7c887d1f | 157 | return 0; |
158 | p++; | |
159 | if (*p == '*') | |
160 | range->elength = range_spec_unknown; | |
b644367b | 161 | else if (!httpHeaderParseSize(p, &range->elength)) |
7c887d1f | 162 | return 0; |
ed19251a | 163 | debug(68, 8) ("parsed content-range field: %ld-%ld / %ld\n", |
164 | (long int) range->spec.offset, (long int) range->spec.offset + range->spec.length - 1, | |
165 | (long int) range->elength); | |
7c887d1f | 166 | return 1; |
167 | } | |
168 | ||
169 | void | |
b644367b | 170 | httpHdrContRangeDestroy(HttpHdrContRange * range) |
7c887d1f | 171 | { |
172 | assert(range); | |
db1cd23c | 173 | memFree(range, MEM_HTTP_HDR_CONTENT_RANGE); |
7c887d1f | 174 | } |
175 | ||
176 | HttpHdrContRange * | |
177 | httpHdrContRangeDup(const HttpHdrContRange * range) | |
178 | { | |
179 | HttpHdrContRange *dup; | |
180 | assert(range); | |
181 | dup = httpHdrContRangeCreate(); | |
182 | *dup = *range; | |
183 | return dup; | |
184 | } | |
185 | ||
186 | void | |
187 | httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p) | |
188 | { | |
189 | assert(range && p); | |
190 | httpHdrRangeRespSpecPackInto(&range->spec, p); | |
e6ccf245 | 191 | /* Ensure typecast is safe */ |
192 | assert (range->elength >= 0); | |
193 | if (!known_spec((size_t)range->elength)) | |
7c887d1f | 194 | packerPrintf(p, "/*"); |
195 | else | |
ed19251a | 196 | packerPrintf(p, "/%ld", (long int) range->elength); |
7c887d1f | 197 | } |
d192d11f | 198 | |
199 | void | |
9bc73deb | 200 | httpHdrContRangeSet(HttpHdrContRange * cr, HttpHdrRangeSpec spec, ssize_t ent_len) |
d192d11f | 201 | { |
202 | assert(cr && ent_len >= 0); | |
203 | cr->spec = spec; | |
204 | cr->elength = ent_len; | |
205 | } |