]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrContRange.cc
change FSF address
[thirdparty/squid.git] / src / HttpHdrContRange.cc
1
2 /*
3 * $Id: HttpHdrContRange.cc,v 1.6 1998/07/20 17:19:02 wessels Exp $
4 *
5 * DEBUG: section 68 HTTP Content-Range Header
6 * AUTHOR: Alex Rousskov
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * --------------------------------------------------------
10 *
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
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 */
31
32 #include "squid.h"
33
34 /*
35 * Currently only byte ranges are supported
36 *
37 * Content-Range = "Content-Range" ":" content-range-spec
38 * content-range-spec = byte-content-range-spec
39 * byte-content-range-spec = bytes-unit SP
40 * ( byte-range-resp-spec | "*") "/"
41 * ( entity-length | "*" )
42 * byte-range-resp-spec = first-byte-pos "-" last-byte-pos
43 * entity-length = 1*DIGIT
44 */
45
46
47 /* local constants */
48 #define range_spec_unknown ((size_t)-1)
49
50 /* local routines */
51 #define known_spec(s) ((s) != range_spec_unknown)
52 #define size_min(a,b) ((a) <= (b) ? (a) : (b))
53 #define size_diff(a,b) ((a) >= (b) ? ((a)-(b)) : 0)
54
55 /* globals */
56
57 /* parses range-resp-spec and inits spec, returns true on success */
58 static int
59 httpHdrRangeRespSpecParseInit(HttpHdrRangeSpec * spec, const char *field, int flen)
60 {
61 const char *p;
62 assert(spec);
63 spec->offset = spec->length = range_spec_unknown;
64 if (flen < 2)
65 return 0;
66 /* is spec given ? */
67 if (*field == '*')
68 return 1;
69 /* check format, must be %d-%d */
70 if (!((p = strchr(field, '-')) && (p - field < flen))) {
71 debug(68, 2) ("invalid (no '-') resp-range-spec near: '%s'\n", field);
72 return 0;
73 }
74 /* parse offset */
75 if (!httpHeaderParseSize(field, &spec->offset))
76 return 0;
77 p++;
78 /* do we have last-pos ? */
79 if (p - field < flen) {
80 size_t last_pos;
81 if (!httpHeaderParseSize(p, &last_pos))
82 return 0;
83 spec->length = size_diff(last_pos + 1, spec->offset);
84 }
85 /* we managed to parse, check if the result makes sence */
86 if (known_spec(spec->length) && !spec->length) {
87 debug(68, 2) ("invalid range (%d += %d) in resp-range-spec near: '%s'\n",
88 spec->offset, spec->length, field);
89 return 0;
90 }
91 return 1;
92 }
93
94 static void
95 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec * spec, Packer * p)
96 {
97 if (!known_spec(spec->offset) || !known_spec(spec->length))
98 packerPrintf(p, "*");
99 else
100 packerPrintf(p, "%d-%d",
101 spec->offset, spec->offset + spec->length - 1);
102 }
103
104 /*
105 * Content Range
106 */
107
108 HttpHdrContRange *
109 httpHdrContRangeCreate()
110 {
111 HttpHdrContRange *r = memAllocate(MEM_HTTP_HDR_CONTENT_RANGE);
112 r->spec.offset = r->spec.length = range_spec_unknown;
113 r->elength = range_spec_unknown;
114 return r;
115 }
116
117 HttpHdrContRange *
118 httpHdrContRangeParseCreate(const char *str)
119 {
120 HttpHdrContRange *r = httpHdrContRangeCreate();
121 if (!httpHdrContRangeParseInit(r, str)) {
122 httpHdrContRangeDestroy(r);
123 r = NULL;
124 }
125 return r;
126 }
127
128 /* returns true if ranges are valid; inits HttpHdrContRange */
129 int
130 httpHdrContRangeParseInit(HttpHdrContRange * range, const char *str)
131 {
132 const char *p;
133 assert(range && str);
134 debug(68, 8) ("parsing content-range field: '%s'\n", str);
135 /* check range type */
136 if (strncasecmp(str, "bytes ", 6))
137 return 0;
138 str += 6;
139 /* split */
140 if (!(p = strchr(str, '/')))
141 return 0;
142 if (*str == '*')
143 range->spec.offset = range->spec.length = range_spec_unknown;
144 else if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p - str))
145 return 0;
146 p++;
147 if (*p == '*')
148 range->elength = range_spec_unknown;
149 else if (!httpHeaderParseSize(p, &range->elength))
150 return 0;
151 debug(68, 8) ("parsed content-range field: %d-%d / %d\n",
152 range->spec.offset, range->spec.offset + range->spec.length - 1,
153 range->elength);
154 return 1;
155 }
156
157 void
158 httpHdrContRangeDestroy(HttpHdrContRange * range)
159 {
160 assert(range);
161 memFree(MEM_HTTP_HDR_CONTENT_RANGE, range);
162 }
163
164 HttpHdrContRange *
165 httpHdrContRangeDup(const HttpHdrContRange * range)
166 {
167 HttpHdrContRange *dup;
168 assert(range);
169 dup = httpHdrContRangeCreate();
170 *dup = *range;
171 return dup;
172 }
173
174 void
175 httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p)
176 {
177 assert(range && p);
178 httpHdrRangeRespSpecPackInto(&range->spec, p);
179 if (!known_spec(range->elength))
180 packerPrintf(p, "/*");
181 else
182 packerPrintf(p, "/%d", range->elength);
183 }
184
185 void
186 httpHdrContRangeSet(HttpHdrContRange *cr, HttpHdrRangeSpec spec, size_t ent_len)
187 {
188 assert(cr && ent_len >= 0);
189 cr->spec = spec;
190 cr->elength = ent_len;
191 }