]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrRange.cc
Fix uninitialized member fields in Http* objects
[thirdparty/squid.git] / src / HttpHdrRange.cc
1
2 /*
3 * DEBUG: section 64 HTTP Range Header
4 * AUTHOR: Alex Rousskov
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "Store.h"
36 #include "HttpHeaderRange.h"
37 #include "client_side_request.h"
38 #include "HttpReply.h"
39 #include "HttpHeaderTools.h"
40 #include "StrList.h"
41
42 /*
43 * Currently only byte ranges are supported
44 *
45 * Essentially, there are three types of byte ranges:
46 *
47 * 1) first-byte-pos "-" last-byte-pos // range
48 * 2) first-byte-pos "-" // trailer
49 * 3) "-" suffix-length // suffix (last length bytes)
50 *
51 *
52 * When Range field is parsed, we have no clue about the content
53 * length of the document. Thus, we simply code an "absent" part
54 * using HttpHdrRangeSpec::UnknownPosition constant.
55 *
56 * Note: when response length becomes known, we convert any range
57 * spec into type one above. (Canonization process).
58 */
59
60 /* local routines */
61 #define known_spec(s) ((s) > HttpHdrRangeSpec::UnknownPosition)
62
63 /* globals */
64 size_t HttpHdrRange::ParsedCount = 0;
65 int64_t const HttpHdrRangeSpec::UnknownPosition = -1;
66
67 /*
68 * Range-Spec
69 */
70
71 HttpHdrRangeSpec::HttpHdrRangeSpec() : offset(UnknownPosition), length(UnknownPosition) {}
72
73 /* parses range-spec and returns new object on success */
74 HttpHdrRangeSpec *
75 HttpHdrRangeSpec::Create(const char *field, int flen)
76 {
77 HttpHdrRangeSpec spec;
78
79 if (!spec.parseInit(field, flen))
80 return NULL;
81
82 return new HttpHdrRangeSpec(spec);
83 }
84
85 bool
86 HttpHdrRangeSpec::parseInit(const char *field, int flen)
87 {
88 const char *p;
89
90 if (flen < 2)
91 return false;
92
93 /* is it a suffix-byte-range-spec ? */
94 if (*field == '-') {
95 if (!httpHeaderParseOffset(field + 1, &length))
96 return false;
97 } else
98 /* must have a '-' somewhere in _this_ field */
99 if (!((p = strchr(field, '-')) && (p - field < flen))) {
100 debugs(64, 2, "invalid (missing '-') range-spec near: '" << field << "'");
101 return false;
102 } else {
103 if (!httpHeaderParseOffset(field, &offset))
104 return false;
105
106 ++p;
107
108 /* do we have last-pos ? */
109 if (p - field < flen) {
110 int64_t last_pos;
111
112 if (!httpHeaderParseOffset(p, &last_pos))
113 return false;
114
115 // RFC 2616 s14.35.1 MUST: last-byte-pos >= first-byte-pos
116 if (last_pos < offset) {
117 debugs(64, 2, "invalid (last-byte-pos < first-byte-pos) range-spec near: " << field);
118 return false;
119 }
120
121 HttpHdrRangeSpec::HttpRange aSpec (offset, last_pos + 1);
122
123 length = aSpec.size();
124 }
125 }
126
127 return true;
128 }
129
130 void
131 HttpHdrRangeSpec::packInto(Packer * packer) const
132 {
133 if (!known_spec(offset)) /* suffix */
134 packerPrintf(packer, "-%" PRId64, length);
135 else if (!known_spec(length)) /* trailer */
136 packerPrintf(packer, "%" PRId64 "-", offset);
137 else /* range */
138 packerPrintf(packer, "%" PRId64 "-%" PRId64,
139 offset, offset + length - 1);
140 }
141
142 void
143 HttpHdrRangeSpec::outputInfo( char const *note) const
144 {
145 debugs(64, 5, "HttpHdrRangeSpec::canonize: " << note << ": [" <<
146 offset << ", " << offset + length <<
147 ") len: " << length);
148 }
149
150 /* fills "absent" positions in range specification based on response body size
151 * returns true if the range is still valid
152 * range is valid if its intersection with [0,length-1] is not empty
153 */
154 int
155 HttpHdrRangeSpec::canonize(int64_t clen)
156 {
157 outputInfo ("have");
158 HttpRange object(0, clen);
159
160 if (!known_spec(offset)) { /* suffix */
161 assert(known_spec(length));
162 offset = object.intersection(HttpRange (clen - length, clen)).start;
163 } else if (!known_spec(length)) { /* trailer */
164 assert(known_spec(offset));
165 HttpRange newRange = object.intersection(HttpRange (offset, clen));
166 length = newRange.size();
167 }
168 /* we have a "range" now, adjust length if needed */
169 assert(known_spec(length));
170
171 assert(known_spec(offset));
172
173 HttpRange newRange = object.intersection (HttpRange (offset, offset + length));
174
175 length = newRange.size();
176
177 outputInfo ("done");
178
179 return length > 0;
180 }
181
182 /* merges recepient with donor if possible; returns true on success
183 * both specs must be canonized prior to merger, of course */
184 bool
185 HttpHdrRangeSpec::mergeWith(const HttpHdrRangeSpec * donor)
186 {
187 bool merged (false);
188 #if MERGING_BREAKS_NOTHING
189 /* Note: this code works, but some clients may not like its effects */
190 int64_t rhs = offset + length; /* no -1 ! */
191 const int64_t donor_rhs = donor->offset + donor->length; /* no -1 ! */
192 assert(known_spec(offset));
193 assert(known_spec(donor->offset));
194 assert(length > 0);
195 assert(donor->length > 0);
196 /* do we have a left hand side overlap? */
197
198 if (donor->offset < offset && offset <= donor_rhs) {
199 offset = donor->offset; /* decrease left offset */
200 merged = 1;
201 }
202
203 /* do we have a right hand side overlap? */
204 if (donor->offset <= rhs && rhs < donor_rhs) {
205 rhs = donor_rhs; /* increase right offset */
206 merged = 1;
207 }
208
209 /* adjust length if offsets have been changed */
210 if (merged) {
211 assert(rhs > offset);
212 length = rhs - offset;
213 } else {
214 /* does recepient contain donor? */
215 merged =
216 offset <= donor->offset && donor->offset < rhs;
217 }
218
219 #endif
220 return merged;
221 }
222
223 /*
224 * Range
225 */
226
227 HttpHdrRange::HttpHdrRange() : clen(HttpHdrRangeSpec::UnknownPosition)
228 {}
229
230 HttpHdrRange *
231 HttpHdrRange::ParseCreate(const String * range_spec)
232 {
233 HttpHdrRange *r = new HttpHdrRange;
234
235 if (!r->parseInit(range_spec)) {
236 delete r;
237 r = NULL;
238 }
239
240 return r;
241 }
242
243 /* returns true if ranges are valid; inits HttpHdrRange */
244 bool
245 HttpHdrRange::parseInit(const String * range_spec)
246 {
247 const char *item;
248 const char *pos = NULL;
249 int ilen;
250 assert(this && range_spec);
251 ++ParsedCount;
252 debugs(64, 8, "parsing range field: '" << range_spec << "'");
253 /* check range type */
254
255 if (range_spec->caseCmp("bytes=", 6))
256 return 0;
257
258 /* skip "bytes="; hack! */
259 pos = range_spec->termedBuf() + 6;
260
261 /* iterate through comma separated list */
262 while (strListGetItem(range_spec, ',', &item, &ilen, &pos)) {
263 HttpHdrRangeSpec *spec = HttpHdrRangeSpec::Create(item, ilen);
264 /*
265 * RFC 2616 section 14.35.1: MUST ignore Range with
266 * at least one syntactically invalid byte-range-specs.
267 */
268 if (!spec) {
269 while (!specs.empty())
270 delete specs.pop_back();
271 debugs(64, 2, "ignoring invalid range field: '" << range_spec << "'");
272 break;
273 }
274
275 specs.push_back(spec);
276 }
277
278 debugs(64, 8, "got range specs: " << specs.size());
279 return !specs.empty();
280 }
281
282 HttpHdrRange::~HttpHdrRange()
283 {
284 while (specs.size())
285 delete specs.pop_back();
286 }
287
288 HttpHdrRange::HttpHdrRange(HttpHdrRange const &old) :
289 specs(),
290 clen(HttpHdrRangeSpec::UnknownPosition)
291 {
292 specs.reserve(old.specs.size());
293
294 for (const_iterator i = old.begin(); i != old.end(); ++i)
295 specs.push_back(new HttpHdrRangeSpec ( **i));
296
297 assert(old.specs.size() == specs.size());
298 }
299
300 HttpHdrRange::iterator
301 HttpHdrRange::begin()
302 {
303 return specs.begin();
304 }
305
306 HttpHdrRange::iterator
307 HttpHdrRange::end()
308 {
309 return specs.end();
310 }
311
312 HttpHdrRange::const_iterator
313 HttpHdrRange::begin() const
314 {
315 return specs.begin();
316 }
317
318 HttpHdrRange::const_iterator
319 HttpHdrRange::end() const
320 {
321 return specs.end();
322 }
323
324 void
325 HttpHdrRange::packInto(Packer * packer) const
326 {
327 const_iterator pos = begin();
328 assert(this);
329
330 while (pos != end()) {
331 if (pos != begin())
332 packerAppend(packer, ",", 1);
333
334 (*pos)->packInto(packer);
335
336 ++pos;
337 }
338 }
339
340 void
341 HttpHdrRange::merge (Vector<HttpHdrRangeSpec *> &basis)
342 {
343 /* reset old array */
344 specs.clean();
345 /* merge specs:
346 * take one spec from "goods" and merge it with specs from
347 * "specs" (if any) until there is no overlap */
348 iterator i = basis.begin();
349
350 while (i != basis.end()) {
351 if (specs.size() && (*i)->mergeWith(specs.back())) {
352 /* merged with current so get rid of the prev one */
353 delete specs.pop_back();
354 continue; /* re-iterate */
355 }
356
357 specs.push_back (*i);
358 ++i; /* progress */
359 }
360
361 debugs(64, 3, "HttpHdrRange::merge: had " << basis.size() <<
362 " specs, merged " << basis.size() - specs.size() << " specs");
363 }
364
365 void
366 HttpHdrRange::getCanonizedSpecs (Vector<HttpHdrRangeSpec *> &copy)
367 {
368 /* canonize each entry and destroy bad ones if any */
369
370 for (iterator pos (begin()); pos != end(); ++pos) {
371 if ((*pos)->canonize(clen))
372 copy.push_back (*pos);
373 else
374 delete (*pos);
375 }
376
377 debugs(64, 3, "HttpHdrRange::getCanonizedSpecs: found " <<
378 specs.size() - copy.size() << " bad specs");
379 }
380
381 #include "HttpHdrContRange.h"
382
383 /*
384 * canonizes all range specs within a set preserving the order
385 * returns true if the set is valid after canonization;
386 * the set is valid if
387 * - all range specs are valid and
388 * - there is at least one range spec
389 */
390 int
391 HttpHdrRange::canonize(HttpReply *rep)
392 {
393 assert(this && rep);
394
395 if (rep->content_range)
396 clen = rep->content_range->elength;
397 else
398 clen = rep->content_length;
399
400 return canonize (clen);
401 }
402
403 int
404 HttpHdrRange::canonize (int64_t newClen)
405 {
406 clen = newClen;
407 debugs(64, 3, "HttpHdrRange::canonize: started with " << specs.count <<
408 " specs, clen: " << clen);
409 Vector<HttpHdrRangeSpec*> goods;
410 getCanonizedSpecs(goods);
411 merge (goods);
412 debugs(64, 3, "HttpHdrRange::canonize: finished with " << specs.count <<
413 " specs");
414 return specs.count > 0;
415 }
416
417 /* hack: returns true if range specs are too "complex" for Squid to handle */
418 /* requires that specs are "canonized" first! */
419 bool
420 HttpHdrRange::isComplex() const
421 {
422 int64_t offset = 0;
423 assert(this);
424 /* check that all rangers are in "strong" order */
425 const_iterator pos (begin());
426
427 while (pos != end()) {
428 /* Ensure typecasts is safe */
429 assert ((*pos)->offset >= 0);
430
431 if ((*pos)->offset < offset)
432 return 1;
433
434 offset = (*pos)->offset + (*pos)->length;
435
436 ++pos;
437 }
438
439 return 0;
440 }
441
442 /*
443 * hack: returns true if range specs may be too "complex" when "canonized".
444 * see also: HttpHdrRange::isComplex.
445 */
446 bool
447 HttpHdrRange::willBeComplex() const
448 {
449 assert(this);
450 /* check that all rangers are in "strong" order, */
451 /* as far as we can tell without the content length */
452 int64_t offset = 0;
453
454 for (const_iterator pos (begin()); pos != end(); ++pos) {
455 if (!known_spec((*pos)->offset)) /* ignore unknowns */
456 continue;
457
458 /* Ensure typecasts is safe */
459 assert ((*pos)->offset >= 0);
460
461 if ((*pos)->offset < offset)
462 return true;
463
464 offset = (*pos)->offset;
465
466 if (known_spec((*pos)->length)) /* avoid unknowns */
467 offset += (*pos)->length;
468 }
469
470 return false;
471 }
472
473 /*
474 * Returns lowest known offset in range spec(s),
475 * or HttpHdrRangeSpec::UnknownPosition
476 * this is used for size limiting
477 */
478 int64_t
479 HttpHdrRange::firstOffset() const
480 {
481 int64_t offset = HttpHdrRangeSpec::UnknownPosition;
482 assert(this);
483 const_iterator pos = begin();
484
485 while (pos != end()) {
486 if ((*pos)->offset < offset || !known_spec(offset))
487 offset = (*pos)->offset;
488
489 ++pos;
490 }
491
492 return offset;
493 }
494
495 /*
496 * Returns lowest offset in range spec(s), 0 if unknown.
497 * This is used for finding out where we need to start if all
498 * ranges are combined into one, for example FTP REST.
499 * Use 0 for size if unknown
500 */
501 int64_t
502 HttpHdrRange::lowestOffset(int64_t size) const
503 {
504 int64_t offset = HttpHdrRangeSpec::UnknownPosition;
505 const_iterator pos = begin();
506 assert(this);
507
508 while (pos != end()) {
509 int64_t current = (*pos)->offset;
510
511 if (!known_spec(current)) {
512 if ((*pos)->length > size || !known_spec((*pos)->length))
513 return 0; /* Unknown. Assume start of file */
514
515 current = size - (*pos)->length;
516 }
517
518 if (current < offset || !known_spec(offset))
519 offset = current;
520
521 ++pos;
522 }
523
524 return known_spec(offset) ? offset : 0;
525 }
526
527 /*
528 * \retval true Fetch only requested ranges. The first range is larger that configured limit.
529 * \retval false Full download. Not a range request, no limit, or the limit is not yet reached.
530 */
531 bool
532 HttpHdrRange::offsetLimitExceeded(const int64_t limit) const
533 {
534 if (NULL == this)
535 /* not a range request */
536 return false;
537
538 if (limit == 0)
539 /* 0 == disabled */
540 return true;
541
542 if (-1 == limit)
543 /* 'none' == forced */
544 return false;
545
546 if (firstOffset() == -1)
547 /* tail request */
548 return true;
549
550 if (limit >= firstOffset())
551 /* below the limit */
552 return false;
553
554 return true;
555 }
556
557 bool
558 HttpHdrRange::contains(HttpHdrRangeSpec& r) const
559 {
560 assert(r.length >= 0);
561 HttpHdrRangeSpec::HttpRange rrange(r.offset, r.offset + r.length);
562
563 for (const_iterator i = begin(); i != end(); ++i) {
564 HttpHdrRangeSpec::HttpRange irange((*i)->offset, (*i)->offset + (*i)->length);
565 HttpHdrRangeSpec::HttpRange intersection = rrange.intersection(irange);
566
567 if (intersection.start == irange.start && intersection.size() == irange.size())
568 return true;
569 }
570
571 return false;
572 }
573
574 const HttpHdrRangeSpec *
575 HttpHdrRangeIter::currentSpec() const
576 {
577 if (pos.incrementable())
578 return *pos;
579
580 return NULL;
581 }
582
583 void
584 HttpHdrRangeIter::updateSpec()
585 {
586 assert (debt_size == 0);
587 assert (valid);
588
589 if (pos.incrementable()) {
590 debt(currentSpec()->length);
591 }
592 }
593
594 int64_t
595 HttpHdrRangeIter::debt() const
596 {
597 debugs(64, 3, "HttpHdrRangeIter::debt: debt is " << debt_size);
598 return debt_size;
599 }
600
601 void HttpHdrRangeIter::debt(int64_t newDebt)
602 {
603 debugs(64, 3, "HttpHdrRangeIter::debt: was " << debt_size << " now " << newDebt);
604 debt_size = newDebt;
605 }