]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrRange.cc
Merged from trunk
[thirdparty/squid.git] / src / HttpHdrRange.cc
1
2 /*
3 * $Id$
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-old.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, "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 // RFC 2616 s14.35.1 MUST: last-byte-pos >= first-byte-pos
117 if (last_pos < offset) {
118 debugs(64, 2, "invalid (last-byte-pos < first-byte-pos) range-spec near: " << field);
119 return false;
120 }
121
122 HttpHdrRangeSpec::HttpRange aSpec (offset, last_pos + 1);
123
124 length = aSpec.size();
125 }
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 assert(known_spec(length));
163 offset = object.intersection(HttpRange (clen - length, clen)).start;
164 } else if (!known_spec(length)) { /* trailer */
165 assert(known_spec(offset));
166 HttpRange newRange = object.intersection(HttpRange (offset, clen));
167 length = newRange.size();
168 }
169 /* we have a "range" now, adjust length if needed */
170 assert(known_spec(length));
171
172 assert(known_spec(offset));
173
174 HttpRange newRange = object.intersection (HttpRange (offset, offset + length));
175
176 length = newRange.size();
177
178 outputInfo ("done");
179
180 return length > 0;
181 }
182
183 /* merges recepient with donor if possible; returns true on success
184 * both specs must be canonized prior to merger, of course */
185 bool
186 HttpHdrRangeSpec::mergeWith(const HttpHdrRangeSpec * donor)
187 {
188 bool merged (false);
189 #if MERGING_BREAKS_NOTHING
190 /* Note: this code works, but some clients may not like its effects */
191 int64_t rhs = offset + length; /* no -1 ! */
192 const int64_t donor_rhs = donor->offset + donor->length; /* no -1 ! */
193 assert(known_spec(offset));
194 assert(known_spec(donor->offset));
195 assert(length > 0);
196 assert(donor->length > 0);
197 /* do we have a left hand side overlap? */
198
199 if (donor->offset < offset && offset <= donor_rhs) {
200 offset = donor->offset; /* decrease left offset */
201 merged = 1;
202 }
203
204 /* do we have a right hand side overlap? */
205 if (donor->offset <= rhs && rhs < donor_rhs) {
206 rhs = donor_rhs; /* increase right offset */
207 merged = 1;
208 }
209
210 /* adjust length if offsets have been changed */
211 if (merged) {
212 assert(rhs > offset);
213 length = rhs - offset;
214 } else {
215 /* does recepient contain donor? */
216 merged =
217 offset <= donor->offset && donor->offset < rhs;
218 }
219
220 #endif
221 return merged;
222 }
223
224 /*
225 * Range
226 */
227
228 HttpHdrRange::HttpHdrRange () : clen (HttpHdrRangeSpec::UnknownPosition)
229 {}
230
231 HttpHdrRange *
232 HttpHdrRange::ParseCreate(const String * range_spec)
233 {
234 HttpHdrRange *r = new HttpHdrRange;
235
236 if (!r->parseInit(range_spec)) {
237 delete r;
238 r = NULL;
239 }
240
241 return r;
242 }
243
244 /* returns true if ranges are valid; inits HttpHdrRange */
245 bool
246 HttpHdrRange::parseInit(const String * range_spec)
247 {
248 const char *item;
249 const char *pos = NULL;
250 int ilen;
251 assert(this && range_spec);
252 ++ParsedCount;
253 debugs(64, 8, "parsing range field: '" << range_spec << "'");
254 /* check range type */
255
256 if (range_spec->caseCmp("bytes=", 6))
257 return 0;
258
259 /* skip "bytes="; hack! */
260 pos = range_spec->termedBuf() + 6;
261
262 /* iterate through comma separated list */
263 while (strListGetItem(range_spec, ',', &item, &ilen, &pos)) {
264 HttpHdrRangeSpec *spec = HttpHdrRangeSpec::Create(item, ilen);
265 /*
266 * RFC 2616 section 14.35.1: MUST ignore Range with
267 * at least one syntactically invalid byte-range-specs.
268 */
269 if (!spec) {
270 while (!specs.empty())
271 delete specs.pop_back();
272 debugs(64, 2, "ignoring invalid range field: '" << range_spec << "'");
273 break;
274 }
275
276 specs.push_back(spec);
277 }
278
279 debugs(64, 8, "got range specs: " << specs.size());
280 return !specs.empty();
281 }
282
283 HttpHdrRange::~HttpHdrRange()
284 {
285 while (specs.size())
286 delete specs.pop_back();
287 }
288
289 HttpHdrRange::HttpHdrRange(HttpHdrRange const &old) : specs()
290 {
291 specs.reserve(old.specs.size());
292
293 for (const_iterator i = old.begin(); i != old.end(); ++i)
294 specs.push_back(new HttpHdrRangeSpec ( **i));
295
296 assert(old.specs.size() == specs.size());
297 }
298
299 HttpHdrRange::iterator
300 HttpHdrRange::begin()
301 {
302 return specs.begin();
303 }
304
305 HttpHdrRange::iterator
306 HttpHdrRange::end()
307 {
308 return specs.end();
309 }
310
311 HttpHdrRange::const_iterator
312 HttpHdrRange::begin() const
313 {
314 return specs.begin();
315 }
316
317 HttpHdrRange::const_iterator
318 HttpHdrRange::end() const
319 {
320 return specs.end();
321 }
322
323 void
324 HttpHdrRange::packInto(Packer * packer) const
325 {
326 const_iterator pos = begin();
327 assert(this);
328
329 while (pos != end()) {
330 if (pos != begin())
331 packerAppend(packer, ",", 1);
332
333 (*pos)->packInto(packer);
334
335 ++pos;
336 }
337 }
338
339 void
340 HttpHdrRange::merge (Vector<HttpHdrRangeSpec *> &basis)
341 {
342 /* reset old array */
343 specs.clean();
344 /* merge specs:
345 * take one spec from "goods" and merge it with specs from
346 * "specs" (if any) until there is no overlap */
347 iterator i = basis.begin();
348
349 while (i != basis.end()) {
350 if (specs.size() && (*i)->mergeWith(specs.back())) {
351 /* merged with current so get rid of the prev one */
352 delete specs.pop_back();
353 continue; /* re-iterate */
354 }
355
356 specs.push_back (*i);
357 ++i; /* progress */
358 }
359
360 debugs(64, 3, "HttpHdrRange::merge: had " << basis.size() <<
361 " specs, merged " << basis.size() - specs.size() << " specs");
362 }
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 }