]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrContRange.cc
Author: Tsantilas Christos <chtsanti@users.sourceforge.net>
[thirdparty/squid.git] / src / HttpHdrContRange.cc
1
2 /*
3 * $Id: HttpHdrContRange.cc,v 1.18 2007/04/28 22:26:37 hno Exp $
4 *
5 * DEBUG: section 68 HTTP Content-Range Header
6 * AUTHOR: Alex Rousskov
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "HttpHdrContRange.h"
38
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 */
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
64 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec * spec, const char *field, int flen)
65 {
66 const char *p;
67 assert(spec);
68 spec->offset = spec->length = range_spec_unknown;
69
70 if (flen < 2)
71 return 0;
72
73 /* is spec given ? */
74 if (*field == '*')
75 return 1;
76
77 /* check format, must be %d-%d */
78 if (!((p = strchr(field, '-')) && (p - field < flen))) {
79 debugs(68, 2, "invalid (no '-') resp-range-spec near: '" << field << "'");
80 return 0;
81 }
82
83 /* parse offset */
84 if (!httpHeaderParseSize(field, &spec->offset))
85 return 0;
86
87 p++;
88
89 /* do we have last-pos ? */
90 if (p - field < flen) {
91 ssize_t last_pos;
92
93 if (!httpHeaderParseSize(p, &last_pos))
94 return 0;
95
96 spec->length = size_diff(last_pos + 1, spec->offset);
97 }
98
99 /* Ensure typecast is safe */
100 assert (spec->length >= 0);
101
102 /* we managed to parse, check if the result makes sence */
103 if (known_spec((size_t)spec->length) && spec->length == 0) {
104 debugs(68, 2, "invalid range (" << (long int) spec->offset << " += " <<
105 (long int) spec->length << ") in resp-range-spec near: '" << field << "'");
106 return 0;
107 }
108
109 return 1;
110 }
111
112 static void
113 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec * spec, Packer * p)
114 {
115 /* Ensure typecast is safe */
116 assert (spec->length >= 0);
117 assert (spec->length >= 0);
118
119 if (!known_spec((size_t)spec->offset) || !known_spec((size_t)spec->length))
120 packerPrintf(p, "*");
121 else
122 packerPrintf(p, "bytes %ld-%ld",
123 (long int) spec->offset, (long int) spec->offset + spec->length - 1);
124 }
125
126 /*
127 * Content Range
128 */
129
130 HttpHdrContRange *
131 httpHdrContRangeCreate(void)
132 {
133 HttpHdrContRange *r = (HttpHdrContRange *)memAllocate(MEM_HTTP_HDR_CONTENT_RANGE);
134 r->spec.offset = r->spec.length = range_spec_unknown;
135 r->elength = range_spec_unknown;
136 return r;
137 }
138
139 HttpHdrContRange *
140 httpHdrContRangeParseCreate(const char *str)
141 {
142 HttpHdrContRange *r = httpHdrContRangeCreate();
143
144 if (!httpHdrContRangeParseInit(r, str)) {
145 httpHdrContRangeDestroy(r);
146 r = NULL;
147 }
148
149 return r;
150 }
151
152 /* returns true if ranges are valid; inits HttpHdrContRange */
153 int
154 httpHdrContRangeParseInit(HttpHdrContRange * range, const char *str)
155 {
156 const char *p;
157 assert(range && str);
158 debugs(68, 8, "parsing content-range field: '" << str << "'");
159 /* check range type */
160
161 if (strncasecmp(str, "bytes ", 6))
162 return 0;
163
164 str += 6;
165
166 /* split */
167 if (!(p = strchr(str, '/')))
168 return 0;
169
170 if (*str == '*')
171 range->spec.offset = range->spec.length = range_spec_unknown;
172 else if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p - str))
173 return 0;
174
175 p++;
176
177 if (*p == '*')
178 range->elength = range_spec_unknown;
179 else if (!httpHeaderParseSize(p, &range->elength))
180 return 0;
181
182 debugs(68, 8, "parsed content-range field: " <<
183 (long int) range->spec.offset << "-" <<
184 (long int) range->spec.offset + range->spec.length - 1 << " / " <<
185 (long int) range->elength);
186
187 return 1;
188 }
189
190 void
191 httpHdrContRangeDestroy(HttpHdrContRange * range)
192 {
193 assert(range);
194 memFree(range, MEM_HTTP_HDR_CONTENT_RANGE);
195 }
196
197 HttpHdrContRange *
198 httpHdrContRangeDup(const HttpHdrContRange * range)
199 {
200 HttpHdrContRange *dup;
201 assert(range);
202 dup = httpHdrContRangeCreate();
203 *dup = *range;
204 return dup;
205 }
206
207 void
208 httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p)
209 {
210 assert(range && p);
211 httpHdrRangeRespSpecPackInto(&range->spec, p);
212 /* Ensure typecast is safe */
213 assert (range->elength >= 0);
214
215 if (!known_spec((size_t)range->elength))
216 packerPrintf(p, "/*");
217 else
218 packerPrintf(p, "/%ld", (long int) range->elength);
219 }
220
221 void
222 httpHdrContRangeSet(HttpHdrContRange * cr, HttpHdrRangeSpec spec, ssize_t ent_len)
223 {
224 assert(cr && ent_len >= 0);
225 cr->spec = spec;
226 cr->elength = ent_len;
227 }