]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrRange.cc
Author: wessels & Christos Tsantilas
[thirdparty/squid.git] / src / HttpHdrRange.cc
1
2 /*
3 * $Id: HttpHdrRange.cc,v 1.45 2007/08/13 17:20:51 hno Exp $
4 *
5 * DEBUG: section 64 HTTP 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 "Store.h"
38 #include "HttpHeaderRange.h"
39 #include "client_side_request.h"
40 #include "HttpReply.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
61 /* local routines */
62 #define known_spec(s) ((s) > HttpHdrRangeSpec::UnknownPosition)
63
64 /* globals */
65 size_t HttpHdrRange::ParsedCount = 0;
66 int64_t const HttpHdrRangeSpec::UnknownPosition = -1;
67
68 /*
69 * Range-Spec
70 */
71
72 HttpHdrRangeSpec::HttpHdrRangeSpec() : offset(UnknownPosition), length(UnknownPosition){}
73
74 /* parses range-spec and returns new object on success */
75 HttpHdrRangeSpec *
76 HttpHdrRangeSpec::Create(const char *field, int flen)
77 {
78 HttpHdrRangeSpec spec;
79
80 if (!spec.parseInit(field, flen))
81 return NULL;
82
83 return new HttpHdrRangeSpec(spec);
84 }
85
86 bool
87 HttpHdrRangeSpec::parseInit(const char *field, int flen)
88 {
89 const char *p;
90
91 if (flen < 2)
92 return false;
93
94 /* is it a suffix-byte-range-spec ? */
95 if (*field == '-') {
96 if (!httpHeaderParseOffset(field + 1, &length))
97 return false;
98 } else
99 /* must have a '-' somewhere in _this_ field */
100 if (!((p = strchr(field, '-')) || (p - field >= flen))) {
101 debugs(64, 2, "ignoring invalid (missing '-') range-spec near: '" << field << "'");
102 return false;
103 } else {
104 if (!httpHeaderParseOffset(field, &offset))
105 return false;
106
107 p++;
108
109 /* do we have last-pos ? */
110 if (p - field < flen) {
111 int64_t last_pos;
112
113 if (!httpHeaderParseOffset(p, &last_pos))
114 return false;
115
116 HttpHdrRangeSpec::HttpRange aSpec (offset, last_pos + 1);
117
118 length = aSpec.size();
119 }
120 }
121
122 /* we managed to parse, check if the result makes sence */
123 if (length == 0) {
124 debugs(64, 2, "ignoring invalid (zero length) range-spec near: '" << field << "'");
125 return false;
126 }
127
128 return true;
129 }
130
131 void
132 HttpHdrRangeSpec::packInto(Packer * packer) const
133 {
134 if (!known_spec(offset)) /* suffix */
135 packerPrintf(packer, "-%"PRId64, length);
136 else if (!known_spec(length)) /* trailer */
137 packerPrintf(packer, "%"PRId64"-", offset);
138 else /* range */
139 packerPrintf(packer, "%"PRId64"-%"PRId64,
140 offset, offset + length - 1);
141 }
142
143 void
144 HttpHdrRangeSpec::outputInfo( char const *note) const
145 {
146 debugs(64, 5, "HttpHdrRangeSpec::canonize: " << note << ": [" <<
147 offset << ", " << offset + length <<
148 ") len: " << length);
149 }
150
151 /* fills "absent" positions in range specification based on response body size
152 * returns true if the range is still valid
153 * range is valid if its intersection with [0,length-1] is not empty
154 */
155 int
156 HttpHdrRangeSpec::canonize(int64_t clen)
157 {
158 outputInfo ("have");
159 HttpRange object(0, clen);
160
161 if (!known_spec(offset)) /* suffix */
162 {
163 assert(known_spec(length));
164 offset = object.intersection(HttpRange (clen - length, clen)).start;
165 } else if (!known_spec(length)) /* trailer */
166 {
167 assert(known_spec(offset));
168 HttpRange newRange = object.intersection(HttpRange (offset, clen));
169 length = newRange.size();
170 }
171 /* we have a "range" now, adjust length if needed */
172 assert(known_spec(length));
173
174 assert(known_spec(offset));
175
176 HttpRange newRange = object.intersection (HttpRange (offset, offset + length));
177
178 length = newRange.size();
179
180 outputInfo ("done");
181
182 return length > 0;
183 }
184
185 /* merges recepient with donor if possible; returns true on success
186 * both specs must be canonized prior to merger, of course */
187 bool
188 HttpHdrRangeSpec::mergeWith(const HttpHdrRangeSpec * donor)
189 {
190 bool merged (false);
191 #if MERGING_BREAKS_NOTHING
192 /* Note: this code works, but some clients may not like its effects */
193 int64_t rhs = offset + length; /* no -1 ! */
194 const int64_t donor_rhs = donor->offset + donor->length; /* no -1 ! */
195 assert(known_spec(offset));
196 assert(known_spec(donor->offset));
197 assert(length > 0);
198 assert(donor->length > 0);
199 /* do we have a left hand side overlap? */
200
201 if (donor->offset < offset && offset <= donor_rhs) {
202 offset = donor->offset; /* decrease left offset */
203 merged = 1;
204 }
205
206 /* do we have a right hand side overlap? */
207 if (donor->offset <= rhs && rhs < donor_rhs) {
208 rhs = donor_rhs; /* increase right offset */
209 merged = 1;
210 }
211
212 /* adjust length if offsets have been changed */
213 if (merged) {
214 assert(rhs > offset);
215 length = rhs - offset;
216 } else {
217 /* does recepient contain donor? */
218 merged =
219 offset <= donor->offset && donor->offset < rhs;
220 }
221
222 #endif
223 return merged;
224 }
225
226 /*
227 * Range
228 */
229
230 HttpHdrRange::HttpHdrRange () : clen (HttpHdrRangeSpec::UnknownPosition)
231 {}
232
233 HttpHdrRange *
234 HttpHdrRange::ParseCreate(const String * range_spec)
235 {
236 HttpHdrRange *r = new HttpHdrRange;
237
238 if (!r->parseInit(range_spec)) {
239 delete r;
240 r = NULL;
241 }
242
243 return r;
244 }
245
246 /* returns true if ranges are valid; inits HttpHdrRange */
247 bool
248 HttpHdrRange::parseInit(const String * range_spec)
249 {
250 const char *item;
251 const char *pos = NULL;
252 int ilen;
253 int count = 0;
254 assert(this && range_spec);
255 ++ParsedCount;
256 debugs(64, 8, "parsing range field: '" << range_spec->buf() << "'");
257 /* check range type */
258
259 if (range_spec->caseCmp("bytes=", 6))
260 return 0;
261
262 /* skip "bytes="; hack! */
263 pos = range_spec->buf() + 5;
264
265 /* iterate through comma separated list */
266 while (strListGetItem(range_spec, ',', &item, &ilen, &pos)) {
267 HttpHdrRangeSpec *spec = HttpHdrRangeSpec::Create(item, ilen);
268 /*
269 * HTTP/1.1 draft says we must ignore the whole header field if one spec
270 * is invalid. However, RFC 2068 just says that we must ignore that spec.
271 */
272
273 if (spec)
274 specs.push_back(spec);
275
276 ++count;
277 }
278
279 debugs(64, 8, "parsed range range count: " << count << ", kept " <<
280 specs.size());
281 return specs.count != 0;
282 }
283
284 HttpHdrRange::~HttpHdrRange()
285 {
286 while (specs.size())
287 delete specs.pop_back();
288 }
289
290 HttpHdrRange::HttpHdrRange(HttpHdrRange const &old) : specs()
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
366 void
367 HttpHdrRange::getCanonizedSpecs (Vector<HttpHdrRangeSpec *> &copy)
368 {
369 /* canonize each entry and destroy bad ones if any */
370
371 for (iterator pos (begin()); pos != end(); ++pos) {
372 if ((*pos)->canonize(clen))
373 copy.push_back (*pos);
374 else
375 delete (*pos);
376 }
377
378 debugs(64, 3, "HttpHdrRange::getCanonizedSpecs: found " <<
379 specs.size() - copy.size() << " bad specs");
380 }
381
382 #include "HttpHdrContRange.h"
383
384 /*
385 * canonizes all range specs within a set preserving the order
386 * returns true if the set is valid after canonization;
387 * the set is valid if
388 * - all range specs are valid and
389 * - there is at least one range spec
390 */
391 int
392 HttpHdrRange::canonize(HttpReply *rep)
393 {
394 assert(this && rep);
395
396 if (rep->content_range)
397 clen = rep->content_range->elength;
398 else
399 clen = rep->content_length;
400
401 return canonize (clen);
402 }
403
404 int
405 HttpHdrRange::canonize (int64_t newClen)
406 {
407 clen = newClen;
408 debugs(64, 3, "HttpHdrRange::canonize: started with " << specs.count <<
409 " specs, clen: " << clen);
410 Vector<HttpHdrRangeSpec*> goods;
411 getCanonizedSpecs(goods);
412 merge (goods);
413 debugs(64, 3, "HttpHdrRange::canonize: finished with " << specs.count <<
414 " specs");
415 return specs.count > 0;
416 }
417
418 /* hack: returns true if range specs are too "complex" for Squid to handle */
419 /* requires that specs are "canonized" first! */
420 bool
421 HttpHdrRange::isComplex() const
422 {
423 int64_t offset = 0;
424 assert(this);
425 /* check that all rangers are in "strong" order */
426 const_iterator pos (begin());
427
428 while (pos != end()) {
429 /* Ensure typecasts is safe */
430 assert ((*pos)->offset >= 0);
431
432 if ((*pos)->offset < offset)
433 return 1;
434
435 offset = (*pos)->offset + (*pos)->length;
436
437 ++pos;
438 }
439
440 return 0;
441 }
442
443 /*
444 * hack: returns true if range specs may be too "complex" when "canonized".
445 * see also: HttpHdrRange::isComplex.
446 */
447 bool
448 HttpHdrRange::willBeComplex() const
449 {
450 assert(this);
451 /* check that all rangers are in "strong" order, */
452 /* as far as we can tell without the content length */
453 int64_t offset = 0;
454
455 for (const_iterator pos (begin()); pos != end(); ++pos) {
456 if (!known_spec((*pos)->offset)) /* ignore unknowns */
457 continue;
458
459 /* Ensure typecasts is safe */
460 assert ((*pos)->offset >= 0);
461
462 if ((*pos)->offset < offset)
463 return true;
464
465 offset = (*pos)->offset;
466
467 if (known_spec((*pos)->length)) /* avoid unknowns */
468 offset += (*pos)->length;
469 }
470
471 return false;
472 }
473
474 /*
475 * Returns lowest known offset in range spec(s),
476 * or HttpHdrRangeSpec::UnknownPosition
477 * this is used for size limiting
478 */
479 int64_t
480 HttpHdrRange::firstOffset() const
481 {
482 int64_t offset = HttpHdrRangeSpec::UnknownPosition;
483 assert(this);
484 const_iterator pos = begin();
485
486 while (pos != end()) {
487 if ((*pos)->offset < offset || !known_spec(offset))
488 offset = (*pos)->offset;
489
490 ++pos;
491 }
492
493 return offset;
494 }
495
496 /*
497 * Returns lowest offset in range spec(s), 0 if unknown.
498 * This is used for finding out where we need to start if all
499 * ranges are combined into one, for example FTP REST.
500 * Use 0 for size if unknown
501 */
502 int64_t
503 HttpHdrRange::lowestOffset(int64_t size) const
504 {
505 int64_t offset = HttpHdrRangeSpec::UnknownPosition;
506 const_iterator pos = begin();
507 assert(this);
508
509 while (pos != end()) {
510 int64_t current = (*pos)->offset;
511
512 if (!known_spec(current)) {
513 if ((*pos)->length > size || !known_spec((*pos)->length))
514 return 0; /* Unknown. Assume start of file */
515
516 current = size - (*pos)->length;
517 }
518
519 if (current < offset || !known_spec(offset))
520 offset = current;
521
522 ++pos;
523 }
524
525 return known_spec(offset) ? offset : 0;
526 }
527
528 /*
529 * Return true if the first range offset is larger than the configured
530 * limit.
531 * Note that exceeding the limit (returning true) results in only
532 * grabbing the needed range elements from the origin.
533 */
534 bool
535 HttpHdrRange::offsetLimitExceeded() const
536 {
537 if (NULL == this)
538 /* not a range request */
539 return false;
540
541 if (-1 == Config.rangeOffsetLimit)
542 /* disabled */
543 return false;
544
545 if (firstOffset() == -1)
546 /* tail request */
547 return true;
548
549 if (Config.rangeOffsetLimit >= firstOffset())
550 /* below the limit */
551 return false;
552
553 return true;
554 }
555
556 bool
557 HttpHdrRange::contains(HttpHdrRangeSpec& r) const
558 {
559 assert(r.length >= 0);
560 HttpHdrRangeSpec::HttpRange rrange(r.offset, r.offset + r.length);
561
562 for (const_iterator i = begin(); i != end(); ++i) {
563 HttpHdrRangeSpec::HttpRange irange((*i)->offset, (*i)->offset + (*i)->length);
564 HttpHdrRangeSpec::HttpRange intersection = rrange.intersection(irange);
565
566 if (intersection.start == irange.start && intersection.size() == irange.size())
567 return true;
568 }
569
570 return false;
571 }
572
573 const HttpHdrRangeSpec *
574 HttpHdrRangeIter::currentSpec() const
575 {
576 if (pos.incrementable())
577 return *pos;
578
579 return NULL;
580 }
581
582 void
583 HttpHdrRangeIter::updateSpec()
584 {
585 assert (debt_size == 0);
586 assert (valid);
587
588 if (pos.incrementable()) {
589 debt(currentSpec()->length);
590 }
591 }
592
593 int64_t
594 HttpHdrRangeIter::debt() const
595 {
596 debugs(64, 3, "HttpHdrRangeIter::debt: debt is " << debt_size);
597 return debt_size;
598 }
599
600 void HttpHdrRangeIter::debt(int64_t newDebt)
601 {
602 debugs(64, 3, "HttpHdrRangeIter::debt: was " << debt_size << " now " << newDebt);
603 debt_size = newDebt;
604 }