]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrContRange.cc
Initial revision.
[thirdparty/squid.git] / src / HttpHdrContRange.cc
1
2 /*
3 * $Id: HttpHdrContRange.cc,v 1.1 1998/03/08 21:03:01 rousskov 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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32 #include "squid.h"
33
34 #if 0
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 #endif
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 0;
69 /* check format, must be %d-%d */
70 if (!((p = strchr(field, '-')) && (p-field < flen))) {
71 debug(68, 2) ("invalid resp-range-spec near: '%s'\n", field);
72 return 0;
73 }
74 /* parse offset */
75 if (!httpHeaderParseSize(field+1, &spec->offset))
76 return NULL;
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, 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 resp-range-spec near: '%s'\n", field);
88 return 0;
89 }
90 return 1;
91 }
92
93 static void
94 httpHdrRangeRespSpecPackInto(const HttpHdrRangeSpec *spec, Packer *p)
95 {
96 if (!known_spec(spec->offset) || !known_spec(spec->length))
97 packerPrintf(p, "*");
98 else
99 packerPrintf(p, "%d-%d",
100 spec->offset, spec->offset+spec->length-1);
101 }
102
103 /*
104 * Content Range
105 */
106
107 HttpHdrContRange *
108 httpHdrContRangeCreate()
109 {
110 HttpHdrContRange *r = memAllocate(MEM_HTTP_HDR_CONTENT_RANGE);
111 r->spec.offset = r->spec.length = range_spec_unknown;
112 r->elength = range_spec_unknown;
113 return r;
114 }
115
116 HttpHdrContRange *
117 httpHdrContRangeParseCreate(const char *str)
118 {
119 HttpHdrContRange *r = httpHdrContRangeCreate();
120 if (!httpHdrContRangeParseInit(r, str)) {
121 httpHdrContRangeDestroy(r);
122 r = NULL;
123 }
124 return r;
125 }
126
127 /* returns true if ranges are valid; inits HttpHdrContRange */
128 int
129 httpHdrContRangeParseInit(HttpHdrContRange *range, const char *str)
130 {
131 const char *p;
132 assert(range && str);
133 /* split */
134 if (!(p = strchr(str, '/')))
135 return 0;
136 if (*str == '*')
137 range->spec.offset = range->spec.length = range_spec_unknown;
138 else
139 if (!httpHdrRangeRespSpecParseInit(&range->spec, str, p-str))
140 return 0;
141 p++;
142 if (*p == '*')
143 range->elength = range_spec_unknown;
144 else
145 if (!httpHeaderParseSize(p, &range->elength))
146 return 0;
147 return 1;
148 }
149
150 void
151 httpHdrContRangeDestroy(HttpHdrContRange *range)
152 {
153 assert(range);
154 memFree(MEM_HTTP_HDR_CONTENT_RANGE, range);
155 }
156
157 HttpHdrContRange *
158 httpHdrContRangeDup(const HttpHdrContRange * range)
159 {
160 HttpHdrContRange *dup;
161 assert(range);
162 dup = httpHdrContRangeCreate();
163 *dup = *range;
164 return dup;
165 }
166
167 void
168 httpHdrContRangePackInto(const HttpHdrContRange * range, Packer * p)
169 {
170 assert(range && p);
171 httpHdrRangeRespSpecPackInto(&range->spec, p);
172 if (!known_spec(range->elength))
173 packerPrintf(p, "/*");
174 else
175 packerPrintf(p, "/%d", range->elength);
176 }